Closing out half lots. - page 13

 
Sorry, but I do not understand how that would help me at all. It doesn't matter what happens in the for loop and the void that is called. It's about what I am first calling in the "int start()" surely?

As I've pasted above, If I half the order, irrelevant what the OrderTicket() is, ANY open "OP_BUY" will be called again and again?

I need a way to write the code so that within the "in start()" and that code above, it will NOT keep calling the "OP_BUY" every time AFTER it has already closed one half?
 
DomGilberto:
Sorry, but I do not understand how that would help me at all. It doesn't matter what happens in the for loop and the void that is called. It's about what I am first calling in the "int start()" surely?

As I've pasted above, If I half the order, irrelevant what the OrderTicket() is, ANY open "OP_BUY" will be called again and again?

I need a way to write the code so that within the "in start()" and that code above, it will NOT keep calling the "OP_BUY" every time AFTER it has already closed one half?

The void that is called is just a function that is being called from within start() . . . it's just the same as if the code were directly in start().

pseudo code:

look for OP_BUYs

if found - is the TP at BE ?

if TP is not at BE and Bid >= first target move TP to BE and close half the order

if TP is at BE do nothing

 

YES!!! Thank you RaptorUK!

Sorry, I am not as stupid as what I make out you know ;) - Just sometimes things have to be slightly reiterated to me when I am new to something...

I realized that everyone has been telling me that pages ago - I apologize for not picking up or understanding it!


RIGHT! We have finally managed to get half closed, move to break even and stop the continuous closing of the halves at the same price :D! So it's pretty much spot on perfect!!

All I am going to do now, is essentially copy and paste this for my other exits and change a few rules and hay presto! Bobs your uncle = it's sorted :)

I will post it all up on here to make it fair for others to use if they so wish - After-all, a lot of patience from others has allowed me to not only code it, but understand it too :)

 
That's weird - sometimes when I do a backtest the half close @ 1:1 doesn't work, but then when I do a shorter back-test is does work? Weird... is that because strategy tester is crap, or do I have a flaw with the code?
 
Oh and what would one suggest doing if price does not reach the first target of 1:1 BUT the stoploss is GREATER than the entry....? How would I create a rule around that then....
 
DomGilberto:
Oh and what would one suggest doing if price does not reach the first target of 1:1 BUT the stoploss is GREATER than the entry....? How would I create a rule around that then....


when do you modify to breakeven ??

if if target not reached trade will be closed at OrderStopLoss()

 
deVries:


when do you modify to breakeven ??

if if target not reached trade will be closed at OrderStopLoss()


I close half off using OrderClose() and if that returns true, then I call the breakeven void to move the stoploss to break even? That way the rest of the open position in lots wont continuously be closed because if the position has been moved to break even, then i've written it that there will be no more closing of that position until it reaches its fixed take profit price of 1:2.

However, some trades will go equal to the entry or beyond break even BEFORE the trade has reached the first target, NOT triggering the half close...

Also, some trades the half off exit and then move to break even works perfectly.... some trades that it should work on, it doesn't do a thing? Could this be down to poor data and a flaw with strategy tester, or is there an inconsistency with my code?

Oh and I am still getting OrderModify 1 - fucking irritating...

 

How are you identifying the order after you part closed it to then modify the SL to break even ? You know you cant use the original ticket number right ? When you part close an order the original ticket number is closed. The remaining part is given a new ticket number. One way to identify it is get the OrderOpenTime() before you part close. After you part close an order the remaining part will get a new ticket number but the OrderOpenTime() will be the same as before.

You could also use that OrderOpenTime() to identify it and prevent it being part closed again and again.

Alternatively you could modify it before you part close it. The remaining part of your order will retain the modified SL or TP.

Here is a test. Opened at 0.09 part close 0.07 leaving 0.02

Notice how ticket number changes but open time remains the same:

23:42:48 2013.08.01 11:28 SDCMegaTrend EURUSD,M1: open #14 buy 0.09 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 ok
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: close #14 buy 0.07 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 at price 1.3253
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: Alert: Ticket 14 OrderOpenTime before part close: 1375356521
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: Alert: Ticket 15 OrderOpenTime: 1375356521
23:42:48 2013.08.01 12:45 SDCMegaTrend EURUSD,M1: close #15 buy 0.02 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 at price 1.3242

 
SDC:

How are you identifying the order after you part closed it to then modify the SL to break even ? You know you cant use the original ticket number right ? When you part close an order the original ticket number is closed. The remaining part is given a new ticket number. One way to identify it is get the OrderOpenTime() before you part close. After you part close an order the remaining part will get a new ticket number but the OrderOpenTime() will be the same as before.

You could also use that OrderOpenTime() to identify it and prevent it being part closed again and again.

Alternatively you could modify it before you part close it. The remaining part of your order will retain the modified SL or TP.

Here is a test. Opened at 0.09 part close 0.07 leaving 0.02

Notice how ticket number changes but open time remains the same:

23:42:48 2013.08.01 11:28 SDCMegaTrend EURUSD,M1: open #14 buy 0.09 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 ok
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: close #14 buy 0.07 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 at price 1.3253
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: Alert: Ticket 14 OrderOpenTime before part close: 1375356521
23:42:48 2013.08.01 12:26 SDCMegaTrend EURUSD,M1: Alert: Ticket 15 OrderOpenTime: 1375356521
23:42:48 2013.08.01 12:45 SDCMegaTrend EURUSD,M1: close #15 buy 0.02 EURUSD at 1.3240 sl: 1.3037 tp: 1.3737 at price 1.3242


Ah - Awesome, thank you. I completely forgot about OrderOpenTime() - that's a great way of doing it! Thanks for the tip - I will experiment with that now.

Following me previous post, I have managed to get it running a lot smoother now - OrderModify error 1 pops up only rarely now. It seems to be working more smoothly now.

The only problem is, is that if I have a trade where the stop loss IS >= than OrderEntry() - (assuming we're talking about a long), then it will not close half of the order because the main criteria for closing half, has not been met.

This code lone here is what initiates the closing of half of the lots, and the stopping of the continuous closing of halves at the same price - (Its not the only thing in "in start()", just made it easier to see):



   

int start()
{

   if(OpenOrdersThisPair(Symbol())>0) //If there is a trade open = do below, which ever is applicable.
      {
      if(OrderType()==OP_BUY && OrderOpenPrice() > OrderStopLoss())
         {
         CloseHalfOrder(); // Closes half at 1:1 - then calls another void to break even on the trade +3.
         }
      
      if(OrderType()==OP_SELL && OrderOpenPrice() < OrderStopLoss())
         {
         CloseHalfOrder1(); // Closes half at 1:1 - then calls another void to break even on the trade +3.
         }   
      } 
}
How would I incorporate the OrderOpenTime() function to have better control in making sure every trade closes there half lots at 1:1 WITHOUT missing any where by the stop IS >= the OrderOpen()?
 

There are several ways you can make sure every order is part closed, my favorite way of doing it is to modify the TP by one pip when i part closed it.

So for example I need a TP of 100 pips, so I open the order with a TP of 99 pips. I know before the take profit is ever reached my order will be part closed at 40 pips. When that happens I modify the TP to 100 pips. Therfore any order that has a profit >= 40 pips and a TP < 100 pips needs to be part closed. Any order with a TP > 99 pips has already been part closed. If you do it that way it makes no difference what the SL is. Just make sure you Normalize all order price doubles before making calculations on them.

Reason: