Closing out half lots. - page 8

 
DomGilberto:

This is printing in the Journal during my for loop, where by, in the case of a pending Buy Stop Order, if the 60 EMA is > than the OrderStopLoss, it will close the pending order, open a new one with new SL (which is behind the 60EMA), adjust the take profit to entertain the 1:2 reward, and change the lots relative to the stop in points and my risk profile (say 2%).

So when this is all being done, the "Current Lots" which is "LotSize" custom variable within OrderSend works, it is sending off a lot size that is not correct for the server to receive? "0.18215"? Could this be an issue, despite the orders actually being sent through anyway - this question is more in connection to "could this be the reason why I cannot call "OrderType==OP_BUY"?

You should fix that, validate the lot size in the same way that you already have . . . you could even code a separate function to do it based on the code you already have . . . for example:

double ValidateLots(double PositionSize)
   {
   double LotStep, MinLot;

   LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   MinLot  = MarketInfo(Symbol(), MODE_MINLOT);
   
   return( MathFloor(PositionSize / LotStep) * LotStep);
   }


Instead of using LotSize in your OrderSend() you would use ValidateLots(LotSize)

I doubt that this is causing your issue though . . .

 
Hmm - yeah I just played about with it. It appears that the order just gets sent through as the server only allows 0.00 (2 decimal places) anyway. So the remaining digits are almost irrelevant.

Argh! So frustrating lol - stuck on this for too long now :(

I cannot see why this is an issue and I cannot select an "OP_BUY"... I know I've probably exhausted this topic, but any last stabs in the dark would be really appreciated - thanks for helping me as well!!
if (direction==0){ <<<//--Buy--// This gets called upon if all the set-up is apparent.
      
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10; //--// (in process of changing at the moment)
      
      if(OpenOrdersThisPair(Symbol())==0)  //--//<--- No order open = then open one...
      {
      int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); //--//<-- This is the first Order to be placed.
      }
      
         if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips) //--//<<-- This will 99% of the time be true and the order above will be cancelled and looped in the "for" until the order is triggered.
         {
         
               for(int b=OrdersTotal()-1; b >=0; b--) 
                  {
    
                     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)==true)
                     {
                     Print(" Stop loss value for the order is ", OrderStopLoss());
                     }
    
                     else if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)==false)
                     {
                     Print(" OrderSelect failed error code is ",GetLastError());
                     }
                     if(OrderMagicNumber()==MagicNumber)
                        if(OrderSymbol()==Symbol())
                           if(OrderType()==OP_BUYSTOP)
                              if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips)
                                //if(OpenOrdersThisPair(Symbol())==1)
                                 { 
                                 DeleteOrder = OrderDelete(OrderTicket());
                                 if(DeleteOrder!=TRUE)Print("Buy Delete Order Last Error = ", GetLastError());
                                 }
                                  
                                 if(OpenOrdersThisPair(Symbol())==0)//<<-- If there are no open orders = place a new order.
                                  { 
                                  int NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); 
                                  if(NewBuyOrder == -1)Print("New Buy Order Last Error = ", GetLastError());
                                  if(NewBuyOrder > 0)Print("NEW BUY ORDER:- Lots to open: ", DoubleToStr(LotSize, Digits), " Entry Price: ", DoubleToStr(buyPrice, Digits), 
                                  " Buy Stop Loss: ", DoubleToStr(bsl, Digits), " Buy Take Profit: ", DoubleToStr(btp, Digits), " Magic Number is: ", DoubleToStr(MagicNumber, Digits));  
                                  } 
                  }
             } 
       }
 
DomGilberto:
Hmm - yeah I just played about with it. It appears that the order just gets sent through as the server only allows 0.00 (2 decimal places) anyway. So the remaining digits are almost irrelevant.

Argh! So frustrating lol - stuck on this for too long now :(

I cannot see why this is an issue and I cannot select an "OP_BUY"... I know I've probably exhausted this topic, but any last stabs in the dark would be really appreciated - thanks for helping me as well!!

I think you need to show all your code . . .

I see some issues here:

      int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); //--//<-- This is the first Order to be placed.
      }
       //  where are you checking the return value from the OrderSend() and reporting errors ?

         if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips)    //  where is the OrderSelect() ?
 
All the code won't fit on here? I've put it up on pastebin though?

-http://pastebin.com/eaY1wKbN

Sorry about the syntax - little harder to see everything, but they dont have MQL4? So put it under C++
 
DomGilberto:
All the code won't fit on here? I've put it up on pastebin though?

-http://pastebin.com/eaY1wKbN

Sorry about the syntax - little harder to see everything, but they dont have MQL4? So put it under C++

No problem . . .

When you do this . . .

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+

   if(OrderSelect(OrderTicket(),SELECT_BY_TICKET,MODE_TRADES)==true && OpenOrdersThisPair(Symbol())==1 && OrderType()==OP_BUY)
     {

. . . what Order is already selected ? was it the pending order ? your OrderSelect() will fail if your OrderTicket() fails unless you already have an Order selected. Do you really need this code at all ? later on you have a loop where you select the orders and check their symbol and Magic Number and type . . .

This need fixing too . . .

         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())

               Print("Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));

         if(OrderType()==OP_BUY && Bid>=FirstTarget_Buy+(Point/2)) // Doesn't seem to even go further than this as I do not get an error from OrderClose?
           {
            Print("Bid >= FirstTarget_Buy - Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
            bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half,Bid,3,CLR_NONE);
            if(Close_Half_Order_Buy!=TRUE)Print("Close_Half_Order_Buy Last Error = ",GetLastError());
           }

. . . ad some braces . . .

         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               {
               Print("Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));

               if(OrderType()==OP_BUY && Bid>=FirstTarget_Buy+(Point/2)) // Doesn't seem to even go further than this as I do not get an error from OrderClose?
                 {
                 Print("Bid >= FirstTarget_Buy - Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
                 bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half,Bid,3,CLR_NONE);
                 if(Close_Half_Order_Buy!=TRUE)Print("Close_Half_Order_Buy Last Error = ",GetLastError());
                 }
              }
 

Ok - I wacked more prints in to get a bit of clarity on whats being done and what point. I have pasted it below.

The line where it says "Order Select for closing:-..." that is what is being selected at the point "Close OP_BUY Half Lots @ 1:1 Function" you've asked above. It appears that the ticket number is different at this point of selection? I had deleted the "...&& OrderType()==OP_BUY" part out of it, as it seems to print the rest when this part is not in the "if" statement... Does this give a little more clarity?

"First Buy Order Placed:-" is the first OrderSend function going through at the start
"Checking Buy Order:-" Is the one within the for loop to delete and re-open the pending order if the MA is > the OrderStopLoss().
"NEW BUY ORDER:-" is the new order being placed inside that for loop.
"Order Select for closing:-..." - as mentioned above already, this is the part that's being selected to close half of the lots.

Sorry if that was confusing, as I have just added these parts in - you won't see this within that pastebin link above... just simple print functions :)

2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: Current Bid: 1.35972 FirstTarget_Buy: 1.37931
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: FirstTarget_Buy: 1.37931 // This is the price where half the lots will close.
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: Order Lots Open = 0.18
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: Order Ticker Number = 9
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: The Lots to close is: 0.09000
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: The Lotstep is: 0.01000
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: The minimum lots are: 0.01000
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: Order Select for closing:- Lots open: 0.18 Entry Price: 1.3679 Buy Stop Loss: 1.3565 Buy Take Profit: 1.3907 Magic Number is: 1234 Order Ticket Number: 9
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: NEW BUY ORDER:- Lots to open: 0.18215 Entry Price: 1.36790 Buy Stop Loss: 1.35649 Buy Take Profit: 1.39072 Magic Number is: 1234.00000 Order Ticket Number: 8
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: open #9 buy stop 0.18 EURUSD at 1.36790 sl: 1.35649 tp: 1.39072 ok
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: delete #8 buy stop 0.18 EURUSD at 1.36790 sl: 1.35649 tp: 1.39072 ok
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: Checking Buy Order:- Lots to open: 0.18 Entry Price: 1.3679 Buy Stop Loss: 1.3565 Buy Take Profit: 1.3907 Magic Number is: 1234 Order Ticket Number: 8
2013.08.02 13:33:57  2013.02.01 15:00  trendfishing_play_ground EURUSD,H1: First Buy Order placed:- Lots to open: 0.18215 Entry Price: 1.36790 Buy Stop Loss: 1.35649 Buy Take Profit: 1.39072 Magic Number is: 1234.00000 Order Ticket Number: 7
 
DomGilberto:

Ok - I wacked more prints in to get a bit of clarity on whats being done and what point. I have pasted it below.

The line where it says "Order Select for closing:-..." that is what is being selected at the point "Close OP_BUY Half Lots @ 1:1 Function" you've asked above. It appears that the ticket number is different at this point of selection? I had deleted the "...&& OrderType()==OP_BUY" part out of it, as it seems to print the rest when this part is not in the "if" statement... Does this give a little more clarity?

"First Buy Order Placed:-" is the first OrderSend function going through at the start
"Checking Buy Order:-" Is the one within the for loop to delete and re-open the pending order if the MA is > the OrderStopLoss().
"NEW BUY ORDER:-" is the new order being placed inside that for loop.
"Order Select for closing:-..." - as mentioned above already, this is the part that's being selected to close half of the lots.

Sorry if that was confusing, as I have just added these parts in - you won't see this within that pastebin link above... just simple print functions :)


Try the attached . . . it's my best guess at what you are trying to do . . .
Files:
dom.mq4  24 kb
 

Ah awesome - thank you. Its a large step in the right direction. I think because it's within a for loop, it wants to close out half and half and half @ the price... Instead of just doing it once? Not only that, but it seems to not close half off as soon as price >= to the exit price I want, instead it goes beyond it, and then when price swings back lower into the exit price I want, it closes then - strange... Appreciate your patience in helping me! Very grateful for your time!!


UPDATE: Also, I have just figured out, that if I change the expert properties (extern int) before running strategy tester, then the close half does not work at all - things that effect the close half is "MA_PadAmount" this is how many points I want to add as a pad to the stop loss that trails in pips, and "OrderPrice_PadAmount" which allows me to specify how many pips I can put as a pad on the entry price of the orders...

 
DomGilberto:
Ah awesome - thank you. Its a large step in the right direction. I think because it's within a for loop, it wants to close out half and half and half @ the price... Instead of just doing it once? Not only that, but it seems to not close half off as soon as price >= to the exit price I want, instead it goes beyond it, and then when price swings back lower into the exit price I want, it closes then - strange... Appreciate your patience in helping me! Very grateful for your time!!
It's not the for loop, it's just that it has no way of knowing that half has already been closed . . . how did you plan to handle that ?
 
Yea - I literally just figured that out when I removed the for loop. Am I right in saying that if I track the OrderTicket number from when it is first selected, once the OrderClose has done its thing and it returns true, then I could verify this with the OrderTicket number as they will be different - thus breaking the loop and wait until another new order comes through?

Not entirely sure how to write that, but is that roughly on the right lines?
Reason: