def genDataMap(self, startTime, endTime, verbose): ''' Runs the market with the order book data to create a map of times to prices. ''' def storePrice(action, lob): bestAsk = lob.getBestAsk() bestBid = lob.getBestBid() midPrice = None if bestAsk != None and bestBid != None: midPrice = bestBid + (bestAsk - bestBid) / 2.0 self.dataOnlyPrices[action['uei']] = midPrice self.initiateDataModel(verbose) dataLOB = OrderBook() processVerbose = False if verbose: print "\nCreating data map..." l = 0 c = 0 m = 0 ma = 0 time = 0 dayEnded = False while not dayEnded: if time % 10000 == 0: print "time is %d" % time action, dayEnded = self.dataModel.getNextAction(dataLOB) if action != None: atype = action['type'] if atype == 'market': ma += 1 dataLOB.processOrder(action, True, processVerbose) elif atype == 'limit': storePrice(action, dataLOB) l += 1 dataLOB.processOrder(action, True, processVerbose) elif action['type'] == 'cancel': dataLOB.cancelOrder(action['side'], action['idNum'], time=action['timestamp']) c += 1 elif action['type'] == 'modify': dataLOB.modifyOrder(action['idNum'], action, time=action['timestamp']) m += 1 time += 1 if verbose: print "\nCreated data map..." print "Limits: %d" % l print "Cancels: %d" % c print "Modifys: %d" % m print "Markets: %d" % ma
def genDataMap(self, startTime, endTime, verbose): ''' Runs the market with the order book data to create a map of times to prices. ''' def storePrice(action, lob): bestAsk = lob.getBestAsk() bestBid = lob.getBestBid() midPrice = None if bestAsk!=None and bestBid!=None: midPrice = bestBid + (bestAsk-bestBid)/2.0 self.dataOnlyPrices[action['uei']] = midPrice self.initiateDataModel(verbose) dataLOB = OrderBook() processVerbose = False if verbose: print "\nCreating data map..." l = 0 c = 0 m = 0 ma = 0 time = 0 dayEnded = False while not dayEnded: if time % 10000 == 0: print "time is %d" % time action, dayEnded = self.dataModel.getNextAction(dataLOB) if action != None: atype = action['type'] if atype == 'market': ma+=1 dataLOB.processOrder(action, True, processVerbose) elif atype =='limit': storePrice(action, dataLOB) l+=1 dataLOB.processOrder(action, True, processVerbose) elif action['type'] == 'cancel': dataLOB.cancelOrder(action['side'], action['idNum'], time = action['timestamp']) c+=1 elif action['type'] == 'modify': dataLOB.modifyOrder(action['idNum'], action, time = action['timestamp']) m+=1 time += 1 if verbose: print "\nCreated data map..." print "Limits: %d" % l print "Cancels: %d" % c print "Modifys: %d" % m print "Markets: %d" % ma
def __init__(self, usingAgents, symbol, filename=None): ''' Constructor ''' self.usingAgents = usingAgents self.dataModel = None self.inDatafile = filename self.symbol = symbol.ljust(6) self.traders = {} self.exchange = OrderBook() self.dataLOB = OrderBook() self.dataOnlyPrices = {} self.beforePrices = [] self.afterPrices = []
def __init__(self,numTran,frunflag,volume,time): self.trancnt=numTran self.episodeOver = False self.lastAction = {} self.lastObs = {} #self.lob = OrderBook() self.someOrders=[] self.maxask=1 self.maxbid=1 #self.bid_orders=self.simulate_current_order_book() self.fepisode_flag=False self.bestprice=0 self.trans_sorted=[] self.lob = OrderBook() if frunflag==False: self.bid_orders=self.simulate_current_order_book() self.lob=deepcopy(lob) self.calculate_optimal_return(volume,time)
''' Created on Apr 11, 2013 @author: Ash Booth For a full walkthrough of PyLOB functionality and usage, see the wiki @ https://github.com/ab24v07/PyLOB/wiki ''' if __name__ == '__main__': from PyLOB import OrderBook # Create a LOB object lob = OrderBook() ########### Limit Orders ############# # Create some limit orders someOrders = [{'type' : 'limit', 'side' : 'ask', 'qty' : 5, 'price' : 101, 'tid' : 100}, {'type' : 'limit', 'side' : 'ask', 'qty' : 5, 'price' : 103, 'tid' : 101}, {'type' : 'limit',
class EnvCls: def __init__(self,numTran,frunflag,volume,time): self.trancnt=numTran self.episodeOver = False self.lastAction = {} self.lastObs = {} #self.lob = OrderBook() self.someOrders=[] self.maxask=1 self.maxbid=1 #self.bid_orders=self.simulate_current_order_book() self.fepisode_flag=False self.bestprice=0 self.trans_sorted=[] self.lob = OrderBook() if frunflag==False: self.bid_orders=self.simulate_current_order_book() self.lob=deepcopy(lob) self.calculate_optimal_return(volume,time) #self.initbook=OrderBook() #self.initbook=deepcopy(self.lob) def env_start(self,volume,time,ep_flag): bbid,bbid_vol,bask,bask_vol=self.get_best_prices() self.lastObs = {"volume":volume,"time":time,"bbid":bbid,"bbid_vol":bbid_vol,"bask":bask,"bask_vol":bask_vol} self.episodeOver = False self.fepisode_flag=ep_flag return self.lastObs def simulate_current_order_book(self): self.someOrders,self.maxask,self.maxbid=order_book.generate_random_trans(self.trancnt,self.maxask,self.maxbid) """ Add orders to LOB""" for order in self.someOrders: trades, idNum = lob.processOrder(order, False, False) return lob.bids.orderMap def calculate_optimal_return(self,volume,time): All_trans_list=[] """This part of the code calculate the return by selling the remaining volume at the market price""" for key,value in self.lob.bids.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) All_trans_list.append(stock_data) All_trans_sorted=sorted(All_trans_list, key=lambda x: (-x[1], x[2])) return_val=0 volume_to_be_sold=volume for i in range(0,len(All_trans_sorted)): qty=All_trans_sorted[i][0] price=All_trans_sorted[i][1] if (qty<=volume_to_be_sold): return_val=return_val + (qty*price) volume_to_be_sold=(volume_to_be_sold - qty) if (volume_to_be_sold ==0): break else: return_val=return_val+(volume_to_be_sold*price) break print ("The optimal return from this environment is",return_val) def get_return_from_last_action(self): All_trans_list=[] """This part of the code calculate the return by selling the remaining volume at the market price""" for key,value in self.lob.bids.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) All_trans_list.append(stock_data) All_trans_sorted=sorted(All_trans_list, key=lambda x: (-x[1], x[2])) volume_to_be_sold=self.lastObs['volume'] reward=0 for i in range(0,len(All_trans_sorted)): qty=All_trans_sorted[i][0] price=All_trans_sorted[i][1] if (qty<=volume_to_be_sold): reward=reward + (qty*price) volume_to_be_sold=(volume_to_be_sold - qty) if (volume_to_be_sold ==0): break else: reward=reward+(volume_to_be_sold*price) break return reward,All_trans_sorted[0][1] def get_return_from_last_action1(self): """I am selling at the current market price""" volume_to_be_sold=self.lastAction['vol'] reward=0 #print "The last transaction array is",self.trans_sorted for i in range(0,len(self.trans_sorted)): qty=self.trans_sorted[i][0] price=self.trans_sorted[i][1] #print "The quantity and price to be sold is",qty,price if (qty<=volume_to_be_sold): reward=reward + (qty*price) volume_to_be_sold=(volume_to_be_sold - qty) if (volume_to_be_sold ==0): break else: reward=reward+(volume_to_be_sold*price) break #print "The reward out of the last action is",reward return reward,volume_to_be_sold def sell_current_market_price(self): self.trans_sorted=[] """This part of the code calculate the return by selling the remaining volume at the market price""" for key,value in self.lob.bids.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) self.trans_sorted.append(stock_data) self.trans_sorted=sorted(self.trans_sorted, key=lambda x: (-x[1], x[2])) #print "The length of sorted transaction is", len(self.trans_sorted),self.lastAction,self.lastObs return self.trans_sorted[0][1] """This function is for encoding extra features in the state encoding""" def get_best_prices(self): All_trans_list_bid=[] All_trans_list_ask=[] for key,value in self.lob.bids.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) All_trans_list_bid.append(stock_data) All_trans_sorted_bid=sorted(All_trans_list_bid, key=lambda x: (-x[1], x[2])) bbid=All_trans_sorted_bid[0][1] bbid_vol=All_trans_sorted_bid[0][0] for key,value in self.lob.asks.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) All_trans_list_ask.append(stock_data) All_trans_sorted_ask=sorted(All_trans_list_ask, key=lambda x: (-x[1], x[2])) bask=All_trans_sorted_ask[len(All_trans_sorted_ask)-1][1] bask_vol=All_trans_sorted_ask[len(All_trans_sorted_ask)-1][0] return bbid,bbid_vol,bask,bask_vol def get_return_from_each_action(self): All_trans_list=[] """This part of the code calculate the return by selling the volume specified in the action at the price specified in the action""" for key,value in self.lob.bids.orderMap.iteritems(): stock_data=(value.qty,value.price,value.timestamp) All_trans_list.append(stock_data) All_trans_sorted=sorted(All_trans_list, key=lambda x: (-x[1], x[2])) volume_to_be_sold=self.lastAction['vol'] price_to_be_sold=self.lastAction['price'] #print "The limit order book array is" #print All_trans_sorted """Initialialization of reward""" #print "volume and price in which to be sold",volume_to_be_sold,price_to_be_sold reward=0 for i in range(0,len(All_trans_sorted)): price=All_trans_sorted[i][1] qty=All_trans_sorted[i][0] if price< price_to_be_sold: break else: if qty<=volume_to_be_sold: reward=reward + (qty*price) volume_to_be_sold=(volume_to_be_sold - qty) if (volume_to_be_sold ==0): break else: reward=reward+(volume_to_be_sold * price) break #print reward return reward,volume_to_be_sold def env_step(self,thisAction,ep_flag): reward=0 if self.lastObs['time']==0: thisAction['vol']= self.lastObs['volume'] thisAction['price']=0 self.lastAction=thisAction """creates the new state s'""" thisObs={'volume':self.lastObs['volume']-thisAction['vol'],'time':self.lastObs['time']-1} #print 'thisObs is',thisObs if (thisObs['volume']==0 and thisObs['time']==-1): self.episodeOver=True reward,price=self.get_return_from_last_action() self.lastAction['vol']=self.lastObs['volume'] self.lastAction['price']=price elif (thisObs['volume']==0 and thisObs['time'] > 0): self.episodeOver=True #print 'I am setting the episode over flag to False' reward=self.get_return_from_each_action() else: reward=self.get_return_from_each_action() self.lastObs=thisObs if self.fepisode_flag==False: self.bid_orders=self.simulate_current_order_book() return thisObs,reward,self.episodeOver def execute_action_on_current_limitbook(self): LimitOrder = { 'type' : 'limit', 'side' : 'ask', 'qty' : self.lastAction['vol'], 'price': self.lastAction['price'] , 'tid' : self.maxask } self.maxask=self.maxask+1 trades, orderInBook = self.lob.processOrder(LimitOrder, False, False) def env_end(self,reward): print "This is the environment end step" """The new environment step written by sdas""" def env_step1(self,thisAction,ep_flag): reward=0 #print "Action in the environment is",thisAction self.lastAction=thisAction if self.lastObs['time']==1: reward,vol_unexec=self.get_return_from_last_action1() else: reward,vol_unexec=self.get_return_from_each_action() #thisObs={'volume':self.lastObs['volume']-thisAction['vol'],'time':self.lastObs['time']-1} #self.lastObs=thisObs #print"The new state returned by environemnt is",self.lastObs self.execute_action_on_current_limitbook() bbid,bbid_vol,bask,bask_vol=self.get_best_prices() thisObs={'volume':vol_unexec,'time':self.lastObs['time']-1,"bbid":bbid,"bbid_vol":bbid_vol,"bask":bask,"bask_vol":bask_vol} self.lastObs=thisObs # """**********************************************************""" # """This portion is just used for Testing purpose""" # All_trans_bid=[] # All_trans_ask=[] # for key,value in self.lob.bids.orderMap.iteritems(): # # stock_data=(value.qty,value.price,value.timestamp) # All_trans_bid.append(stock_data) # # All_trans_bid_sorted=sorted(All_trans_bid, key=lambda x: (-x[1], x[2])) # # for key,value in self.lob.asks.orderMap.iteritems(): # # stock_data=(value.qty,value.price,value.timestamp) # All_trans_ask.append(stock_data) # # All_trans_ask_sorted=sorted(All_trans_ask, key=lambda x: (-x[1], x[2])) # # print "The current bid side lob is", All_trans_bid_sorted # print "The current ask side lob is", All_trans_ask_sorted # """**********************************************************""" if (thisObs['time']==1): self.bestprice=self.sell_current_market_price() if (thisObs['volume']==0 or thisObs['time']==0): self.episodeOver=True """This change was done so that transactions are only inserted at every time step of the first episode, beyond that it remains constant""" #if self.fepisode_flag==False: #self.bid_orders=self.simulate_current_order_book() """This change was done for just one stable environment accross all episodes""" if self.episodeOver==True: #self.lob=deepcopy(self.initbook) self.lob=deepcopy(lob) #print self.lob #print "The length of the order book when episode ends is",len(self.lob.bids.orderMap) return thisObs,reward,self.bestprice,self.episodeOver
# -*- coding: utf-8 -*- """ Created on Fri Apr 22 22:05:37 2016 @author: maydas """ from PyLOB import OrderBook from copy import deepcopy import numpy as np import random as rand import simulate_limit_order_book as order_book import math lob=OrderBook() class EnvCls: def __init__(self,numTran,frunflag,volume,time): self.trancnt=numTran self.episodeOver = False self.lastAction = {} self.lastObs = {} #self.lob = OrderBook() self.someOrders=[] self.maxask=1 self.maxbid=1 #self.bid_orders=self.simulate_current_order_book() self.fepisode_flag=False self.bestprice=0 self.trans_sorted=[] self.lob = OrderBook() if frunflag==False:
class Market(object): ''' classdocs ''' def __init__(self, usingAgents, symbol, filename=None): ''' Constructor ''' self.usingAgents = usingAgents self.dataModel = None self.inDatafile = filename self.symbol = symbol.ljust(6) self.traders = {} self.exchange = OrderBook() self.dataLOB = OrderBook() self.dataOnlyPrices = {} self.beforePrices = [] self.afterPrices = [] def initiateDataModel(self, verbose): ''' Creates the traders from traders_spec. Returns tuple (buyers, sellers). ''' self.dataModel = DataModel(self.symbol) self.dataModel.readFile(self.inDatafile, verbose) def populateMarket(self, tradersSpec, verbose): ''' Creates the traders from traders_spec. Returns tuple (buyers, sellers). ''' def traderType(agentType, name): if agentType == 'MM': return MarketMaker('MM', name, 0.00) elif agentType == 'HFT': return HFT('HFT', name, 0.00) elif agentType == 'FBYR': return FBuyer('FBYR', name, 0.00) elif agentType == 'FSLR': return FSeller('FSLR', name, 0.00) else: sys.exit("FATAL: don't know agent type %s\n" % agentType) if self.usingAgents: n_agents = 0 for agentType in tradersSpec: ttype = agentType[0] for a in range(agentType[1]): tname = 'B%02d' % n_agents # Trader i.d. string self.traders[tname] = traderType(ttype, tname) n_agents += 1 if n_agents < 1: print 'WARNING: No agents specified\n' if self.usingAgents and verbose : for t in range(n_agents): name = 'B%02d' % t print(self.traders[name]) def tradeStats(self, expid, dumpfile, time): ''' Dumps CSV statistics on self.exchange data and trader population to file for later analysis. ''' if self.usingAgents: trader_types = {} for t in self.traders: ttype = self.traders[t].ttype if ttype in trader_types.keys(): t_balance = (trader_types[ttype]['balance_sum'] + self.traders[t].balance) n = trader_types[ttype]['n'] + 1 else: t_balance = self.traders[t].balance n = 1 trader_types[ttype] = {'n':n, 'balance_sum':t_balance} dumpfile.write('%s, %06d, ' % (expid, time)) for ttype in sorted(list(trader_types.keys())): n = trader_types[ttype]['n'] s = trader_types[ttype]['balance_sum'] dumpfile.write('%s, %d, %d, %f, ' % (ttype, s, n, s / float(n))) dumpfile.write('\n'); def plotPrices(self): import pylab prices = [] times = [] print "Num of trades = ", len(self.exchange.tape) for tapeitem in self.exchange.tape: prices.append(tapeitem['price']) times.append(tapeitem['time']) pylab.plot(times,prices,'k') pylab.show() def run(self, sessId, startTime, endTime, traderSpec, dumpFile, agentProb): ''' One day run of the market ''' # def printTime(milliseconds): # x = milliseconds / 1000 # seconds = x % 60 # x /= 60 # minutes = x % 60 # x /= 60 # hours = x % 24 self.dataModel.resetModel() self.populateMarket(traderSpec, False) processVerbose = False respondVerbose = False bookkeepVerbose = False time = startTime time = 0 day_ended = False while not day_ended: timeLeft = (endTime - time) / endTime if time % 10000 == 0: print "time is %d" % time trades = [] if self.usingAgents and random.random() < agentProb: # Get action from agents tid = random.choice(self.traders.keys()) action = self.traders[tid].getAction(time, timeLeft, self.exchange) fromData = False else: # Get action from data action, day_ended = self.dataModel.getNextAction(self.exchange) fromData = True if action != None: if 'timestamp' in action: time = action['timestamp'] atype = action['type'] if atype == 'market' or atype =='limit': if fromData: if atype == 'limit': oriBA = self.dataLOB.getBestAsk() oriBB = self.dataLOB.getBestBid() newBA = self.exchange.getBestAsk() newBB = self.exchange.getBestBid() if (oriBA != None and oriBB != None and newBA != None and newBB != None): oriMid = oriBB + (oriBA-oriBB)/2.0 newMid = newBB + (newBA-newBB)/2.0 deviation = ((action['price']-oriMid) / oriMid) newPrice = newMid + (newMid*deviation) if newPrice != action['price']: print newPrice print action['price'] print '\n' pass action['price'] = newPrice self.dataLOB.processOrder(action, True, processVerbose) res_trades, orderInBook = self.exchange.processOrder(action, fromData, processVerbose) if res_trades: for t in res_trades: trades.append(t) elif action['type'] == 'cancel': if fromData: self.exchange.cancelOrder(action['side'], action['idNum'], time = action['timestamp']) self.dataLOB.cancelOrder(action['side'], action['idNum'], time = action['timestamp']) else: self.exchange.cancelOrder(action['side'], action['idNum']) elif action['type'] == 'modify': if fromData: self.exchange.modifyOrder(action['idNum'], action, time = action['timestamp']) self.dataLOB.modifyOrder(action['idNum'], action, time = action['timestamp']) else: self.exchange.modifyOrder(action['idNum'], action) # Does the originating trader need to be informed of limit # orders that have been put in the book? if orderInBook and not fromData: self.traders[tid].orderInBook(orderInBook) for trade in trades: # Counter-parties update order lists and blotters if trade['party1'][0] != -1: self.traders[trade['party1'][0]].bookkeep(trade, trade['party1'][1], trade['party1'][2], bookkeepVerbose) if trade['party2'][0] != -1: self.traders[trade['party2'][0]].bookkeep(trade, trade['party2'][1], trade['party2'][2], bookkeepVerbose) # Traders respond to whatever happened if self.usingAgents: for t in self.traders.keys(): self.traders[t].respond(time, self.exchange, trade, respondVerbose) time+=1 print "experiment finished..." # end of an experiment -- dump the tape self.exchange.tapeDump('transactions.csv', 'w', 'keep') # write trade_stats for this experiment NB end-of-session summary only self.tradeStats(sessId, dumpFile, time) def genDataMap(self, startTime, endTime, verbose): ''' Runs the market with the order book data to create a map of times to prices. ''' def storePrice(action, lob): bestAsk = lob.getBestAsk() bestBid = lob.getBestBid() midPrice = None if bestAsk!=None and bestBid!=None: midPrice = bestBid + (bestAsk-bestBid)/2.0 self.dataOnlyPrices[action['uei']] = midPrice self.initiateDataModel(verbose) dataLOB = OrderBook() processVerbose = False if verbose: print "\nCreating data map..." l = 0 c = 0 m = 0 ma = 0 time = 0 dayEnded = False while not dayEnded: if time % 10000 == 0: print "time is %d" % time action, dayEnded = self.dataModel.getNextAction(dataLOB) if action != None: atype = action['type'] if atype == 'market': ma+=1 dataLOB.processOrder(action, True, processVerbose) elif atype =='limit': storePrice(action, dataLOB) l+=1 dataLOB.processOrder(action, True, processVerbose) elif action['type'] == 'cancel': dataLOB.cancelOrder(action['side'], action['idNum'], time = action['timestamp']) c+=1 elif action['type'] == 'modify': dataLOB.modifyOrder(action['idNum'], action, time = action['timestamp']) m+=1 time += 1 if verbose: print "\nCreated data map..." print "Limits: %d" % l print "Cancels: %d" % c print "Modifys: %d" % m print "Markets: %d" % ma
class Market(object): ''' classdocs ''' def __init__(self, usingAgents, symbol, filename=None): ''' Constructor ''' self.usingAgents = usingAgents self.dataModel = None self.inDatafile = filename self.symbol = symbol.ljust(6) self.traders = {} self.exchange = OrderBook() self.dataLOB = OrderBook() self.dataOnlyPrices = {} self.beforePrices = [] self.afterPrices = [] def initiateDataModel(self, verbose): ''' Creates the traders from traders_spec. Returns tuple (buyers, sellers). ''' self.dataModel = DataModel(self.symbol) self.dataModel.readFile(self.inDatafile, verbose) def populateMarket(self, tradersSpec, verbose): ''' Creates the traders from traders_spec. Returns tuple (buyers, sellers). ''' def traderType(agentType, name): if agentType == 'MM': return MarketMaker('MM', name, 0.00) elif agentType == 'HFT': return HFT('HFT', name, 0.00) elif agentType == 'FBYR': return FBuyer('FBYR', name, 0.00) elif agentType == 'FSLR': return FSeller('FSLR', name, 0.00) else: sys.exit("FATAL: don't know agent type %s\n" % agentType) if self.usingAgents: n_agents = 0 for agentType in tradersSpec: ttype = agentType[0] for a in range(agentType[1]): tname = 'B%02d' % n_agents # Trader i.d. string self.traders[tname] = traderType(ttype, tname) n_agents += 1 if n_agents < 1: print 'WARNING: No agents specified\n' if self.usingAgents and verbose: for t in range(n_agents): name = 'B%02d' % t print(self.traders[name]) def tradeStats(self, expid, dumpfile, time): ''' Dumps CSV statistics on self.exchange data and trader population to file for later analysis. ''' if self.usingAgents: trader_types = {} for t in self.traders: ttype = self.traders[t].ttype if ttype in trader_types.keys(): t_balance = (trader_types[ttype]['balance_sum'] + self.traders[t].balance) n = trader_types[ttype]['n'] + 1 else: t_balance = self.traders[t].balance n = 1 trader_types[ttype] = {'n': n, 'balance_sum': t_balance} dumpfile.write('%s, %06d, ' % (expid, time)) for ttype in sorted(list(trader_types.keys())): n = trader_types[ttype]['n'] s = trader_types[ttype]['balance_sum'] dumpfile.write('%s, %d, %d, %f, ' % (ttype, s, n, s / float(n))) dumpfile.write('\n') def plotPrices(self): import pylab prices = [] times = [] print "Num of trades = ", len(self.exchange.tape) for tapeitem in self.exchange.tape: prices.append(tapeitem['price']) times.append(tapeitem['time']) pylab.plot(times, prices, 'k') pylab.show() def run(self, sessId, startTime, endTime, traderSpec, dumpFile, agentProb): ''' One day run of the market ''' # def printTime(milliseconds): # x = milliseconds / 1000 # seconds = x % 60 # x /= 60 # minutes = x % 60 # x /= 60 # hours = x % 24 self.dataModel.resetModel() self.populateMarket(traderSpec, False) processVerbose = False respondVerbose = False bookkeepVerbose = False time = startTime time = 0 day_ended = False while not day_ended: timeLeft = (endTime - time) / endTime if time % 10000 == 0: print "time is %d" % time trades = [] if self.usingAgents and random.random() < agentProb: # Get action from agents tid = random.choice(self.traders.keys()) action = self.traders[tid].getAction(time, timeLeft, self.exchange) fromData = False else: # Get action from data action, day_ended = self.dataModel.getNextAction(self.exchange) fromData = True if action != None: if 'timestamp' in action: time = action['timestamp'] atype = action['type'] if atype == 'market' or atype == 'limit': if fromData: if atype == 'limit': oriBA = self.dataLOB.getBestAsk() oriBB = self.dataLOB.getBestBid() newBA = self.exchange.getBestAsk() newBB = self.exchange.getBestBid() if (oriBA != None and oriBB != None and newBA != None and newBB != None): oriMid = oriBB + (oriBA - oriBB) / 2.0 newMid = newBB + (newBA - newBB) / 2.0 deviation = ((action['price'] - oriMid) / oriMid) newPrice = newMid + (newMid * deviation) if newPrice != action['price']: print newPrice print action['price'] print '\n' pass action['price'] = newPrice self.dataLOB.processOrder(action, True, processVerbose) res_trades, orderInBook = self.exchange.processOrder( action, fromData, processVerbose) if res_trades: for t in res_trades: trades.append(t) elif action['type'] == 'cancel': if fromData: self.exchange.cancelOrder(action['side'], action['idNum'], time=action['timestamp']) self.dataLOB.cancelOrder(action['side'], action['idNum'], time=action['timestamp']) else: self.exchange.cancelOrder(action['side'], action['idNum']) elif action['type'] == 'modify': if fromData: self.exchange.modifyOrder(action['idNum'], action, time=action['timestamp']) self.dataLOB.modifyOrder(action['idNum'], action, time=action['timestamp']) else: self.exchange.modifyOrder(action['idNum'], action) # Does the originating trader need to be informed of limit # orders that have been put in the book? if orderInBook and not fromData: self.traders[tid].orderInBook(orderInBook) for trade in trades: # Counter-parties update order lists and blotters if trade['party1'][0] != -1: self.traders[trade['party1'][0]].bookkeep( trade, trade['party1'][1], trade['party1'][2], bookkeepVerbose) if trade['party2'][0] != -1: self.traders[trade['party2'][0]].bookkeep( trade, trade['party2'][1], trade['party2'][2], bookkeepVerbose) # Traders respond to whatever happened if self.usingAgents: for t in self.traders.keys(): self.traders[t].respond(time, self.exchange, trade, respondVerbose) time += 1 print "experiment finished..." # end of an experiment -- dump the tape self.exchange.tapeDump('transactions.csv', 'w', 'keep') # write trade_stats for this experiment NB end-of-session summary only self.tradeStats(sessId, dumpFile, time) def genDataMap(self, startTime, endTime, verbose): ''' Runs the market with the order book data to create a map of times to prices. ''' def storePrice(action, lob): bestAsk = lob.getBestAsk() bestBid = lob.getBestBid() midPrice = None if bestAsk != None and bestBid != None: midPrice = bestBid + (bestAsk - bestBid) / 2.0 self.dataOnlyPrices[action['uei']] = midPrice self.initiateDataModel(verbose) dataLOB = OrderBook() processVerbose = False if verbose: print "\nCreating data map..." l = 0 c = 0 m = 0 ma = 0 time = 0 dayEnded = False while not dayEnded: if time % 10000 == 0: print "time is %d" % time action, dayEnded = self.dataModel.getNextAction(dataLOB) if action != None: atype = action['type'] if atype == 'market': ma += 1 dataLOB.processOrder(action, True, processVerbose) elif atype == 'limit': storePrice(action, dataLOB) l += 1 dataLOB.processOrder(action, True, processVerbose) elif action['type'] == 'cancel': dataLOB.cancelOrder(action['side'], action['idNum'], time=action['timestamp']) c += 1 elif action['type'] == 'modify': dataLOB.modifyOrder(action['idNum'], action, time=action['timestamp']) m += 1 time += 1 if verbose: print "\nCreated data map..." print "Limits: %d" % l print "Cancels: %d" % c print "Modifys: %d" % m print "Markets: %d" % ma
''' Created on Apr 11, 2013 @author: Ash Booth For a full walkthrough of PyLOB functionality and usage, see the wiki @ https://github.com/ab24v07/PyLOB/wiki ''' if __name__ == '__main__': from PyLOB import OrderBook # Create a LOB object lob = OrderBook() ########### Limit Orders ############# # Create some limit orders someOrders = [ { 'type': 'limit', 'side': 'ask', 'qty': 5, 'price': 101, 'tid': 100 }, { 'type': 'limit', 'side': 'ask',
All_trans_bid = sorted(All_trans_bid, key=lambda x: (-x[1], x[2])) for key, value in lob.asks.orderMap.iteritems(): stock_data = (value.qty, value.price, value.timestamp) All_trans_ask.append(stock_data) All_trans_ask = sorted(All_trans_ask, key=lambda x: (x[1], x[2])) return All_trans_bid[0][1], All_trans_ask[0][1] if __name__ == '__main__': from PyLOB import OrderBook """ Create a LOB object""" lob = OrderBook() """########### Limit Orders #############""" trancount = 5000 maxask = 1 maxbid = 1 i = 0 while (True): if (i == 1): break someOrders, maxask, maxbid = generate_random_trans_with_average( trancount, maxask, maxbid, 240, 140, 50, 50) """ Add orders to LOB""" for order in someOrders: trades, idNum = lob.processOrder(order, False, False) i = i + 1
def getVWAPs(self, startTime, endTime, numBins, symbol, numDays=30): symbol = symbol.ljust(6) if numDays>self.numFiles: print "\nWarning, do not have enough data for %d day VWAP" % numDays print "capping to %d\n" % self.numFiles numDays = self.numFiles props = [] lenOfDay = endTime-startTime binSize = lenOfDay/numBins for d in range(numDays): print "Day %d" % d f = self.files[d] dataModel = DataModel(symbol) dataModel.readFile(f, False) exchange = OrderBook() processVerbose = False time = startTime day_ended = False vols = [] for i in range(numBins): vols.append(0) while not day_ended: # Get action from data action, day_ended = dataModel.getNextAction(exchange) if day_ended: pass if action != None: time = action['timestamp'] atype = action['type'] if atype == 'market' or atype =='limit': res_trades, orderInBook = exchange.processOrder(action, True, processVerbose) if res_trades: for t in res_trades: if (time-startTime)/binSize >= len(vols): pass vols[(time-startTime)/binSize] += t['qty'] elif action['type'] == 'cancel': exchange.cancelOrder(action['side'], action['idNum'], time = action['timestamp']) elif action['type'] == 'modify': exchange.modifyOrder(action['idNum'], action, time = action['timestamp']) # Day over, work out proportions and add to props totalVol = sum(vols) props.append([v/float(totalVol) for v in vols]) print "volumes: ", vols print "proportions: ", [v/float(totalVol) for v in vols], '\n' # Calculate average props sums = [0 for i in range(numBins)] for day in props: for bin_index in range(numBins): sums[bin_index] += day[bin_index] return [s/numDays for s in sums]
def getVWAPs(self, startTime, endTime, numBins, symbol, numDays=30): symbol = symbol.ljust(6) if numDays > self.numFiles: print "\nWarning, do not have enough data for %d day VWAP" % numDays print "capping to %d\n" % self.numFiles numDays = self.numFiles props = [] lenOfDay = endTime - startTime binSize = lenOfDay / numBins for d in range(numDays): print "Day %d" % d f = self.files[d] dataModel = DataModel(symbol) dataModel.readFile(f, False) exchange = OrderBook() processVerbose = False time = startTime day_ended = False vols = [] for i in range(numBins): vols.append(0) while not day_ended: # Get action from data action, day_ended = dataModel.getNextAction(exchange) if day_ended: pass if action != None: time = action['timestamp'] atype = action['type'] if atype == 'market' or atype == 'limit': res_trades, orderInBook = exchange.processOrder( action, True, processVerbose) if res_trades: for t in res_trades: if (time - startTime) / binSize >= len(vols): pass vols[(time - startTime) / binSize] += t['qty'] elif action['type'] == 'cancel': exchange.cancelOrder(action['side'], action['idNum'], time=action['timestamp']) elif action['type'] == 'modify': exchange.modifyOrder(action['idNum'], action, time=action['timestamp']) # Day over, work out proportions and add to props totalVol = sum(vols) props.append([v / float(totalVol) for v in vols]) print "volumes: ", vols print "proportions: ", [v / float(totalVol) for v in vols], '\n' # Calculate average props sums = [0 for i in range(numBins)] for day in props: for bin_index in range(numBins): sums[bin_index] += day[bin_index] return [s / numDays for s in sums]