Different values for history calculations and realtime..

 

I've encountered something very strange I think. I have some years of experience with metatrader, and never had this problem before.

The problem is that when I apply an indicator, and it calculates the values backwards in time (history values) everything is perfect. Then, when it runs in realtime, I get different values, even if I force the calculations to be made on the previous bar (where all values are already set). If compile the indicator again, it replaces the chart with correct values.

I first though that is was some values that transferred to the next bar, but I've double checked this and unfortunately this wasn't the solution.

The code I use is:

[code]

#property indicator_buffers 5
#property indicator_color1 Green
#property indicator_color2 PaleGreen
#property indicator_color3 Silver
#property indicator_color4 Tomato
#property indicator_color5 Red
#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 4
#property indicator_width4 4
#property indicator_width5 4

#property indicator_minimum -5
#property indicator_maximum 5

#property indicator_separate_window

double MapBuffer1[], MapBuffer2[], MapBuffer3[],
MapBuffer4[], MapBuffer5[], MapBuffer6[];

double tpo=0;
int length=14;


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY_VALUE);
SetIndexBuffer(0, MapBuffer1);
SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY_VALUE);
SetIndexBuffer(1, MapBuffer2);
SetIndexStyle(2, DRAW_HISTOGRAM, EMPTY_VALUE);
SetIndexBuffer(2, MapBuffer3);
SetIndexStyle(3, DRAW_HISTOGRAM, EMPTY_VALUE);
SetIndexBuffer(3, MapBuffer4);
SetIndexStyle(4, DRAW_HISTOGRAM, EMPTY_VALUE);
SetIndexBuffer(4, MapBuffer5);
IndicatorShortName("TLP");

//SetIndexEmptyValue(0, 0);
//SetIndexEmptyValue(1, 0);


SetIndexDrawBegin(0,50);
SetIndexDrawBegin(1,50);
SetIndexDrawBegin(2,50);
SetIndexDrawBegin(3,50);
SetIndexDrawBegin(4,50);

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
int i=Bars-counted_bars; // Index of the first uncounted
while(i>0) // Loop for uncounted bars {
{


// ---- DirMovement --- //
//variables
int colorcode;

colorcode=0;
colorcode=Bias(i+1);


MapBuffer1[i+1]=0;
MapBuffer2[i+1]=0;
MapBuffer3[i+1]=0;
MapBuffer4[i+1]=0;
MapBuffer5[i+1]=0;

if(colorcode==4 || colorcode==3 || colorcode==2) MapBuffer1[i+1]=1;
if(colorcode==1) MapBuffer2[i+1]=1;
if(colorcode==0) MapBuffer3[i+1]=1;
if(colorcode==-1) MapBuffer4[i+1]=1;
if(colorcode==-2 || colorcode==-3 || colorcode==-4) MapBuffer5[i+1]=1;

i--;
}




//----
return(0);
}[/code]

In the code I left out the calculations for the last (active) bar, hence "while(i>0)". But htis doesn't solve the problem either. Have anyone had this problem, or any similar?

Two functions are called in the code, but I can't show these based on privacy restrictions. The should affect anything anyway, since all calculations are supposed to made "in the past".

Thank you in advance.

 

I have a similar problem when I tried working on an indicator I modified. It's similar to EMA, so I have to calculated backwards from the oldest data to the latest ones.


When I dragged it onto the chart, I did the manual calculation, it's correct. But when I used iCustom to link it into the EA for testing, the result was not right, which means the value showing on the same indicator which is put onto the chart is different from the "Print" clause I output the value when running the strategy tester. Therefore, I can't run it correctly when the indicator has the right value, the tester gave the wrong entries, EVERY TIME!!


Also I noticed that in my code:

int counted_bars=IndicatorCounted();
 
   if(counted_bars>0)
      counted_bars--;
   else
      counted_bars=1;
   int limit=Bars-counted_bars;

"Limit" outputs 2 here when it is attached to the chart, outputs 3 when running the strategy tester.


I am using Version 4.00 build 218 Alpari version. Did I do something wrong or a bug was introducted into the platform in this release??


Please advise, please!!

 
SysInv:


In the code I left out the calculations for the last (active) bar, hence "while(i>0)". But htis doesn't solve the problem either. Have anyone had this problem, or any similar?

Two functions are called in the code, but I can't show these based on privacy restrictions. The should affect anything anyway, since all calculations are supposed to made "in the past".

Thank you in advance.

Your code contains bug. It shoild be so:

while(i>=0) // Loop for uncounted bars {
{

 
Rosh wrote >>

Your code contains bug. It shoild be so:

while(i>=0) // Loop for uncounted bars {
{

Thank you for your answer Rosh, I tried a few different solutions and maybe the code wasn't what I first used. I tried to delete the >= and only use > to avoid calculating on the current bar (therefore force it to calculate on closed bars) and in this case I also use i=Bars-Counted_bars-1to make it recalculate the last closed bar.

I also tried to use >= and then force the calculation backwards each time, with MapBuffer[i+1]= (the algorithms +1) but this doesn't solve my problem either...

Reason: