Back

Plotting simple EMA gets cut off early?

Hi, I'm trying to implement a simple EMA crossover strategy as practice for learning the Quantconnect platform, and when I use the consolidated bars with a rolling window, I can only seem to chart up to a certain point before the chart gets cut off. The backtest is supposed to be from June 2019 to March 2020, but this is the result:

 

https://i.imgur.com/LZF3Ikc.png

Here is the code I'm using:

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Consolidators import *
from datetime import datetime, timedelta
import decimal as d
import numpy as np

class ema_cross(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2019, 6, 1) # Set Start Date
self.SetEndDate(datetime.now())

# Set up cash and BTC
self.SetCash("USD", 10000)
self.SetCash("BTC", 0)
self.crypto_pair = "BTCUSD"

# Set up basic symbol and fee structure
self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
self.AddCrypto(self.crypto_pair, Resolution.Minute)

# warmup indicator
#self.SetWarmUp(timedelta(minutes=6000))


# Define our EMA resolution, lookback and
self.short_tf_resolution = 30
self.quick_lookback = 50
self.long_lookback = 200


# define ema_quick
self.ema_quick = ExponentialMovingAverage(self.crypto_pair, self.quick_lookback)
self.ema_quick.Updated += self.ema_quick_update
self.ema_quick_win = RollingWindow[IndicatorDataPoint](5)


# define ema_long
self.ema_long = ExponentialMovingAverage(self.crypto_pair, self.long_lookback)
self.ema_long.Updated += self.ema_long_update
self.ema_long_win = RollingWindow[IndicatorDataPoint](5)

## Consolidator
shortTimeFrameConsolidator = TradeBarConsolidator(timedelta(minutes=30))
shortTimeFrameConsolidator.DataConsolidated += self.shortTimeFrameHandler
self.RegisterIndicator(self.crypto_pair, self.ema_quick, shortTimeFrameConsolidator)
self.RegisterIndicator(self.crypto_pair, self.ema_long, shortTimeFrameConsolidator)
self.SubscriptionManager.AddConsolidator(self.crypto_pair, shortTimeFrameConsolidator)

def ema_quick_update(self, sender, updated):
self.ema_quick_win.Add(updated)

def ema_long_update(self, sender, updated):
self.ema_long_win.Add(updated)

def shortTimeFrameHandler(self, sender, bar):
if not (self.ema_quick_win.IsReady):
return
self.ema_quick.Update(bar.EndTime, bar.Close)
self.ema_long.Update(bar.EndTime, bar.Close)
self.Plot("Short EMA", self.ema_quick)
self.Plot("Long EMA", self.ema_long)

def OnData(self, data):
pass

 

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.


Attached backtest for convenience

0


Hi Diana,

You exceeded the maximum number of data points per series, which is 4000. If you check your logs, you should see an error message:

Exceeded maximum data points per series, chart update skipped. Chart Name Chart. Series name Short EMA. Limit is currently set at 4000

To work around this you can plot less often. One way to achieve this is by increasing your consolidator time period. I set it to 120 minutes.

shortTimeFrameConsolidator = TradeBarConsolidator(timedelta(minutes=120))


Best
Rahul

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