Closing out half lots. - page 3

 
DomGilberto:

Does MathFloor round decimal places DOWN to the nearest WHOLE number?

MathFloor() does what it says in the documentation . . . "The MathFloor function returns a numeric value representing the largest integer that is less than or equal to x."

DomGilberto:


Does FirstTarget make sense as a custom parameter within "OrderClose()" - Assuming "sizeClose" was correct? (see quoted code)

No . . . you have this:

FirstTarget = OrderTakeProfit()-OrderOpenPrice()/2+OrderOpenPrice();

// meaning this

FirstTarget = OrderTakeProfit() - ( OrderOpenPrice() / 2 ) + OrderOpenPrice();

when you probably mean this . . .

FirstTarget = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 ) ;

Use brackets (and spaces) to make things clear and unambiguous.

DomGilberto:


Your code did not help - sorry.

Why ? is it wrong ? in what way ?

DomGilberto:


Can you print custom variables? If so, I must be doing it wrong as it is not showing up in the journal.

Yes, you can print any variables . . . what are you doing ? show your code.

 
 if(OpenOrdersThisPair(Symbol())==1 && OrderType() == OP_BUYSTOP)
   {
      OrderSelect(OrderTicket(),SELECT_BY_POS,MODE_TRADES); 
      
      double Lots=OrderLots(); 
      
      double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );{
         
         double minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                half    = MathFloor(Lots/2/lotStep)*lotStep;
                
                if (half < minLot) half = Lots;
         
         half=Lots/2; 
         OrderClose(OrderTicket(),half,FirstTarget_Buy,3,DarkViolet);
         }
   } 
     
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   if(OpenOrdersThisPair(Symbol())==1 && OrderType() == OP_SELLSTOP)
    {
      OrderSelect(OrderTicket(),SELECT_BY_POS,MODE_TRADES);
      
      double FirstTarget_Sell = OrderOpenPrice() - ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );{
       
       if (half < minLot) half = Lots;
       half=Lots/2;
       OrderClose(OrderTicket(),half,FirstTarget_Sell,3,DarkViolet); 
       }
    } 


With regards to printing custom variables, how would I go about doing this for "FirstTarget_Buy"?

I am going to spend time going through all my code and printing a lot out - I understand how to do it with bools and ints (integer -1 fails and bool is a true or false statement - easy.) but I do not understand how to do it when you assign functions to a custom variable?

Thanks for shedding light on "FirsTarget" formula above! Sorry, I meant to say that your code did not help in a sense that I am not understanding how to put the formula together... I find it really counter intuitive - You would have thought that calling "OrderSelect" would allow you to simply see what the lot size was on that tickorder... and then simply split it...

Sorry for coming across like a complete moron!

 
DomGilberto:

Sorry for coming across like a complete moron!

Not at all, you just need to find your blind spot and enlighten it, once you have you will wonder why you found it so hard, everything is easy when you know how.

DomGilberto:

With regards to printing custom variables, how would I go about doing this for "FirstTarget_Buy"?

I am going to spend time going through all my code and printing a lot out - I understand how to do it with bools and ints (integer -1 fails and bool is a true or false statement - easy.) but I do not understand how to do it when you assign functions to a custom variable?

Like this . . . also note the comment about curly braces
 if(OpenOrdersThisPair(Symbol())==1 && OrderType() == OP_BUYSTOP)
   {
      OrderSelect(OrderTicket(),SELECT_BY_POS,MODE_TRADES); 
      
      double Lots=OrderLots(); 
      
      double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );

      Print("FirstTarget_Buy: ", DoubleToStr(FirstTarget_Buy, Digits) );   // prints FirstTarget_Buy with the same number of digits as the current symbol
         {                                                                                        // why this brace ?   
         
         double minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                half    = MathFloor(Lots/2/lotStep)*lotStep;
                
                if (half < minLot) half = Lots;
         
         half=Lots/2; 
         OrderClose(OrderTicket(),half,FirstTarget_Buy,3,DarkViolet);
         }                                                               // why this brace ?  
   } 

DomGilberto:

I meant to say that your code did not help in a sense that I am not understanding how to put the formula together... I find it really counter intuitive - You would have thought that calling "OrderSelect" would allow you to simply see what the lot size was on that tickorder... and then simply split it...

You can simply OrderSelect the order, the use OrderLots() and divide by two . . . you can do that, and in many situations it will work. I write code fo all cases I can foresee so that I don't have to continually fix it when it breaks . . .

If your position size is 2.0 lots; this divided by two is 1.0 lots and this will work . . .

If your position size is 0.3 lots; this divided by two is 0.15 lots and this will be an invalid position size if MODE_LOTSTEP is 0.1 . . . you can use 0.1 or 0.2 NOT 0.15 so you need to code this check and adjustment.


What you have here will not work in all cases . . .

double Lots = OrderLots(); 
      

         
         half = Lots / 2; 
 

Ahhhh! I've just spent last 15-20 minutes wacking prints all over the place - I am starting to understand where I am going wrong. I am basically selecting the PENDING order... HOWEVER, because it is a pending order, at the point I select it, there is a pretty high chance the OrderLots, stop and targets would have changed, therefore I'll need to put it in a loop to make it synced with how my pending orders are being constantly calculated before they trigger.

That darn doubletostr :P - Spot on RaptorUK! I can now see that this is calculated correctly, thanks to your adjustment :)

I think I know how to do most of it now - Just out of curiosity, what "if" statement would work to ONLY call the order that has been TRIGGERED. At the moment, I have it calling upon when I have a market order open, but I am trying to figure out how I call upon when the pending order has been triggered?

The code below and the corresponding "if" statement just seems to call upon open pending orders. Just want a way to "go to" a specific custom function (void for example) on the basis that an order has been triggered? (if that makes sense!)

Thank you for clearing so much up for me :)

int OpenOrdersThisPair(string pair)
{
   int total=0;
      for(int i=OrdersTotal()-1; i >= 0; i--)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==pair) total++;
         }
         return (total);
}         

if(OpenOrdersThisPair(Symbol())>0)
 
DomGilberto:

Ahhhh! I've just spent last 15-20 minutes wacking prints all over the place - I am starting to understand where I am going wrong.

Easy when you know how

DomGilberto:


I think I know how to do most of it now - Just out of curiosity, what "if" statement would work to ONLY call the order that has been TRIGGERED. At the moment, I have it calling upon when I have a market order open, but I am trying to figure out how I call upon when the pending order has been triggered?

When a Pending Order has been triggered it will become an OP_BUY or OP_SELL, so . . .

if(OrderType() == OP_BUY || OrderType() == OP_SELL)
   { 
   
   }

One tip, use m o r e spaces . . . when you get a divide by zero error and you want to find where it is happening it is easy to search for space/space and just find your divisions instead of all your comments . . . so I use spaces around = + - / % etc.

 
Ah of course (facepalm!)

Good tip - I will do! Thank you for your help :) I'll post back if I manage to do it!
 
Ok, I'm back! ... Not so good news though - I have tried and tested multiple variations of code with no luck. This is where I am at right now.

I am getting no information regarding the prints? Absolutely nothing is coming up in the Journal. I assume this is because nothing is being selected as far as the OrderType goes right from the first "if" statement? From what I can see here on the code, I would have thought that I am not far off, at least...?
if(OrderType() == OP_BUY ){
   
      for( int c=OrdersTotal()-1; c >=0; c-- )
      {
         if(OrderSelect(c,SELECT_BY_TICKET,MODE_TRADES)==true){
            Print("Order Ticker Number = ",OrderTicket());
            Print("Order Lots Open = ", OrderLots());
            } 
         else if(OrderSelect(c,SELECT_BY_TICKET,MODE_TRADES)==false){
            Print("Order Select returned the error of ", GetLastError());
            }
            if(OrderMagicNumber()==MagicNumber)
              if(OrderSymbol()==Symbol())
                if(OrderType()==OP_BUY)
                  if(OpenOrdersThisPair(Symbol())==1) 
                    //if( Bid == FirstTarget_Buy)            
         
         
         double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );{
            Print("FirstTarget_Buy: ", DoubleToStr( FirstTarget_Buy, Digits ));
            }
         
         double minLot  = MarketInfo(Symbol(), MODE_MINLOT);{
                Print("The minimum lots are: ", DoubleToStr( minLot, Digits ));
                }
                        
         double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);{
                Print("The Lotstep is: ", DoubleToStr( lotStep, Digits ));
                }
                
         double half    = MathCeil(OrderLots()/2/lotStep)*lotStep;{
                Print("The Lots to close is: ", DoubleToStr( half, Digits ));
                }

                bool Close_Half_Order = OrderClose(OrderTicket(),half,FirstTarget_Buy,3,CLR_NONE);
            if(Close_Half_Order!=TRUE)Print("Close_Half_Order Last Error = ", GetLastError());
      } 
   }
 
DomGilberto:
Ok, I'm back! ... Not so good news though - I have tried and tested multiple variations of code with no luck. This is where I am at right now.

I am getting no information regarding the prints? Absolutely nothing is coming up in the Journal. I assume this is because nothing is being selected as far as the OrderType goes right from the first "if" statement? From what I can see here on the code, I would have thought that I am not far off, at least...?

How are you selecting the Order before doing this ?

if(OrderType() == OP_BUY ){

You still have issues here

      for( int c=OrdersTotal()-1; c >=0; c-- )
      {
         if(OrderSelect(c,SELECT_BY_TICKET,MODE_TRADES)==true){
            Print("Order Ticker Number = ",OrderTicket());
            Print("Order Lots Open = ", OrderLots());
            } 
         else if(OrderSelect(c,SELECT_BY_TICKET,MODE_TRADES)==false){
            Print("Order Select returned the error of ", GetLastError());  //  OrderSelect has failed,  why do you continue . . . 
            }
            if(OrderMagicNumber()==MagicNumber)                            //  . . . to this line ?
              if(OrderSymbol()==Symbol())
                if(OrderType()==OP_BUY)
                  if(OpenOrdersThisPair(Symbol())==1) 
                    //if( Bid == FirstTarget_Buy) 
 

Why not . . .

for( int c = OrdersTotal()-1; c >= 0; c-- )
   {
   if( OrderSelect(c, SELECT_BY_TICKET, MODE_TRADES) )
      {
      Print("Order Ticker Number = ",OrderTicket());
      Print("Order Lots Open = ", OrderLots());
      } 
   else 
      {
      Print("Order Select returned the error of ", GetLastError()); 
      continue;                                                        // try next order position in the loop
      }
   if(OrderMagicNumber() == MagicNumber)    
      if(OrderSymbol() == Symbol())
         if(OrderType() == OP_BUY)
            if(OpenOrdersThisPair(Symbol()) == 1) 
 
I can't believe I am making these rookies errors... Annoys me! Yeah, I got it printing now and selecting the order. This is the code that is working in terms of the print - the lots are still not being closed out though? I have also pasted the journal messages below.
  if(OrderSelect(OrdersTotal(),SELECT_BY_TICKET,MODE_TRADES)==true && OpenOrdersThisPair(Symbol())==1){
         
         double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );{
           Print("FirstTarget_Buy: ", DoubleToStr( FirstTarget_Buy, Digits ));
         }
         
         double minLot  = MarketInfo(Symbol(), MODE_MINLOT);{
                Print("The minimum lots are: ", DoubleToStr( minLot, Digits ));
                }
                        
         double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);{
                Print("The Lotstep is: ", DoubleToStr( lotStep, Digits ));
                }
                
         double half    = MathCeil(OrderLots()/2/lotStep)*lotStep;{
                Print("The Lots to close is: ", DoubleToStr( half, Digits ));
                }
      for( int c=OrdersTotal()-1; c >=0; c-- )
      {
         if(OrderSelect(c,SELECT_BY_TICKET,MODE_TRADES)==true){
            Print("Order Ticker Number = ",OrderTicket());
            Print("Order Lots Open = ", OrderLots());
            } 
         else{
           Print("Order Select returned the error of ", GetLastError()); // Order Select does not seem to want to work?
            continue;
            }
            if(OrderMagicNumber()==MagicNumber)
              if(OrderSymbol()==Symbol())
                if(OrderType()==OP_BUY)
                  if(OpenOrdersThisPair(Symbol())==1) 

                bool Close_Half_Order = OrderClose(OrderTicket(),half,FirstTarget_Buy,3,CLR_NONE);
            if(Close_Half_Order!=TRUE)Print("Close_Half_Order Last Error = ", GetLastError());
      } 
   }
2013.07.31 11:13:52	2013.02.01 16:00  trendfishing_play_ground EURUSD,H1: Order Select returned the error of 0    // Not sure what is going on here?		
2013.07.31 11:13:52	2013.02.01 16:00  trendfishing_play_ground EURUSD,H1: The Lots to close is: 0.09000           // This is the right calculation based 
                                                                                                                      // upon the total lots opened on a pending order - to half.
2013.07.31 11:13:52	2013.02.01 16:00  trendfishing_play_ground EURUSD,H1: The Lotstep is: 0.01000
2013.07.31 11:13:52	2013.02.01 16:00  trendfishing_play_ground EURUSD,H1: The minimum lots are: 0.01000
2013.07.31 11:13:52	2013.02.01 16:00  trendfishing_play_ground EURUSD,H1: FirstTarget_Buy: 1.37931                // This price is also correct.


Reason: