Problem order modify pending order

To add comments, please log in or register
AbdelEl
11
AbdelEl  

Hello,

I am using MT5 to program my EA. I am trying to trail a pending order however I struggled a lot since I am using MT5 and not MT4.. Many of previous solutions for previous problems didn't work with my query..


My code for trailing a sell position is:

#include <Trade\Trade.mqh>


CTrade TradingCP;
double currentBid, currentAsk;


void OnTick()
  {
//---
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   if(PositionsTotal()==0)
   TradingCP.Sell(0.10,NULL,Bid,(Bid+200*_Point),0,NULL);
   
   CheckTrailingSell(Bid);
  }
  
void CheckTrailingSell(double Bid)
{
 double SL=NormalizeDouble(Bid+200*_Point,_Digits);
 for(int i=PositionsTotal()-1; i>=0; i--)
 {
    string symbol=PositionGetSymbol(i); 
    
    if(_Symbol==symbol) // if currency pair is equal
    if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) 
        {
        // get the ticket number
        ulong PositionTicket=PositionGetInteger(POSITION_TICKET); 
        double CurrentStopLoss=PositionGetDouble(POSITION_SL);
        if(CurrentStopLoss>SL) 
            {
             TradingCP.PositionModify(PositionTicket,SL,0);
            }
    
        }
 }
} 

I am trying to apply the same logic for a sell stop (pending order) but it doesn't function. I tried using Ordermodify();... nothing works.. 


I would really appreciate your help :)

Kind Regards!

Vladimir Karputov
Moderator
182231
Vladimir Karputov  
AbdelEl :

Hello,

I am using MT5 to program my EA. I am trying to trail a pending order however I struggled a lot since I am using MT5 and not MT4.. Many of previous solutions for previous problems didn't work with my query..


My code for trailing a sell position is:

I am trying to apply the same logic for a sell stop (pending order) but it doesn't function . I tried using Ordermodify();... nothing works.. 


I would really appreciate your help :)

Kind Regards!

Please read the help. "Position" is NOT an ORDER! You can't write like that

    if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL) 

right so:

    ***==POSITION_TYPE_SELL)
William Roeder
20790
William Roeder  
   TradingCP.Sell(0.10,NULL,Bid,(Bid+200*_Point),0,NULL);
  1. You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for either direction?
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger 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.)

  2. SL/TP (stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

AbdelEl
11
AbdelEl  
Vladimir Karputov:

Please read the help. "Position" is NOT an ORDER! You can't write like that

right so:

Thank you for your responses. I understood what you explained however, I find myself in a loop.. I still can't code a script that can edit the pending orders.. Could you advice me how can I script it? Because I really spent hours reading documentation/help and no results..
Vladimir Karputov
Moderator
182231
Vladimir Karputov  
AbdelEl :
Thank you for your responses. I understood what you explained however, I find myself in a loop.. I still can't code a script that can edit the pending orders.. Could you advice me how can I script it? Because I really spent hours reading documentation/help and no results..

See an example: how to work with positions and pending orders (the example shows a way to go through the list of positions and orders)

Example: Calculate Positions and Pending Orders

How to start with MQL5
How to start with MQL5
  • 2020.03.12
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
To add comments, please log in or register