Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Created April 19, 2014 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelcolvin/11082286 to your computer and use it in GitHub Desktop.
Save samuelcolvin/11082286 to your computer and use it in GitHub Desktop.
Betwise Smart Market Performance Analysis
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from datetime import datetime as dtdt
pd.set_option('display.width', 160)
pd.set_option('display.max_rows', 50)
pd.set_option('display.max_info_rows', 20)
pd.set_option('display.max_info_columns', 20)
csv_file = 'smart_markets.csv'
# loading from csv is much faster than html, so we use that if we can
if os.path.exists(csv_file):
print 'loading from csv...'
df = pd.read_csv(csv_file)
else:
# smart_markets_download.html is just http://www.betwise.co.uk/smart_markets_pandl saved
print 'loading from html...'
df = pd.read_html('smart_markets_download.html', header=0, infer_types=False)[0]
df.to_csv(csv_file)
df = df.reindex( index=df.index[ ::-1 ] )
df['Date'] = df['Date'].apply(lambda x: dtdt.strptime(x, '%d-%m-%Y'))
df = df.set_index('Date')
df['BSP Win'] = df['BSP Win'].astype(float)
df['loss'] = df.apply(lambda row: (0,1)[row['Result'] != 'NR'], axis=1)
df['loss2'] = df.apply(lambda row: (0,1)[row['Result'] != 'NR' and pd.notnull(row['BSP Win'])], axis=1)
df['profit'] = df.apply(lambda row: (0, row['BSP Win'])[row['Result'] == '1' and pd.notnull(row['BSP Win'])], axis=1)
df['PL'] = df.apply(lambda row: row['profit']-row['loss'], axis=1)
df['PL2'] = df.apply(lambda row: row['profit']-row['loss2'], axis=1)
cpl1 = 'cum P&L'
df[cpl1] = df.PL.cumsum()
cpl2 = 'cum P&L NULL removed'
df[cpl2] = df.PL2.cumsum()
print df
print df.dtypes
df[[cpl1, cpl2]].plot()
plt.savefig('smart_markets_PandL.jpg')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment