Value in Indicator and EA is different, WHY?

 
 

I created an indicator similar as Zigzag, but simplify as Percent Of Change of the price.

It seems working fine and I can not find out the problem.

But when I use this indicator in EA, the value show in EA is different from real value.

Appreciate someone can help or give some ideas why? or may email to me alan-zhou@126.com.

Thanks in advance.

See the below indicator for 8 days. Which is EUR/DAY Daily until 31/12/2011.

The real value as below same as above indicator:

Below is the value in EA, day1,3,4 is different from real one.

//+------------------------------------------------------------------+
//|                                            Percent Of Change.mq4 |
//|                                      Copyright  2012, smallcase. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012 smallcase."

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- indicator parameters
extern double percent=0.01; //转向百分比
//---- indicator buffers
double ZigzagBuffer[];
bool downloadhistory=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(1);
//---- drawing settings
   SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
   SetIndexBuffer(0,ZigzagBuffer);
   SetIndexEmptyValue(0,0.0);

//---- indicator short name
   IndicatorShortName("POC("+percent+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int i, counted_bars = IndicatorCounted();
   int limit,whatlookfor;
   int shift,lastpos;
   double lasthigh,lastlow;

   if (counted_bars==0 && downloadhistory) // history was downloaded
      ArrayInitialize(ZigzagBuffer,0.0);
     
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
      whatlookfor=0;
      lastlow=Low[limit];
      lasthigh=High[limit];  
      
   for (shift=limit;shift>=0;shift--)
     {

      switch(whatlookfor)
        {
         case 0: // look for peak or lawn 寻找高点或者低点
              if (High[shift]>lastlow*(1+percent))   //如果寻找到第一个有效高点,就去寻找低点的程序
                 {
                  lasthigh=High[shift];   //最高点就是当前高点
                  lastlow=Low[shift];  //最低点也是当前低点
                  lastpos=shift;  //最后一个指标位置也是当前点
                  whatlookfor=-1;  //转去程序-1
                  ZigzagBuffer[shift]=lasthigh; //指标值就是当前高点  
                  ZigzagBuffer[limit]=Low[limit];  //前指标是前低点
                 }
               if (Low[shift]<lasthigh*(1-percent)) //如果寻找到第一个有效低点,就去寻找高点的程序
                 {
                  lastlow=Low[shift];
                  lasthigh=High[shift];
                  lastpos=shift;
                  whatlookfor=1;
                  ZigzagBuffer[shift]=lastlow;
                  ZigzagBuffer[limit]=High[limit]; 
                 }
             break;  
             
       case -1: // look for lawn 高点,寻找低点

             if (High[shift]>lasthigh)    //高过原高点的情况下,指标上移
              {
               ZigzagBuffer[lastpos]=0.0;  //前一个指标归0
               lastpos=shift;             //最后一个指标是当前位置
               lasthigh=High[shift];      //最高点是当前高点
               lastlow=Low[shift];        //最低点也是当前低点
               ZigzagBuffer[shift]=lasthigh; //指标就是当前最高点
               whatlookfor=-1;               //程序指向-1
              }
              
             else if (Low[shift]<=lasthigh*(1-percent)) //当前低点低过设定转向百分比的时候,也就是找到有效低点
              {
               lasthigh=High[shift];
               lastlow=Low[shift];
               lastpos=shift;            
               ZigzagBuffer[shift]=lastlow;
               whatlookfor=1;
               }

            else if (Low[shift]<lastlow)  //当前低点低过前最低点时
              {
               lastlow=Low[shift];  //当前低点就是最低点
               whatlookfor=-1;
              } 
            break;
            
        case 1: // look for peak 低点,寻找高点
            if (Low[shift]<lastlow)
              {
               ZigzagBuffer[lastpos]=0.0;
               lastpos=shift;
               lasthigh=High[shift];
               lastlow=Low[shift]; 
               ZigzagBuffer[shift]=lastlow;
               whatlookfor=1;
              }           
              
             else if (High[shift]>=lastlow*(1+percent))
              {
               lasthigh=High[shift];
               lastlow=Low[shift];
               lastpos=shift;
               ZigzagBuffer[shift]=lasthigh;
               whatlookfor=-1;
              }   

            else if (High[shift]>lasthigh)
              {
               lasthigh=High[shift];
               whatlookfor=1;
              } 

          break;  

         default: return; 
        }
     }

   return(0);

  }
//+------------------------------------------------------------------+
Files:
 
Maybe you should show your EA code rather than the code for the Indicator which you have already said is working OK.
 
smallcase:
It seems working fine and I can not find out the problem.
If it's working there is no problem :)
 
RaptorUK:
Maybe you should show your EA code rather than the code for the Indicator which you have already said is working OK.

EA just print out the result.

percent=0.01;

Print("poc1:",iCustom(NULL,0,"POC", percent,0,1));
Print("poc2:",iCustom(NULL,0,"POC", percent,0,2));
Print("poc3:",iCustom(NULL,0,"POC", percent,0,3));
Print("poc4:",iCustom(NULL,0,"POC", percent,0,4));
Print("poc5:",iCustom(NULL,0,"POC", percent,0,5));
Print("poc6:",iCustom(NULL,0,"POC", percent,0,6));
Print("poc7:",iCustom(NULL,0,"POC", percent,0,7));
Print("poc8:",iCustom(NULL,0,"POC", percent,0,8));

 

Strange your indicator is a kind of a Zigzag indicator you have only one buffer

but a zigzag has a high and a low value

your indicator is at bar 0 already pointing a new high

So maybe you have to make another buffer in the indicator for separating highs and lows

buffer0 looks pointing high

 
smallcase:

EA just print out the result.


But the EA is probably where the error is . . . show your code.
 
deVries:

Strange your indicator is a kind of a Zigzag indicator you have only one buffer

but a zigzag has a high and a low value

your indicator is at bar 0 already pointing a new high

So maybe you have to make another buffer in the indicator for separating highs and lows

buffer0 looks pointing high

You can download the indicator to try, 1 buffer is enough, include high and low.

Of course I modify Zigzag, which have 3 buffer.

 
RaptorUK:
But the EA is probably where the error is . . . show your code.


//+------------------------------------------------------------------+
//|                                                          SSR.mq4 |
//|                       Copyright ?2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

//#property copyright "Copyright 2012, smallcase"
//#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| 参数预设                                                         |
//+------------------------------------------------------------------+
#define MAGICMA  20050610

extern double Lots            = 0.1;  //开仓大小
extern double percent         = 0.01;   //回弹百分比
extern int    period          = 200;    //基本趋势
extern int    MODE_MA   =1;  
extern double risk =0.015; //止损百分比

//double ZigzagBuffer[1000];
//bool downloadhistory=false;

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading 
   if(Bars<100 || IsTradeAllowed()==false) return;

  if(CalculateCurrentOrders(Symbol())!=0) CheckForClose();  
  if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
//----
  }

//+------------------------------------------------------------------+
//| Check for open order conditions 开仓条件                             |
//+------------------------------------------------------------------+
   
void CheckForOpen()
  {
   double poc1,poc2,poc3,ma;
   int    res, shift=1, pos1,pos2,pos3;
//---- go trading only for first tiks of new bar 仅在新烛的第一柱交易。是为了避免重复交易,只能在开市价交易
   if(Volume[0]>1) return;
        
poc1=iCustom(NULL,0,"POC",percent,0,shift); 

    
// 取得平均指数和POC的值


Print("poc1:",iCustom(NULL,0,"POC", percent,0,1));
Print("poc2:",iCustom(NULL,0,"POC", percent,0,2));
Print("poc3:",iCustom(NULL,0,"POC", percent,0,3));
Print("poc4:",iCustom(NULL,0,"POC", percent,0,4));
Print("poc5:",iCustom(NULL,0,"POC", percent,0,5));
Print("poc6:",iCustom(NULL,0,"POC", percent,0,6));
Print("poc7:",iCustom(NULL,0,"POC", percent,0,7));
Print("poc8:",iCustom(NULL,0,"POC", percent,0,8));

         
  ma=iMA(NULL,0, period,0,MODE_MA,PRICE_CLOSE,pos1);
}

//+------------------------------------------------------------------+
//| Check for close order conditions      清仓                           |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma1;
   double ma2;
   double ma3;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      

      //---- check order type 查看定单类型
      if(OrderType()==OP_BUY)
        {
      //   if(lasthigh/Low[1]>RSI) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(ma1>ma2) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
}

//+------------------------------------------------------------------+
//| Calculate open positions         计算开仓数量                    |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }    
see, the code is really simple, not even start, just print the value and found the problem.
 
smallcase:
see, the code is really simple, not even start, just print the value and found the problem.
Your iCustom call calls an Indicator called POC not PercenteOfxChange
 
RaptorUK:
Your iCustom call calls an Indicator called POC not PercenteOfxChange

Yes, "POC" and "Percent Of Change" is the same.
 
Trie this indicator
//+------------------------------------------------------------------+
//|                                                    Comments4.mq4 |
//|                                Copyright © 2012, Tjipke de Vries |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Tjipke de Vries"
#property link      ""

#property indicator_chart_window

extern double percent = 0.01;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   subPrintDetails();
//----
   return(0);
  }
//+------------------------------------------------------------------+

//----------------------- PRINT COMMENT FUNCTION
void subPrintDetails()
{
   string sComment   = "";
   string sp         = "----------------------------------------\n";
   string NL         = "\n";
   
   double poc1=iCustom(NULL,0,"POC", percent,0,1);
   double poc2=iCustom(NULL,0,"POC", percent,0,2);
   double poc3=iCustom(NULL,0,"POC", percent,0,3);
   double poc4=iCustom(NULL,0,"POC", percent,0,4);
   double poc5=iCustom(NULL,0,"POC", percent,0,5);
   double poc6=iCustom(NULL,0,"POC", percent,0,6);
   double poc7=iCustom(NULL,0,"POC", percent,0,7);
   double poc8=iCustom(NULL,0,"POC", percent,0,8);

       
   sComment = "Comments 4              Copyright © 2012, Tjipke" + NL;
   sComment = sComment + NL;

   sComment = sComment + "poc1 " + DoubleToStr(poc1,Digits) + NL;
   sComment = sComment + "poc2 " + DoubleToStr(poc2,Digits) + NL;
   sComment = sComment + "poc3 " + DoubleToStr(poc3,Digits) + NL;
   sComment = sComment + "poc4 " + DoubleToStr(poc4,Digits) + NL;
   sComment = sComment + "poc5 " + DoubleToStr(poc5,Digits) + NL;
   sComment = sComment + "poc6 " + DoubleToStr(poc6,Digits) + NL;
   sComment = sComment + "poc7 " + DoubleToStr(poc7,Digits) + NL;
   sComment = sComment + "poc8 " + DoubleToStr(poc8,Digits) + NL;
   sComment = sComment + NL;


   Comment(sComment);
}
//+------------------------------------------------------------------+
Reason: