def tick_handler(self, session, tick): print('hit tick handler') print('tick', tick) securities = api_calls.get_securities(session) for ticker in self.tickers: tas_data = api_calls.get_tas_param(session, ticker, self.lookback) total_volume = 0 self.data[ticker]['VWAP'] = 0 for j in tas_data: self.data[ticker]['VWAP'] += (j['quantity'] * j['price']) total_volume += j['quantity'] if total_volume > 0: self.data[ticker][ 'VWAP'] = self.data[ticker]['VWAP'] / total_volume else: for h in securities: if h['ticker'] == ticker: self.data[ticker]['VWAP'] = h['start_price'] self.data[ticker]['estimated_max_vol'] = total_volume * (tick / 300) for j in securities: if j['ticker'] == ticker: self.data[ticker]['current_stock'] = j['position'] self.limits = api_calls.get_limits(session) tenders = api_calls.get_tenders(session) for i in tenders: if i['action'] == "BUY": self.buy_handler(i, session) else: self.sell_handler(i, session) for i in self.tickers: self.trading(i, tick, session)
def tick_handler(self, session, tick): print('hit tick handler') print('tick', tick) self.limits = api_calls.get_limits(session) if (self.limits['gross']>self.limits['gross_limit']) or (self.limits['net']>self.limits['net_limit']): print('limit somehow exceeded') print(self.limits) raise Exception self.trade_limit = { 'TAME': False, 'CRZY': False } if tick == 0 or tick == 300: return securities = api_calls.get_securities(session) for ticker in self.tickers: tas_data = api_calls.get_tas(session, ticker) total_volume = 0 self.data[ticker]['VWAP'] = 0 for j in tas_data: self.data[ticker]['VWAP'] += (j['quantity']*j['price']) total_volume += j['quantity'] if total_volume > 0: self.data[ticker]['VWAP'] = self.data[ticker]['VWAP']/total_volume else: for h in securities: if h['ticker']==ticker: self.data[ticker]['VWAP']= h['start_price'] for h in securities: if h['ticker'] == ticker: self.data[ticker]['current_price'] = (h['bid']+h['ask'])/2 self.data[ticker]['bid'] = h['bid'] self.data[ticker]['ask'] = h['ask'] self.data[ticker]['estimated_max_vol'] = total_volume * (300/tick) for j in securities: if j['ticker']==ticker: self.data[ticker]['current_stock']=j['position'] if tick >= 50: history = api_calls.get_history(session,ticker) stdev = [] for i in history: stdev.append(i['close']) self.data[ticker]['std'] = numpy.std(stdev) self.limits = api_calls.get_limits(session) tenders = api_calls.get_tenders(session) for i in tenders: if i['action'] == "BUY": self.buy_handler(i, session, tick) else: self.sell_handler(i, session, tick) self.limits = api_calls.get_limits(session) if (self.limits['gross']>self.limits['gross_limit']) or (self.limits['net']>self.limits['net_limit']): print('limit somehow exceeded') print(self.limits) raise Exception self.unwind_handler(tick, session)
def gather_data(self, tick, tickers, s): if tick == 300: for ticker in tickers: self.payload[ticker]['tas'] = api_calls.get_tas(s, ticker) self.payload[ticker]['history'] = api_calls.get_history( s, ticker) self.payload['trader']['tick_data'].append({ "tick": tick, "nlv": api_calls.get_trader_data(s)['nlv'] }) self.payload['orders'] = api_calls.get_all_orders(s) self.db_insert() else: for ticker in tickers: if ticker not in self.payload: self.payload[ticker] = {"tick_data": []} self.payload[ticker]["tick_data"].append({ "tick": tick, "book": api_calls.get_trading_data(s, ticker) }) self.payload['tenders']['tick_data'].append({ "tick": tick, "tenders": api_calls.get_tenders(s) }) securities = api_calls.get_securities(s) temp = {} for security in securities: temp[security['ticker']] = { "VWAP": security['vwap'], "Position": security['position'], "Volume": security['volume'] } self.payload['trader']['tick_data'].append({ "tick": tick, "nlv": api_calls.get_trader_data(s)['nlv'], "securities_data": temp })