def AddOrder(self, order): Singleton.Debug(f"Portfolio.AddOrder: {order}") Singleton.Log(f"Order size: {order.Quantity}") Singleton.Log( f"isclose({order.Quantity}, 0, abs_tol={Singleton.Securities[order.Symbol].SymbolProperties.LotSize})" ) if isclose(order.Quantity, 0, abs_tol=Singleton.Securities[ order.Symbol].SymbolProperties.LotSize): Singleton.Log( "Warning: Avoiding submitting order that has zero quantity.") return Singleton.Debug(f"AddOrder: {order}") self.__orders.append(order)
def OnWarmupFinished(self): Singleton.Debug("OnWarmupFinished") if self.LiveMode: Singleton.Broker.ImportFromBroker() self.__initial_value = Singleton.Portfolio.TotalPortfolioValue self.Log(f"setting initial value to {self.__initial_value}") self.__cost = 0.0 for i in self.__algorithms: # TypeError : unsupported operand type(s) for -=: 'Cash' and 'CashAmount' Singleton.Broker.Portfolio.Cash -= i.Portfolio.Cash for symbol, position in i.Portfolio.items(): Singleton.Broker.Portfolio[ symbol].Quantity -= position.Quantity cost = i.Allocation * self.__initial_value i.Portfolio.SetCash(cost) self.__cost += i.Portfolio.TotalPortfolioValue self.__initial_cost = self.__cost for i in self.__algorithms: i.OnWarmupFinished()
def HandleOrderEvent(self, order_event): Singleton.Debug(f"> HandleOrderEvent (1): OrderEvent: {order_event}") order = self._submitted.pop(order_event.OrderId, None) if not order: Singleton.Debug( f"Could not find order id {order_event.OrderId} in queue: {self._submitted}" ) return Singleton.Debug(f"> HandleOrderEvent (2): Order: {order}") if Helper.is_order_done(order_event.Status): order.Portfolio.ProcessFill(order_event, order) order.Portfolio.Algorithm.OnOrderEvent(order_event) order.Portfolio.Algorithm.TotalOrders += 1 else: # Re-add orders that are still open. self._submitted[order_event.OrderId] = order
def OnEndOfDay(self): Singleton.Debug("OnEndOfDay: {}".format(Singleton.Time)) for i in self.__algorithms: i.OnEndOfDay() is_new_year = self.Time.year != self.__year if is_new_year: self.__year = self.Time.year is_new_month = self.Time.month != self.__month if is_new_month: self.__month = self.Time.month if self.__plot_orders and is_new_month: for i in self.__algorithms: self.Plot('Orders', i.Name, i.TotalOrders) if is_new_year: self.ResetOrders() if self.__reset: self.ResetPlot() if self.__plot_every_n_days_i % self.__plot_every_n_days == 0: if self._benchmark: self.Plot('Annual Saw Tooth Returns', self._benchmark.Name, self._benchmark.Performance) self.Plot('Strategy Equity', self._benchmark.Name, self._benchmark.Performance * self.__initial_cost) accum = 0.0 pos = 0 for i in self.__algorithms: accum += i.Portfolio.TotalPortfolioValue pos += 1 self.Plot('Annual Saw Tooth Returns', i.Name, i.Performance) if self.__plot_value: self.Plot("Value", i.Name, i.Portfolio.TotalPortfolioValue) if self.__plot_allocation: self.Plot("Allocation", i.Name, round(100.0 * i.Allocation, 1)) self.__plot_every_n_days_i += 1 if self.__email_address: for i in self.__algorithms: if i.Email.HasContent: i.Email.Send(f"{i.Name} (OnEndOfDay)")
def Debug(self, message): Singleton.Debug("[%s] %s" % (self.Name, message))
def OnOrderEvent(self, order_event): Singleton.Debug(f"> OnOrderEvent: {order_event}") Singleton.Broker.HandleOrderEvent(order_event)
def OnSecuritiesChanged(self, changes): Singleton.Debug(f"OnSecuritiesChanged {changes}") for i in self.__algorithms: # Only call if there's a relevant stock in i i.OnSecuritiesChanged(changes)
def OnDividend(self): Singleton.Debug("OnDividend") for i in self.__algorithms: i.OnDividend()
def OnData(self, data): Singleton.Debug("OnData") for i in self.__algorithms: i.OnData(data)
def ProcessFill(self, order_event, order): Singleton.Debug("> ProcessFill: %s" % order_event) self._fill_order(order.Symbol, order_event.FillQuantity, order_event.FillPrice, order_event.OrderFee.Value.Amount)