Hello,
The outcome of the following code is exactly the same as if “Close” was 0 in the “ONCE” instructions, and all the remaining code did not exist.
GRAPHing my variables produces flat lines whereas GRAPHing the formulas used to calculate them produces correct graphs.
It looks like assignations don’t work.
What am I missing here?
(It’s a very simple ProBackTest system based on 2 trailing stops, one to open a long position and another one to close it.)
Thanks for your help!
Stephane
// Initialisation
ONCE StopLossBuy = PriceBaseStage * ROUND(Close / PriceBaseStage) + BuyLevel
ONCE RaiseConfirmationPrice = PriceBaseStage * ROUND(Close / PriceBaseStage) + RaiseConfirmationLevel
ONCE StopLossSell = PriceBaseStage * (ROUND(Close / PriceBaseStage) - 1) + SellLevel
ONCE DropConfirmationPrice = PriceBaseStage * (ROUND(Close / PriceBaseStage) - 1) + DropConfirmationLevel
ONCE CapitalInitial = 10000
//////////////////////////////////////////////////////////////////////////////////////////
// VARIABLES
//////////////////////////////////////////////////////////////////////////////////////////
//PriceBaseStage [1000],
//DropConfirmationLevel [700;900][50]
//BuyLevel [100;300][50]
//RaiseConfirmationLevel[700;900][50]
//SellLevel [100;300][50]
GRAPH StopLossBuy
GRAPH RaiseConfirmationPrice
GRAPH StopLossSell
GRAPH DropConfirmationPrice
//////////////////////////////////////////////////////////////////////////////////////////
// CALCUL DES STOP LOSS
//////////////////////////////////////////////////////////////////////////////////////////
// Suivre la baisse
StopLossBuy = StopLossBuy[1]
DropConfirmationPrice = DropConfirmationPrice[1]
IF (Low <= DropConfirmationPrice) THEN
// La baisse est confirmée, on diminue le stop loss d'achat et la prochaine confirmation de baisse
StopLossBuy = StopLossBuy - PriceBaseStage
DropConfirmationPrice = DropConfirmationPrice - PriceBaseStage
ENDIF
// Suivre la hausse
StopLossSell = StopLossSell[1]
RaiseConfirmationPrice = RaiseConfirmationPrice[1]
IF (High >= RaiseConfirmationPrice) THEN
// La hausse est confirmée, on augmente le stop loss de vente et la prochaine confirmation de hausse
StopLossSell = StopLossSell + PriceBaseStage
RaiseConfirmationPrice = RaiseConfirmationPrice + PriceBaseStage
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////
// CALCUL DES SIGNAUX
//////////////////////////////////////////////////////////////////////////////////////////
// Condition pour ouvrir une position longue
ConditionAchat = (High >= StopLossBuy)
// Condition pour clôturer une position longue ouverte
ConditionVente = (Low <= StopLossSell)
//////////////////////////////////////////////////////////////////////////////////////////
// POSITIONS
//////////////////////////////////////////////////////////////////////////////////////////
// Conditions pour ouvrir une position acheteuse
IF NOT LongOnMarket AND ConditionAchat THEN
Quantite = Round(0.9 * (CapitalInitial + StrategyProfit) / Close)
BUY Quantite CONTRACTS AT MARKET
ENDIF
// Conditions pour fermer une position acheteuse
If LongOnMarket AND ConditionVente THEN
SELL AT MARKET
ENDIF
Please do not open multiple topics with the same question.
First idea that come to mind:
- at first run, PriceBaseStage is equal to 0 and a division by zero error has the effect of stopping future evaluations of the variables
- try to limit the preloaded bars with a defparam preloadbars=0 , to exclude calculation before the real start date of the backtest
Thank you Nicolas.
PriceBaseStage is a variable which is set to 1000, so it is never equal to 0.
Unfortunately, DEFPARAM PreLoadBars = 0 doesn’t change anything.
I was suggested by Fifi743 to use an indicator instead of doing the calculations in the system. It generally helps.
However here, it doesn’t improve the outcome.
Looks like the ONCE calculations are made out of any bars (hence “Close” seemingly equal to 0).
Has anyone experienced this phenomenom? I’m pretty sure there’s an obvious fix or workaround…
Thanks!
Could you please give me a example with fixed settings (with no optimization) applied to a specific instrument on a precise date/time? Sorry but i’m a bit a confused on when/where to test it as it should.
Finally got the solution from Noobywan on the French forum.
Instead of using ONCE, rely on testing BARINDEX.
Thanks Noobywan!
IF BARINDEX = 1
(Initializations)
END IF
IF BARINDEX > 1
(Regular processing)
ENDIF