Error 130 & 136 while setting new SL using OrderModify

 

Hello,


I am getting Error 130 and Error 136 when i am trying to add new SL using order Modify.


Code :

void StopLoss_NewsTrading(int StopLoss_NewsTrading_magic)
  {
int gNewsTrading_SL = MarketInfo(Symbol(), MODE_STOPLEVEL) + 10;
// Loop through all orders
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // Check if the order is open
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == StopLoss_NewsTrading_magic)
           {
            if(OrderType() == OP_BUY)
               ResetLastError();
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - gNewsTrading_SL * Point, OrderTakeProfit(), 0, clrNONE);
            if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
               Print(__FUNCTION__ + " => Buy SL Error Code : " + GetLastError());
            if(OrderType() == OP_SELL)
               ResetLastError();
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + gNewsTrading_SL * Point, OrderTakeProfit(), 0, clrNONE);
            if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
               Print(__FUNCTION__ + " => Sell SL Error Code : " + GetLastError());
           }
        }
     }
  }



Additionally,


For Buy order (Which should i use) :

Bid - gNewsTrading_SL * Point

(OR)

Ask - gNewsTrading_SL * Point


For Sell Order (Which should i use) :

Bid + gNewsTrading_SL * Point

(OR)

Ask + gNewsTrading_SL * Point

 
anuj71:

Hello,

I am getting Error 130 and Error 136 when i am trying to add new SL using order Modify.


you really need to start by following some articles or tutorials, at the moment you are posting snippets of code every day that do not work and expecting others to fix them, I don't know where you get them but clearly they are very poor quality and some of them include decompiled parts..

in this case start by sorting the logic out and separate it correctly with { }, at present all your nested IF statements are doing is controlling the ResetLastError function.

 
Paul Anscombe #:
IF statements are doing is controlling the ResetLastError function.

Yes, small typo error.


Paul Anscombe #:
start by following some articles or tutorials,

Already reading. if i do not find, i post here.


Paul Anscombe #:
you are posting snippets of code every day

Because i am new MQL4 Coder with just 2 month of experience.


I fixed my code, here it is :


void StopLoss_NewsTrading(int StopLoss_NewsTrading_magic)
  {

   double buyEntryPriceDifference = (Bid - LastOrderPrice(OP_BUY, BuyStopMagic)) / Point;
   int buyEntryPriceDifference_int = int (buyEntryPriceDifference) + 1;
   int buyEntryPriceDifference_if = buyEntryPriceDifference_int > 0 ? buyEntryPriceDifference_int : 0;
   int BuyStopLoss_NewsHedge = buyEntryPriceDifference_if + gStopLevel + gNewsTrading_SL;

   double SellEntryPriceDifference = (LastOrderPrice(OP_SELL, SellStopMagic) - Ask) / Point;
   int SellEntryPriceDifference_int = int (SellEntryPriceDifference) + 1;
   int SellEntryPriceDifference_if = SellEntryPriceDifference_int > 0 ? SellEntryPriceDifference_int : 0;
   int SellStopLoss_NewsHedge = SellEntryPriceDifference_if + gStopLevel + gNewsTrading_SL;

   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // Check if the order is open
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == StopLoss_NewsTrading_magic)
           {
            if(OrderType() == OP_BUY)
              {
               ResetLastError();
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid - BuyStopLoss_NewsHedge * Pips, OrderTakeProfit(), 0, clrNONE);
               if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
                  Print(__FUNCTION__ + " => Buy SL Error Code : " + GetLastError());
              }
            if(OrderType() == OP_SELL)
              {
               ResetLastError();
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask + SellStopLoss_NewsHedge * Pips, OrderTakeProfit(), 0, clrNONE);
               if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
                  Print(__FUNCTION__ + " => Sell SL Error Code : " + GetLastError());
              }
           }
        }
     }
  }
 
anuj71: For Buy order (Which should i use) :

Bid - gNewsTrading_SL * Point

(OR)

Ask - gNewsTrading_SL * Point

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
William Roeder #:

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Is this correct ?


      double TakeProfit_BuyEntry = (TP != 0) ? (Ask + TP * Pips) : 0;
      double TakeProfit_SellEntry = (TP != 0) ? (Bid - TP * Pips) : 0;
      double StopLoss_BuyEntry = (SL != 0) ? (Ask - SL * Pips) : 0;
      double StopLoss_SellEntry = (SL != 0) ? (Bid + SL * Pips) : 0;
      if((!Close_Buy_Limit) && Buy_Limit && trades_count(BuyLimitMagic) == 0)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_BUY, Lot_Size, Ask, Slippage, StopLoss_BuyEntry, TakeProfit_BuyEntry, (BuyLimitTradeComment + trades_count(BuyLimitMagic)), BuyLimitMagic, 0, White);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Buy Limit Order Error Code : " + GetLastError());
        }
      if((!Close_Sell_Limit) && Sell_Limit && trades_count(SellLimitMagic) == 0)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, Slippage, StopLoss_SellEntry, TakeProfit_SellEntry, (SellLimitTradeComment + trades_count(SellLimitMagic)), SellLimitMagic, 0, Aqua);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Sell Limit Order Error Code : " + GetLastError());
        }


 
anuj71 #: Is this correct ?
      double TakeProfit_BuyEntry = (TP != 0) ? (Ask + TP * Pips) : 0;
      double TakeProfit_SellEntry = (TP != 0) ? (Bid - TP * Pips) : 0;

What part of “using Ask±n, makes your SL shorter and your TP longer” was unclear to you?

 
William Roeder #:

What part of “using Ask±n, makes your SL shorter and your TP longer” was unclear to you?

Yes, still i am confused.


For Buy orders.

Take Profit : Ask + TP

Stop Loss : Ask - SL

(Or)


Take Profit : Ask + TP

Stop Loss : Bid - SL



For Sell Orders :


Take Profit : Bid - TP

Stop Loss : Bid + SL