Understanding Input Doubles Relative to Price

 

I am trying to understand regarding input price as a double values against the actual market price in pips.

For example I am working on a simple indicators where a user specify how close the price is relative to the 8 day EMA and then it will alert to buy or sell as an example.

Per the scenario below.

if the current BID price - the current 8EMA is less than equal to "price close to ema" then alert buy or sell

double priceToEMA = 30.0;

double smallEMA = iMA(NULL,0,ma_period_small,0,ma_method_small,applied_price_small,2) ;

currentBid = MarketInfo(NULL,MODE_BID);

If ( (currentBid - smallEMA) <= priceToEMA) then

{ Alert("Buy as price is close to EMA"); }

=============================================

Assuming I trade NAS100 with the current price of 12395.50, smallEMA of 12392.10 and priceToEma = 30.0

So the NAS100 price difference is 12395.50 - 12392.10 = 3.40 difference

given that the priceToEMA is 30.0 compares to the price difference of 3.40, will the priceToEMA be converted as 3.00 given NAS100 has 2 decimal pip in the IF statement

   if (3.40 <= 3.00) then

or will the price comparison on the above IF statement be 30.0

   if (3.40 <= 30.0) then

Can someone help me understand when comparing a regular double vs a security priced in pips in the above example...how will MQL4 execute the above code

Which one of the above will be the correct way MQL4 will read it and ALERT the user that a BUY/SELL can be executed or no alerts at all.

Thanks in advance.

 
double smallEMA = iMA(NULL,0,ma_period_small,0,ma_method_small,applied_price_small,2) ;

currentBid = MarketInfo(NULL,MODE_BID);
  1. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  2. Why the variable currentBid? Why not just use the predefined variable Bid?

 
William Roeder #:
  1. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  2. Why the variable currentBid? Why not just use the predefined variable Bid?

This is like my current code right now as per the above post scenario, I want the EA to open orders only when the current price is within the range pre-set as it nears the small EMA (8 day EMA) but I as run it against the test account many times it execute the orders where the price is current price is very far away from the 8 day ema for example.  Thus I was wondering if the comparison is against the 30.0 double or 3.4 as explained.  I am new to MQL4 and just trying to learn and use the function as I search them up, I was not aware of other functions and mechanics.

Here's the case scenario


double actual_diff;
int mySignal = myTrend();

If (mySignal=1 && actual_diff <= priceToEMA)then
  { openOrder(Buy); //open order to buy }

If (mySignal=-1 && actual_diff <= priceToEMA) then
  { openOrder(sell); //sell order }


int myTrend()//find ema cross over or under
  { 
      
      double MA_1_big = iMA(NULL,0,ma_period_big,0,ma_method_big,applied_price_big,1);
      double MA_2_big = iMA(NULL,0,ma_period_big,0,ma_method_big,applied_price_big,2);
     
      double  MA_1_small = iMA(NULL,0,ma_period_small,0,ma_method_small,applied_price_small,1);
      double  MA_2_small = iMA(NULL,0,ma_period_small,0,ma_method_small,applied_price_small,2);
      if(MA_1_small > MarketInfo(NULL,MODE_ASK))
        {
         actual_diff = MA_2_small - MarketInfo(NULL,MODE_ASK); //downtrend thus ema - current price is price difference
        }
     if(MA_1_small < MarketInfo(NULL,MODE_BID))
        {
         actual_diff = MarketInfo(NULL,MODE_BID) - MA_2_small; //up trend so current price - small ema price difference
        }
     
      if(MA_2_big <= MA_2_small && MA_1_big >= MA_1_small)//ema crossed down sell signal
         {
            sigType=-1;
         }
       if(MA_2_big >= MA_2_small && MA_1_big <= MA_1_small)//ema cross up buy signal
         {
            // if actual_diff <= priceDifference then signal is sell
          sigType=1; //buy
         }    
//--- 
    return(sigType);
  }
Reason: