Created
October 25, 2013 17:18
-
-
Save raddy/7158320 to your computer and use it in GitHub Desktop.
simple future mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
def calc_pnl_futs(actions,observations): | |
pnls=[] | |
if observations.last_valid_index() == None: | |
return np.zeros(len(observations)) | |
settle = np.mean(observations[observations.last_valid_index()]) | |
for a,o in zip(actions,observations): | |
if a<0: | |
pnls.append(o[0]-settle) | |
elif a>0: | |
pnls.append(settle-o[1]) | |
else: | |
pnls.append(0) | |
return np.array(pnls) * 5 | |
def market_make_futs(observed,predicted,initial_edge,inventory_martingales=[1,1.5,2,2.5]): | |
actions = [] | |
inventory = 0 | |
for o,p in zip(observed,predicted): | |
edge_to_add = (bisect.bisect(inventory_martingales,abs(inventory))+1) * initial_edge | |
edge_to_unwind = (bisect.bisect(inventory_martingales,abs(inventory))-1) * -1 * initial_edge | |
print inventory,edge_to_add,edge_to_unwind | |
if inventory<inventory_martingales[-1] and ((p-o[1])>edge_to_add or (inventory<0 and (p-o[1])>edge_to_unwind)): | |
actions.append(1) | |
# print 'Buying...Inventory: ',inventory, ' Edge: ', (p-o[1]), o, p | |
inventory+=1 | |
elif inventory>(-1*inventory_martingales[-1]) and ((o[0]-p)>edge_to_add or (inventory>0 and (o[0]-p)>edge_to_unwind)): | |
actions.append(-1) | |
#print 'Selling...Inventory: ',inventory, ' Edge: ', (o[0]-p), o, p | |
inventory-=1 | |
else: | |
actions.append(0) | |
actions = np.array(actions) | |
return [actions,calc_pnl_futs(actions,observed)] | |
b = interesting_data.fut_bid.dropna() | |
a = interesting_data.fut_ask.dropna() | |
m = (b+a)/2 | |
s = pd.Series(zip(b,a),index=b.index) | |
m_kf = pd.Series(univariate_kf(m.values,m[m.first_valid_index()],1,1e6),index=m.index) | |
actions,pnls = market_make_futs(s[::100],m_kf[::100],4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment