How do i compare the current tick price with my calculated entry price?

 

Hi guys,


I am writing an EA that automatically enters a market execution when the entry price that i have calculated is equal to the current price. thereby negating the use of a pending order.

but using the strategy tester, i am unable to get an accurate output. it seems like there is an issue with how the current price and the entry price is being compared.

The market price has clearly ran past the entry price but the order was not executed.

Any ideas on why that would be the case?

Much appreciated

            //Calculation for entry,takeprofit,stoploss,pos_size

            RefreshRates();
            
            tickprice = NormalizeDouble(MarketInfo(Symbol(), MODE_TICKVALUE),Digits);
            if(signal == 1 && compare_doubles(tickprice,entry,5) == 0)
            {
               Print("Entering buy order");
               OrderSend(Symbol(),OP_BUY,pos_size,Ask,0,stoploss,takeprofit);
               Print("Buy order has been placed");
            }
            else
               if(signal == -1 &&  compare_doubles(tickprice,entry,5) == 0)
               {
                  Print("Entering sell order");
                  OrderSend(Symbol(),OP_SELL,pos_size,Bid,0,stoploss,takeprofit);
                  Print("Sell order has been placed");
               }
 
tickprice = NormalizeDouble(MarketInfo(Symbol(), MODE_TICKVALUE),Digits);

You shold be comparing Bid or Ask, not tick value

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 

Hi Keith,


Sorry for the mistake in raising it under the MQL5 section

As per what was being discussed

i initially started with comparing Bid and Ask using multiple methods alas to no avail.

i have tried normalizing both variables, i also have also tried using the function compare_double as shown below

but it does not seem to be able to accurately execute my order when the entry price matches the current price despite them being equal.

  	    RefreshRates();
            
            if(signal == 1 && compare_doubles(ent,Ask,5) == 0)
            {
               Print("Entering buy order");
               OrderSend(Symbol(),OP_BUY,pos_size,Ask,0,sl,tp);
               Print("Buy order has been placed");
            }
            else
               if(signal == -1 &&  compare_doubles(ent,Bid,5) == 0)
               {
                  Print("Entering sell order");
                  OrderSend(Symbol(),OP_SELL,pos_size,Bid,0,sl,tp);
                  Print("Sell order has been placed");
               }

int compare_doubles(double var1,double var2,int precision)
  {
   double point = MathPow(10,-precision);
   int var1_int = var1/point;
   int var2_int = var2/point;
   if(var1_int>var2_int)
      return 1;
   else if(var1_int<var2_int)
      return -1;
   return 0;
  }


Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
  1. Doubles are rarely equal. There is no need for such functions if you understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

  2. What happens if the market moves more than one point at your entry level?
    Your code
    if(signal == 1 && compare_doubles(ent,Ask,5) == 0)
    Simplified
    if(signal == 1 && Ask >= ent)

 

Thanks William,


I used 

if(signal == 1 && Ask >= ent)

and it worked fine. Turns out i declared the entry variable in my ontick() function which caused it to be stored as 0 the entire time.

After using a global variable for the entry variable instead, everything seems to be in order.

Massive rookie mistake haha....