Closing out half lots. - page 7

 
  if(OrderSelect(OrdersTotal(),SELECT_BY_TICKET,MODE_TRADES)==true && OpenOrdersThisPair(Symbol())==1 && OrderType()==OP_BUY){ // Even though I am calling "OP_BUY" here, it doesn't seem to work?
         
         }
         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_POS,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;
            }
            
           double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );
           {
           Print("FirstTarget_Buy: ", DoubleToStr( FirstTarget_Buy, Digits ));
           }
            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))
                  {
                  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());
                  }

         } 
   }
     
I know it's a little scruffy! I have been playing around with it loads - so quickly put it back to where we were talking from? I assume you meant this whole part, right?
 
   if (direction==0){ //--Buy--//
      
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10;
      
      if(OpenOrdersThisPair(Symbol())==0){
         int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); // This puts out the initial order.
      }
      
         if(OrderStopLoss()<iMA(NULL,0,MA_Period,0,1,0,0)-MA_PadAmount*pips){// This triggers the For loop if the pending order needs adjusting relative to where the MA is.
         
               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("DeleteOrder Last Error = ", GetLastError());
                                  
                                 if(OpenOrdersThisPair(Symbol())==0) 
                                  NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); // New Pending order with calibrated stops, lots and targets.
                                   if(NewBuyOrder!=TRUE)Print("New Buy Order Last Error = ", GetLastError());  // This is giving me an "Error 0", despite the trade ACTUALLY being placed correctly...?   

                  }
                 } 
             
               if(NewBuyOrder == true){
               Print("New Buy Order Placed #", NewBuyOrder);
               }
               else{
               Print("New Buy Order Send Failed, error # ", GetLastError());   
               }
               }
This is the Buy section - just spits out a "OP_BUYSTOP"
 
DomGilberto:
I know it's a little scruffy! I have been playing around with it loads - so quickly put it back to where we were talking from? I assume you meant this whole part, right?

Erm . . . you need to take a step back, take a deep breath and read your code carefully, function by function, line by line . . . ask yourself "what am I trying to do here ?" and then check if your code is going to do that . . . for example:

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

         { // Even though I am calling "OP_BUY" here, it doesn't seem to work?
         
         }

This if test executes a comment if it is true ? ? ? ? ? ignoring that for a moment . . . how is OrdersTotal() a ticket number ? SELECT_BY_TICKET it's not even a valid Position if you used SELECT_BY_POS


I don't think this code is what is giving you your problem . . . even though it has some issues.

 

Yea - good spot, although that doesn't make any material difference to be honest.

Hmmm....

 
DomGilberto:

Yea - good spot, although that doesn't make any material difference to be honest.

Hmmm....

No it doesn't . . .

RaptorUK:


I don't think this code is what is giving you your problem . . . even though it has some issues.

I don't think you are showing the code that is creating the issue . . .

 
DomGilberto:
This is the Buy section - just spits out a "OP_BUYSTOP"

This . . .

if(OpenOrdersThisPair(Symbol())==0) 
   NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); // New Pending order with calibrated stops, lots and targets.

if(NewBuyOrder!=TRUE)Print("New Buy Order Last Error = ", GetLastError());  // This is giving me an "Error 0", despite the trade ACTUALLY being placed correctly...? 

. . . is a {} braces issue . . .

if(OpenOrdersThisPair(Symbol())==0) 
   {
   NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green); // New Pending order with calibrated stops, lots and targets.

   if(NewBuyOrder != TRUE)Print("New Buy Order Last Error = ", GetLastError());  // This is giving me an "Error 0", despite the trade ACTUALLY being placed correctly...? 
   }

sort your indenting and you will "see" these issues quickly.

 
Yea, again good shout, I've gone through and sorted out a few of the indenting issues... still stuck with the same issues though? What other code are you insinuating it could be? I couldn't imagine it being from any other part of my coding...?
 
if(OrderSelect(OrdersTotal(),SELECT_BY_POS,MODE_TRADES)==true && OpenOrdersThisPair(Symbol())==1) // If I put "OrderType() == OP_BUY" it won't work at all. This means that it could just grab a pending order - which is wrong.
  { 
         
         
         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_POS,MODE_TRADES)==true)
            {
            Print("Order Ticker Number = ",OrderTicket());
            Print("Order Lots Open = ", OrderLots());
            } 
         else{
           Print("Order Select returned the error of ", GetLastError()); 
           continue;
           }

           double FirstTarget_Buy = OrderOpenPrice() + ( ( OrderTakeProfit()-OrderOpenPrice() ) / 2 );
           {
           Print("FirstTarget_Buy: ", DoubleToStr( FirstTarget_Buy, Digits ));
           }
            
            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)) // Does not go further than this. 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());
                  }
                   
       } 
   }
I'm stuck - I have tried shifting and changing a lot and now It must be a case of missing something I don't know exists... has to be? OrderType()==OP_BUY does not seem to work? See notes in code above - I have checked indenting... This is epic! haha...
 
DomGilberto:
Yea, again good shout, I've gone through and sorted out a few of the indenting issues... still stuck with the same issues though? What other code are you insinuating it could be? I couldn't imagine it being from any other part of my coding...?
LOL, I don't know, I haven't seen it all . . . but something is stopping the code that does the printing from running when you have an OP_BUY
 
2013.08.02 10:52:47	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

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"?

This is a longggggg process lol...

Reason: