Skip to content

Instantly share code, notes, and snippets.

@raddy
Created March 1, 2015 15:29
Show Gist options
  • Save raddy/e11c4388795b7a8c9186 to your computer and use it in GitHub Desktop.
Save raddy/e11c4388795b7a8c9186 to your computer and use it in GitHub Desktop.
mechanical markets style plots updated with better line widths
import seaborn
import pandas as pd
import numpy as np
import matplotlib.patches as mpatches
#create random trade pnls
n = 5000
sigma = .1
pnl1 = pd.DataFrame(np.random.randn(n,15)) * sigma
pnl2 = pd.DataFrame(np.random.randn(n,15)) * sigma
pnl3 = pd.DataFrame(np.random.randn(n,15)) * sigma
#make pnl2 slightly better
pnl2.ix[:,8:]+=np.array([[.1,.2,.25,.3,.3,.25,.15],]*n)
#make pnl3 slightly worse
pnl3.ix[:,8:]-=np.array([[.15,.2,.2,.2,.25,.15,.05],]*n)
myticks = ['-10s','-1s','-100ms','-10ms','-1ms','-100us','-10us','0','+10us','+100us','+1ms','+10ms','+100ms','+1s','+10s']
pnl1.columns = myticks
pnl2.columns = myticks
pnl3.columns = myticks
#create random volumes
vol1 = pd.DataFrame(np.random.randn(n,15) * 1/sigma**2 + (np.arctan(np.arange(0,15)-7.5) * 10000))
vol2 = pd.DataFrame(np.random.randn(n,15) * 1/sigma**2 + (np.arctan(np.arange(0,15)-7.5) * 14000))
vol3 = pd.DataFrame(np.random.randn(n,15) * 1/sigma**2 + (np.arctan(np.arange(0,15)-7.5) * 6000))
def mech_markets_chart(pnls,volumes,labels):
#styling
figure(figsize(8,6))
seaborn.set_style("whitegrid")
pg = seaborn.PairGrid(pnls[0], x_vars=["Time"], y_vars=["PnL", "Exchange Volume"],
aspect=4, palette="coolwarm")
palette = seaborn.color_palette("coolwarm",len(pnls))
c = seaborn.plotting_context("talk", rc={"lines.linewidth": 1})
mylegend = dict()
#top chart
for i,p in enumerate(pnls):
seaborn.violinplot(p,color=palette[i],widths=.5,ax=pg.axes[0][0],alpha=.35,positions=0,label=labels[i],linewidth=.25)
with c:
seaborn.pointplot(p.columns,p.mean(),ax=pg.axes[0][0],x_order=p.columns,color=palette[i],hline=0,label=labels[i]);
mylegend[labels[i]] = mpatches.Patch(color=palette[i])
pg.axes[0][0].set_ylabel("PnL(BPs)")
pg.axes[0][0].set_xlabel("")
#volume chart
for i,v in enumerate(volumes):
with c:
seaborn.pointplot(v.columns,v.mean(),ax=pg.axes[1][0],x_order=v.columns,color=palette[i],hline=0,label=labels[i]);
pg.axes[1][0].set_ylabel("Exchange Volume Diff")
pg.axes[1][0].set_xlabel("Time Since Trade")
#grids
pg.axes[0][0].grid(True, which='minor')
pg.axes[1][0].grid(True, which='minor')
xticks(np.arange(15),myticks,rotation=45)
pg.add_legend(mylegend)
seaborn.despine(left=True)
mech_markets_chart([pnl1,pnl2,pnl3],[vol1,vol2,vol3],['first','second','third']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment