from datetime import timedeltaclass CalmRedAnt(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2021, 5, 1) #Set Start Date
self.SetEndDate(2021, 12, 1) #Set End Date
self.SetCash(10000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.SetBrokerageModel(BrokerageName.FTX, AccountType.Cash)
self.SetTimeZone("America/New_York")
#self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
#self.SettlementModel = DelayedSettlementModel(3, TimeSpan.FromHours(8))
#self.AddCrypto("BTCUSD").Symbol
self.symbols = []
self.symbols.append("BTCUSD")#, self.symbols.append("XRPUSD"), \
#self.symbols.append("ETHUSD"), self.symbols.append("SOLUSD"), \
#---TO ADD
#self.symbols.append("BNBUSD"), self.symbols.append("LINKUSD"), \
#self.symbols.append("BICOUSD"), self.symbols.append("BATUSD"), \
#self.symbols.append("AVAXUSD")
#self.spy = self.AddSecurity("SPY", Resolution.Hour).Symbol
#self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(timedelta(minutes=720)), Action(self.trade))
#self.SetBenchmark("BTCUSD")
#INDICATORS
self.rsi = RelativeStrengthIndex(14, MovingAverageType.Exponential)
self.ema_f = ExponentialMovingAverage(3)
self.ema_d = ExponentialMovingAverage(7)
self.sma_h_s = ExponentialMovingAverage(14)
self.sma_h_f = ExponentialMovingAverage(7)
self.sto = Stochastic(3,1,14)
self.adx = AverageDirectionalIndex(14)
self.psar = ParabolicStopAndReverse(0.02, 0.02, 0.2)
self.indicators_by_symbol = {}
#self.SetWarmUp(timedelta(25))
for symbol in self.symbols:
self.AddCrypto(symbol, Resolution.Daily).Symbol
self.RegisterIndicator(symbol, self.rsi, Resolution.Daily)
self.RegisterIndicator(symbol, self.ema_f, Resolution.Daily)
self.RegisterIndicator(symbol, self.sma_h_s, Resolution.Daily)
self.RegisterIndicator(symbol, self.sma_h_f, Resolution.Daily)
self.RegisterIndicator(symbol, self.sto, Resolution.Daily)
self.RegisterIndicator(symbol, self.ema_d, Resolution.Daily)
self.RegisterIndicator(symbol, self.psar, Resolution.Daily)
#self.RegisterIndicator(symbol, self.upper_b, Resolution.Daily)
#self.RegisterIndicator(symbol, self.lower_b, Resolution.Daily)
#self.RegisterIndicator(symbol, self.upper_psar, Resolution.Daily)
self.RegisterIndicator(symbol, self.adx, Resolution.Daily)
self.indicators_by_symbol[symbol] = {
'rsi' : self.rsi,
'ema_f' :self.ema_f,
'sma_h_s' :self.sma_h_s,
'sma_h_f' :self.sma_h_f,
'sto' : self.sto,
'ema_d' : self.ema_d,
#'upper_b' : self.upper_b,
#'lower_b' : self.lower_b,
'psar' : self.psar,
#'upper_psar' : self.upper_psar,
'adx' : self.adx
}
def OnData(self, slice):
for symbol in self.indicators_by_symbol.keys():
if slice.Bars.ContainsKey(symbol):
history = self.History([symbol], 25, Resolution.Daily)
for bar in history.itertuples():
tradeBar = TradeBar(bar.Index[1], bar.Index[0], bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(1))
self.indicators_by_symbol[symbol]['sto'].Update(tradeBar)
self.indicators_by_symbol[symbol]['adx'].Update(tradeBar)
self.indicators_by_symbol[symbol]['psar'].Update(tradeBar)
#self.indicators_by_symbol[symbol]['upper_psar'].Update(bar.Index[1], bar.close)
self.indicators_by_symbol[symbol]['sma_h_s'].Update(bar.Index[1], bar.close)
self.indicators_by_symbol[symbol]['sma_h_f'].Update(bar.Index[1], bar.close)
self.indicators_by_symbol[symbol]['ema_f'].Update(bar.Index[1], bar.close)
self.indicators_by_symbol[symbol]['ema_d'].Update(bar.Index[1], bar.close)
self.indicators_by_symbol[symbol]['rsi'].Update(bar.Index[1], bar.close)
#self.indicators_by_symbol[symbol]['upper_b'].Update(bar.Index[1], bar.close)
#self.indicators_by_symbol[symbol]['lower_b'].Update(bar.Index[1], bar.close)
#if (self.indicators_by_symbol[symbol]['sma_h_s'].Current.Value < self.Securities[symbol].Price) and (self.indicators_by_symbol[symbol]['sma_h_f'].Current.Value < self.Securities[symbol].Price)
#self.StochRSI = IndicatorExtensions.Of(self.indicators_by_symbol[symbol]['rsi'], self.indicators_by_symbol[symbol]['sto']).Current.Value
#self.Debug('rsi_stoch: ' + str(self.StochRSI))
#if str(symbol)=="BTCUSD":
#self.cash = self.Portfolio.MarginRemaining
#self.cashy = self.cash/len(self.symbols)
#INDICATORS READY?
if not self.indicators_by_symbol[symbol]['rsi'].IsReady and not self.indicators_by_symbol[symbol]['sto'].IsReady\
and not self.indicators_by_symbol[symbol]['ema_d'].IsReady and not self.indicators_by_symbol[symbol]['psar'].IsReady:
return self.Debug('indicators not ready')
#OTHER INDICATORS
deviationPct = 0.10
self.upper_b = float(self.indicators_by_symbol[symbol]['ema_d'].Current.Value * (1+deviationPct))
self.lower_b = float(self.indicators_by_symbol[symbol]['ema_d'].Current.Value * (1-deviationPct))
self.upper_psar = float(self.indicators_by_symbol[symbol]['ema_d'].Current.Value * (1.2))
self.psar_m = float(IndicatorExtensions.Of(self.indicators_by_symbol[symbol]['ema_d'], self.indicators_by_symbol[symbol]['psar']).Current.Value)
#INFO
self.Debug('Info: ' + str(self.Time.date) + str(symbol) + ' price '+ str(self.Securities[symbol].Price) +' RSI ' + str(self.indicators_by_symbol[symbol]['rsi'].Current.Value) + ' sto ' + str(self.indicators_by_symbol[symbol]['sto'].Current.Value) + ' lower_b ' + str(self.lower_b) + ' ADX ' + str(self.indicators_by_symbol[symbol]['adx'].Current.Value))
#BUY
if self.indicators_by_symbol[symbol]['rsi'].Current.Value < 30.0:
#quantity = self.cashy/self.Securities[symbol].Price
#self.LimitOrder(symbol, quantity, self.Securities[symbol].Price)
if self.Portfolio.MarginRemaining > (1/len(self.symbols))*self.Securities[symbol].Price:
self.SetHoldings(symbol, 1/len(self.symbols))
self.Debug(str(symbol) +' BUY RSI ' + str(self.indicators_by_symbol[symbol]['rsi'].Current.Value))
elif self.indicators_by_symbol[symbol]['sto'].Current.Value < 20.0:
#quantity = self.cashy/self.Securities[symbol].Price
#self.LimitOrder(symbol, quantity, self.Securities[symbol].Price)
if self.Portfolio.MarginRemaining > (1/len(self.symbols))*self.Securities[symbol].Price:
self.SetHoldings(symbol, 1/len(self.symbols))
self.Debug(str(symbol) +' BUY sto ' + str(self.indicators_by_symbol[symbol]['sto'].Current.Value))
elif self.lower_b > self.Securities[symbol].Price:
#quantity = self.cashy/self.Securities[symbol].Price
#self.LimitOrder(symbol, quantity, self.Securities[symbol].Price)
if self.Portfolio.MarginRemaining > (1/len(self.symbols))*self.Securities[symbol].Price:
self.SetHoldings(symbol, 1/len(self.symbols))
self.Debug(str(symbol) +' BUY bands ' + str(self.lower_b))
elif self.indicators_by_symbol[symbol]['adx'].Current.Value > 70.0:
#quantity = self.cashy/self.Securities[symbol].Price
#self.LimitOrder(symbol, quantity, self.Securities[symbol].Price)
if self.Portfolio.MarginRemaining > (1/len(self.symbols))*self.Securities[symbol].Price:
self.SetHoldings(symbol, 1/len(self.symbols))
self.Debug(str(symbol) +' BUY ADX ' + str(self.indicators_by_symbol[symbol]['adx'].Current.Value))
#SELL
elif self.indicators_by_symbol[symbol]['rsi'].Current.Value > 70.0:
self.Liquidate(symbol)
#self.Debug(str(symbol) +' SELL RSI ' + str(self.indicators_by_symbol[symbol]['rsi'].Current.Value))
elif self.indicators_by_symbol[symbol]['sto'].Current.Value > 80.0:
self.Liquidate(symbol)
#self.Debug(str(symbol) +' SELL sto ' + str(self.indicators_by_symbol[symbol]['sto'].Current.Value))
elif self.upper_b < self.Securities[symbol].Price:
self.Liquidate(symbol)
#self.Debug(str(symbol) +' SELL bands ' + str(self.indicators_by_symbol[symbol]['upper_b'].Current.Value))
elif self.psar_m > self.upper_psar:
self.Liquidate(symbol)
#self.Debug(str(symbol) +' SELL PSAR ' + str(self.upper_psar))
#elif self.indicators_by_symbol[symbol]['adx'].Current.Value < 30.0:
# self.Liquidate(symbol)
# self.Debug(str(symbol) +' SELL ADX ' + str(self.indicators_by_symbol[symbol]['adx'].Current.Value))
else:
self.Debug('none')
else:
self.Debug('NOT slice.Bars.ContainsKey(symbol)')
Eduardo Barros Verez
I found the error sorry for the inconvenienceÂ
Â
Eduardo Barros Verez
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!