Search code examples
algorithmic-tradingmql4metatrader4mt4

iCustom() buffer - how to get values from Custom Indicator buffers in MT4?


I am trying to write an EA that will place a BUY, when the Custom Indicator shows an arrow { sell or buy }. I am using an iCustom() to do that, but I am struggling with comparing values.

Here is my code:

void OnTick()
  {
//---
       double sell=iCustom(NULL,0,"fx30",0,0);
       double buy=iCustom(NULL,0,"fx30",1,0);
       
       if(sell>0)//sell
       {
        //check if buy trade is running
        //close a buy trade
        //open a sell trade on success
         if(buyTicket>0)
          {
            bool ret=OrderClose(buyTicket, lot, Bid, slipage,clrBlue);
            if(ret==true)
              {
                printf("the sell number is: "+sell);
                sellTicket=OrderSend(Symbol(),OP_SELL,lot,Bid,slipage,NULL,NULL,"MATHUNYA SELL",magic,0,clrRed);
              }
          }
         else
         {
           //we dont have a buy trade open
           //place a sell trade
           //only one trade should be open
           if(sellTicket>0)
           {
             printf("sell order already running");
           }
           else
           {
             printf("the sell number is: "+sell);
             sellTicket=OrderSend(Symbol(),OP_SELL,lot,Bid,slipage,NULL,NULL,"MATHUNYA SELL",magic,0,clrRed);
           }     
         }            
       }
       else{
         Print("buy: "+buy+", sell: "+sell+" Time: "+TimeToStr(Time[1]));
         printf("awaiting sell order..");
       }
       
       if(buy>0)//buy
       {
       //check if sell trade is running
       //close a sell trade
       //open a buy trade on success
         if(sellTicket>0)
          {
            bool ret=OrderClose(sellTicket, lot, Ask, slipage,clrYellow);
            if(ret==true)
              {
                printf("the buy number is: "+buy);
                buyTicket=OrderSend(Symbol(),OP_BUY,lot,Ask,slipage,NULL,NULL,"MATHUNYA BUY",magic,0,clrGreen);
              }
          }
         else
         {
           //we dont have a sell trade open
           //place a buy trade
           //only one trade should be open
           if(buyTicket>0)
           {
             printf("buy order already running");
           }
           else
           {
              printf("the buy number is: "+buy);
              buyTicket=OrderSend(Symbol(),OP_BUY,lot,Ask,slipage,NULL,NULL,"MATHUNYA BUY",magic,0,clrGreen);
           }     
         }          
       }else
       {
         Print("buy: "+buy+", sell: "+sell+" Time: "+TimeToStr(Time[1]));
         printf("awaiting buy order..");
       }
      
  }
//+------------------------------------------------------------------+

// global variables

int    buyTicket  =   0;
int    sellTicket =   0;
double lot        =   0.01;
int    slipage    =   3;
int    magic      = 321;

Solution

  • what do you mean by comparing values? as I can see, you only compare sell>0 and buy>0. My advice: immediately below that comparing add line
    Print(__LINE__," indicator value = "+DoubleToStr(sell,Digits)); and same with buy. It is likely, but of course we do not have crystal ball here, that your indicator returns value (e.g. 100 or 1.16000) on one buffer, and EMPTY_VALUE on another. EMPTY_VALUE is 2^31-1 in mql4 so it is larger then zero. if you will see that indicator value is 2147483648.00000 or something like that, - you need to replace sell > 0 with sell != EMPTY_VALUE or combine.