I know this an old and very rudimentary topic but something important...
So there are various ways to code an EA to trade once per bar.
One popular way is:
Another way which I've used is:
However, both of those ways seem to fire off trades not at bar open when the EA is re-compiled live or sometimes when the EA is being loaded live in an active market. Since the EA thinks it's the bar open because it's a new bar on start of the EA, if the algo is triggered the EA fires off a buy or sell. Sometimes that's very dangerous. A nerve wracking hassle at best to try to compensate and cover the trade blind.
I just thought of a filter that others may be using or have already tried, I googled the code but no exact hits so I'm putting it out here to see if others have used it or have thoughts regarding it. Of course I have briefly tested it on the tester and it works, but of course live demo and live real account server trading is a different story. I wanted to get some thoughts before I test it live.
The code is simple...
The logic behind this is that Time[0] is opening time of current or new bar. If TimeCurrent() is greater than Time[0], then the bar open has already passed and it won't fire off a trade, but if Time[0] equals TimeCurrent() that's exactly at the bar open. Or, in the case of the bar opening time has arrived, but the last tick received for TimeCurrent() is a few or several milliseconds behind and hasn't updated yet, but the bar has opened, so trade is on.
Any thoughts?
Trying to implement the most simple and most effective solution that will do the job without hiccups like trades being fired off when ea is recompiled.
Any reasons why if (Time[0] >= TimeCurrent()) wouldn't work?
So there are various ways to code an EA to trade once per bar.
One popular way is:
Inserted Code
int BarsCount = 0; if (Bars>BarsCount) { Do the algo and then BarsCount = Bars; }
Another way which I've used is:
Inserted Code
static datetime prevtime = 0; if(prevtime == Time[0]) return; prevtime = Time[0]; Do the algo here after the code
However, both of those ways seem to fire off trades not at bar open when the EA is re-compiled live or sometimes when the EA is being loaded live in an active market. Since the EA thinks it's the bar open because it's a new bar on start of the EA, if the algo is triggered the EA fires off a buy or sell. Sometimes that's very dangerous. A nerve wracking hassle at best to try to compensate and cover the trade blind.
I just thought of a filter that others may be using or have already tried, I googled the code but no exact hits so I'm putting it out here to see if others have used it or have thoughts regarding it. Of course I have briefly tested it on the tester and it works, but of course live demo and live real account server trading is a different story. I wanted to get some thoughts before I test it live.
The code is simple...
Inserted Code
if (Time[0] >= TimeCurrent()) { Do the algo here }
The logic behind this is that Time[0] is opening time of current or new bar. If TimeCurrent() is greater than Time[0], then the bar open has already passed and it won't fire off a trade, but if Time[0] equals TimeCurrent() that's exactly at the bar open. Or, in the case of the bar opening time has arrived, but the last tick received for TimeCurrent() is a few or several milliseconds behind and hasn't updated yet, but the bar has opened, so trade is on.
Any thoughts?
Trying to implement the most simple and most effective solution that will do the job without hiccups like trades being fired off when ea is recompiled.
Any reasons why if (Time[0] >= TimeCurrent()) wouldn't work?