def order_target_percent_quantopian(self, security, percent, style=MarketOrder()): print 'place_order_percent_value' import math hold=self.how_many_I_am_holding(security, style='portfolio_percentage') for ct in self.data: if same_security(security, ct): return self.order_quantopian(security, int(math.floor((percent-hold)*self.context.portfolio.portfolio_value/self.data[ct].price)) , style=style)
def order_target_value_quantopian(self, security, value, style=MarketOrder()): print 'place_order_target_value' import math hold=self.how_many_I_am_holding(security, style='value') for ct in self.data: if same_security(security, ct): return self.order_quantopian(security, int(math.floor((value-hold)/self.data[ct].price)) , style=style)
def order_value_quantopian(self, security, value, style=MarketOrder()): print 'order_value_quantopian' import math for ct in self.data: if same_security(security, ct): return self.order_quantopian( security, int(math.floor(value / self.data[ct].price)), style=style)
def order_percent_quantopian(self, security, percent, style=MarketOrder()): print 'order_percent_quantopian' import math for ct in self.data: if same_security(security, ct): return self.order_quantopian( security, int( math.floor(self.context.portfolio.portfolio_value / self.data[ct].price)), style=style)
def order_target_value_quantopian(self, security, value, style=MarketOrder()): print 'place_order_target_value' import math hold = self.how_many_I_am_holding(security, style='value') for ct in self.data: if same_security(security, ct): return self.order_quantopian( security, int(math.floor((value - hold) / self.data[ct].price)), style=style)
def get_open_order_quantopian(self, sid=None): """ function to get open orders similar to that defined in Quantopian """ if sid==None: result={} for ct in self.context.portfolio.openOrderBook: result[self.context.portfolio.openOrderBook[ct].sid]=self.context.portfolio.openOrderBook[ct] return result else: result={} for ct in self.context.portfolio.openOrderBook: if same_security(self.context.portfolio.openOrderBook[ct].sid,sid): result[self.context.portfolio.openOrderBook[ct].sid]=self.context.portfolio.openOrderBook[ct] return result
def how_many_I_am_holding(self, security, style='shares'): """ return the current holdings of a security, in styles of shares, value or percentage """ for ct in self.context.portfolio.positions: if same_security(ct,security): if style=='shares': return self.context.portfolio.positions[ct].amount if style=='value': return self.context.portfolio.positions[ct].amount*self.context.portfolio.positions[ct].last_sale_price if style=='portfolio_percentage': if self.context.portfolio.portfolio_value > 0.00001: # self.throwError('how_many_I_am_holding','Zero portfolio value') return self.context.portfolio.positions[ct].last_sale_price / self.context.portfolio.portfolio_value return 0
def order_target_percent_quantopian(self, security, percent, style=MarketOrder()): print 'place_order_percent_value' import math hold = self.how_many_I_am_holding(security, style='portfolio_percentage') for ct in self.data: if same_security(security, ct): return self.order_quantopian( security, int( math.floor((percent - hold) * self.context.portfolio.portfolio_value / self.data[ct].price)), style=style)
def get_open_order_quantopian(self, sid=None): """ function to get open orders similar to that defined in Quantopian """ if sid == None: result = {} for ct in self.context.portfolio.openOrderBook: result[self.context.portfolio.openOrderBook[ct]. sid] = self.context.portfolio.openOrderBook[ct] return result else: result = {} for ct in self.context.portfolio.openOrderBook: if same_security(self.context.portfolio.openOrderBook[ct].sid, sid): result[self.context.portfolio.openOrderBook[ct]. sid] = self.context.portfolio.openOrderBook[ct] return result
def how_many_I_am_holding(self, security, style='shares'): """ return the current holdings of a security, in styles of shares, value or percentage """ for ct in self.context.portfolio.positions: if same_security(ct, security): if style == 'shares': return self.context.portfolio.positions[ct].amount if style == 'value': return self.context.portfolio.positions[ ct].amount * self.context.portfolio.positions[ ct].last_sale_price if style == 'portfolio_percentage': if self.context.portfolio.portfolio_value > 0.00001: # self.throwError('how_many_I_am_holding','Zero portfolio value') return self.context.portfolio.positions[ ct].last_sale_price / self.context.portfolio.portfolio_value return 0
def runAlgorithm(self): time.sleep(0.1) # sleep to avoid exceeding IB's max data rate self.reqCurrentTime() # initialize if self.traderState.is_state(self.traderState.INIT): if self.accountManagerState.is_state(self.accountManagerState.INIT): self.log.info(__name__ + ": " + "entering INIT stage") print 'entering INIT stage' self.req_real_time_price() # request market data self.reqAccountUpdates(True,self.accountCode) # Request to update account info # Request multiple hist price self.re_send = 0 self.returned_hist= {} for security in self.data: for period in self.context.hist_frame: self.req_hist_price(security, endtime=datetime.datetime.now(), barSize=period) self.set_timer() # change machine state self.accountManagerState.set_state( self.accountManagerState.WAIT_FOR_INIT_CALLBACK) if self.accountManagerState.is_state( self.accountManagerState.WAIT_FOR_INIT_CALLBACK): self.check_timer(self.accountManagerState.WAIT_FOR_INIT_CALLBACK,20) if self.req_hist_price_check_end(): for req_id in self.returned_hist: for security in self.data: if same_security(security, self.returned_hist[req_id].security): self.data[security].hist[self.returned_hist[req_id].period]=self.returned_hist[req_id].hist self.traderState.set_state(self.traderState.TRADE) self.accountManagerState.set_state(self.accountManagerState.SLEEP) self.log.info(__name__ + ": " + "completing init stage") print 'INIT stage completed' # if self.traderState.is_state(self.traderState.TRADE): # at the beginning of every minute, update hist and accountINFO if self.stime.second==0 and self.stime_previous.second!=0: #print 'beginning of minute' self.re_send = 0 self.returned_hist= {} for security in self.data: for period in self.context.hist_frame: if period=='1 min': goback='120 S' elif period=='1 day': goback='2 D' elif period=='1 hour': goback='7200 S' elif period=='4 hours': goback='1 D' elif period=='10 mins': goback='1200 S' self.req_hist_price(security, endtime=self.stime, goback=goback, barSize=period) self.set_timer() self.accountManagerState.set_state(self.accountManagerState.WAIT_FOR_BAR_PRICE_CALLBACK) if self.accountManagerState.is_state(self.accountManagerState.WAIT_FOR_BAR_PRICE_CALLBACK): if self.req_hist_price_check_end(): # After receive the new hist, combine them with the old hist for req_id in self.returned_hist: #print self.returned_hist[req_id].hist for security in self.data: if same_security(security, self.returned_hist[req_id].security): temp=self.data[security].hist[self.returned_hist[req_id].period] empty=pd.DataFrame(columns=['open','high','low','close','volume']) for index in temp.index: if index not in self.returned_hist[req_id].hist.index: empty=empty.append(temp.loc[index]) self.data[security].hist[self.returned_hist[req_id].period]=empty.append(self.returned_hist[req_id].hist) #print empty #print self.returned_hist[req_id].hist #print self.data[security].hist[self.returned_hist[req_id].period] #self.data[security].hist[self.returned_hist[req_id].period]= \ #temp.append(self.returned_hist[req_id].hist.loc[index]) for security in self.data: for period in self.context.hist_frame: if len(self.data[security].hist[period])>300: self.data[security].hist[period]=self.data[security].hist[period][-300:] #print self.data[security].hist[period] self.accountManagerState.set_state(self.accountManagerState.EVERY_BAR_RUN) # Run handle_data regularly handle_data(self.context, self.data) #print self.stime self.stime_previous=self.stime
def runAlgorithm(self): time.sleep(0.1) # sleep to avoid exceeding IB's max data rate self.reqCurrentTime() # initialize if self.traderState.is_state(self.traderState.INIT): if self.accountManagerState.is_state(self.accountManagerState.INIT): self.log.info(__name__ + ": " + "entering INIT stage") print 'entering INIT stage' self.req_real_time_price() # request market data self.reqAccountUpdates(True,self.accountCode) # Request to update account info # Request multiple hist price self.re_send = 0 self.returned_hist= {} for security in self.data: for period in self.context.hist_frame: self.req_hist_price(security, endtime=datetime.datetime.now(), barSize=period) self.set_timer() # change machine state self.accountManagerState.set_state( self.accountManagerState.WAIT_FOR_INIT_CALLBACK) if self.accountManagerState.is_state( self.accountManagerState.WAIT_FOR_INIT_CALLBACK): self.check_timer(self.accountManagerState.WAIT_FOR_INIT_CALLBACK,10) if self.req_hist_price_check_end(): for req_id in self.returned_hist: for security in self.data: if same_security(security, self.returned_hist[req_id].security): self.data[security].hist[self.returned_hist[req_id].period]=self.returned_hist[req_id].hist self.traderState.set_state(self.traderState.TRADE) self.accountManagerState.set_state(self.accountManagerState.SLEEP) self.log.info(__name__ + ": " + "completing init stage") print 'INIT stage completed' # if self.traderState.is_state(self.traderState.TRADE): # at the beginning of every minute, update hist and accountINFO if self.stime.second==0 and self.stime_previous.second!=0: #print 'beginning of minute' self.re_send = 0 self.returned_hist= {} for security in self.data: for period in self.context.hist_frame: if period=='1 min': goback='120 S' elif period=='1 day': goback='2 D' elif period=='1 hour': goback='7200 S' elif period=='4 hours': goback='1 D' elif period=='10 mins': goback='1200 S' self.req_hist_price(security, endtime=self.stime, goback=goback, barSize=period) self.set_timer() self.accountManagerState.set_state(self.accountManagerState.WAIT_FOR_BAR_PRICE_CALLBACK) if self.accountManagerState.is_state(self.accountManagerState.WAIT_FOR_BAR_PRICE_CALLBACK): if self.req_hist_price_check_end(): # After receive the new hist, combine them with the old hist for req_id in self.returned_hist: #print self.returned_hist[req_id].hist for security in self.data: if same_security(security, self.returned_hist[req_id].security): temp=self.data[security].hist[self.returned_hist[req_id].period] empty=pd.DataFrame(columns=['open','high','low','close','volume']) for index in temp.index: if index not in self.returned_hist[req_id].hist.index: empty=empty.append(temp.loc[index]) self.data[security].hist[self.returned_hist[req_id].period]=empty.append(self.returned_hist[req_id].hist) #print empty #print self.returned_hist[req_id].hist #print self.data[security].hist[self.returned_hist[req_id].period] #self.data[security].hist[self.returned_hist[req_id].period]= \ #temp.append(self.returned_hist[req_id].hist.loc[index]) for security in self.data: for period in self.context.hist_frame: if len(self.data[security].hist[period])>300: self.data[security].hist[period]=self.data[security].hist[period][-300:] #print self.data[security].hist[period] self.accountManagerState.set_state(self.accountManagerState.EVERY_BAR_RUN) # Run handle_data regularly handle_data(self.context, self.data) #print self.stime self.stime_previous=self.stime
def order_percent_quantopian(self, security, percent, style=MarketOrder()): print 'order_percent_quantopian' import math for ct in self.data: if same_security(security, ct): return self.order_quantopian(security, int(math.floor(self.context.portfolio.portfolio_value/self.data[ct].price)) , style=style)
def order_value_quantopian(self, security, value, style=MarketOrder()): print 'order_value_quantopian' import math for ct in self.data: if same_security(security, ct): return self.order_quantopian(security, int(math.floor(value/self.data[ct].price)), style=style)