Need help fixing my points/pips/digits problem in my mql4 EA. Any help would be really appreciated.

 

So, I want my EA on my ECN broker for all currency to have S/L at 30 points and T/P at 60 point. Here's my input code:

input double               StopLoss     = 30;          //Fixed Stop Loss (in Points)
input double               TakeProfit   = 60;          //Fixed Take Profit (in Points)
input int                  TrailingStop = 3;           //Trailing Stop (in Points)


I already write this:


//+------------------------------------------------------------------+
//|   expert OnTick function                                         |
//+------------------------------------------------------------------+
void OnTick()
  {
   double TheTakeProfit=0;
   double TheStopLoss=0;
   double MyPoint=Point;
   if(Digits==2 || Digits==3 || Digits ==5) MyPoint=Point*10;
 
   if(TotalOrdersCount()==0 && vSpread < MaxSpread) 
   {
      int result=0;
      
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~BUY~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       {
               result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
               if(result>0)
                 {
                  TheStopLoss=0;
                  TheTakeProfit=0;
                  if(TakeProfit > 0) TheTakeProfit = Ask + TakeProfit * MyPoint;
                  if(StopLoss > 0)   TheStopLoss   = Ask - StopLoss * MyPoint;
                  int MyOrderSelect = OrderSelect(result,SELECT_BY_TICKET);
                  int MyOrderModify = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
                 }
       }     
   }
  }


And this is my Trailing Stop:


   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      int MyOrderSelect=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)
           {
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     int MyOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),
                                                   OrderStopLoss()+TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                    }
                 }
              }
           }
         else 
           {            
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     int MyOrderModify=OrderModify(OrderTicket(),OrderOpenPrice(),
                                                   OrderStopLoss()-MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                    }
                 }
              }
           }
        }
     }


The PROBLEM is when I want something like this:

Currency: EURGBP
Price: 0.84605
S/L: 0.84575 (30 points difference)
T/P: 0.84665 (60 point difference)

This is what really happened:

Currency: EURGBP
Price: 0.84605
S/L: 0.84305 (300 points difference)
T/P: 0.85265 (600 point difference)

This error also happened to USDJPY and XAUUSD.

I am really confused, so I search problem like this in forum, and find some threads:

I already tried everything they said and fixed my code, to no avail.

Can someone in this forum give me the simple fixes in my Code?

Any help would be really appreciated, I would share my EA Code for free.

 
   double MyPoint=Point;
   if(Digits==2 || Digits==3 || Digits ==5) MyPoint=Point*10;
 

What do you think that this does????

Did you write this code ?

This looks like the low quality code produced by an EA builder.

 

Try to exclude this line

// if(Digits==2 || Digits==3 || Digits ==5) MyPoint=Point*10;
 
Keith Watford:

What do you think that this does????

Did you write this code ?

This looks like the low quality code produced by an EA builder.


Yes, sorry if this is low quality code. I tried writing this code myself, and I learned how to write code all by myself.

 
Airat Safin:

Try to exclude this line

Already did that.

After I exclude that line, S/L and T/P wouldn't be set in all currency, except in USDJPY and XAUUSD.

 
  • If your EA work on a single symbol you can do the Digits check in the OnInit function and not for each new tick because it do not change during EA execution.
  • In cycles, always count down from OrdersTotal()-1 to 0 and not the opposite.
  • Using MyPoint your values will be espressed in pips, not in point: you are working on a 5 digits symbol and you have a function to multiply *10 your points. Your TP = 30 will become 300. What's wrong?
 
Daniel Sastraamidjaja:

So, I want my EA on my ECN broker for all currency to have S/L at 30 points and T/P at 60 point. Here's my input code:


I already write this:



And this is my Trailing Stop:



The PROBLEM is when I want something like this:

Currency: EURGBP
Price: 0.84605
S/L: 0.84575 (30 points difference)
T/P: 0.84665 (60 point difference)

This is what really happened:

Currency: EURGBP
Price: 0.84605
S/L: 0.84305 (300 points difference)
T/P: 0.85265 (600 point difference)

This error also happened to USDJPY and XAUUSD.

I am really confused, so I search problem like this in forum, and find some threads:

I already tried everything they said and fixed my code, to no avail.

Can someone in this forum give me the simple fixes in my Code?

Any help would be really appreciated, I would share my EA Code for free.

Try using MarketInfo(sym,...)
To get digits value. 

 
andrew:
Try using MarketInfo(sym,...)
To get digits value. 

Where do I write the code into? Sorry I'm a newbie here... Your help would be really thankful.


Fabio Cavalloni:
  • If your EA work on a single symbol you can do the Digits check in the OnInit function and not for each new tick because it do not change during EA execution.
  • In cycles, always count down from OrdersTotal()-1 to 0 and not the opposite.
  • Using MyPoint your values will be espressed in pips, not in point: you are working on a 5 digits symbol and you have a function to multiply *10 your points. Your TP = 30 will become 300. What's wrong?

 If I'm not using *10, my code wouldn't set S/L and T/P (both values become 0). And How to do Digit check in OnInit function?  Sorry I'm a newbie here... Your help would be really thankful.

 

You need to check distance from current price that need to be grater than STOP_LEVEL allowed by your broker.

I suggest to use bigger values for make a try. Use 100 and 100 for SL and TP. That are 10 pips (if you don't multiply *10)  or 100 pips in the other case.

 
Fabio Cavalloni:

You need to check distance from current price that need to be grater than STOP_LEVEL allowed by your broker.

I suggest to use bigger values for make a try. Use 100 and 100 for SL and TP. That are 10 pips (if you don't multiply *10)  or 100 pips in the other case.

I'm trying it now. Thank you for your suggestion, would update soon.
 
Daniel Sastraamidjaja:

Yes, sorry if this is low quality code. I tried writing this code myself, and I learned how to write code all by myself.

Then it is an amazing coincidence that your code is uncannily similar to that generated by an EA builder.

Reason: