Overall Statistics
from datetime import timedelta
import math

class TachyonOptimizedCoil(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 11, 15)  # Set Start Date
        self.SetEndDate(2018,11,30) # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        # subscribe SPY with minute level resolution
        self.AddEquity("SPY", Resolution.Minute)
        
        # consolidate data into 1-day bar
        oneDayConsolidator = TradeBarConsolidator(timedelta(1))
        oneDayConsolidator.DataConsolidated += self.OneDayBarHandler
        self.SubscriptionManager.AddConsolidator("SPY", oneDayConsolidator)
        
        # store high from previous day
        self.previousHigh = -1


    def OnData(self, data):
        # On first day, do nothing
        if self.previousHigh < 0:
            return
        
        # place order only if we haven't made orders or fulfilled orders
        if not self.Portfolio.Invested and len(self.Transactions.GetOpenOrders()) == 0:
            quantity = math.floor(self.Portfolio.Cash / self.previousHigh)
            self.Log(f'limit order price: {self.previousHigh}')
            self.StopMarketOrder("SPY",quantity,self.previousHigh)
        
    def OneDayBarHandler(self, sender, bar):
        # update self.high
        self.previousHigh = bar.High
        
        # liquidate for new order on next day
        self.Liquidate()
        #self.Log(f'previous day high: {self.previousHigh}')
        
    def OnOrderEvent(self, orderEvent):
        self.Log(orderEvent)