Download MetaTrader 5

How to read values from indicators in EA

To add comments, please log in or register
Mark Cornbill
38
Mark Cornbill  

Hi Guys

 I have a template EA script (Robust_EA_Template.mq4 from (commercial link removed by moderator) and have it compiling / working.

 I have two indicators that I want this EA to read it's values from. The ALF indicator (AdaptiveLaguerreFilter.mq4), which I need to run in two different timeframes and return its value.  And the Linear Price Bar.mq4 indicator, which I also need to return its value.

 I think the difficulty is going to come when I need to read the value of the ALF indicator, every minute, running on two different timeframes:

1. Once as if it were being run on a 5 minute chart
2. Once as if it were being run on a 1 minute chart

 I know that iCustom is a command to return the values of indicators, however I'm stuck just knowing how to get these indicators into the main EA code.  I know I will be checking for the values of these indicators in the "ShortSignal" and "LongSignal" parts of the below EA code:

 

#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| EA Inputs                                                        |
//+------------------------------------------------------------------+
extern double  takeProfit         = 500.0;        // Take Profit (pips)
extern double  stopLoss           = 500.0;        // Stop Loss (pips)
input  double  lots               = 0.1;         // Lots
extern int     slippage           = 50;           // Maximum Slippage (pips)
input  int     magicNumber        = 1443726994;  // Magic Number
//+------------------------------------------------------------------+
//| Initialize Variables                                             |
//+------------------------------------------------------------------+
double buyStopLossPips=200.0;
double buyTakeProfitPips= 200.0;
double sellStopLossPips = 200.0;
double sellTakeProfitPips=200.0;
datetime lastBar=D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Initialization Loop                                              |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Bars<30) // Check for enough bars in chart
     {
      Print("Error initializing: Not enough bars in chart");
      return INIT_FAILED;
     }

   if(takeProfit<2) // Check Take Profit
     {
      Print("Error initializing: Take Profit less than 10");
      return INIT_PARAMETERS_INCORRECT;
     }

   if(stopLoss<2) // Check Stop Loss
     {
      Print("Error initializing: Stop Loss less than 10");
      return INIT_PARAMETERS_INCORRECT;
     }

   if(Digits==5 || Digits==3) // Adjust for 3- or 5-digit Brokers
     {
      takeProfit*=100;
      stopLoss *= 100;
      slippage *= 100;
     }

   Print(" Lots=",lots," Take Profit=",takeProfit," Stop Loss=",stopLoss," Slippage=",slippage);
   return INIT_SUCCEEDED;
  }
//+------------------------------------------------------------------+
//| Main Loop                                                        |
//+------------------------------------------------------------------+
void OnTick()
  {
// Calculate Buy or Sell Signal
   int signal=LongSignal()+ShortSignal();
   processSignal(signal);
  }
//+------------------------------------------------------------------+
//| Set Take Profit and Stop Loss                                    |
//+------------------------------------------------------------------+
void setTakeProfitAndStopLoss()
  {
   buyTakeProfitPips=NormalizeDouble(Ask+takeProfit*Point,Digits);
   buyStopLossPips=NormalizeDouble(Ask-stopLoss*Point,Digits);
   sellTakeProfitPips=NormalizeDouble(Bid-takeProfit*Point,Digits);
   sellStopLossPips=NormalizeDouble(Bid+stopLoss*Point,Digits);
  }
//+------------------------------------------------------------------+
//| Check for open position and entry signal                         |
//+------------------------------------------------------------------+
void processSignal(const int signal)
  {
   setTakeProfitAndStopLoss(); // Set Take Profit and Stop Loss

   if(isPositionOpen()) // Check for open position
     {
      processSignalHasPositionOpen(signal);
     }
   else // If no open position
     {
      processSignalNoPositionOpen(signal);
     }
  }
//+------------------------------------------------------------------+
//| Check for currently open position                                |
//+------------------------------------------------------------------+
bool isPositionOpen()
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()==magicNumber && OrderSymbol()==Symbol())
        {
         return true;
        }
     }
   return false;
  }
//+------------------------------------------------------------------+
//| Entry process if position already open                           |
//+------------------------------------------------------------------+
void processSignalHasPositionOpen(const int signal)
  {
   switch(OrderType())
     {
      case OP_BUY:   // A buy order exists
         if(signal<0)
           {
            Print("Short Conditions Met");
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Violet))
              {
               Print("Error closing buy order: ",GetLastError());
                 } else if(OrderSend(Symbol(),OP_SELL,lots,Bid,slippage,sellStopLossPips,sellTakeProfitPips,"TRAIDE EA",magicNumber,0,Red)<0) {
               Print("Error opening sell order: ",GetLastError());
              }
              } else if(signal>0) {
            Print("Long Conditions Still Met");
           }
         break;
      case OP_SELL:  // A sell order exists
         if(signal>0)
           {
            Print("Long Conditions Met");
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Violet))
              {
               Print("Error closing sell order: ",GetLastError());
                 } else if(OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,buyStopLossPips,buyTakeProfitPips,"TRAIDE EA",magicNumber,0,Green)<0) {
               Print("Error opening buy order: ",GetLastError());
              }
              } else if(signal<0) {
            Print("Short Conditions Still Met");
           }
         break;
      default:
         Print("Error, unexpected OrderType: ",OrderType());
         break;
     }
  }
//+------------------------------------------------------------------+
//| Entry process if no position currently open                      |
//+------------------------------------------------------------------+
void processSignalNoPositionOpen(const int signal)
  {
   if(signal>0)
     {
      Print("Long Conditions Met");
      if(OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,buyStopLossPips,buyTakeProfitPips,"TRAIDE EA",magicNumber,0,Green)<0)
        {
         Print("Error opening buy order: ",GetLastError());
        }
        } else if(signal<0) {
      Print("Short Conditions Met");
      if(OrderSend(Symbol(),OP_SELL,lots,Bid,slippage,sellStopLossPips,sellTakeProfitPips,"TRAIDE EA",magicNumber,0,Red)<0)
        {
         Print("Error opening sell order: ",GetLastError());
        }
     }
  }
//+------------------------------------------------------------------+
//| Erros leading to Deinitializtion                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   switch(reason)
     {
      case REASON_PROGRAM:
         Print("Closing EA: Expert Advisor terminated its operation by calling the ExpertRemove() function (REASON_PROGRAM).");
         closeEA();
         break;
      case REASON_REMOVE:
         Print("Closing EA: Program has been deleted from the chart (REASON_REMOVE).");
         closeEA();
         break;
      case REASON_RECOMPILE:
         Print("Reinitializing EA: Program has been recompiled (REASON_RECOMPILE).");
         reinitEA();
         break;
      case REASON_CHARTCHANGE:
         Print("Reinitializing EA: Symbol or chart period has been changed (REASON_CHARTCHANGE).");
         reinitEA();
         break;
      case REASON_CHARTCLOSE:
         Print("Closing: Chart has been closed (REASON_CHARTCLOSE).");
         closeEA();
         break;
      case REASON_PARAMETERS:
         Print("Reinitializing EA: Input parameters have been changed by user (REASON_PARAMETERS).");
         reinitEA();
         break;
      case REASON_ACCOUNT:
         Print("Reinitializing EA: Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings (REASON_ACCOUNT).");
         reinitEA();
         break;
      case REASON_TEMPLATE:
         Print("Reinitializing EA: A new template has been applied (REASON_TEMPLATE).");
         reinitEA();
         break;
      case REASON_INITFAILED:
         Print("Closing EA: OnInit() handler has returned a nonzero value.");
         closeEA();
         break;
      case REASON_CLOSE:
         Print("Closing EA: Terminal has been closed.");
         closeEA();
         break;
      default:
         Print("Error, unexpected OnDeInit reason: ",reason);
         break;
     }
  }
//+------------------------------------------------------------------+
//| Close position process                                           |
//+------------------------------------------------------------------+
void closeEA()
  {
   if(isPositionOpen())
     {
      switch(OrderType())
        {
         case OP_BUY:   // A buy order exists
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Violet))
              {
               Print("Error closing buy order: ",GetLastError());
              }
            break;
         case OP_SELL:  // A sell order exists
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Violet))
              {
               Print("Error closing sell order: ",GetLastError());
              }
            break;
         default:
            Print("Error closeEA, unexpected OrderType: ",OrderType());
            break;
        }
     }
  }
//+------------------------------------------------------------------+
//| Reinitialize in case of error                                    |
//+------------------------------------------------------------------+
void reinitEA()
  {
   reinitGlobals();
   if(OnInit()!=INIT_SUCCEEDED)
     {
      Print("Closing EA: Reinitializing failed.");
      closeEA();
     }
  }
//+------------------------------------------------------------------+
//| Reinitialize global variables                                    |
//+------------------------------------------------------------------+
void reinitGlobals()
  {
   buyStopLossPips=0.0;
   buyTakeProfitPips= 0.0;
   sellStopLossPips = 0.0;
   sellTakeProfitPips=0.0;
   lastBar=D'1970.01.01 00:00';
  }
//+------------------------------------------------------------------+
//| Long and Short Entry Conditions                                  |
//+------------------------------------------------------------------+
int indCCI0period = 14; // Indicator 1 period
int indRSI1period = 14; // Indicator 2 period
//+------------------------------------------------------------------+
//| Long Entry(Return "1" for long entry, "0" for no entry)          |
//+------------------------------------------------------------------+
int LongSignal()
  {
//   double CCI0 = iCCI(NULL,0,indCCI0period,PRICE_CLOSE,1);
//   double RSI1 = iRSI(NULL,0,indRSI1period,PRICE_CLOSE,1);
//   int match=0;
//   if(CCI0>-200 && CCI0<=-150) match++;
//   else if(CCI0>-100 && CCI0<=-50) match++;
//   if(RSI1>0 && RSI1<=25) match++;
   if(match == 2) return 1;
   return 0;
  }
//+------------------------------------------------------------------+
//| Short Entry(Return "-1" for long entry, "0" for no entry)        |
//+------------------------------------------------------------------+
int ShortSignal()
  {
//   double CCI0 = iCCI(NULL,0,indCCI0period,PRICE_CLOSE,1);
//   double RSI1 = iRSI(NULL,0,indRSI1period,PRICE_CLOSE,1);
//   int match=0;
//   if(CCI0 > 50 && CCI0 <= 150) match++;
//   if(RSI1 > 80 && RSI1 <= 100) match++;
//   if(match == 2) return -1;
   return 0;
  }
//+------------------------------------------------------------------+

 

Are there any fine folk out there that could help me out please?  I'm ok with basic programming, but a little out of my depth with MQL!

Thanks
Mark 

Mark Cornbill
38
Mark Cornbill  

Please disregard this post.

 Realised that this is an MQL5 forum, I am using MQL4.

P.S> I have figured out how to get all indicators loaded and values captured - the difficulty now is getting the logic right for reading two inndicator lines crossing each other for entry condition, struggling! 

Stuart Browne
6596
Stuart Browne  
mcornbill:

Please disregard this post.

 Realised that this is an MQL5 forum, I am using MQL4.

P.S> I have figured out how to get all indicators loaded and values captured - the difficulty now is getting the logic right for reading two inndicator lines crossing each other for entry condition, struggling! 

That's fine, we accept both MQL4 and MQL5 discussions here now.

As for the logic, all you need is something like: Was the last bar below and is the new bar above? Vice versa for a cross down
To add comments, please log in or register