Cant Find error in code

 

Dear Friends

I am new to MQL4 language and trying to learn it, and building my own Expert for 2 Moving average crossover, I know there are some already built solutions, but I have some other logics to be incorporated so writing my own code, below is the basic version of my code where I dont see any issues, but I am facing some problems while running this expert:

Code: Basic logic is if Fast MA crosses the slow Ma it should place buy order and vice versa.

void checkcross(int fma, int sma)
  {
   BuySig = false;
   SellSig = false;
   RefreshRates();
   double fasma[], slowma[];
   int i=0;
   ArrayResize(fasma,3);
   ArrayResize(slowma,3);
   for(i=0; i<3; i++)
  {
   fasma[i] = iMA(Symbol(), 0, fma,8,MaMethod,PriceType,i);
   slowma[i] = iMA(Symbol(), 0, sma,8,MaMethod,PriceType,i);
  }
  if(fasma[2] < slowma[2] && fasma[0] > slowma[0]) BuySig= True;
  if(fasma[2] > slowma[2] && fasma[0] < slowma[0]) SellSig= True;
  if(BuySig){
      // Buy Order Command
  }
  if(SellSig){
      // Sell Order Coammnd
  }
}

Issues I am facing:

1. Its not making BuySig parameter true in any case hence not placing buy order, even on chart I can see the cross between FastMA & SlowMA visually in the required direction. Sell orders are being placed without any issues, and I had also set Alert command to check values of Buy & Sell Signal.

2. Though I have placed cross check in recent 3 bars only, but its placing sell order even if the cross happend 6-7 bars before the current bar.

The above function is called only on open of new Bar, thats verified by me by placing alerts, hence this function will only run then.


Am I doing something wrong with code, no error in compilation, even no error in placing orders tried running that expert on demo account also.

So experts I need little help here to understand what I am doing wrong.

 
Nilesh Toshniwal:

Dear Friends

I am new to MQL4 language and trying to learn it, and building my own Expert for 2 Moving average crossover, I know there are some already built solutions, but I have some other logics to be incorporated so writing my own code, below is the basic version of my code where I dont see any issues, but I am facing some problems while running this expert:

Code: Basic logic is if Fast MA crosses the slow Ma it should place buy order and vice versa.

Issues I am facing:

1. Its not making BuySig parameter true in any case hence not placing buy order, even on chart I can see the cross between FastMA & SlowMA visually in the required direction. Sell orders are being placed without any issues, and I had also set Alert command to check values of Buy & Sell Signal.

2. Though I have placed cross check in recent 3 bars only, but its placing sell order even if the cross happend 6-7 bars before the current bar.

The above function is called only on open of new Bar, thats verified by me by placing alerts, hence this function will only run then.


Am I doing something wrong with code, no error in compilation, even no error in placing orders tried running that expert on demo account also.

So experts I need little help here to understand what I am doing wrong.

Change the shift value from 8 to 0.

You use the shift value 8 bars in front the current bar, and you want BuySig = true at bar 0, this will never happen, except coincidence

 
Thanks for your quick response, I made the shift to Zero, and running another trial. will keep posted.
 
  1. Next time post all relevant code. We have no idea what MaMethod, PriceType are.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  2. Your code
       BuySig = false;
       SellSig = false;
       RefreshRates();
       double fasma[], slowma[];
       int i=0;
       ArrayResize(fasma,3);
       ArrayResize(slowma,3);
       for(i=0; i<3; i++)
      {
       fasma[i] = iMA(Symbol(), 0, fma,8,MaMethod,PriceType,i);
       slowma[i] = iMA(Symbol(), 0, sma,8,MaMethod,PriceType,i);
      }
      if(fasma[2] < slowma[2] && fasma[0] > slowma[0]) BuySig= True;
      if(fasma[2] > slowma[2] && fasma[0] < slowma[0]) SellSig= True;
    Simplified
    // RefreshRates();
       double fasma[3], slowma[3];
       for(int i=0; i<3; i++)
      {
       fasma[i] = iMA(Symbol(), 0, fma,8,MaMethod,PriceType,i);
       slowma[i] = iMA(Symbol(), 0, sma,8,MaMethod,PriceType,i);
      }
      BuySig = fasma[2] < slowma[2] && fasma[0] > slowma[0];
      SellSig= fasma[2] > slowma[2] && fasma[0] < slowma[0];
    RefreshRates updates:
    Predefined variables: Ask, Bars, Bid, Close[], High[], Low[], Open[], Point, Time[], Volume[]
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference
              Predefined Variables - MQL4 Reference

    you aren't using any of them.

  3. Do you understand that the MAs might have crossed between bar 2 and bar 1, or between bar 1 and bar 0, or might have crossed and uncross between 2 and zero?
Reason: