Closing out half lots.

 

So I managed to incorporate a loop that deletes the pending order and re-applies another one on the basis that the stop is synced with the moving average. The lots would be calculated based upon the pip distance of entry to stop. Not only this, but I have managed to work out how the profit target working with the stop distance as a ratio (extern int - something I choose 1-2-3 R;R etc.) - so this too also moves as well.

So thanks for comments on previous posts with regards to Printing out my code and other bits and bobs!


Anyway - I am trying to close out half of the position when price reaches 50% of my 2x ratio profit target... I know I need to Print things out to the Journal and I am currently writing that in now, but could anyone tell me if I am writing this wrong in anyway? Perhaps with regards to "OrderLots()/2"?

"btp" = returns a specific price.

         if(Bid == btp-OrderOpenPrice()/2+OrderOpenPrice()){
         
         for(int q=OrdersTotal()-1; q >=0; q--)
         {
            if(OrderSelect(q,SELECT_BY_POS,MODE_TRADES)==true){
              Print(" Stop loss value for the order is ", OrderStopLoss());
              Print("lots for the order ",OrderLots());
            }
            else if(OrderSelect(q,SELECT_BY_POS,MODE_TRADES)==false){
              Print(" OrderSelect failed error code is ",GetLastError());
            }
            if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol()==Symbol())
              if(OrderType()==OP_BUYSTOP)
               
               CloseHalfLong = OrderClose(OrderTicket(),OrderLots()/2,Bid,3,CLR_NONE);
         
            if(CloseHalfLong!=TRUE)Print("LastError = ", GetLastError());
         }
         }
 
DomGilberto:

So I managed to incorporate a loop that deletes the pending order and re-applies another one on the basis that the stop is synced with the moving average. The lots would be calculated based upon the pip distance of entry to stop. Not only this, but I have managed to work out how the profit target working with the stop distance as a ratio (extern int - something I choose 1-2-3 R;R etc.) - so this too also moves as well.

So thanks for comments on previous posts with regards to Printing out my code and other bits and bobs!


Anyway - I am trying to close out half of the position when price reaches 50% of my 2x ratio profit target... I know I need to Print things out to the Journal and I am currently writing that in now, but could anyone tell me if I am writing this wrong in anyway? Perhaps with regards to "OrderLots()/2"?

"btp" = returns a specific price.

I don't think that OrderLots()/2 will cut it ( pardon the pun ) in all situations, I'm pretty sure you need to validate the position size you are looking to close against MarketInfo() MODE_LOTSTEP and MODE_MINLOT

Look here: https://www.mql5.com/en/forum/143966

 
RaptorUK: I don't think that OrderLots()/2 will cut it ( pardon the pun ) in all situations, I'm pretty sure you need to validate the position size you are looking to close against MarketInfo() MODE_LOTSTEP and MODE_MINLOT
Exactly. See my CloseOrder()
 

Thanks RaptorUK - Not sure where I am looking WHRoeder on your link, but thanks.

Is it just me, or is the process for closing lots on an open position a little counter intuitive... seems unnecessarily complicated, considering what it is I am wanting to do...

Man, I think I have looked at this code enough for one day - I am not understanding how I use MarkerInfo() with OrderClose... it seems unnecessarily complex.

 
DomGilberto:

Thanks RaptorUK - Not sure where I am looking WHRoeder on your link, but thanks.

Is it just me, or is the process for closing lots on an open position a little counter intuitive... seems unnecessarily complicated, considering what it is I am wanting to do...

Man, I think I have looked at this code enough for one day - I am not understanding how I use MarkerInfo() with OrderClose... it seems unnecessarily complex.

If your Order position size is 0.3 lots and you want to close half that would be 0.15 lots, if your Broker's MODE_LOTSTEP is 0.1 then 0.15 is an invalid lot size . . . so before you use the OrderClose() validate your position size you are about to close.
 
Ah I got it! Thanks RaptorUK! Last question, when you say validate the lot size, are you saying compare it using If statements?
 
DomGilberto: Not sure where I am looking WHRoeder on your link, but thanks.
Top most link on that page
 
Ah gotchya! That is some epic coding... :o!

I hope I can retain that level of programming one day... (with a little more patience on your behalf - just kidding :P)


Thanks WHRoeder!
 
DomGilberto:
Ah I got it! Thanks RaptorUK! Last question, when you say validate the lot size, are you saying compare it using If statements?

Nope, I'm saying adjust it so that it complies with MODE_LOTSTEP and MODE_MINLOT . . . then it is valid ( validated ), if you look at the link I posted . . .

mlots = MathFloor(mlots / lotstep) * lotstep; 

Assume mlots was 0.15 mlots (MODE_MINLOT) was 0.1 and lotstep (MODE_LOTSTEP) is 0.1 so the code would do this

mlots = MathFloor(0.15 / 0.1) * 0.1; 
//  MathFloor(0.15 / 0.1) ==  MathFloor( 1.5 )  gives 1

//  mlots = 1 * 0.1;  

so mlots would be adjusted from 0.15 to 0.1 and would be valid.

 
Ah spot on! Thanks for clearing that up - got it!