Hi everyone,
I am just starting out learning how to code with very simple scripts. The code below should trigger a Long Order when the MA20 crosses above the MA50 and sell when the MA20 goes below the MA50. However when backtesting there are a lot of orders that where triggered that shouldn’t have been. Am I doing something obviously wrong?
// Conditions to enter long positions// Conditions to enter long positions
IF NOT LongOnMarket AND Average[20](close) CROSSES OVER Average[50](close) THEN
BUY 20 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket AND Average[20](close) CROSSES UNDER Average[50](close)THEN
SELL AT MARKET
ENDIF
See attached image for trade that should not have been triggered.
Any pointers really appreciated. Also I did not find a search on the forum, I am sure this was asked before. If there is a search please let me know.
Your code shows SMA and your image shows EMA.
oh.. thanks. Fixed it and now seems to be working fine.