Execute trade on new open candle

 

Hello,

I have written my first trading bot and have almost all logic in place but struggling with how to execute the trade. 

I have all the logic in place for previous candle[1] and as soon as next candle[0] opens I want to place trade if condition on candle[1] is met.

The doc does not seem to have an onOpen trade function or similiar.

Anybody know how this can be done or point me in the right direction?


Thanks

 

".. point me in the right direction?" Yes, it's common problem, so search for "new Candle":

  1. Documentation ~13 papers,
  2. Articles ~120 papers
  3. CodeBase ~110 solutions
  4. Forum  ~4800 posts
 
thanks!
 
//+------------------------------------------------------------------+
//| Checking for the new bar                                         |
//+------------------------------------------------------------------+
bool CheckNewBar()
  {
//--- Variable for storing the opening time of the current bar
   static datetime new_bar = NULL;
//--- Array for getting the opening time of the current bar
   static datetime time_last_bar[1] = {0};
//--- Get the opening time of the current bar
//    If an error occurred when getting the time, print the relevant message
   if(CopyTime(_Symbol,PERIOD_CURRENT, 0, 1, time_last_bar) == -1)
     {
      Print(__FUNCTION__, ": Error copying the opening time of the bar: " + IntegerToString(GetLastError()) + "");
     }
//--- If this is a first function call
   if(new_bar == NULL)
     {
      // Set the time
      new_bar = time_last_bar[0];
      return(false); // Return false and exit
     }
//--- If the time is different
   if(new_bar != time_last_bar[0])
     {
      new_bar = time_last_bar[0]; // Set the time and exit
      return(true); // Store the time and return true
     }
//--- If we have reached this line, then the bar is not new, return false
   return(false);
  }
 

I guess you don't need to do anything. It opens new position when bar[1] is closed by default. Because you don't have a bar[1] until bar[1] is closed.


Edit: I mean bar[0] closes and then it becomes the new bar[1]. There's no low, high and close value in bar[0] until it closes even if it seems to have already. Because you don't know what will happen in the last few seconds before bar[0] closes. I tried to access Open[0], Low[0] before in mql4 and it always return 0(or no value) to me.

 
William William #:
I guess you don't need to do anything. It opens new position when bar[1] is closed by default. Because you don't have a bar[1] until bar[1] is closed.

????

When bar[0] closes it becomes the new bar[1].

 
Keith Watford #:

????

When bar[0] closes it becomes the new bar[1].

yes, that's what i mean. 

 
William William #:

I guess you don't need to do anything. It opens new position when bar[1] is closed by default. Because you don't have a bar[1] until bar[1] is closed.


Edit: I mean bar[0] closes and then it becomes the new bar[1]. There's no low, high and close value in bar[0] until it closes even if it seems to have already. Because you don't know what will happen in the last few seconds before bar[0] closes. I tried to access Open[0], Low[0] before in mql4 and it always return 0(or no value) to me.

You added this after my post.

Bar[0] always has an open, close, high and low.