Search code examples
mql4metatrader4mt4

MQL4: OrderModify() always gets 130 response


I want my EA to modify an order automatically, to put a StopLoss once in a profit state, but why it always error?

I already get the minimum point level as well, but it did not give me any luck.

Here is the vars

        double _stopLevelTimes = 1.5;
        int     StopLevel      = (int) ( miStopLevel + miSpread );

What I want is to make the stop level 1.5 times higher then the minimum stop loss points level and I already add the spread too.

Those vars are filled automatically from brokers.

        if (  OrderType()      == 0
           && OrderOpenPrice() >  OrderStopLoss()
              ){
              _stopLoss   = NormalizeDouble( OrderOpenPrice()
                                           + ( Point() * (  StopLevel
                                                         * _stopLevelTimes
                                                           )
                                               ),
                                             miDigits
                                             );
              _clr        = clrBlue;
              RefreshRates();
              modified    = OrderModify( OrderTicket(),
                                         OrderOpenPrice(),
                                         _stopLoss,
                                         OrderTakeProfit(), 0, _clr );
        }
        else
        if (  OrderType()      == 1
           && OrderOpenPrice() <  OrderStopLoss()
              ){  
              _stopLoss   = NormalizeDouble( OrderOpenPrice()
                                           - ( Point() * (  StopLevel
                                                         * _stopLevelTimes
                                                           )
                                               ),
                                             miDigits
                                             );
              _clr        = clrRed;
              RefreshRates();
              modified    = OrderModify( OrderTicket(),
                                         OrderOpenPrice(),
                                         _stopLoss,
                                         OrderTakeProfit(), 0, _clr );
        }

Solution

  • Even ( IT Manager at Forex Falcon ) ought know a few hard facts:

    • Every MQL4 code has transferred several responsibilities to code-designers, submitting a mandatory OrderSelect() before any db.Pool-operations. Not because of the db.Pool-SELECT-free mode of operations in the OrderModify(), but because your code compares db.Pool-record-values ( and such record must be first explicitly SELECT-ed )

    • There are more values to monitor, than just a Spread and a StopLevel. There could be a third reason for Error_130, once your code requests MT4-Server side to modify an Order, which fortunately remains inside a FreezeLevel zones, setup by your Broker's [ Terms and Conditions ]. Be sure to re-read them, as they rule all your XTO/non-XTO.

    • Last but not least, your belief will get shattered, as both the StopLevel, FreezeLevel and Spread need not be constant and both Brokers and MetaQuotes MQL4 Documentation warns about situations, when returned values ought be understood not as a value, but as an indication about actually not having a value to work with, but to flag floating ( unknown ) values.

    Best next step?

    In all cases, log both actual and requested new values, into a file and check, if these meet the Broker published rules in [Terms and Condition ].

    If not, reconcile any such documented non-conformities with Broker representative and your local FSA-authority.