Add on winning trade

 
Hello! So I have made this EA which is very simple, once a condition is met it will open a trade.
And it will keep adding another 4 trades if conditions are met.

All ticket numbers are stored in global area

int Ticket1=0,Tikcet2=0,Ticket3=0 ...Ticketx=0;
if(condition1)
{ Ticket1 = OrderOpen();}
if(condition2 && Ticket1>0)
{ Ticket2 = OrderOpen();}
if(condition3 && Ticket2>0)
{ Ticket3 = OrderOpen():}
//
//
//
if(conditionx && Ticketx-1>0)
{Ticketx = OrderOpen();}

All orders have StopLoss, and Last order have a closing on TakeProfit also, once TakeProfit is triggered, it will close all orders.

Take profit is not included in the OrderSend() function, is a filter to close the trades.

Once this Take profit level is reached, orders are closed and all Ticket variables are reset to 0;


if(closingcondition)
{ CloseAllTrades();
   ResetTicket();
}

Questions:

1. Is any other more elegant way to add on winning trades ?


2. All orders have a trailing stop which in normal conditions it should not be triggered before closing conditions are true, but sometimes it is happening.

The problem is that when an order is closed on SL, it will not reset ticket number to 0, therefore the sequence will not stop, and will continue to add trades.

How can I solve that? When SL is triggered for any of the orders, to close all orders.


What I did, but not yet tested, i made a check on first order from the sequence ( Ticket1), if this order has a OrderCloseTime()>0 && Ticket2 still open it means that Ticket1 order was closed on SL, so I Close all trades.

Is any better way to check this?

Because it may happen that Ticket3 got closed by SL, and Ticket2 still open. 


Thank you!

 
Nobody?
 
Daniel Cioca #: Nobody?

You are on my do not help list for your two rude replies. https://www.mql5.com/en/forum/387647#comment_27410230 https://www.mql5.com/en/forum/378755#comment_24938730

 
William Roeder #:

You are on my do not help list for your two rude replies. https://www.mql5.com/en/forum/387647#comment_27410230 https://www.mql5.com/en/forum/378755#comment_24938730

Ohh William, but that was long time ago when I did not know you.

Now I know that you are grumpy and harsh sometimes, but you are an experienced and offering a lot of help, even if you are doing it in a hard way

You did answer to me several times after that.

So apologies for that, but sometimes you deserve it. Some of us are well intended it, but being in the beginning, we might ask questions wrong way. 

 

extern  int  MaxOrders = 4;


if (buys < MaxOrders /* add other buy conditions here */ )   { /* add buy function here */ } 
if (sells < MaxOrders /* add other sell conditions here */ )   { /* add sell function here */ }


 
Daniel Cioca #: Ohh William, but that was long time ago when I did not know you.

How is that an excuse for being rude?

Daniel Cioca #: So apologies for that, but sometimes you deserve it.

I may have been short with you because you failed to read the documentation, failed to read the answer and learn, etc. None of that, excuses you for being rude.

On my Do Not Help List, live in ignorance.

 

Done something on issue no 2 ( detect orders closed by StopLoss and if detected trigger all orders close)


I have observed that for orders closed by SL, at the OrderComment() it is added  "[sl]". Like if in my comment I put "BuyOrder", if this order is closed by SL, the order comment will be "BuyOrder[sl]". Is this from my broker? Or platform is doing that?!


And observing that, I made a function to search for this "[sl]" under all orders comments. 

void FindSomething()
  {

   ArrayResize(array,6);
   ArrayFill(array,0,1,ticket1);
   ArrayFill(array,1,1,ticket2);
   ArrayFill(array,2,1,ticket3);
   ArrayFill(array,3,1,ticket4);
   ArrayFill(array,4,1,ticket5);
   ArrayFill(array,5,1,ticket6);

   for(int i=5; i>=0; i--)
     {
      if(array[i] > 0)
        {
         if(OrderSelect(array[i],SELECT_BY_TICKET))
           {
            string sample="[sl]";
            string comment=OrderComment();
            int tickett=OrderTicket();
            datetime closetime=OrderCloseTime();

            int len = StringLen(comment);
            string split = StringSubstr(comment,len-4,len);
            if(split == sample)
              {
               Print("split=",split," ticket=",tickett," closetime=",opentime);
               CloseAllOrders();
              }
              
           }
        }
     }
  }

What do you think William?

 
Daniel Cioca #: I have observed that for orders closed by SL, at the OrderComment() it is added  "[sl]".
Broker dependent.

Not a good idea to use comments, brokers can change comments, including complete replacement.

 
William Roeder #:
Broker dependent.

Thanks! I'll think on something else

 

What about if I compare OrderStopLoss() with OrdersClosePrice() ?

int orderstoploss    = (int)OrderStopLoss()    / SymbolInfoDouble(_Symbol,SYMBOL_TRADE__TICKSIZE;
int ordercloseprice  = (int)OrderClosePrice()  / SymbolInfoDouble(_Symbol,SYMBOL_TRADE__TICKSIZE;


if( ordersstoploss == ordercloseprice )
    CloseAllOrders ; 
 
Daniel Cioca #:What about if I compare OrderStopLoss() with OrdersClosePrice() ?

Due to slippage, the close price is not always the same as the stop-loss (except in the Strategy Tester where it is always the same irrespective of slippage).

I'm not too certain of what you are trying to achieve, but I would simply look for when a position is closed and consider that as the trigger to close the rest.

A closed position is when OrderCloseTime() is no longer zero.

Reason: