How to close multiple orders with trailing stop respectively?

To add comments, please log in or register
JIHUN NAM
128
JIHUN NAM  

Hello, m8


I created my own EA.


My EA will open orders twice per signal.

And they should close with trailing stop....


When I did backtesting, they opened at the same time.. However, when they closed orders.. they did not close at the same time.. only first-order closed properly.. but the second one did not close together.. How can I fix it ??


What I would like to fix is that


ex)  01:23     1.1000   Buy    Open   (order number 1)

       01:23     1.1000   Buy    Open   (order number 2)


        01:24     1.1100 modify  (order number 1)

        01:24      1.1100 modify (order number 2)

         

          01:26    1.1200   s/l   (order number 1)

           01:26   1.1200    s/l  (order number 2)



However, currently, my EA showing that


ex)  01:23     1.1000   Buy    Open   (order number 1)

       01:23     1.1000   Buy    Open   (order number 2)


        01:24     1.1100 modify  (order number 1)

        01:24      1.1100 modify (order number 2)

         

          01:26    1.1200   s/l   (order number 1)

           01:26   1.1200    modify  (order number 2)


              01:27  1.1300   modify (order number 2)

              01:29  1.1400    s/l  (order number 2)



Something like this.....



As you can see,  open orders at the same time(23:40)... but  different close and time(#1 : 1:51 ~ 1:58, then close at 2:18 | #2 : 2:26 ~ 2:39 then close at 2:47)...   I want them  open & close at the same time !!


These are my parts of code below


// Order part

if(totalpairs()==0 &&TradeBan == false && Ask <= PriceBuy && Ask > PriceBuy - 100 * Point && AccountFreeMarginCheck(Symbol(), OP_BUY, Lots) > 0 && Q ==0 && PriceBuy  != 0 && MarketInfo(Symbol(),MODE_SPREAD) <= MaxSpread)

     {


      if(TakeProfit == 0)
         TP = StartPrice;

      if(TakeProfit != 0)
         TP = Bid + TakeProfit * Point;


      int ret = OrderSend(NULL, OP_BUY, Lots, Ask, 10, Bid - StopLoss * Point, TP, "12point", Magic, 0, clrNONE);
   

      int ret2 = OrderSend(NULL, OP_BUY, Lots, Ask, 10, Bid - StopLoss * Point, TP, "12point", Magic, 0, clrNONE);


      TrallPrice = Ask;

      TrailingSwitch = 1;


     }


// Trailing stop part

void Trall()

  {

   bool res = false;


   if(OrdersTotal() > 0)

     {


      for(int i = 0; i < OrdersTotal(); i++)

        {

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))



            if(OrderType() == OP_BUY  && TrallPrice + TrallingStart  * Point <= Bid && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 1)
              {
               res = false;
               res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrallingStop * Point, OrderTakeProfit(), Green);
               if(res == true)
                 {
                  TrallPrice = Bid ;
                  TrailingSwitch = 2;
                 }
              }

         if(OrderType() == OP_SELL && TrallPrice - TrallingStart  * Point >= Ask && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 1)
           {
            res = false;
            res = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrallingStop * Point, OrderTakeProfit(), Green);
            if(res == true)
              {
               TrallPrice = Ask ;
               TrailingSwitch = 2;
              }
           }


         if(OrderType() == OP_BUY  && TrallPrice + TrallingStep * Point <= Bid && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 2)
           {
            res = false;
            res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrallingStop * Point, OrderTakeProfit(), Green);
            if(res == true)
              {
               TrallPrice = Bid;
              }
           }

         if(OrderType() == OP_SELL && TrallPrice - TrallingStep * Point >= Ask && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 2)
           {
            res = false;
            res = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrallingStop * Point, OrderTakeProfit(), Green);
            if(res == true)
              {
               TrallPrice = Ask;
              }
           }

        }


     }


  }


Any solutions?? 


Cheers,


좆같네 ㅅㅂ..


      

William Roeder
22449
William Roeder  
      for(int i = 0; i < OrdersTotal(); i++)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

            if(OrderType() == OP_BUY  && TrallPrice + TrallingStart  * Point <= Bid && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() && TrailingSwitch == 1)
              {
               res = false;
               res = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrallingStop * Point, OrderTakeProfit(), Green);
               if(res == true)
                 {
                  TrallPrice = Bid ;
                  TrailingSwitch = 2;
                 }
              }
  1. Drop your useless variables (TrallPrice, TralingSwitch, TrallingStart)
  2. The word is trail or trailing; not Trall. Be consistant at least.
  3. Don't copy and paste code, put it one place or make a variable.
  4.       for(int i = OrdersTotal()-1; i >= 0; --i) if(
             OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
          && && OrderMagicNumber() == Magic 
          && OrderSymbol() == Symbol()
          ){
            if(OrderType() == OP_BUY){
               double SL = MathMax(OrderOpenPrice(), OrderClosePrice() ) - TrallingStop * Point;
               if(SL > OrderStopLoss() + _Point)
                   res = OrderModify(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit(), Green);
            }
    The MathMax means don't trail until BE or better.

  5. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
    2. For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

To add comments, please log in or register