failed modify [Invalid request] error

 
My goal is place an automatic stop loss when I click either buy or sell, and below is the code I tried. It results two errors.

1st one "failed modify buy 0.00 sl: 0.0000, tp: 0.0000 -> sl: -100.000, tp:0.0000 [Invalid request] error"

2nd one "failed modify buy 0.00 sl: 0.0000, tp: 0.0000 -> sl: 8692.45, tp:0.0000 [Invalid request] error"

A successful modification should be something like "modify #1258801 buy 0.01 BTCUSD sl: 0.0000, tp: 0.0000 -> sl: 8692.45, tp:0.0000"



The code I tried fails this way

1. order number and lot that I trade

2. wrong stop loss at -100 rather than enter_price-100



Any help or reference will be very appreciated. By the way, this is my first mql5 code I ever work. So, I am clearly novice. Thank you.


input int SL = 100;

void OnTradeTransaction(const MqlTradeTransaction &txs, const MqlTradeRequest &req, const MqlTradeResult &res)
{
    MqlTradeRequest rq = {0};
    MqlTradeResult tr = {0};
    double sl = 0;
    if (HistoryDealGetInteger(txs.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)
        if (txs.volume != 0 && txs.type != TRADE_TRANSACTION_HISTORY_UPDATE)
        {
            PositionSelect(txs.symbol);
            if (PositionGetDouble(POSITION_SL) == 0)
            {
                rq.action = TRADE_ACTION_SLTP;
                rq.position = result.order;
                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    sl = PositionGetDouble(POSITION_PRICE_OPEN) - SL;
                    rq.sl = NormalizeDouble(sl, SymbolInfoInteger(txs.symbol, SYMBOL_DIGITS));
                    rq.tp = 0;
                    OrderSend(rq, tr);
                }    
                else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
                {
                    sl = PositionGetDouble(POSITION_PRICE_OPEN) + SL;
                    rq.sl = NormalizeDouble(sl, SymbolInfoInteger(txs.symbol, SYMBOL_DIGITS));
                    rq.tp = 0;
                    OrderSend(rq, tr);
                }    
            }
        }
}
Files:
journal.JPG  117 kb
 
mocs520: . wrong stop loss at -100 rather than enter_price-100
input int SL = 100;
⋮
rq.sl = NormalizeDouble(sl, SymbolInfoInteger(txs.symbol, SYMBOL_DIGITS));
  1. A Stop Loss is a price. There is a difference between setting the SL to 100 vs. setting it the open price - 100, vs. setting it the open price - 100 points.

  2. Using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forum

 
William Roeder:
  1. A Stop Loss is a price. There is a difference between setting the SL to 100 vs. setting it the open price - 100, vs. setting it the open price - 100 points.

  2. Using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forum

@ William, Thank you for your reply. I tried to fix it, but no luck yet. Thank you again.
 
mocs520:

You forgot to use the HistoryDealSelect.

 
fxsaber:

You forgot to use the HistoryDealSelect.

@fxsaber, thank you for your reply. Can you provide a sample code line with HistoryDealSelect? It would be very appreciated.
 
mocs520:
@fxsaber, thank you for your reply. Can you provide a sample code line with HistoryDealSelect? It would be very appreciated.
if (HistoryDealSelect(txs.deal) && (HistoryDealGetInteger(txs.deal, DEAL_ENTRY) == DEAL_ENTRY_IN))
Reason: