How to close trade on moving average crossover? Or moving average direction change?
-
Hi,
I've read similar posts but I'm not able to get it working. How do I close an open trade if the moving averages crossover? Or, if a moving average changes direction?
I've got 'for each trade' (buy), condition MA crossover, close buy trade, and the reciprocal for sell trades.
In backtest I'm getting journal errors: Invalid price parameter for trade function (4107)
Here is my code:
http://fxdreema.com/shared/zNXAextCAny help would be appreciated, thanks!
-
Effectively this is doing what you need, but you don't need the pink for each trade, these blocks work as part of 2, dark pink, then light pink, or a light pink inside another block. I would set up a small test project doing 1 thing, when working, I would add to my main project.
-
Yeah, thanks jstap.
I tried everything! I tried removing the pink blocks 'for each trade'. I tried using blocks 'if trade', still no success. And I tried just having the condition, Then linked the close trade command. I'm still getting the same errors in the backtester. For some reason it just doesn't like the MA crossover condition for closing the trade.
Can you please suggest any other option to get this working?
-
Try this, set up a cross to open a trade, then set up a cross to close the trade, nothing more, run this on back test, and watch what happens, I don't use this, but I have and worked for me,
-
No, jstap,
Sorry to say it but I created this test EA, very simple. I still have the same errors:
https://fxdreema.com/shared/1GK404ATc

I would highly appreciate anyone that can solve this issue. And why in the error log, so many decimal places for the EURJPY? Also I notice when I use default project rule settings, it doesn't work properly with my broker on YEN pairs. I have to modify the rules to ?JPY = 0.01 every time.
@fxDreema your help is much appreciated, thankyou -
You didn't set buy and sell in close: this works: https://fxdreema.com/shared/OC9QWN4bd
MA cross test.ex4 -
Thanks jstap, yes I fixed them but forgot to save.
No I'm still getting constant errors here:
https://fxdreema.com/shared/CB9VJjOJbIt must only be happening with my broker then? Is that possible? It's a very frustrating problem. Strange because it recognises the command to open a trade but all sorts of problems closing it using the exact same logic
-
If you share the mql4 I can probably fix it for you, or someone will find it easier to fix than just going by the project blocks.
-
Thanks Ipod, I think the problem is with my data. I think there are inconsistencies. I've had to change the code entirely to remove that issue. The TP worked as as virtual one when certain conditions were met. Now I've just changed it to a fixed TP. Once my research is done I'll try to simulate the issue and get back to you
-
Sounds odd though, defo something going on with the logic you have created.