Back

EMA Giving incorrect values and questions

Hi all,

My apologies as I am rather unfamiliar with the documentation and it is a bit confusing. 

First, I should explain what the attached code is attempting to do. I am trying to get a 9 day EMA and when it crosses the current price, buy the stock. Otherwise, we should short it until they cross over. However, the information about the EMA that is in the console and the results being so significantly high is concerning.

I have a few questions that will hopefully lead to the problem. 

1. When we set the resolution of the EMA, is that how often the EMA is updated or how long the EMA is? (Ticks, days etc)

2.  How does the EMA update in QC?

3. Is the EMA based on days or what 'period'?

Thanks so much all.

Update Backtest








0

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.


There are some issues with your code. First, here is what you are trying to do:

class EMAMomentumUniverse(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020, 1, 30)
self.SetEndDate(2020, 4, 30)
self.SetCash(100000)

self.symbol = "CHK"
self.AddEquity(self.symbol, Resolution.Daily)

self.SetWarmup(10)

self.ema = ExponentialMovingAverage(9)
self.RegisterIndicator(self.symbol, self.ema, Resolution.Daily)

def OnData(self, data):

if self.IsWarmingUp:
return

# bullish
if self.Securities[self.symbol].Close < self.ema.Current.Value:
self.Liquidate()
self.SetHoldings(self.symbol, 0.95)
self.Debug("{} Close:{} > EMA:{}".format(self.Time, self.Securities[self.symbol].Close , self.ema.Current.Value))

# bearish
else:
self.Liquidate()
self.SetHoldings(self.symbol, -0.95)
self.Debug("{} Close:{} < EMA:{}".format(self.Time, self.Securities[self.symbol].Close , self.ema.Current.Value))

Your code has a couple issues:

- You are trying to use a daily EMA, but your algorithm timeframe is only 1 day (4/30/20 to 4/30/20).
- You are using Tick Resolution instead of Daily Resolution for both your equity and your EMAs
- You have scheduled an event to liquidate your portfolio at the end of the day at 4:00 but that event will actually fire on market open the next day
- You are trying to use a 9-period indicator, but your algorithm doesn't warm up with 9 days of preceding data

Keep these in mind for your next strategy.

To answer your questions:
1) This depends on a couple factors
2) You can update the EMA manually or register it to automatically
2) This also depends on a couple factors

 

1

Thanks so much for your answer! So if I wanted to make it so that the EMA followed the price across the day at every tick and the symbol would be traded based on the EMA at that tick, what would be changed from your example?

My understanding is that the EMA that is being set is only updating every day at the end of the day, however, I believe most EMAs update every tick but are based on days worth of data. 

Thanks!

0

Hi Nathan,

I'm sorry for any confusion our documentation may be causing. Below, I'll answer the 3 questions that were posed. Afterwards, I recommend reading our documentation's Getting Started page to begin building a solid understanding of our API. Also, review our documentation on Indicators. It answers all 3 of the questions that were raised.

(1) When we set the resolution of the EMA, that is how often the indicator is updated. The period argument is how many resolution steps the EMA is calculated over.

(2) When we call the EMA method in Initialize like we do in this algorithm, the indicator is automatically registered to receive updated pricing without the user needing to do anything. Whenever we want the current value of the EMA, we can just call
self.ema.Current.Value

(3) The EMA period is based on the resolution that is passed as an argument when it is created, or the associated equity's resolution if none is passed. In the attached backtest, I've set the resolution to minute. Therefore, the EMA will be updated at the end of each minute.

Other software packages may use ticks in their calculation of a daily EMA value. For example, if we are looking a chart with daily candles and a 9-period EMA. If the most recent candle hasn't closed yet, the 9 period EMA will utilize the preceding 8 candles' closing prices and the 9th candle's latest tick price. With our platform, the EMA is updated at the close of the candle.

I've attached an example algorithm which demonstrates this strategy using minute resolution data. The algorithm trades based on the CHK current price relative to its 9-period EMA. One minute before the close, the algorithm liquidates all of its positions.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed