How do I refer to int, doubles, indicators in more than one function

 

Hey guys,


Im having an issue which Im sure the answer will be straight forward. Im basically building an EA in MT4 which is built on basic indicators, for my Stop loss i am refering back to these indicator values, position type. However I want to know how I can refer back to an indicators value, candle price or calculated number that was calculated in a previous function. Basically I want to write a function to return a number then be able to refer to that number in many other functions in the EA without having to do the calculation again.

I my example below you can see my Buy and Sell conditions. in my ManageOpen function. How can I check in a seperate function if that condition has been met, like my SLP function in my example without having to get the EA to run through my buy/sell conditions again.


void ManageOpen()
  {
   double Open_1 = iOpen(NULL,0,1);
   double Open_2 = iOpen(NULL,0,2);
   double Open_3 = iOpen(NULL,0,3);
   double Open_4 = iOpen(NULL,0,4);

   double Close_1 = iClose(NULL,0,1);
   double Close_2 = iClose(NULL,0,2);
   double Close_3 = iClose(NULL,0,3);
   double Close_4 = iClose(NULL,0,4);

   double High_1 = iHigh(NULL,0,1);
   double High_2 = iHigh(NULL,0,2);
   double High_3 = iHigh(NULL,0,3);
   double High_4 = iHigh(NULL,0,4);

   double Low_1 = iLow(NULL,0,1);
   double Low_2 = iLow(NULL,0,2);
   double Low_3 = iLow(NULL,0,3);
   double Low_4 = iLow(NULL,0,4);


   double close= iClose(NULL,0,1);
   double open = iOpen(NULL,0,1);
   double bodyrange=MathAbs(iClose(NULL,0,1)-iOpen(NULL,0,1));
   double barrange = (iHigh(NULL,0,1) - iLow(NULL,0,1));
   double low=iLow(NULL,0,1);

       
   double close1= iClose(NULL,0,1);
   double open1 = iOpen(NULL,0,1);
   double bodyrange1=MathAbs(iOpen(NULL,0,1)-iClose(NULL,0,1));
   double barrange1 = (iHigh(NULL,0,1) - iLow(NULL,0,1));
   double low1=iLow(NULL,0,1);
  
   
   double FMA1 = iMA(NULL,0,Fast_MA,0,MA_Type_Fast,PRICE_CLOSE,1);
   double FMA2 = iMA(NULL,0,Fast_MA,0,MA_Type_Fast,PRICE_CLOSE,2);
   double FMA3 = iMA(NULL,0,Fast_MA,0,MA_Type_Fast,PRICE_CLOSE,3);
   double FMA4 = iMA(NULL,0,Fast_MA,0,MA_Type_Fast,PRICE_CLOSE,4);
   
   
   double SMA = iMA(NULL,0,Slow_MA,0,MA_Type_Slow,PRICE_CLOSE,1);

   
   int Lowest = iLowest(NULL,0,MODE_LOW,Room_To_Left_Count,1);
   int Highest = iHighest(NULL,0,MODE_HIGH,Room_To_Left_Count,1);
   
   bool Bullish_Pinbar_Formed  =  bodyrange<0.25*barrange && open>low+0.75*barrange; 
   bool Bearish_Pinbar_Formed =   bodyrange1<0.25*barrange1 && open1<low1+0.25*barrange1;
 
   
   
   const bool canOpenLong  = Bullish_Pinbar_Formed && Lowest==1 && ((High_1 - Low_1) > (High_2 - Low_2)) ;
   const bool canOpenShort = Bearish_Pinbar_Formed && Highest==1 && ((High_1 - Low_1) > (High_2 - Low_2));
 
   if(canOpenLong && canOpenShort)
      return;

   if(canOpenLong)                -------------------------HOW TO REFER TO THIS BOOL IN ANOTHER FUNCTION
      {OpenPosition(OP_BUY);
     
      }
      
   else if(canOpenShort)
      {OpenPosition(OP_SELL);
     
      }
      }
   
   //------------------------------------------ CALCULATE STOP LOSS PIPS-------------------------
   
   double SLP()     
 
   {
   double close= iClose(NULL,0,1);
   double open = iOpen(NULL,0,1);
   double bodyrange=MathAbs(iClose(NULL,0,1)-iOpen(NULL,0,1));
   double barrange = (iHigh(NULL,0,1) - iLow(NULL,0,1));
   double low=iLow(NULL,0,1);

       
   double close1= iClose(NULL,0,1);
   double open1 = iOpen(NULL,0,1);
   double bodyrange1=MathAbs(iOpen(NULL,0,1)-iClose(NULL,0,1));
   double barrange1 = (iHigh(NULL,0,1) - iLow(NULL,0,1));
   double low1=iLow(NULL,0,1);    
  
   bool ind0long  = bodyrange<0.25*barrange && open>low+0.75*barrange; 
   bool ind0short = bodyrange1<0.25*barrange1 && open1<low1+0.25*barrange1;
      
      
      if (ind0long)
         {
         double SL = (iLow(NULL,0,1) - (Stop_Loss_Buffer*pip));
         int StopLossPip = (close1 - SL)/pip;
         
         return (StopLossPip);
         }
         
         else
         {
         double SL =((iHigh(NULL,0,1)+ (Stop_Loss_Buffer*pip)));
         int StopLossPip = (SL - close1)/pip;
         
         return (StopLossPips);    
         }
         
         }   
 
Michael Ronald Beasley: Im having an issue which Im sure the answer will be straight forward. Im basically building an EA in MT4 which is built on basic indicators, for my Stop loss i am refering back to these indicator values, position type. However I want to know how I can refer back to an indicators value, candle price or calculated number that was calculated in a previous function. Basically I want to write a function to return a number then be able to refer to that number in many other functions in the EA without having to do the calculation again. I my example below you can see my Buy and Sell conditions. in my ManageOpen function. How can I check in a seperate function if that condition has been met, like my SLP function in my example without having to get the EA to run through my buy/sell conditions again.

Firstly, in MQL4, you don't have to use iOpen, iClose, etc. for getting OHLC values for the current Sumbol and Time-frame. You can use the built-in time series arrays, like Open[], Close[]. That way, you don't have to save the values in variables as they are already predefined global variables.

For saving indicator values in global variables, here is an example:

input int                FastMAPeriod = 13;
input ENUM_MA_METHOD     FastMAType   = MODE_SMA;
input ENUM_APPLIED_PRICE FastMAPrice  = PRICE_CLOSE;

// Define Global Variables for storing Fast MA Values
#define COUNT_FAST_MA 4
double FastMAValue[ COUNT_FAST_MA ];

void GetFastMAValues()
{
   for( int i = 0; i < COUNT_FAST_MA; i++ )
      FastMAValue[ i ] = iMA( _Symbol, _Period, FastMAPeriod, 0, FastMAType, FastMAPrice, i + 1 );
}

void OnTick()
{
   // Check for New Bar
   static datetime BarTimeCurrent  = WRONG_VALUE;
          datetime BarTimePrevious = BarTimeCurrent;
                   BarTimeCurrent  = Time[0];
          bool     NewBarFlag      = ( BarTimeCurrent != BarTimePrevious );

   // Process MA Indicator Values
   if( NewBarFlag )
   {
      GetFastMAValues(); // Get the Fast MA Values
      
      if( ( FastMAValue[0] > FastMAValue[1] ) && ( Close[1] > Close[2] ) )
      { /* Trend is up! Do something ... */ }
      else
      {
         if( ( FastMAValue[0] < FastMAValue[1] ) && ( Close[1] < Close[2] ) )
         { /* Trend is down! Do something ... */ }
         else
         { /* Lets do something else ...      */ }
      }
   }
}
Open - Predefined Variables - MQL4 Reference
Open - Predefined Variables - MQL4 Reference
  • docs.mql4.com
Open - Predefined Variables - MQL4 Reference