Hi I am trying to create an EMA Cross over strategy where it buys when the 9 crosses the 55 to the upside. closes position when 9 crosses the 13 to the downside and is able to reopen the position if the 9-13-21 all cross back to the upside before it crosses the 55 to the downside. vice versa on the short side
I am struggling with my coding of this I can get the code to function when its just a simple cross over strategy but adding in this additional layer of complexity is creating my bot to not place orders
#Parameters
CRYPTO = "BTCUSDT"; EMA_S = 55; EMA_C1 = 9; EMA_C2 = 13; EMA_F = 21;
class MovingAverageCrossAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 6, 1)
self.SetEndDate(2022, 1, 1)
self.SetCash("USDT", 100000)
self.SetBrokerageModel(BrokerageName.Binance, AccountType.Margin)
self.crypto = self.AddCrypto(CRYPTO, Resolution.Hour).Symbol
self.sell_Ticket = False
self.buy_Ticket = False
self.position_close_long= False
self.position_close_short = False
self.close_one = self.EMA(self.crypto, EMA_C1, Resolution.Hour)
self.close_two = self.EMA(self.crypto, EMA_C2, Resolution.Hour)
self.fast = self.EMA(self.crypto, EMA_F, Resolution.Hour)
self.slow = self.EMA(self.crypto, EMA_S, Resolution.Hour)
#warmup
self.SetWarmUp(5*EMA_S, Resolution.Minute)
def OnData(self, data):
if self.IsWarmingUp:
return
if not self.slow.IsReady:
return
self.Plot(self.crypto, "Price", self.Securities[self.crypto].Price)
self.Plot(self.crypto, "ema_22", self.fast.Current.Value )
self.Plot(self.crypto, "ema_55", self.slow.Current.Value )
self.Plot(self.crypto, "ema_9", self.close_one.Current.Value )
self.Plot(self.crypto, "ema_13", self.close_two.Current.Value )
#open initial long when ema ribbon crosses to the upside
if not self.Portfolio[self.crypto].IsLong:
if not self.sell_Ticket:
if not self.position_close_long:
if self.close_one.Current.Value > self.close_two.Current.Value > self.fast.Current.Value > self.slow.Current.Value:
self.SetHoldings(self.crypto, 0.95)
self.sell_Ticket = True
#close long when the two fastest emas cross, create a boolan to keep track
if self.Portfolio[self.crypto].IsLong:
if self.close_one.Current.Value < self.close_two.Current.Value:
self.Liquidate(self.crypto)
self.position_close_long = True
#reopen position if it was closed and it was a fake out
if not self.Portfolio[self.crypto].IsLong:
if self.sell_Ticket and self.position_close_long:
if self.fast.Current.Value < self.close_two.Current.Value < self.close_one.Current.Value :
self.SetHoldings(self.crypto, 0.95)
self.position_close_long = False
#reset all boolans when ribbon flips
if not self.Portfolio[self.crypto].IsLong:
if self.position_close_long:
if self.fast.Current.Value < self.slow.Current.Value:
self.sell_ticket = False
self.position_close_long = False
if not self.Portfolio[self.crypto].IsShort:
if not self.buy_Ticket:
if not self.position_close_short:
if self.close_one.Current.Value < self.close_two.Current.Value < self.fast.Current.Value < self.slow.Current.Value:
self.SetHoldings(self.crypto, -0.95)
self.buy_Ticket = True
if self.Portfolio[self.crypto].IsShort:
if self.close_two.Current.Value > self.close_one.Current.Value:
self.Liquidate(self.crypto)
self.position_close_short = True
if not self.Portfolio[self.crypto].IsShort:
if self.buy_Ticket and self.position_close_short:
if self.fast.Current.Value > self.close_two.Current.Value > self.close_one.Current.Value :
self.SetHoldings(self.crypto, -0.95)
self.position_close_short = False
if not self.Portfolio[self.crypto].IsShort:
if self.buy_Ticket and self.position_close_short:
if self.slow.Current.Value < self.fast.Current.Value:
self.position_close_short = False
self.buy_ticket = False
Vladimir
Conda
I am not sure what you changed here besides consolidating some of my strings. When I try to run the back test on your code I just get a few errors? Am I missing something?
Conda
im getting this error. What does this mean?
Vladimir
conda
-→ im getting this error. What does this mean?
This means that orders on Binance will be rounded down to the nearest lot size of 0.00001.
There were no warnings when I commented out line 17 in my code.
Conda
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!