Trouble Closing order , logic problems?

 

Hey there, I want to close an order when 3 candles have closed in the stop loss area. It just seems like the logic is never true. I have other conditions for closing trades and they work fine. Here is my approach.

P.s The Print functions don't get printed.


//---------------------------------------------------
// If 3 bars closed above stop loss this function will return true, 0 = shorts 1 = longs

   bool close3stop(double stopprice, int direction)
      {
        int count=0;
        
          for(int b=1; b<=3; b++)
            {
             
               if((Close[b]>stopprice) && (direction==0)) {count++;}  //short trades
               if((Close[b]<stopprice) && (direction==1)) {count++;}  //long trades
            }     
         if(count==3) { return(true); } else { return(false); }
      }

//----------------------------------------------------



// start() 


for(int tradecounter = OrdersTotal(); tradecounter >= 0 ; tradecounter--)
         {                                                                                   //Open trade for loop
         
            if(
                (OrderSelect(tradecounter, SELECT_BY_POS)) &&
                (OrderMagicNumber()==eamagicnumber) &&
                (OrderSymbol()==Symbol())
                
               ) 
         
                 {                                                                          // Check each trade conditions 
                     // Ea Trade Found - Run Checks 
                 
                 //---- If 3 bars close in stop loss area close trade ----------------    
                     if(
                        (OrderType()==OP_BUY) &&
                        (close3stop(OrderStopLoss(), 1)==true )
                       ) 
                           { 
                              Print(" 3 closes in stop loss closing trade");
                              OrderClose(OrderTicket(), OrderLots(), Bid, 3, Brown); 
                           }
                           
                     if(
                        (OrderType()==OP_SELL) &&
                        (close3stop(OrderStopLoss(), 0)==true )
                        
                       ) 
                           { 
                              Print(" 3 closes in stop loss closing trade");
                              OrderClose(OrderTicket(), OrderLots(), Ask, 3, Brown); 
                           }
                //--------------------------------------------------------------------
                 
                 
                 
                  //---- If price has passed the 1:1 area use the Kijun sen as a traling stop -----
                     if(
                        (OrderType()==OP_BUY) &&
                        (Bid > (OrderOpenPrice()+(OrderOpenPrice()-OrderStopLoss()))) &&
                        (Bid < kijunsen)
                       ) 
                           { 
                              OrderClose(OrderTicket(), OrderLots(), Bid, 3, Green); 
                           }
                       
                     if(
                        (OrderType()==OP_SELL) &&
                        (Ask < (OrderOpenPrice()-(OrderStopLoss()-OrderOpenPrice()))) &&
                        (Ask > kijunsen)
                        
                       ) 
                           { 
                              OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red); 
                           }
                 //-------------------------------------------------------------------------------
                 
                 
                  
                  
                  }                                                                             // End Check each trade conditions
         
         }                                                                                      // End open trade for loop
       }                                                                                        // End trade open check
 
dazamate:

Hey there, I want to close an order when 3 candles have closed in the stop loss area. It just seems like the logic is never true. I have other conditions for closing trades and they work fine. Here is my approach.

P.s The Print functions don't get printed.

If one of the 3 candles has closed below the SL of a Buy the likelihood is that the trade will have been closed by the SL.

One small point . . .

for(int tradecounter = OrdersTotal(); tradecounter >= 0 ; tradecounter--)  //  <---  shouldn't this start at OrdersTotal() - 1 ?
 

Raptor You make me look so stupid haha, It should be if 3 candles close under the Order open price.


Do you have to -1 of the total orders because there is an order at number 0? I get ya, I will make them changes.


Happy New Year Raptor!

 
dazamate:

Happy New Year Raptor!

Happy New year to you too . . . just happy to try and help. :-)
Reason: