Add on winning trade - page 2

 
Fernando Carreiro #:


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

Thank you for that, but this will tell me that the order is closed. I need to know if the order is closed by stop loss.

So to explain a litlle,  on proper signal it will open first trade, and then if price going my way after 20 pips it open another trade, and so on until max of 5 trades. All ticket numbers are saved under variables ticket1 ticket2  ... ticket5.

Ticket5 has a TakeProfit , and if that level is hit, all trades will close, and all Ticket1...Ticket5 variables are set to 0.

Every trade opening( except first trade), has a condition for opening 20 pips away from previous, and previous Ticket variable to be > "0". Like second trade will open if price traveled 20 pips more, and Ticket1>0.

When a trade  is closed by other means then StopLoss, will trigger also closing all trades, and reset all variables to "0". ( I have a condition for closing )

But if , for example, trade 3 is closed by StopLoss, Ticket3 variable will not reset to "0", therefore it will be opened again.

And this is what I want to achieve: to detect when order is closed by StopLoss, so I can close all trades and reset all Ticket variables to 0.

Thank you for your inputs!

 
Daniel Cioca #:Thank you for that, but this will tell me that the order is closed. I need to know if the order is closed by stop loss.

So to explain a litlle,  on proper signal it will open first trade, and then if price going my way after 20 pips it open another trade, and so on until max of 5 trades. All ticket numbers are saved under variables ticket1 ticket2  ... ticket5.

Ticket5 has a TakeProfit , and if that level is hit, all trades will close, and all Ticket1...Ticket5 variables are set to 0.

Every trade opening( except first trade), has a condition for opening 20 pips away from previous, and previous Ticket variable to be > "0". Like second trade will open if price traveled 20 pips more, and Ticket1>0.

When a trade  is closed by other means then StopLoss, will trigger also closing all trades, and reset all variables to "0". ( I have a condition for closing )

But if , for example, trade 3 is closed by StopLoss, Ticket3 variable will not reset to "0", therefore it will be opened again.

And this is what I want to achieve: to detect when order is closed by StopLoss, so I can close all trades and reset all Ticket variables to 0.

You seem to be overcomplicating things. You are describing a pyramid (or stacking) method to improve profits, however you a missing some details.

Firstly, don't rely on the ticket numbering being consecutive. There is no such guarantee. So, don't test for the ticket numbering. Just assume ticket numbering is random. Instead keep track of the batch details, such as number of positions, most extreme opening price, net mean open price, net mean stop-loss, etc.

Secondly, apply your stop-loss to the entire batch (on each position) based on the net mean equivalent. That way, they will all close approximately at the same time when hitting a stop-loss, and even if there are stranglers, you can use any position being closed as the trigger to close the rest.

The above explanation is based on my interpretation of the method you described, but since many details are missing, you may have to adapt it to your specific need.

Here are some links regarding pyramiding:

 
Fernando Carreiro #:


Firstly, don't rely on the ticket numbering being consecutive. There is no such guarantee. So, don't test for the ticket numbering. Just assume ticket numbering is random. Instead keep track of the batch details, such as number of positions, most extreme opening price, net mean open price, net mean stop-loss, etc.


Thank you, I will read indicated documentation.

Ticket1, Ticket2, ... Ticket5, are just the name of the variables where I am saving ticket numbers for each opened trade.  Like Ticket1=OrderSend( .... )

Like this

if(OrderSelect(Ticketbuy,SELECT_BY_TICKET))
      initialorderprice = OrderOpenPrice();

   if((Ask-initialorderprice)/point() > trigdistance && Count.Market()<2 && Ticketbuy >0)
     {
      Ticket2 = Trade.OpenBuyOrder(_Symbol,lotsize,magic,ticket2);
      Trade.ModifyOpenOrder(Ticket2,InpStopLoss,InpTakeProfit);
      secondorderprice = Ask;
     }

   if((Ask-secondorderprice)/point() > trigdistance && Count.Market()<3 && Ticket2 >0)
     {
      Ticket3 = Trade.OpenBuyOrder(_Symbol,lotsize,magic,ticket3);
      Trade.ModifyOpenOrder(Ticket3,InpStopLoss,InpTakeProfit);
       thirdorderprice = Ask;
     }
 
Daniel Cioca #: Thank you, I will read indicated documentation. Ticket1, Ticket2, ... Ticket5, are just the name of the variables where I am saving ticket numbers for each opened trade.  Like Ticket1=OrderSend( .... ) Like this

I've used Pyramid in my EAs quite a bit, but in all my EAs I never save or track ticket numbers. Instead I track the positions as a whole, as a batch, using the methods and math I have described in previous posts.

The reason I do it this way, is because anything can happen — an EA may crash or restart, the terminal may crash and restart or even the PC or VPS, and when that happens all those saved ticket numbers are gone.

So, rethink your logic code so as not do depend on tick numbers. Track the batch of positions instead.

One of the good things in "netting" accounts is that you only have to worry about one position per symbol, but with "hedging" accounts you can simulate that by just scanning the batch of positions and keeping track of the statistics for the batch.

Also by treating it as a batch, and not saving ticket numbers, you don't have to worry about only stacking 2, 3 or 5 positions. You can easily just let it run for countless stacked positions.

So, consider rethinking your code logic.

 
Fernando Carreiro #:


Track the batch of positions instead.


Where should I start to read?