Happy New Year everyone. I was hoping someone can clear my illogical thinking please.
I have a simple algorithm that works as an indicator as expected but when implemented as a backtest it
flags the buy and sell a day later than it should. Its probably my coding or I’ve made an incorrect assumption
but the last thing I want is it flagging a buy when its a sell and visa versa. Also, I dont want to have errors as I refine
from here even on a simple piece of code going forward.
The Indicator code is:
indicator1 = CCI[20]
once lastpoint = 0
c1 = (indicator1 crosses over -100)
c2 = (indicator1 crosses under 100)
c3 = (indicator1 crosses under -100)
c4 = (indicator1 crosses over 100)
L0 = (lastpoint = 0)
L1 = (lastpoint = 1)
Lneg1 = (lastpoint = -1)
IF (c1 AND L0) OR (c1 AND Lneg1) OR (c4 AND Lneg1) THEN
lastpoint = 1
ElSIF (c2 AND L0) OR (c2 AND L1) OR (c3 AND L1)THEN
//SELL AT MARKET
lastpoint = -1
ENDIF
return lastpoint,0
The Backtest code is:
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
once lastpoint = 0
// Conditions to enter long positions
indicator1 = CCI[20]
c1 = (indicator1 crosses over -100)
c2 = (indicator1 crosses under 100)
c3 = (indicator1 crosses under -100)
c4 = (indicator1 crosses over 100)
L0 = (lastpoint = 0)
L1 = (lastpoint = 1)
Lneg1 = (lastpoint = -1)
IF ((c1 AND L0) OR (c1 AND Lneg1) OR (c4 AND Lneg1))THEN
BUY 5000 CASH AT MARKET
lastpoint = 1
// Conditions to exit long positions
ELSIF (c2 AND L0) OR (c2 AND L1) OR (c3 AND L1) THEN
SELL AT MARKET
lastpoint = -1
ENDIF
Nothing too tricky really. Its based on the visual in the attached file and the behaviour can be seen
in the chart dump in the other file.
[attachment file=”56989″]
The backtest clearly lags the indicator and the actual indicator by a day.
Thanks for any assistance you can provide,
John
[attachment file=”56990″]
Code is read at Close and actions are made at next Open, that’s the behavior of ProBacktest/ProOrder and the reason you think you have such differences between indicators and automatic trading programs.
Thank you Nicolas for your answer and prompt response. I think I understand now.
So I shouldnt use the backtest for identifying buy/sell signals for the next day? I should rely on screeners and indicators?
What about for automated trading, does the signal fire on the indicator? Its a little confusing.
Regards
John
Line 9 of the indicator code will only be true once, when the indicator starts, later it will always be either 1 or -1.
Is that what you want?
Hi roberto,
Yes, this is what I wanted to do originally. 0 is the initial state only. I wanted the transition of state to remain until it changes, so I could see whether I remain in or out without having to hunt back.
I’ve done two versions now, and see advantages both ways.
Thanks for pointing it out,
John