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
}
}
}
}