Problems checking for open trade - page 6

 

I have never tried this, but I don't think it is valid . . .

OrderType()==(OP_BUYSTOP||OP_SELLSTOP) &&     // The order selected is either a pending buy on stop order or a buy on sell order
OrderMagicNumber()==(Mnumber1||Mnumber1))    // The orders magic number is the same as the magic number used in this ea

I think you need to do this . . .

((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&     // The order selected is either a pending buy on stop order or a buy on sell order
((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber2))    // The orders magic number is the same as the magic number used in this ea

the reason why I think this is . . . what does this equate to ? (OP_BUYSTOP||OP_SELLSTOP) It might work if the variables were bool . . . but I don't think it can work with int.

One minor thing, you don't need the OderSelect before the OrderDelete. In general you only need OrderSelect when you use an Order function that doesn't use int ticket. e.g. OrderComment( ), OrderCommission( ), etc

 
Ok thankyou Raptor, I just updated the code on my post. I just remembered how you guys mentioned it was no good to use bars for a counter so I changed it around to use bar time. Ill fix up the logic and see if it works.
 

Ok I repaired the logic. But it does not delete the orders still.


As I mentioned before I changed the counter from bars to time...


extern string  sComment4                = "Max Hours allowed before pending orders are deleted";
extern int     pendinglimit        = 4;

// * EVERYTIME A TRADE GETS TRIGGERED 

bartraded = TimeHour(TimeCurrent());


   if(TimeHour(TimeCurrent()) > (bartraded + pendinglimit) && Tradeopen()==true)      // Check to see if pending orders have expired
     {
        for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&               // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)))       // The orders magic number is the same as the magic number used in this ea
          
               {
                OrderDelete(tnumber);                                                 // Delete it
               }
        }
     }
 

dazamate:
Ok thankyou Raptor, I just updated the code on my post. I just remembered how you guys mentioned it was no good to use bars for a counter so I changed it around to use bar time. Ill fix up the logic and see if it works.
"Bars is unreliable (once you reach max bars on chart it won't change and code breaks.) Volume[0]==1 is unreliable, if you miss a tick the code breaks. Always use time."

is true, but you need to look at the context of when this comment was made . . . Bars is unreliable . . .

int Bars 

Number of bars in the current chart

but that doesn't mean you can't use bar numbers instead of number of hours. For example how will your code cope on Friday evening and on into Sunday ? does pendinglimit equal 4 hours or 4 bars ?

Also what happens with this if a trade is placed at 22:00 ? 22 + 4 = 26 ?

if(TimeHour(TimeCurrent()) > (bartraded + pendinglimit) && Tradeopen()==true)      // Check to see if pending orders have expired
 

Alright I see the problem there with the time strategy. But if we use bars as a counter once the chart reaches max bars it will fail there too, right?

I will get back to it and see what I can come up with...

 

Ok how about this approach


extern string  sComment4                = "Max Hours allowed before pending orders are deleted";
extern int     pendinglimit        = 4;
 
// * EVERYTIME A TRADE GETS TRIGGERED* 

bartraded = 0;


  if (Time0 == Time[0]) return; Time0 = Time[0];          // make sure each bar is only scanned once
       {   
//-----------------------------------------------------------------------------------------------------------------------------------------------
//TRADE COUNTER
   

   bartraded++;  // Each time a new bar opens add 1 to the counter

//------------------------------------------------------------------------------------------------------------------------------------------------
// DELETE PENDING ORDERS THAT HAVE NOT BEEN TRIGGERED WITHIN 'pendinglimit'

   if(bartraded > pendinglimit && Tradeopen()==true)      // Check to see if pending orders have expired
     {
        for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&               // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)))       // The orders magic number is the same as the magic number used in this ea
          
               {
                OrderDelete(tnumber);                                                 // Delete it
               }
        }
     }
 
dazamate:

Alright I see the problem there with the time strategy. But if we use bars as a counter once the chart reaches max bars it will fail there too, right?


Nope, the current H1 bar is always bar 0 . . in 60 mins the current H1 bar will be bar 0 . . the bars issue is if you use Bars, meaning the total number of bars on your chart, or if you are looking at the bars to the far left of your chart.

The bar number for TimeHour(TimeCurrent() is 0, you can get the bar number for the time when your trade was placed (you need to calculate this not store it ! ! !) using iBarShift then do something like this . . .

if (iBarShift(symbol, TF, Time_order_Placed)+1 >  pendinglimit) && Tradeopen()==true)  
 

OK 3rd time lucky, It took me a while to figure out what you were trying to say there. So here is what I have done. But the orders still don't get deleted >:(


static datetime tradeopened;


//Everytime a trade is opened I run
 tradeopened = TimeCurrent();


 if((iBarShift(hothand(),60 ,tradeopened)>pendinglimit) && Tradeopen()==true)          // Check to see if pending orders have expired
     {
        for(int tnumber = OrdersTotal()-1; tnumber >= 0 ; tnumber--)                  //scan through open orders
        {
          if (OrderSelect(tnumber, SELECT_BY_POS) &&   
             ((OrderType()==OP_BUYSTOP)|| (OrderType()== OP_SELLSTOP)) &&     // The order selected is either a pending buy on stop order or a buy on sell order
             ((OrderMagicNumber()== Mnumber1) || (OrderMagicNumber()==Mnumber3)))     // The orders magic number is the same as the magic number used in this ea
          
               {
                OrderDelete(tnumber);                                                 // Delete it
               }
        }
     }
 
Yeah just reading over your comment again and what I have done is wrong,
tradeopened = TimeCurrent()
will not give me the bar opening time would I have to use Time[0]?
 

Try this ;-)

OrderDelete(OrderTicket( ) );                    // Delete it