Back

Copied Moving Average Cross tutorial to the letter but getting an error

I've reached about the 14 min mark of the tutorial video, and he successfully backtests. I get an error at the line I italicized saying there is no way to override, but the error doesn't show when he runs it in the tutorial. Removing "override" allows the program to compile and backtest, but no trades are executed. My only extensive programming experience is in Matlab, which is basically psuedo-code by comparison to this. I don't know what most of the code is for or what it is doing, including override.

Main:

using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;

namespace QuantConnect
{
    using QuantConnect.Securities;
    using QuantConnect.Models;
    
    public partial class MovingAverageDemo : QCAlgorithm, IAlgorithm
    {
        string symbol = "SPY";
        
        ExponentialMovingAverage emaFast = new ExponentialMovingAverage(10);
        ExponentialMovingAverage emaSlow = new ExponentialMovingAverage(50);
              
        public override void Initialize()
        {
            
            SetStartDate(2014, 01, 01);
            SetEndDate(2014, 04, 01);
            SetCash(50000);
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            
        }
        
        public override void OnTradeBar(Dictionary < string, TradeBar > data)
        {
            decimal price = data[symbol].Close;
            emaFast.AddSample(price);
            emaSlow.AddSample(price);
            
            if (emaFast.EMA > emaSlow.EMA)
            {
                Order(symbol, 100);
            }
            else if (emaFast.EMA < emaSlow.EMA)
            {
                Order(symbol, -100);
            }
        }
    }
}

ExponentialMovingAverage:

using System;
using System.Collections;
using System.Collections.Generic;

namespace QuantConnect {

    using QuantConnect.Securities;
    using QuantConnect.Models;

    public class ExponentialMovingAverage
    {
      private decimal _period;
      private decimal _ema;
      
      private int _samples;
      
      public decimal EMA
      {
          get{return _ema;}
      }
      
      public ExponentialMovingAverage(decimal period)
      {
          
          this._period = period;
          this._samples = 0;
          
      }
      
      public decimal AddSample(decimal price)
      {
          
          if (_samples == 0)
          {
              _ema = price;
          }
          else
          {
              _ema = (1/_period)*price+((_period-1)/_period)*_ema;
          }
          _samples++;
          return _ema;         
      }     
    }

}

Update Backtest








Hi Gage, this example from the video tutorials is outdated.

The OnTradeBar method was dropped, you should use OnData(TradeBars data) method instead. You can learn more about handling data with the docs.

Please chechout QuantConnect University example:
- "Strategy Example: Moving Average Cross Strategy".

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