Partial Close EA with in custom RR based on sl

 

Hello 

The general idea is partial at 10RR 50% and the rest run.

The partial price calculated is correct, but the partial did not happend, and i don't know why ?

Thanks for your help




extern int RR = 10;

extern int sl = 25;

void
OnTick()   {    if(OrdersTotal() == 0)    {         int buyticket = OrderSend(_Symbol,OP_BUY,1,Ask,3,Ask - (25*_Point),0,"Manager",3,0,clrGreen); //test position              double buy_partial_level = OrderOpenPrice()+(sl*RR*_Point);       double sell_partial_level = OrderOpenPrice()+(sl*RR*_Point);              Partial(buy_partial_level);       Print("",buy_partial_level); //test the partial level is correct    }   } void Partial(double qpartial_level) {    if(OrdersTotal())    {       if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))       {Print("unable to select an order");}       if(OrderType() == OP_BUY && Bid > qpartial_level)       {          if(!OrderClose(OrderTicket(),OrderLots()/2,Bid,3,clrRed))          {Print("failure to close the order");}       }       else if(OrderType() == OP_SELL && Ask < qpartial_level)       {          if(!OrderClose(OrderTicket(),OrderLots()/2,Ask,3,clrRed))          {Print("failure to close the order");}       }    }        }
Files:
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. int buyticket = OrderSend(_Symbol,OP_BUY,1,Ask,3,Ask - (25*_Point),0,"Manager",3,0,clrGreen); //test position

    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 to 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)

  3. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012)

  4.       int buyticket = OrderSend(_Symbol,OP_BUY,1,Ask,3,Ask - (25*_Point),0,"Manager",3,0,clrGreen); //test position
          double buy_partial_level = OrderOpenPrice()+(sl*RR*_Point);
    

    You can not use any Trade Functions until you first select an order.

  5.       if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  6.          if(!OrderClose(OrderTicket(),OrderLots()/2,Bid,3,clrRed))
    1. You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

    2. You also must check if you have already done it, to avoid repeated closing. Alternatives:

      • Move SL to Break Even+1 before the partial close. That way, you know that you already did it.

      • Set a flag in persistent storage (files, global variables w/flush)

      • Open two orders initially, and close one (manually or by TP.)

Reason: