Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
2.942
Tracking Error
0.222
Treynor Ratio
0
Total Fees
$0.00
# https://backtest-rookies.com/2018/12/07/quantconnect-working-with-heikin-ashi-data/
# https://www.quantconnect.com/forum/discussion/1017/simple-heikin-ashi-algorithmhttps://www.quantconnect.com/terminal/
# use heiki ashi for indicator : https://www.quantconnect.com/forum/discussion/1456/how-to-set-candlesticks-to-heiken-ashi-as-default/p1
# todo fee model 
# todo add benchmark
import datetime as datetime
class DynamicTachyonThrustAssembly(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020,5, 10)   # Set Start Date
        self.SetEndDate(2020,5,15)       # Set End Date
        self.SetCash(10000)             # Set Strategy Cash
        
        self.longStock = "SPXL"
        self.shortStock = "SPXS"
        self.AddEquity(self.longStock, Resolution.Minute)
        self.AddEquity(self.shortStock, Resolution.Minute)
        
        self.sma5 = self.SMA(self.longStock, 5)
        self.sma5.Updated += self.emaUpdate
        self.emaWin = RollingWindow[IndicatorDataPoint](5)
        
        self.ha = self.HeikinAshi(self.longStock, Resolution.Minute)
        self.ha.Updated += self.haUpdate
        self.haWin = RollingWindow[TradeBar](5)
        
        chart = Chart("chart")
        chart.AddSeries(Series("Price", SeriesType.Line, 0))
        chart.AddSeries(Series("sma5", SeriesType.Line, 0))
        self.AddChart(chart)
        
        self.lastPrice = 0.0
    def emaUpdate(self, sender, updated):
        self.emaWin.Add(updated)
        
    def haUpdate(self, sender, updated):
        if self.ha.IsReady:
            self.sma5.Update(updated)
            tradeBar = TradeBar(self.ha.Current.EndTime, self.longStock, self.ha.Open, self.ha.High, self.ha.Low, self.ha.Close, 0);
            self.haWin.Add(tradeBar)

    def OnData(self, data):
        if data["SPXL"] is None:
            return
        
        if not self.emaWin.IsReady:
            return 
        
        # only print log in the last 15 minutes 
        if self.Time.time() > datetime.datetime(2020,5,15,15,45,0).time() and self.Time.time() < datetime.datetime(2020,5,15,15,59,0).time():
            self.Debug("--------{}--------<br>".format(self.Time))
            sum = 0
            slices = self.History(5)
            for slice in slices:
                sum = sum + slice.Bars["SPXL"].Close
                self.Debug("slices time : {}, and close : {}<br>".format(slice.Time, slice.Bars["SPXL"].Close))
            self.Debug("the sma 5 based on original close : {}<br>".format(sum/5))
            self.Debug("the sma 5 based on Heikin ashi : {}<br>".format(self.sma5.Current.Value))
            self.Debug("ha close : {}<br>".format(self.ha.Close))
            for i in self.haWin:
                self.Debug("haWin : {}<br>".format(i.Close))
        self.lastPrice = data["SPXL"].Close
        
    def OnEndOfDay(self):
        self.Plot("chart", "Price", self.lastPrice)
        self.Plot("chart", "sma5", self.sma5.Current.Value)