Hello, I was trying to write an algo using EMA crossover to long or short Bitcoin. I was trying to using 13 / 48 EMA crossover and supposedly there should be more than one cross as the time period I set was a year long. However, when I did the backtesting, there were only two orders: buy and sell. I was wondering how to edit my code so that it can keep trading? Thanks.
class MovingAverageCrossAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 4, 1) #Set Start Date
self.SetEndDate(2022, 3, 31) #Set End Date
self.SetCash("USDT", 100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.Binance, AccountType.Margin)
self.AddCrypto("BTCUSDT", Resolution.Daily, Market.Binance)
# create a 13 day exponential moving average
self.fast = self.EMA("BTCUSDT", 13, Resolution.Daily)
# create a 48 day exponential moving average
self.slow = self.EMA("BTCUSDT", 48, Resolution.Daily)
self.previous = None
def OnData(self, data):
# wait for our slow ema to fully initialize
if not self.slow.IsReady:
return
holdings = self.Portfolio["BTCUSDT"].Quantity
if holdings <= 0:
# if the fast is greater than the slow, we'll go long
if self.fast.Current.Value > self.slow.Current.Value:
self.SetHoldings("BTCUSDT", 1)
# we only want to liquidate if we're currently long
# if the fast is less than the slow we'll liquidate our long
if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value:
self.Liquidate("BTCUSDT")
self.previous = self.Time
Vladimir
Andy Lee
--> trying to write an algo using EMA crossover to long or short Bitcoin.
Here is “CRYPTO EMAC LS” which generate 9 trades.
If you are satisfied with my answer, please accept it and don't forget to like it
Andy Lee
Vladimir
May I know what does self.SetWarmUp(5*EMA_S, Resolution.Daily) mean?
Vladimir
Andy Lee
-→ May I know what does self.SetWarmUp(5*EMA_S, Resolution.Daily) mean?
The Exponential Moving Average (EMA) from Digital Signal Processing view is an Infinite Impulse Response (IIR) filter.
The latter value of IIR filter substantially depends on the initial value of the time series.
To reduce the dependence on the initial value, it is recommended increasing the size of the rolling window of WarmUp to 5-6 times of the EMA period for it to mature or use a Finite Impulse Response (FIR) filter - Simple Moving Average (SMA).
Andy Lee
Vladimir
Thanks a lot. One last quick question: what is the difference between self.Liquidate() and self.SetHoldings(-1)? When I do liquidate, it won't buy again even when ema crosses.
Vladimir
Andy Lee
The SetHoldings() method automatically calculates the number of asset units to purchase
according to the fraction of the portfolio value provided.
self.SetHoldings(self.crypto, 1) --> 100% of the portfolio value Long self.crypto.
self.SetHoldings(self.crypto, -1) --> 100% of the portfolio value Short self.crypto.
You can liquidate individual stocks, or your entire portfolio using the Liquidate() method.
When called without a ticker provided, it will liquidate all your holdings.
Liquidate(self.crypto) == self.SetHoldings(self.crypto, 0) --> 0% of the portfolio value allocated to self.crypto.
If you are satisfied with my answer, please accept it and don't forget to like it
Vladimir
Andy Lee
I'm wondering if you had a chance to see what I asked you about:
If you are satisfied with my answer, please accept it.
Andy Lee
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!