string function on 2/3 digits

 

Please help with my EA, I would like to buy on the EURUSD & sell on USDJPY (same time) .The problems I encounter are when I place my EA on EURUSD (buy) & sell on the USDJPY (2 or 3 digits) my stop losses & take profits are TOO close to price. On 5 digit (USDCHF & USDCAD) pairs everything works fine. Please if you know the problem. 

int CountSymbol(string Pair)
 {
  int NoSymbol=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Pair)
   NoSymbol++;
  }
  return(NoSymbol);
 }  

int PenOrders()
 {
  int Pending=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
  {
   if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   Pending++;
  }
   
  return(Pending);
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  else if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 } 

void BuyTrade()
 {
  int Final=0;
  
  const string Base[4]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
  const string Base2[3]={"USDJPY","USDCHF","USDCAD"};
  
  string symbol="";
  symbol=Base[Final];
  
  string symbol2="";
  symbol2=Base2[Final];
  
  double Price=SymbolInfoDouble(symbol,SYMBOL_ASK);
  double Price2=SymbolInfoDouble(symbol2,SYMBOL_BID);
  
  double TakeProfitBuy=Price+TakeProfit*Pips();
  double StopLossBuy=Price-StopLoss*Pips();
  
  double TakeProfitSell=Price2-TakeProfit2*Pips();
  double StopLossSell=Price2+StopLoss2*Pips();
  
  if(CountSymbol(Symbol())<1)
  {
   int BuyTrade=OrderSend(symbol,OP_BUYSTOP,0.01,Price+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy,"Scalper8",MagicNumber,0,clrBlue); 
   
   int SellTrade=OrderSend(symbol2,OP_SELLSTOP,0.01,Price2-(0.5*Pips()),0,StopLossSell,TakeProfitSell,"Scalper8",MagicNumber,0,clrRed);
  }
  
 } 
 
Scalper8:

Please help with my EA, I would like to buy on the EURUSD & sell on USDJPY (same time) .The problems I encounter are when I place my EA on EURUSD (buy) & sell on the USDJPY (2 or 3 digits) my stop losses & take profits are TOO close to price. On 5 digit (USDCHF & USDCAD) pairs everything works fine. Please if you know the problem. 

I just glanced at this and as I don't use MQL4, may not be correct, but I see you are storing Digit as a double prior to comparing.

double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3)

Doubles can often be subtly different so these comparisons can fail unexpectedly, unless you are precise about the same number of decimal places on both sides - I suggest casting Digit to int to make this comparison more reliable.

Also do you need this if/else if logic? It looks like you do the same calculation in both cases
if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  else if(Digit==4||Digit==5){PipPoint=Point*10;}
 
R4tna C #:

I just glanced at this and as I don't use MQL4, may not be correct, but I see you are storing Digit as a double prior to comparing.

Doubles can often be subtly different so these comparisons can fail unexpectedly, unless you are precise about the same number of decimal places on both sides - I suggest casting Digit to int to make this comparison more reliable.

Also do you need this if/else if logic? It looks like you do the same calculation in both cases

I made the changes but still encounter the same problem with 2 and 3 digits pairs.

double Pips()
 {
  double PipPoint=0;
  int Digit=(int)MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 } 
 
Scalper8 #:

I made the changes but still encounter the same problem with 2 and 3 digits pairs.

I tried simulating but as I use MQL5 can't debug this - can you print out the return values of 

MarketInfo(Symbol(),MODE_DIGITS);

If we see what they are, in both cases of when the logic succeeds and fails, if could help me understand the issue better

 
Scalper8 #:

I made the changes but still encounter the same problem with 2 and 3 digits pairs.

   double Pips(string symbol)
     {
      double PipPoint=MarketInfo(symbol,MODE_POINT);
      double Digit=MarketInfo(symbol,MODE_DIGITS);
      if(Digit=5 || Digit==3)
        {
         PipPoint*=10;
        }
      return(PipPoint);
     }
 
Keith Watford #:

Thank you Keith problem solved!

Reason: