hma indicator, new mt4, array out of range

 

hello, i'm trying to convert old HMA code to new one. it is working in old version without problem.

under build 610, it gives error: array out of range.

i'm not familiar with indicators. what is basic difference between old and new indicators and buffers? is indexing changed?

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 1
#property  indicator_color1  Blue

//---- indicator parameters
extern int HMA_Period=20;

//---- indicator buffers
double     ind_buffer0[];
double     ind_buffer1[];

int        draw_begin0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   //---- indicator buffers mapping
   IndicatorBuffers(2);
   if(!SetIndexBuffer(0,ind_buffer0) && !SetIndexBuffer(1,ind_buffer1))
      Print("cannot set indicator buffers!");
      
   //   ArraySetAsSeries(ind_buffer1,true);
   //---- drawing settings
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
   draw_begin0=HMA_Period+MathFloor(MathSqrt(HMA_Period));
   SetIndexDrawBegin(0,draw_begin0);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("HMA("+HMA_Period+")");
   SetIndexLabel(0,"Hull Moving Average");
   
   return(0);
  }
  
int start()
  {
   int limit,i;
   int counted_bars=IndicatorCounted();
   
   //---- check for possible errors
   if(counted_bars<1)
     {
      //for(i=1;i<=draw_begin0;i++) ind_buffer0[Bars-i]=0;
      //for(i=1;i<=HMA_Period;i++) ind_buffer1[Bars-i]=0;
     }
     
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   //---- MA difference counted in the 1-st buffer
   for(i=0; i<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
                     
   //---- HMA counted in the 0-th buffer
   for(i=0; i<limit; i++)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);

   return(0);
  }
  
 
cbalta: it is working in old version without problem. under build 610, it gives error: array out of range. what is basic difference?
  1. Under the old elements outside the array were silently converted to zero. Under the new you get array out of range.
  2. No need for the decrement Contradictory information on IndicatorCounted() - MQL4 forum
  3. When i is Bars - 1, iMA(..., i) is looking beyond the end of the array. Need to handle your lookback amounts. HMA has two, the iMA, and onArray past buffer1 values.
  4. You should always count down (can't use future bar, simplifies the code)
// if(counted_bars>0) counted_bars--;
// limit=Bars-counted_bars;
   int lookback1 = MathMax( HMA_Period/2, HMA_Period ),
       lookback2 = lookback1 + MathSqrt(HMA_Period),
       counted2  = counted_bars;
   if(counted_bars < lookback1) counted_bars = lookback1;
   if(counted2     < lookback2) counted2     = lookback2;
   
   //---- MA difference counted in the 1-st buffer
// for(i=0; i<limit; i++)
   for(i=Bars - 1 - counted_bars; i >= 0; i--)
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
                     
   //---- HMA counted in the 0-th buffer
// for(i=0; i<limit; i++)
   for(i=Bars - 1 - counted2; i >= 0; i--)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);
 

hello, thanks for the answer. i'm trying to convert the code as how it is working in old version.

i need last bar value update, it exist in old version.

our old indicators and their iCustom calls from EA's all having risk, under new platform..

 
any idea for calculatin HMA inside an EA, as a function, without external indicator?
 

The way that the indicator buffers are being set is causing a problem..
Try replacing

if(!SetIndexBuffer(0,ind_buffer0) && !SetIndexBuffer(1,ind_buffer1))
      Print("cannot set indicator buffers!");

with..

if(!SetIndexBuffer(0,ind_buffer0))
      Print("cannot set indicator buffers!");
if(!SetIndexBuffer(1,ind_buffer1))
      Print("cannot set indicator buffers!");
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 1
#property  indicator_color1  Blue

//---- indicator parameters
extern int HMA_Period=20;

//---- indicator buffers
double     ind_buffer0[];
double     ind_buffer1[];

int        draw_begin0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   //---- indicator buffers mapping
   IndicatorBuffers(2);
   //if(!SetIndexBuffer(0,ind_buffer0) && !SetIndexBuffer(1,ind_buffer1))
   //   Print("cannot set indicator buffers!");
   if(!SetIndexBuffer(0,ind_buffer0))
      Print("cannot set indicator buffers!");
   if(!SetIndexBuffer(1,ind_buffer1))
      Print("cannot set indicator buffers!");      
   //   ArraySetAsSeries(ind_buffer1,true);
   //---- drawing settings
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
   draw_begin0=HMA_Period+MathFloor(MathSqrt(HMA_Period));
   SetIndexDrawBegin(0,draw_begin0);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("HMA("+HMA_Period+")");
   SetIndexLabel(0,"Hull Moving Average");
   
   return(0);
  }
  
int start()
  {
   int limit,i;
   int counted_bars=IndicatorCounted();
   
   //---- check for possible errors
   if(counted_bars<1)
     {
      //for(i=1;i<=draw_begin0;i++) ind_buffer0[Bars-i]=0;
      //for(i=1;i<=HMA_Period;i++) ind_buffer1[Bars-i]=0;
     }
     
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   //---- MA difference counted in the 1-st buffer
   for(i=0; i<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
                     
   //---- HMA counted in the 0-th buffer
   for(i=0; i<limit; i++)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);

   return(0);
  }
    
See if that will work for you. It seems to work in my 610. PipPip...Jimdandy
 
thank you very much, Jimdany. worked.
 
cbalta:
thank you very much, Jimdany. worked.


You are welcome. Glad I could help. Every once in a while I get something right.

Another satisfied visitor to Mql4.com! LOL

PipPip...Jimdandy

 
Jimdandy:


You are welcome. Glad I could help. Every once in a while I get something right.

Another satisfied visitor to Mql4.com! LOL

PipPip...Jimdandy


Hi, Jim...

I subscribe to your great video series on your site (highly recommend this to anyone learning mql4...).

I'm trying to get a 2010 era expert I use ( modified by me, not coded) to use something like OnTimer, so it doesn't modify stoplosses and tps every tick...

already had to shut it down on one broker, because of excessive orders or messages...

I tried the info on your videos, and searching the net, but haven't been able to get it to work error free...

I've attached the original:  I modified it so it doesn't increase order size...

any suggestions would be much appreciated... 

Files:
 
//+------------------------------------------------------------------+
//|                                                   º£ÉÏÁú2 v1.mq4 |
//|                                                             ÁõËÉ |
//|                                                    qq£º569638390 |
//+------------------------------------------------------------------+
#property copyright "ÁõËÉ"
#property link      "qq£º569638390"

extern double lot=0.1;
extern double     sl       = 10; 
extern double     maxsl       = 150; 
extern double     tp       =10;
extern double     tp_2       =2;

extern double     weishu       =1;
  
int slippage=2;
int Magic=12332344;


double g_point;
int init() {
   if (Point == 0.00001) g_point = 0.0001;
   else {
      if (Point == 0.001) g_point = 0.01;
      else g_point = Point;
   }
   return (0);
}

int start(){  

ckdingdan();
if(jiange()){
if (buyorsell()==OP_BUY)buy();
if (buyorsell()==OP_SELL)sell();
} // jiange end
ckmd();

return(0);
}


void ckmd(){
if(OrdersTotal()>0){
   int b,s;
   double price,price_b,price_s,lot,SLb,SLs,lot_s,lot_b;
   for (int j=0; j<OrdersTotal(); j++)
   {  if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==true)
      {  if ((Magic==OrderMagicNumber() ) && OrderSymbol()==Symbol())
         {
            price = OrderOpenPrice();
            lot   = OrderLots();
            if (OrderType()==OP_BUY ) {price_b += price*lot; lot_b+=lot; b++;}                     
            if (OrderType()==OP_SELL) {price_s += price*lot; lot_s+=lot; s++;}
         }  
      }  
   }
  if(lot_b!=0)  SLb = price_b/lot_b;
  if(lot_s!=0)  SLs = price_s/lot_s;
    for(int m=0;m < OrdersTotal();m++){ 
    if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()){
    if(b==s){
     if(OrderType()==OP_BUY&&OrderTakeProfit()!=OrderOpenPrice()+tp*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-maxsl*g_point,OrderOpenPrice()+tp*g_point,0,Gold);
     if(OrderType()==OP_SELL&&OrderTakeProfit()!=OrderOpenPrice()-tp*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+maxsl*g_point,OrderOpenPrice()-tp*g_point,0,Gold);
     }//buyorsell end
 
    if(b>s){
     if(OrderType()==OP_BUY&&OrderTakeProfit()!=SLb+Ask-Bid+tp_2*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-maxsl*g_point,SLb+Ask-Bid+tp_2*g_point,0,Aqua);
     if(OrderType()==OP_SELL&&OrderTakeProfit()!=OrderOpenPrice()-tp*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+maxsl*g_point,OrderOpenPrice()-tp*g_point,0,Aqua);
     }//buyorsell end
 
    if(b<s){
     if(OrderType()==OP_BUY&&OrderTakeProfit()!=OrderOpenPrice()+tp*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-maxsl*g_point,OrderOpenPrice()+tp*g_point,0,LightYellow);
     if(OrderType()==OP_SELL&&OrderTakeProfit()!=SLs-Ask+Bid-tp_2*g_point)OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+maxsl*g_point,SLs-Ask+Bid-tp_2*g_point,0,LightYellow);
     }//buyorsell end
 
    }
   }//for end
 
 
 
  }//total end
}


void buy(){
OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,0,0,"BUY",Magic,0,C'11,4,125');Sleep(500);
OrderSend(Symbol(),OP_BUY,ll(OP_BUY),Ask,slippage,0,0,"sell",Magic,0,C'128,2,5');Sleep(500);
return(0);
}

void sell(){
OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,0,0,"BUY",Magic,0,C'128,2,5');Sleep(500);
OrderSend(Symbol(),OP_SELL,ll(OP_SELL),Bid,slippage,0,0,"sell",Magic,0,C'11,4,125');Sleep(500);
return(0);
}

/*
double ll(int dan){
double lo;
double lt=-1;
if(OrdersTotal()>0){
    for(int m=0;m < OrdersTotal();m++){ 
    if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()&&OrderType()==dan){
   lt=MathMax(lt,OrderLots());
    }
  }//  maxtimeend
 } //total end   
 if(lt<lot*3) lo=lt*2;else lo=NormalizeDouble(lt*1.5,weishu);
return(lo);
}
*/


double ll(int dan){
int lo[15];
lo[0]=1;
lo[1]=1;
lo[2]=2;
lo[3]=3;
lo[4]=6;
lo[5]=9;
lo[6]=14;
lo[7]=22;
lo[8]=33;
lo[9]=48;
lo[10]=82;
lo[11]=111;
lo[12]=122;
lo[13]=164;
lo[14]=185;

int lt=0;
if(OrdersTotal()>0){
    for(int m=0;m < OrdersTotal();m++){ 
    if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()&&OrderType()==dan){
   lt++;
    }
  }//  maxtimeend
 } //total end   

double loo=lo[lt+1];
if(weishu==1)loo=loo/10;
if(weishu==2)loo=loo/100;

return(loo);
}



int buyorsell(){
int buy=0,sell=0;
if(OrdersTotal()>0){
for(int m=0;m < OrdersTotal();m++){ 
    if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()){
    if(OrderType()==OP_BUY) buy++;
    if(OrderType()==OP_SELL) sell++;
    }
  }//  forend
 }// totals end
 
   Comment(buy,"****",sell);
if(buy==sell)return(5);
if(buy>sell) return(OP_BUY);
if(buy<sell) return(OP_SELL);
}

bool jiange(){
double time=-1;
double open;
if(OrdersTotal()>0){
    for(int m=0;m < OrdersTotal();m++){ 
    if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()){
    time=MathMax(time,OrderOpenTime());
    }
  }//  maxtimeend
    for(int i=0;i < OrdersTotal();i++){ 
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic)&&OrderSymbol()==Symbol()){
    if(time==OrderOpenTime())  open=OrderOpenPrice();
    }
   }   
if(MathAbs(open-((Ask+Bid)/2))/(sl*g_point)>=1&&open!=0)  return(true); else return(false);
 } //total for end
return(false);
}// jiage end



void ckdingdan(){
if (OrdersTotal()==0){
ckopen();
}else{
 for(int m=0;m < OrdersTotal();m++){ 
         if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES)==false)     break;
    if ((OrderMagicNumber() == Magic&&OrderSymbol()==Symbol())){  
    break;
    ckopen();
    }
   }// for end
}//else end
return;
}

void ckopen(){
OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,0,0,"BUY",Magic,0,Red);Sleep(500);
OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,0,0,"BUY",Magic,0,Green);Sleep(500);
return(0);
Reason: