Hi the EA needs to identify if a trade is currently running or not, im using CheckOpenOrders routine for this but when it is called in code its ignored. can anyone tell me what im doing wrong?
so the idea is that when each bar opens it checks if trade is open or not then it opens one if there is none, however it doesnt work as intended.... pls help!!!!!!!!!!!!
Please try this :)
datetime previous_bar; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- previous_bar=Time[0]; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(IsNewCandle(Time[0])) { //--your code here when get new candle. } else { //--your code here for otherwise. } } //+------------------------------------------------------------------+ bool IsNewCandle(datetime current_candle) { if(Time[0]!=previous_bar) { previous_bar=current_candle; return(true); //-- send you new candle } else return(false); }//--IsNewCandle
Please try this :)
int OnInit() { //--- previous_bar=Time[0]; //--- return(INIT_SUCCEEDED); }
You should avoid using anything like Time[] in OnInit as it may not be loaded/updated yet.
You should avoid using anything like Time[] in OnInit as it may not be loaded/updated yet.
yes, you're right :)
variable name is "previous" then I should write it as previous bar at [1],
I got mistake by [0]
thank you :)
datetime previous_bar; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- previous_bar=Time[1]; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(IsNewCandle(Time[0])) { //--your code here when get new candle. } else { //--your code here for otherwise. } } //+------------------------------------------------------------------+ bool IsNewCandle(datetime current_candle) { if(Time[0]!=previous_bar) { previous_bar=current_candle; return(true); //-- send you new candle } else return(false); }//--IsNewCandle
yes, you're right :)
variable name is "previous" then I should write it as previous bar at [1],
I got mistake by [0]
thank you :)
int OnInit() { //--- previous_bar=Time[1]; //--- return(INIT_SUCCEEDED); }
You should avoid using anything like Time[], Open[], Close[] etc in OnInit as it may not be loaded/updated yet.
You should avoid using anything like Time[], Open[], Close[] etc in OnInit as it may not be loaded/updated yet.
oh ... okay, I will do as you said.
thanks for your advise :)
Btw. function at IsNewCandle() still works, right?
because of initial value in previous_bar is different with current candle in OnTick()
bool IsNewCandle(datetime current_candle) { if(Time[0]!=previous_bar) //-- will not equal when new candle to come { previous_bar=current_candle; //-- initial new time return(true); //-- send you new candle } else return(false); }//--IsNewCandle
Btw. function at IsNewCandle() still works, right?
because of initial value in previous_bar is different with current candle in OnTick()
It depends whether you want to wait for a new bar before doing anything.
If you assign a value to previous_bar in OnInit, then it may be given the wrong value. In this case your code will "detect" a new bar when there isn't actually a new bar, but Time[0] has just updated.
I use a Global variable FirstTick and do all my checks in OnTick when FirstTick is true. When checks are completed I set FirstTick to false.
It depends whether you want to wait for a new bar before doing anything.
If you assign a value to previous_bar in OnInit, then it may be given the wrong value. In this case your code will "detect" a new bar when there isn't actually a new bar, but Time[0] has just updated.
I use a Global variable FirstTick and do all my checks in OnTick when FirstTick is true. When checks are completed I set FirstTick to false.
Yes, you're right,
I just lucky my function is works even I have a mistake at OnInit :))
I thought with easy logical that initial time at variable previous_bar should be different with time at current/running candle to come.
I just lucky :)
thanks
Yes, you're right,
I just lucky my function is works even I have a mistake at OnInit :))
I thought with easy logical that initial time at variable previous_bar should be different with time at current/running candle to come.
I just lucky :)
thanks
hi, thanks both for your help so far, i am also struggling to understand why the code when a trade is open and conditions for new trade are ok it closes the current trade and opens a new one on new bar?????
I want it to hold the trade until the the trailing stop is reached........any further help will be appreciated.
Thanks.
It depends whether you want to wait for a new bar before doing anything.
If you assign a value to previous_bar in OnInit, then it may be given the wrong value. In this case your code will "detect" a new bar when there isn't actually a new bar, but Time[0] has just updated.
I use a Global variable FirstTick and do all my checks in OnTick when FirstTick is true. When checks are completed I set FirstTick to false.
hi, thanks for your help so far, i am also struggling to understand why the code when a trade is open and conditions for new trade are ok it closes the current trade and opens a new one on new bar?????
I want it to hold the trade until the the trailing stop is reached........any further help will be appreciated.
Thanks.
hi, thanks both for your help so far, i am also struggling to understand why the code when a trade is open and conditions for new trade are ok it closes the current trade and opens a new one on new bar?????
I want it to hold the trade until the the trailing stop is reached........any further help will be appreciated.
Thanks.
hi, what is that meant your requirement to check on each new candle was solved by my code above?
I tried here, and it works.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi the EA needs to identify if a trade is currently running or not, im using CheckOpenOrders routine for this but when it is called in code its ignored. can anyone tell me what im doing wrong?
so the idea is that when each bar opens it checks if trade is open or not then it opens one if there is none, however it doesnt work as intended.... pls help!!!!!!!!!!!!