error 130 elimination

BillR

Newbie
Jul 9, 2022
3
0
1
44
I get sporadic 130s when using an MA as my stop loss value, so I want to write something that will check for all OrderModify() calls when I have a change in my take profit (or stop loss) values, like:
MQL4:
retval = OrderModify( OrderTicket(), OrderOpenPrice(), Valid_SL(MyCalcSL,OrderType()), Valid_TP(MyCalcTP,OrderType()), 0, CLR_NONE );

So, should the code below work, given that the order is properly selected, etc.? Also, can I NormalizeDouble in the subroutine and the value won't be modified when it's passed to the OrderModify? I'm fairly inexperienced with MT4 internals and don't know whether it passes parameters by reference address or value.

MQL4:
double Valid_SL( double SL, int OT );
{
RefreshRates();
int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
if (SL < StopLevel) SL = StopLevel;
if ( OT == 0  && Bid - SL < StopLevel * _Point) SL = Bid - StopLevel * _Point;
else if ( OT == 1 && SL - Ask < StopLevel * _Point) SL = Ask + StopLevel * _Point;
return (NormalizeDouble(SL,Digits));
}
 
double Valid_TP( double TP, int OT );
{
RefreshRates();
int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
if (TP < StopLevel) TP = StopLevel;
if ( OT == 0  && TakeProfit - Bid < StopLevel * _Point) TakeProfit = Bid + StopLevel * _Point;
else if ( OT == 1 && Ask - TakeProfit < StopLevel * _Point) TakeProfit = Ask - StopLevel * _Point;
return (NormalizeDouble(TP,Digits));
}
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,677
1,374
144
Odesa
www.earnforex.com
What do you actually pass as SL/TP to your validation functions? From the parameter's type, it looks like it's either actual SL/TP price or absolute distance (double), but from the code, it looks like at first it is in points, and then it is treated as an absolute distance.

I'd modify it like this:
MQL4:
double Valid_SL(int SL, int OT);
{
    int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
    if (SL < StopLevel) SL = StopLevel;
    RefreshRates();
    if (OT == OP_BUY) return NormalizeDouble(Bid - SL * _Point, _Digits);
    else if (OT == OP_SELL) return NormalizeDouble(Ask + SL * _Point, _Digits);
    return -1; // Wrong OT.
}

Doing that for TP should be obvious.
 

BillR

Newbie
Jul 9, 2022
3
0
1
44
What do you actually pass as SL/TP to your validation functions? From the parameter's type, it looks like it's either actual SL/TP price or absolute distance (double), but from the code, it looks like at first it is in points, and then it is treated as an absolute distance.

I'd modify it like this:
MQL4:
double Valid_SL(int SL, int OT);
{
    int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
    if (SL < StopLevel) SL = StopLevel;
    RefreshRates();
    if (OT == OP_BUY) return NormalizeDouble(Bid - SL * _Point, _Digits);
    else if (OT == OP_SELL) return NormalizeDouble(Ask + SL * _Point, _Digits);
    return -1; // Wrong OT.
}

Doing that for TP should be obvious.
Your way is more straightforward than mine, so I'll use yours. Thanks!
 

BillR

Newbie
Jul 9, 2022
3
0
1
44
Sorry So Slow - been out of town all week. Yes, I was writing this so the user could specify the StopLoss in pips; then I'd return it as a normalized value so that I could just use the procedure in the call, documenting that in the OrderModify line. Thanks.