Overall Statistics
Total Trades
1642
Average Win
0.01%
Average Loss
0.00%
Compounding Annual Return
204.131%
Drawdown
100.000%
Expectancy
7.059
Net Profit
1265.193%
Sharpe Ratio
2.665
Probabilistic Sharpe Ratio
86.729%
Loss Rate
7%
Win Rate
93%
Profit-Loss Ratio
7.69
Alpha
0.006
Beta
0.991
Annual Standard Deviation
0.583
Annual Variance
0.34
Information Ratio
-0.109
Tracking Error
0.068
Treynor Ratio
1.568
Total Fees
$2.84
Estimated Strategy Capacity
$770000.00
# MACD and STO with IndicatorExtensions Delay()

class MACDandSTOwithIndicatorExtensionsDelay(QCAlgorithm):
    quantity = 0
    
    def Initialize(self):
        
        self.SetStartDate(2019, 1, 1)  # Set Start Date
        self.SetEndDate(2021, 5, 7)    # Set End Date
        self.SetCash(1000)
        self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash)
        
        res = Resolution.Hour
        self.crypto = self.AddCrypto("BTCUSD", res).Symbol
        self.SetWarmUp(26)
        
        self.atr = self.ATR(self.crypto, 14, MovingAverageType.Exponential, res)
        self.macd = self.MACD(self.crypto, 12, 26, 9, MovingAverageType.Simple, res)
        self.macd_delayed = IndicatorExtensions.Of(Delay(1), self.macd)
        self.macd_delayed_histogram = IndicatorExtensions.Of(Delay(1), self.macd.Histogram)
        self.macd_delayed_signal = IndicatorExtensions.Of(Delay(1), self.macd.Signal)
        
        self.stoch = self.STO(self.crypto, 14, 3, 3, res)
        self.delayed_slowK = IndicatorExtensions.Of(Delay(1), self.stoch.StochK)
        self.delayed_slowD = IndicatorExtensions.Of(Delay(1), self.stoch.StochD)
        
        self.control = False
        self.closesell = 0
    def OnData(self, data):

        if not (self.macd.IsReady and self.stoch.IsReady): return
    
        macd = self.macd.Current.Value
        macd_d = self.macd_delayed.Current.Value
        signal = self.macd.Signal.Current.Value
        signal_d = self.macd_delayed_signal.Current.Value
        hist = self.macd.Histogram.Current.Value
        hist_d = self.macd_delayed_histogram.Current.Value
        stochK = self.stoch.StochK.Current.Value
        stochK_d = self.delayed_slowK.Current.Value
        stochD = self.stoch.StochD.Current.Value
        stochD_d = self.delayed_slowD.Current.Value
        
        par = "BTCUSD"
        
        closed = self.Securities[self.crypto].Close
        usdTotal = self.Portfolio.CashBook["USD"].Amount
        # limitPrice = closed * 1.01 # Sell equal or better than 1% > close.
        quantity = usdTotal * .80 / closed
        stopPrice = closed * .95 # Trigger stop limit when price falls 1%.
        
        
        self.Plot('MACD', 'macd', macd)        
        self.Plot('MACD', 'macd_delayed', macd_d)
        
        self.Plot('SIGNAL', 'signal', signal)        
        self.Plot('SIGNAL', 'delayed_signal', signal_d)
        
        self.Plot('HIST', 'hist', hist)        
        self.Plot('HIST', 'delayed_histogram', hist_d)

        if hist < hist_d and ((stochK > stochD and stochK_d < stochD_d) or stochK > stochD) and hist < 0 and stochD < 40 and self.control == False:
            self.control = True
            self.MarketOrder(par, quantity)
            self.closesell = closed + (float(self.atr.Current.Value) * 1.5)
        
        elif (self.closesell >= closed or (signal > macd and signal_d < macd_d)) and self.control == True:
            self.control = False
            btcTotal = self.Portfolio.CashBook["BTC"].Amount
            closesell = self.Securities[self.crypto].Close
            quantitysell =  btcTotal / closesell
            self.MarketOrder(par, -quantitysell)
        
        
        # elif (signal > macd and signal_d < macd_d) and self.control == True:
        #     self.control = False
        #     limitPrice = round(self.Securities["BTCUSD"].Price * 1.00, 2)
        #     quantity = self.Portfolio.CashBook["BTC"].Amount
        #     self.MarketOrder("BTCUSD", -quantity)
            
            

    '''
    def OnOrderEvent(self, orderEvent):
        self.Debug("{} {}".format(self.Time, orderEvent.ToString()))

    def OnEndOfAlgorithm(self):
        self.Log("{} - TotalPortfolioValue: {}".format(self.Time, self.Portfolio.TotalPortfolioValue))
        self.Log("{} - CashBook: {}".format(self.Time, self.Portfolio.CashBook))
        
    '''