problems with my script

 

hello, I have problems with my code, it's don't work, Normally it have to take long or short after crossing MA. Can you tell me where my code is bad please. thanks

 

int start()
{
   int period = 20;
   double mmh[];

int counted = IndicatorCounted();
int i = Bars - counted -1;

while(i>=0)
{
     double sum = 0;
     for(int k = i ; k <= i+period -1 ; k++)
        {
            sum = sum + Close[k];
        
        }
    mmh[i] = sum / period;    

i--;

}

int total = OrdersTotal();

if (Close[i+1]< mmh[i] && Close[i] > mmh[i])
  {
    if (Volume[i]>3 && total <1)
       {
       
           int res = OrderSend(Symbol(),OP_BUY,1,Ask,5,Ask-100*Point,Ask+10*Point,NULL,69,0,clrRed);
       
       }
  
  }
  

if (Close[i+1]> mmh[i]&&Close[i] < mmh[i])
  {
    if (Volume[0]>3 && total <1)
       {
       
           res = OrderSend(Symbol(),OP_SELL,1,Bid,5,Bid+100*Point,Bid-10*Point,NULL,69,0,clrRed);
       
       }
  
  
  }
  
return(0);
}
 

After your while loop, i=-1

So what values would Close[i] and mmh[i] have? 

It maybe simpler to use iMA

 
  1. Your look back is period. So when IndicatorCounted == 0, i is Bars-1 and k goes to Bars+period-2 which does not exist and script/EA fails.

    4002

    ERR_ARRAY_INDEX_OUT_OF_RANGE

    Array index is out of range

    int counted = IndicatorCounted();
    int i = Bars - counted -1;
    
    while(i>=0){
         double sum = 0;
         for(int k = i ; k <= i+period -1 ; k++){
                sum = sum + Close[k];
    Handle look back properly
    int counted = IndicatorCounted();
    int i = Bars - MathMax(period, counted) -1;
    while(i>=0){

  2. What do you think happens when you try to assign a value to a zero length array?
       double mmh[];
    :
        mmh[i] = sum / period;  
    

  3. After loop i equals -1 close[i] does not exist
  4. Why are you calculating SMA for all bars, when you only look at two values (current ones?)
  5. Check your return codes (OrderSend) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 

thanks for helping guys !

Now my code works, I made so much kiddies error :/ thanks for "checking ordersend and orderselect" links, i will add them


Reason: