| Overall Statistics |
|
Total Trades 862 Average Win 1.36% Average Loss -1.21% Compounding Annual Return -1.939% Drawdown 44.300% Expectancy 0.006 Net Profit -4.493% Sharpe Ratio 0.05 Probabilistic Sharpe Ratio 3.978% Loss Rate 53% Win Rate 47% Profit-Loss Ratio 1.13 Alpha -0.436 Beta 0.286 Annual Standard Deviation 0.228 Annual Variance 0.052 Information Ratio -3.486 Tracking Error 0.446 Treynor Ratio 0.04 Total Fees $906.09 Estimated Strategy Capacity $750000.00 |
# MACD and STO with IndicatorExtensions Delay()
class MACDandSTOwithIndicatorExtensionsDelay(QCAlgorithm):
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(100)
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
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
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
usdTotal = self.Portfolio.CashBook["USD"].Amount
limitPrice = round(self.Securities["BTCUSD"].Price * 0.95, 2)
quantity = usdTotal * 0.5 / limitPrice
self.MarketOrder("BTCUSD", quantity)
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))
'''