Overall Statistics
Total Trades
98
Average Win
0.10%
Average Loss
-0.09%
Compounding Annual Return
328.565%
Drawdown
97.600%
Expectancy
-0.127
Net Profit
83.253%
Sharpe Ratio
3.633
Probabilistic Sharpe Ratio
75.756%
Loss Rate
57%
Win Rate
43%
Profit-Loss Ratio
1.04
Alpha
-0.936
Beta
1.027
Annual Standard Deviation
0.65
Annual Variance
0.422
Information Ratio
-2.189
Tracking Error
0.389
Treynor Ratio
2.3
Total Fees
$3.20
Estimated Strategy Capacity
$50000.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(2019, 6, 1)    # Set End Date
        self.SetCash(1000)
        self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash)
        
        res = Resolution.Hour
        self.crypto = self.AddCrypto("ETHUSD", 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.Exponential, 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.macd_delayedS = IndicatorExtensions.Of(Delay(2), self.macd)
        self.macd_delayed_histogramS = IndicatorExtensions.Of(Delay(2), self.macd.Histogram)
        self.macd_delayed_signalS = IndicatorExtensions.Of(Delay(2), 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.delayed_slowKS = IndicatorExtensions.Of(Delay(1), self.stoch.StochK)
        self.delayed_slowDS = IndicatorExtensions.Of(Delay(1), self.stoch.StochD)
        
        self.control = False
        self.closesell = 0
        self.closeperca = 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
        macd_dS = self.macd_delayedS.Current.Value
        signal = self.macd.Signal.Current.Value
        signal_d = self.macd_delayed_signal.Current.Value
        signal_dS = self.macd_delayed_signalS.Current.Value
        hist_dt = self.macd.Histogram.Current.Value
        hist = self.macd.Histogram.Current.Value * -1
        hist_d = self.macd_delayed_histogram.Current.Value * -1
        hist_dS = self.macd_delayed_histogramS.Current.Value * -1
        stochK = self.stoch.StochK.Current.Value
        stochK_d = self.delayed_slowK.Current.Value
        stochK_dS = self.delayed_slowKS.Current.Value
        stochD = self.stoch.StochD.Current.Value
        stochD_d = self.delayed_slowD.Current.Value
        stochD_dS = self.delayed_slowDS.Current.Value
        
        par = "ETHUSD"
        
        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 * .25 / 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_d < hist_dS and ((stochK_d > stochD_d and stochK_dS < stochD_dS) or stochK_d > stochD_d) and hist_d < 0 and stochD_d < 40 and self.control == False:
            self.control = True
            self.closeperca = closed - (float(self.atr.Current.Value) * 1.5)
            self.closesell = closed + (float(self.atr.Current.Value) * 3.00)
            self.MarketOrder(par, quantity)
            
        
        elif (closed <= self.closeperca or (closed >= self.closesell or (signal_d > macd_d and signal_dS < macd_dS))) and self.control == True:
            self.control = False
            btcTotal = self.Portfolio.CashBook["ETH"].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))
        
    '''