Custom indicator returns wrong data during backtesting

Created at 07 Feb 2015, 19:53
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
30

3082492

Joined 23.01.2015

Custom indicator returns wrong data during backtesting
07 Feb 2015, 19:53


Dear all, 

I have written a custom PivotPoint indicator that works well with realtime data. In backtesting, however, it always returns the last value of its data series instead of the value corresponding to the backtesting period. 

Example: For the backtesting period 5-Feb-15 to 5-Feb-15, the PivotPoint calculated by my indicator is 1.13802. Instead of returning this value to the bot, the indicator returns the value of the next day. That means from the backtesting perspective, it returns data lying in the future. I do not understand why the indicator contains data of the future at the time of processing historical data. I compared this behavior with that of built-in indicators and they seem to work correctly, meaning they return data corresponding to the backtesting period. 

This makes me conclude that the way I have written my indicator is wrong. I have been staring at the screen for hours now and also searched this forum for solutions but could not find any. Hence, your help on solving this problem or working around it would he highly appreciated! 

Below are pieces of code where I compare the behavior of a built-in indicator with that of my custom indicator. 

Best regards, 

Henry
 

Bot:

protected override void OnStart()
{
  ...
  this.rsi = Indicators.RelativeStrengthIndex(this.MarketSeries.Close, 14); 
  this.pp = Indicators.GetIndicator<JGPivotPoints>(this.PivotTimeFrame);
  ...
}

protected override void OnTick()
{
  ...
  double rsiValue = this.rsi.Result.LastValue; // <- Returns the correct value.
  double result = this.pp.PP.LastValue; 
  /* ^ This is where the indicator returns a value lying in the future of the backtesting 
   * period. I expected LastValue to return the most recent value of the backtesting period 
   * instead.
  */  
  ...
}

 

Indicator:

protected override void Initialize()
{
  this.pivotSeries = MarketData.GetSeries(this.PivotTimeFrame);
}

public override void Calculate(int index)
{
  ...
  int pivotIndex = this.pivotSeries.OpenTime.GetIndexByTime(this.MarketSeries.OpenTime[index]) - 1; // Get previously completed period of pivot series.

  double high = this.pivotSeries.High[pivotIndex];
  double low = this.pivotSeries.Low[pivotIndex];
  double close = this.pivotSeries.Close[pivotIndex];

  double pp = this.PP[index] = (high + low + close) / 3;
  this.R3[index] = high + 2 * (pp - low);
  this.R2[index] = pp + (high - low);
  this.R1[index] = (2 * pp) - low;
  this.S1[index] = (2 * pp) - high;
  this.S2[index] = pp - (high - low);
  this.S3[index] = low - 2 * (high - pp);
  ...
}

 

 


@3082492
Replies

3082492
08 Feb 2015, 19:16

RE:

3082492 said:

I found the problem myself. Here is the solution: 

This line in the indicator leads to wrong results even though they show up correctly on the charts:

int pivotIndex = this.pivotSeries.OpenTime.GetIndexByTime(this.MarketSeries.OpenTime[index]) - 1;

Instead, use this to get the previous period (in this case, one day) within the indicator timeframe.

int pivotIndex = this.pivotSeries.OpenTime.GetIndexByTime(this.MarketSeries.OpenTime[index].AddDays(-1));

 

Best regards, 

Henry


@3082492