Trailing Stop Problem

 
Hi,
I have the following problem and seems I'm not able to find the solution.

The code for my trailing stop is the following :


for (iCnt = OrdersTotal()-1; iCnt >= 0; iCnt --)
{
if(OrderSelect(iCnt, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY && OrderMagicNumber() == iMagic && OrderSymbol() == Symbol())
{
bDirectSell = 0;
dTs = Bid - (dPoints*iTrailingStop);
if (OrderStopLoss()<dTs && Bid-OrderOpenPrice()>=(dPoints*iTrailingStop))
{
OrderModify(OrderTicket(),OrderOpenPrice(),dTs,OrderTakeProfit(),0,White);
}
}
if(OrderType() == OP_SELL && OrderMagicNumber() == iMagic && OrderSymbol() == Symbol())
{
bDirectBuy = 0;
dTs = Bid+(dPoints*(iTrailingStop+dSpread));
if ( OrderStopLoss() > dTs && OrderOpenPrice() - (Bid+(dPoints*dSpread)) >= dPoints*iTrailingStop )
{
OrderModify(OrderTicket(),OrderOpenPrice(),dTs,OrderTakeProfit(),0,White);
}
}
}
}

dPoints is just Point*10 when the broker use 5 decimals.
dSpread is used to fix the spread of the pair. (that's why i'm using the Bid price in both case)

Now. When I use this code with a 5 decimals broker, the trailing stop works fine but with a 4 decimals broker, the trailing stop is always 1 pip too early (example: instead of starting at 10 pips, the trailing stop is starting at 10).

An idea anyone ?? Thanks in advance.
 
fftremblay:
dPoints is just Point*10 when the broker use 5 decimals.
(...)

But the predefined variable Point is different for a 4/5 digit broker... https://docs.mql4.com/predefined/variables/point

 
fftremblay wrote >>
(example: instead of starting at 10 pips, the trailing stop is starting at 10).

What does it mean?

 
gordon:

But the predefined variable Point is different for a 4/5 digit broker... https://docs.mql4.com/predefined/variables/point

The code works like that :
if the broker use 4 decimals, dPoints = Point - if 5 decimals, dPoints = Point*10

Roger:

What does it mean?

Sorry, the correct sentence is (example: instead of starting at 10 pips, the trailing stop is starting at 9).
Thanks

 
fftremblay:

The code works like that :
if the broker use 4 decimals, dPoints = Point - if 5 decimals, dPoints = Point*10

Myabe I wasn't clear - the predefined variable Point will already be 0.0001 for a 4 digit broker and 0.00001 for a 5 digit broker (for non-JPY pairs...). So why multiply by 10? The variable Point changes by itself.

 
Don't paste code. Use the SRC button.
dTs = Bid+(dPoints*(iTrailingStop+dSpread));
if ( OrderStopLoss() > dTs && OrderOpenPrice() - (Bid+(dPoints*dSpread)) >= dPoints*iTrailingStop )
You want Bid+dPoints*iTrailingStop+dSpread (I assume dSpread is Ask-Bid not (aks-Bid)/dPoints) and OrderOpenPrice()-Bid>= iTrailingStop.
//++++ These are adjusted for 5 digit brokers.
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init() {
    if (Digits == 5 || Digits == 3) {   // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
For the rest of the comments, Either you have to change all your external parameters when you switch from a 4 to 5 digit broker or have the EA adjust.
If your stop/slippage is specified in Pips then you must convert to a double/Points. On a 5 digit broker a pip is NOT a Point.

To simplifiy your loop, try:
    for(int index = OrdersTotal() - 1; index >= 0; index--) if (
        OrderSelect(index, SELECT_BY_POS)                   // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber + Period.index   // my magic number
    &&  OrderSymbol()       == Symbol() ) {             // and period and symbol
        //... if buy/if sell.
Reason: