Back

Calculating Indicators

I can write Moving average indicators two ways (see below) but when I backtest I get different results. Should the results be the same?

Method 1

public class MyAlgorithm : QCAlgorithm
{

        ExponentialMovingAverage ema;

 //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {

               ema = EMA(symbol, 200, Resolution.Minute);

        }

//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public override void OnData(Slice data) 
        {   
             foreach(var bar in data.Values)
             {
                 if(bar.Symbol == "my symbol")
                {
                       //some logic using the ema for this symbol                

                }

             }

        }

}

 

Method 2

public class MyAlgorithm : QCAlgorithm
{        

        ExponentialMovingAverage ema = new ExponentialMovingAverage(200);

  public override void Initialize()
        {

             // no need to initialize. The data is again minute resolution

          
               AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            

        }

 //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public override void OnData(Slice data) 
        {   
             foreach(var bar in data.Values)
             {
                 if(bar.Symbol == "my symbol")
                {
                    ema.Update(Time,data[bar.Symbol].Close); 

                   //some logic using the ema for this symbol
                }

           }

       }

}

Update Backtest








Hey Iman Mohtashemi, interesting example.

From a quick view, yes, both cases should give the same values.

Can you make an example with forex minute data and share some output (say a CSV file)? Then it’ll be more easy to find the problem.

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