Why do you alert "Buy" when sending a sell order and v-v.?
Direction initiates as 0, so no matter whichever MA is above the other, the first order will be a sell.
If the next cross is 5 moving below 18 MA, the EA will try to close the sell order at Bid, which will fail.
You need to add some error handling code.
Also, if you have a pending order open, openordernumber may not get updated.
prevDirection = direction; direction = 1; if (prevDirection != direction) { Alert("CLOSE SELL!!"); OrderClose(openOrderNumber, openOrderLots, Ask, 3); //If this fails for whatever reason, direction has already been set to // 1, so this will not be called again until there has been 2 more // crosses }
Thanks GumRai! Your reasons explain exactly the issues I was seeing!
WHRoeder, have no idea why you linked that, but thanks... I think?
Thanks GumRai! Your reasons explain exactly the issues I was seeing!
WHRoeder, have no idea why you linked that, but thanks... I think?
Ah, sorry. Thanks Raptor, makes sense. That sample above was not my full code, I just pulled out a sample from what would show the issue with the make order, close order. Sorry, should have been clearer. I do have a close order function that if errors tries again taken form these forums, I think it was actually you or WHRoeder who I stole it off :)
You need to check all of your trading functions, not just OrderSend() . . . OrderSelect(), OrderModify(), OrderClose(), OrderDelete(), etc, etc . . . and then don't skimp on what you Print when there is an error, the error number is not enough, you need to Print everything you need to be able to go back and forensically figure out why your function failed.
You may think this is overkill but when trading live or Demo you can't go back in time and replay the exact scenario that caused the failure . . . not even with the Strategy Tester, so you need to gather the pertinent information in real time.
- 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,
I've done an EA that is supposed to open a trade when the MA's are a certain distance apart and then close the order when they cross.
But the EA misses some crosses and closes at a later cross, sometimes even missing 2 crosses and closing on the third cross.