| Overall Statistics |
|
Total Trades 3 Average Win 0% Average Loss 0.00% Compounding Annual Return -64.114% Drawdown 2.800% Expectancy -1 Net Profit -2.757% Sharpe Ratio -5.336 Probabilistic Sharpe Ratio 0.009% Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.33 Beta 0.204 Annual Standard Deviation 0.094 Annual Variance 0.009 Information Ratio 1.379 Tracking Error 0.254 Treynor Ratio -2.473 Total Fees $9.01 Estimated Strategy Capacity $1400000.00 |
# MACD and STO with IndicatorExtensions Delay()
class MACDandSTOwithIndicatorExtensionsDelay(QCAlgorithm):
quantity = 0
def Initialize(self):
self.SetStartDate(2021, 5, 1) # Set Start Date
self.SetEndDate(2021, 5, 11) # Set End Date
self.SetCash(10000)
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.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
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 = "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 * .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.MarketOrder(par, quantity)
self.closesell = closed + (float(self.atr.Current.Value) * 2.5)
elif (closed >= self.closesell or (signal_d > macd_d and signal_dS < macd_dS)) 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))
'''