Hello everyone, I am coding my first expert advisor, and I have encountered some issues with mine. The first one is that I can't code the EA to close the pending orders or open positions when a new bar appears. The EA is coded to open place one pending order per bar and when the bar close I want it to cancel the pending order of the bar or the open position of the bar at the same time, without affecting the new pending order that will be placed on the bar[0]. Here's the code
Please i'm a newbie don't mind about the other errors lol.
- Delay Between Orders - Expert Advisor
- Select the low or high of previous pending order's bar!!!!
- [ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3.
the logic to make it work: If the current time minus the position time is greater or equal to the amount of seconds in bar - perform task.
items to use:
TimeCurrent();
PositionGetInteger(POSITION_TIME);
PeriodSeconds();
Conor Mcnamara #:
the logic to make it work: If the current time minus the position time is greater or equal to the amount of seconds in bar - perform task.
Sorry I still don't get it, because sometimes, when I run the EA on the 1 minute time frame, the order can be triggered at 40 seconds of the current candle, thus when the current candle is closed, the duration of the order is less then 60 seconds, but for me, I just want it to close not after a predetermined amount of seconds, but only when a new bar shows up. Also, thank you for replying me sir.
the logic to make it work: If the current time minus the position time is greater or equal to the amount of seconds in bar - perform task.
items to use:
TimeCurrent();
PositionGetInteger(POSITION_TIME);
PeriodSeconds();
Well then you could get the actual bar open time instead of the position open time using CopyTime or else iTime with iBarShift. I made a function before that does that. Another approach you could take if you only need the 1 minute timeframe:
if (TimeCurrent() % 60==0){ // do task }
simple working example with CopyTime
datetime lastBarTime = 0; datetime time_buf[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { ArrayResize(time_buf, 1); ArraySetAsSeries(time_buf, true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick(){ if (IsNewBarFormed()){ Print("New bar opened at ", TimeToString(lastBarTime, TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } } //+------------------------------------------------------------------+ datetime GetCurrentBarTime() { CopyTime(_Symbol, _Period, 0, 1, time_buf); return time_buf[0]; } bool IsNewBarFormed() { datetime currentBarTime = GetCurrentBarTime(); if (currentBarTime > lastBarTime){ lastBarTime = currentBarTime; return true; } return false; } //+------------------------------------------------------------------+

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register