Regarding allowing EA to send another order only after a new bar/candle

 

Hi, Currently I am usually the following code to limit to opening a single order

if(OrdersTotal() == 0)

I have also tried below, following a youtube guide

   bool NewCandle()
   {
      static datetime Candle;
      if(Time[0] == Candle)
      {
         return false;
      }
      else(Candle = Time[0]);
      {
         return true;
      }
   }

But none of the following was able to successfully order the EA to send order after the current bar has closed. (i.e., if the condition meets and EA sends an order on current bar, I want EA to stop sending any further orders until the next bar).

So, would it be possible to write a code in such a way that the EA will send another order only after a new bar/candle?

I am still quite new to the coding, so any help would be much appreciated, thank you so much.

 
Show the code that places the trades
 
Keith Watford:
Show the code that places the trades

Currently, it is just

if(OrdersTotal() == 0)
        {
        criteria here and send order
        }

I also tried, but this does not work as it checks only once per candle (i assume?)

void OnTick()  
{    
        if(NewCandle())
        {
                Criteria and Send Orders here
        }
}

bool NewCandle()
   {
      static datetime Candle;
      if(Time[0] == Candle)
      {
         return false;
      }
      else(Candle = Time[0]);
      {
         return true;
      }

However, I want to write a code so that once I send order in that candle (even if it is on the last tick of the candle), the EA will not send another order unless next candle opens.


Also, thank you @Keith Watford, for taking your time.

 
  1. if(OrdersTotal() == 0)

    That limits only one open order. PERIOD.

  2. if(NewCandle()){ Criteria and Send Orders here }
    That limits only opening at the start of a new candle.

  3. hyunjeshin: I want to write a code so that once I send order in that candle (even if it is on the last tick of the candle), the EA will not send another order unless next candle opens.
    So do that. When in doubt, THINK. That has nothing to do with new candle or no open orders. So delete those tests.
  4. Remember the candle time when the last order was sent. Check it against the current candle time before sending another order in the same candle.


 
William Roeder:
  1. That limits only one open order. PERIOD.

  2. That limits only opening at the start of a new candle.

  3. So do that. When in doubt, THINK. That has nothing to do with new candle or no open orders. So delete those tests.
  4. Remember the candle time when the last order was sent. Check it against the current candle time before sending another order in the same candle.


I have been thinking for past 6 hours, and I still stuck.

Can i have some guidance or tip to start going in the right direction (such as: could use x function or etc..)

Would really appreciate it, thank you.

 
I have tried 
   if((OrdersTotal() == 0) || (IsThisBarTradedYet() == true));
        {
        x
        }

   bool  IsThisBarTradedYet()
   {
      if((Bars == BarsOnChart) && (OrdersTotal() == 1))
      {
         return(true);
      }
      else
      {
         BarsOnChart = Bars;
         return(false);
      }
   }

I thought that if theres no open order, it will trigger via OrdersTotal() == o route, and if there is an open order of only 1, it will trigger via the IsThisBarTradedYet Function. However, when i back tested it, it seemed to open quite a few positions.

Any suggestions? @William Roeder or anyone?

 
hyunjeshin:

I have been thinking for past 6 hours, and I still stuck.

Can i have some guidance or tip to start going in the right direction (such as: could use x function or etc..)

Would really appreciate it, thank you.

check your message, sent some example
 
hyunjeshin: Any suggestions? @William Roeder or anyone?
  1. Why did you post something about bars? Where was that mentioned?

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

  2. What part of "nothing to do with new candle or no open orders. So delete those tests," was unclear? Why are you still testing that?

  3. What part of "Remember the candle time when the last order was sent" was unclear? Where did you code that?
 
William Roeder:
  1. Why did you post something about bars? Where was that mentioned?
  2. What part of "nothing to do with new candle or no open orders. So delete those tests," was unclear? Why are you still testing that?

  3. What part of "Remember the candle time when the last order was sent" was unclear? Where did you code th

About your suggestion, i could not figure it out, so that is why i asked for a guidance bro.

Anyways, taking your advice, I fixed it with 

   
if(CurrentBars != Bars)
{
        Functions
        CurrentBars = Bars
}

Do you think it will have any problem, as it is currently working. 

 
Reason: