Search code examples
metatrader4

how to use metatrader 4 predefine variable


How do i use the predefined variable like Open[],Close[],High[],low[]? I cant get my code working, it is as if the variable has no data in it when i run it in EA tester. Sorry guys, im not from programming background, but i do understand basic programming.

void OnTick() {

  if (OrdersTotal()==0)//if we have no open orders
  
  double ma20    = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
  double ma200   = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
  double rsi     = iRSI(NULL,0,12,PRICE_OPEN,0);

        if (ma20>ma200 && rsi<65 && Close[1]<Open[1] && High[1]>Open[1] && High[0]>High[1])     
   
           //send a buy order
           int buyticket = OrderSend
           (
           Symbol(),                        //Currency pair on the chart
           OP_BUY,                          //Buy/sell Command
           0.01,                            //Lot Size
           Ask,                             //Price for the command
           3,                               //Slippage usually 3
           Ask-100*_Point,                  //Stoploss
           Ask+100*_Point,                  //Take Profit                            
           NULL,                            //Commend
           0,                               //Magic No
           0,                               //Expiry
           Green                            //Arrow colour
        
           );
}

Solution

  • Your code works fine, however you haven't properly used brackets to allow your if statements to operate correctly.

    if (OrdersTotal()==0)//if we have no open orders
    {
       double ma20    = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
       double ma200   = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
       double rsi     = iRSI(NULL,0,12,PRICE_OPEN,0);
    
       if (ma20>ma200 && rsi<65 && Close[1]<Open[1] && High[1]>Open[1] && High[0]>High[1])     
       {
          //send a buy order
          int buyticket = OrderSend
          (
             Symbol(),                        //Currency pair on the chart
             OP_BUY,                          //Buy/sell Command
             0.01,                            //Lot Size
             Ask,                             //Price for the command
             3,                               //Slippage usually 3
             Ask-100*_Point,                  //Stoploss
             Ask+100*_Point,                  //Take Profit                            
             NULL,                            //Commend
             0,                               //Magic No
             0,                               //Expiry
             Green                            //Arrow colour
         );
       }
    }