OrderSend keeps opening orders and ignoring conditional

 

Hi there,

I wondered whether you could help me with the 'code' below. I can't get round my head why on every 'tick' the OrderSend is executed repeatedly.

The Algorithm (see below) explained is:

L1 - Asks the question whether the price is within a pre-determined Level

L2 - Asks the question whether the Current Bar Opening Time is greater than the 'Order Opening Time'. Because there is no order sent in the first tick, the condition is true and moves on to sending the order.

L4 - Sets the current time (or in other words the OpenOrderTime of the order, I know I could have used that predetermined variable but the result was the same.)

so..., when the second tick comes along and the price it is still within the predefined levels

 L2  - this condition should be false since the OpenOrderTime (UBand01_OOT ) had been set the time when the order was placed, which means that Time[0] is greater and therefore the condition is 'false'. This should mean that the program must ignore the operators within the condition and move on to the next 'if condition', but instead it sends the order through again, as if it doesn't care whether the condition is 'true' or 'false'. 

Algorithm: 

L1      if((Close[0]>Cls0_ULevelValue_01) && (Close[0]<Cls0_ULevelValue_02))     // Tests whether the Close[0] is within pre-determined level 1 and 2

L2         if((Time[0]>UBand01_OOT) && (TIME0vsTIME1_Drctn == 1))                 // It tests whether there has already an order placed after the Current Bar Opening Time.
            {
L3             UBand01_OrdSend = OrderSend(Symbol(),OP_SELL,SzSell_01,Bid,0,0,0,NULL,00,0,clrRed);
L4             UBand01_OOT = TimeCurrent();
            }
    //---------------------------------------------------------------------------------------------  
      if((Close[0]>Cls0_ULevelValue_02) && (Close[0]<Cls0_ULevelValue_03))    // Tests whether the Close[0] is within pre-determined level 2 and 3
         if((Time[0]>UBand02_OOT) && (TIME0vsTIME1_Drctn == 1))
            {
             UBand02_OrdSend = OrderSend(Symbol(),OP_SELL,SzSell_02,Bid,0,0,0,NULL,00,0,clrRed);
             UBand02_OOT = TimeCurrent();
            }
 
It's an endless loop of opening orders.

TimeCurrent() does not change that fast.

You can add a Sleep(XXXX) but i advise you to take a different approach.

 

Is the UBand01_OOT variable static or globally declared?

static datetime UBand01_OOT;

L1      if((Close[0]>Cls0_ULevelValue_01) && (Close[0]<Cls0_ULevelValue_02))     // Tests whether the Close[0] is within pre-determined level 1 and 2

L2         if((Time[0]>UBand01_OOT) && (TIME0vsTIME1_Drctn == 1))                 // It tests whether there has already an order placed after the Current Bar Opening Time.

            {

L3             UBand01_OrdSend = OrderSend(Symbol(),OP_SELL,SzSell_01,Bid,0,0,0,NULL,00,0,clrRed);

L4             UBand01_OOT = Time[0];

            }
 
Marco vd Heijden:
It's an endless loop of opening orders.

TimeCurrent() does not change that fast.

You can add a Sleep(XXXX) but i advise you to take a different approach.

First of all thank you for taking the time to look into it.

I'm very new to coding so forgive my ignorance.

As far as I understand TimeCurrent() changes every second (or millisecond, I'm not too sure) as I have tested it. This time in my example I am using OperOrderTime().

The new value given to UBand02_OOT should stop the loop from happening, right? as I explained in the image below.

Files:
EA.jpg  96 kb
 

TimeCurrent()

Returns the last known server time,

So you send an order to the server and practically at the same time you ask it's time.

These two are too close to one another to be used in a filter like that.

Can use

iTime()

From M1 to have it changed every minute, or you can incorporate a counter that counts a few numbers back to increase the gap.


 
pipPod:

Is the UBand01_OOT variable static or globally declared?

Hi pipPod, the varaible is datetime declared within the OnTick{} block (so I guess local) and is not static. I am a bit confused about the 'static' in the fact that..., does it Scenario A. - retain the value assigned to it during the 'lifetime' of the EA, in other words until removed from the chart?, or does it Scenario B. - retain the value all the way through the whole code until it reaches the bottom and then can get a new value once a new tick comes along?

 

I will try your suggestion changing the code to static, but the reason I asked the question above is because I need this variable ready to get a new value once the actual candle it is working on completes and a new 'Bar' opens, opening the readiness of the variable to get a new value. 

 
Marco vd Heijden:

TimeCurrent()

Returns the last known server time,

So you send an order to the server and practically at the same time you ask it's time.

These two are too close to one another to be used in a filter like that.

Can use

From M1 to have it changed every minute, or you can incorporate a counter that counts a few numbers to increase the gap.


Are you suggesting I use iTime instead of TimeCurrent()? Wouldn't that be the same value of Time[0]. I tested: iTime(Symbol(),0,0) against Time[0] and the result value is exactly the same. On the other hand OpenOrderTime surely the value would be higher than Time[0], so why this condition still don't work? Again forgive my ignorance
 
DavidMQL4:
Are you suggesting I use iTime instead of TimeCurrent()? Wouldn't that be the same value of Time[0]. I tested: iTime(Symbol(),0,0) against Time[0] and the result value is exactly the same. On the other hand OpenOrderTime surely the value would be higher than Time[0], so why this condition still don't work? Again forgive my ignorance
Yes but it differers from the last known servertime and the bar from M1 changes every minute so there is no way you can run into the same value.
 
Marco vd Heijden:
Yes but it differers from the last known servertime and the bar from M1 changes every minute so there is no way you can run into the same value.

You are sending an order to the server at the same time you ask it's servertime by calling TImeCurrent().

These two requests arrive at the server at basically the same time differeing mere millisecond and are too close to be used as an isolator.

 
Marco vd Heijden:

You are sending an order to the server at the same time you ask it's servertime by calling TImeCurrent().

These two requests arrive at the server at basically the same time differeing mere millisecond and are too close to be used as an isolator.

All right. If I request the OpenOrderTime() of a SendOrder(), certainly this time value would always be higher than Time[0] right?, or am I wrong about that?

So given this principal is correct, it would mean that in the following code, 'Lband20_OOT' datetime value is greater than Time[0] and therefore the condition is false, stopping the OrderSend(), until the next Bar opening. Right or wrong? If wrong could you tell me why OpenOrderTime would not be greater than Time[0] for the condition to become 'false'?

      if((Close[0]<Cls0_LLevelValue_20) && (Close[0]>Cls0_LLevelValue_21))
         {
          if(LBand20_OOT<Time[0])
             {
               if(TIME0vsTIME1_Drctn == -1)
                  {
                   LBand20_OrdSend = OrderSend(Symbol(),OP_BUY,SzBuy_20,Ask,0,0,0,NULL,00,0,clrGreen);
                   OrderSelect(LBand20_OrdSend,SELECT_BY_POS,MODE_TRADES);
                   LBand20_OOT = OrderOpenTime();
                  }
             }
         }
 
DavidMQL4:

All right. If I request the OpenOrderTime() of a SendOrder(), certainly this time value would always be higher than Time[0] right?, or am I wrong about that?

So given this principal is correct, it would mean that in the following code, 'Lband20_OOT' datetime value is greater than Time[0] and therefore the condition is false, stopping the OrderSend(), until the next Bar opening. Right or wrong? If wrong could you tell me why OpenOrderTime would not be greater than Time[0] for the condition to become 'false'?

datetime time_M1;

if((Close[0]<Cls0_LLevelValue_20) && (Close[0]>Cls0_LLevelValue_21))
  {
   if(LBand20_OOT<Time[0])
     {
      if(TIME0vsTIME1_Drctn==-1)
        {
         if(time_M1!=iTime(Symbol(),PERIOD_M1))
           {
            LBand20_OrdSend=OrderSend(Symbol(),OP_BUY,SzBuy_20,Ask,0,0,0,NULL,00,0,clrGreen);
            OrderSelect(LBand20_OrdSend,SELECT_BY_POS,MODE_TRADES);
            LBand20_OOT=OrderOpenTime();
            time_M1=iTime(Symbol(),PERIOD_M1);
           }
        }
     }
  }
//+------------------------------------------------------------------+

I mean a simple 1 Minute delay between orders.

If you need less then use a simple counter.

Reason: