ProRealCode - Trading & Coding with ProRealTime™
Dear all,
I’ve been trying for long to figure out why my code gets divison by zero error. Usually i can’t even start the code without that it goes 1 minute and it shuts down with division by zero error. But on some occasions, dont ask me why, it can start and run, but if i stop and try to run it again it wont work. I’ve narrowed it down to my position sizing calculation, since if i remove this it works fine. But i can figure out how to change the code to have the same position sizing code without causing divison by zero error.
The thought is to not risk more that 0,5% of the Equity by setting a stop in relation to ATR on entry.
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995
MinPositionSize = 1
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
Timeframe (5 minutes)
IF NOT ONMARKET THEN
IF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THEN
MyStop = AverageTrueRange[17](close[3])*ATRMult
BUY Positionsize CONTRACTS AT MARKET
endif
endif
IF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THEN
MyStop = AverageTrueRange[17](close[3])*ATRMult
SELLSHORT Positionsize CONTRACTS AT MARKET
endif
I would be happy with suggestions that i could try,
Best regards Anders
Append these lines to visually detect any cause:
graph ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
graph (Equity-(Equity*MyPercRisk))
graph (MinPositionSize*WCS)
graph WCS //this line could retain 0
Thanks Roberto, i atleast can see that it is due to something in the start. When i change the backtest size it always starts with 0 as you can see in my example pics. I do not know how to solve it though.. 🙂
Tips?
Br Anders
What value should MyStop have?
Ciao,
It should be based on AverageTrueRange, but it seems like it cant calculate the ATR directly when i start the system? Below you can see what MyStop should be, i have it embedded in the buying section to avoid that it recalculates on every bar, because i want the initial ATR based upon the entry candle.
IF NOT ONMARKET THEN
IF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THEN
MyStop = AverageTrueRange[17](close[3])*ATRMult
BUY Positionsize CONTRACTS AT MARKET
endif
endif
Br Anders
Where can I find variables Z1, Z2, Z3, Z4, Z5, Z6, Z7, Z8, Z9 and Z10?
I think it would be easier if you posted the running code so that I can test it.
Defparam cumulateorders = False
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98
MinPositionSize = 1//Minsta size
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
ATRMult = 5
SLMult = 1 //1 = ingen multiplier, starta alltid med 1, försämrad risk/reward vid multiplier
MyProfit = 0 //0 är standard
MyStartTrade = 090000
MyEndTrade = 173000
DaytoFilter = 0
Timeframe (Default)
Z1H = 2204
Z1L = 2191
Z2H = 2250
Z2L = 2245
Z3H = 2284
Z3L = 2277
Z4H = 2308
Z4L = 2300
Z5H = 2362
Z5L = 2356
Z6H = 2413
Z6L = 2404
Z7H = 2383
Z7L = 2376
Z8H = 4620
Z8L = 4615
Z9H = 4665
Z9L = 4670
Z10H = 4698
Z10L = 4704
Z1 = Close > Z1L and Close < Z1H
Z2 = Close > Z2L and Close < Z2H
Z3 = Close > Z3L and Close < Z3H
Z4 = Close > Z4L and Close < Z4H
Z5 = Close > Z5L and Close < Z5H
Z6 = Close > Z6L and Close < Z6H
Z7 = Close > Z7L and Close < Z7H
Z8 = Close > Z8L and Close < Z8H
Z9 = Close > Z9L and Close < Z9H
Z10 = Close > Z10L and Close < Z10H
Long = Average[9](close) < Average[20](close)
Short = Average[9](close) > Average[20](close)
Timeframe (5 minutes)
IF NOT ONMARKET THEN
IF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THEN
IF Long THEN
IF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THEN
MyStop = AverageTrueRange[10](close[1])*ATRMult
BUY Positionsize CONTRACTS AT MARKET
endif
endif
IF Short THEN
IF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THEN
MyStop = AverageTrueRange[10](close[1])*ATRMult
SELLSHORT Positionsize CONTRACTS AT MARKET
endif
endif
endif
endif
Timeframe (Default)
SHL = (Close - (Positionprice+(Mystop*2)))
SellHalfL = (SHL > 0.5)
SHS = ((Positionprice-(Mystop*2)) - Close)
SellHalfS = (SHS > 0.5)
SLL = ((Positionprice-(Mystop*SLMult)) - Close)
StopL = (SLL > 0.5)
SLS = (Close - (Positionprice+(Mystop*SLMult)))
StopS = (SLS > 0.5)
BEL = (Close-Positionprice)
BrkEL = (BEL < 1)
BES = (Positionprice-Close)
BrkES = (BES < 1)
BCL = (Close - (Positionprice+(MyStop)))
BeCalcL = (BCL > 1)
BCS = ((Positionprice-(1)) - Close)
BeCalcS = (BCS > 1)
HalfPos = (Positionsize/2)
IF abs(CountOfPosition) = 0 THEN
Count = 0
endif
IF LONGONMARKET THEN
IF SellHalfL AND abs(CountOfPosition) = PositionSize THEN
SELL HalfPos CONTRACTS AT MARKET
elsif StopL THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
elsif BECalcL THEN
Count = Count + 1
IF Count >= 1 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
endif
IF SHORTONMARKET THEN
IF SellHalfS AND abs(CountOfPosition) = PositionSize THEN
EXITSHORT HalfPos CONTRACTS AT MARKET
elsif StopS THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
elsif BECalcS THEN
Count = Count + 1
IF Count = 1 AND BrkES THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
endif
If MyProfit > 0 then
SET TARGET PPROFIT MyProfit
endif
Thanks, i’ve added the full code.
Br Anders
It’s due to WCS being 0 at the beginning(line 2), so that line 7 will immediately result in a “DIVISION BY 0” error.
Code is read and executed sequentially, you need to set a value for MyStop prior to line 2. Or you may want to move lines 2-7 to a line AFTER MyStop is assigned a correct value, other than 0.
Big THANKS! 😀
Ciao Roberto,
Hope you are well! You solved the divison error for me and i’ve been trading the code for a while. But now i have another “issue”, i’ve tried to build a scaling out system based on how many times my risk im in profit. And i do not want to leave a position in market which would be easier but instead i would like the code to sell based on if the calculation is true.
Issue #1: My self half code works very well when im short. It sells half of the contract and keeps the other half. But when Long, it sell halfs and on next bar sell the other half directly. In my backtest everything looks fine.
Issue #2: When i was in a short pos recently and it scaled out half, it never triggered my other commands to sell remaining half even though it passed well the 2x of profit and should have sold on 1.25 times the entry. I can’t find a reason for why? Everything looks OK in the backtest.
Hope you or someone else could give some guidance, last time you mentioned in which steps the code is read. Could you elaborate this point a bit further?
Code has been traded on: Sverige 30 Cash, 1 min timeframe (if you talk 1k bars back you will have the latest trades i done, which are fine, but not really acc the backtest since it kept the position until i manually closed the short almost 3x of profits)
Br Anders
Defparam cumulateorders = False
ATRMult = 1//4
SellHalf = 1 // 0 = säljer poss till 2:1 RR, 1 säljer halva vid 2:1
BE1 = 1 // 1= BE lyfts in vid 1:1
BEMult = 1 // 1 = ingen multiplier, starta alltid med 1. Vid ex2 ggr måste kurs nå 2 ggr risk innan den flyttar till BE.
BE2 = 1 // 1= BE lyfts in till 1.25:1 vid 2:1
BE3 = 1 // 1= BE lyfts in till 2.5:1 vid 3:1
BE4 = 1 // 1= BE lyfts in till 3.75:1 vid 4:1
SLMult = 1 //1 = ingen multiplier, starta alltid med 1, försämrad risk/reward vid multiplier
MyProfit = 0 //0 är standard
MyStartTrade = 060000
MyEndTrade = 173000
DaytoFilter = 0
Timeframe (Default)
Z1H = 23080 //LONG
Z1L = 23030 //LONG
Z2H = 23000 //LONG
Z2L = 22290 //LONG
Z3H = 22870 //LONG
Z3L = 22770 //LONG
Z4H = 2315 // SHORT
Z4L = 2308 // SHORT
Z5H = 2342 // SHORT
Z5L = 2338 // SHORT
Z6H = 2371 // SHORT
Z6L = 2365 // SHORT
Z7H = 15955
Z7L = 15955
Z8H = 15955
Z8L = 15955
Z9H = 15955
Z9L = 15955
Z10H = 15955
Z10L = 15955
Z11H = 15955
Z11L = 15955
Z12H = 15955
Z12L = 15955
Z13H = 15955
Z13L = 15955
Z14H = 15955
Z14L = 15945
Z15H = 16010
Z15L = 15995
Z16H = 16055
Z16L = 16045
Z17H = 16110
Z17L = 16090
Z18H = 16155
Z18L = 16135
Z19H = 16235
Z19L = 16215
Z20H = 16295
Z20L = 16280
Z21H = 16360
Z21L = 16345
Z22H = 16420
Z22L = 16400
Z23H = 16525
Z23L = 16500
Z24H = 16610
Z24L = 16595
Z25H = 16775
Z25L = 16775
Z1 = Close > Z1L and Close < Z1H
Z2 = Close > Z2L and Close < Z2H
Z3 = Close > Z3L and Close < Z3H
Z4 = Close > Z4L and Close < Z4H
Z5 = Close > Z5L and Close < Z5H
Z6 = Close > Z6L and Close < Z6H
Z7 = Close > Z7L and Close < Z7H
Z8 = Close > Z8L and Close < Z8H
Z9 = Close > Z9L and Close < Z9H
Z10 = Close > Z10L and Close < Z10H
Z11 = Close > Z11L and Close < Z11H
Z12 = Close > Z12L and Close < Z12H
Z13 = Close > Z13L and Close < Z13H
Z14 = Close > Z14L and Close < Z14H
Z15 = Close > Z15L and Close < Z15H
Z16 = Close > Z16L and Close < Z16H
Z17 = Close > Z17L and Close < Z17H
Z18 = Close > Z18L and Close < Z18H
Z19 = Close > Z19L and Close < Z19H
Z20 = Close > Z20L and Close < Z20H
Z21 = Close > Z21L and Close < Z21H
Z22 = Close > Z22L and Close < Z22H
Z23 = Close > Z23L and Close < Z23H
Z24 = Close > Z24L and Close < Z24H
Z25 = Close > Z25L and Close < Z25H
IF NOT ONMARKET THEN
IF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THEN
IF Z1 or Z2 or Z3 or Z7 or Z8 or Z9 or Z10 or Z11 or Z12 or Z13 or Z14 or Z15 or Z16 or Z17 or Z18 or Z19 or Z20 or Z21 or Z22 or Z23 or Z24 or Z25 THEN
MyStop = 8*ATRMult
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98
MinPositionSize = 1//Minsta size
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
BUY Positionsize CONTRACTS AT MARKET
endif
endif
IF Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 or Z11 or Z12 or Z13 or Z14 or Z15 or Z16 or Z17 or Z18 or Z19 or Z20 or Z21 or Z22 or Z23 or Z24 or Z25 THEN
MyStop = 8*ATRMult
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98
MinPositionSize = 1//Minsta size
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
SELLSHORT Positionsize CONTRACTS AT MARKET
endif
endif
Timeframe (Default)
SHL = (Close - (Positionprice+(Mystop*2)))
SellHalfL = (SHL > 0.5)
SHS = ((Positionprice-(Mystop*2)) - Close)
SellHalfS = (SHS > 0.5)
SLL = ((Positionprice-(Mystop*SLMult)) - Close)
StopL = (SLL > 0.5)
SLS = (Close - (Positionprice+(Mystop*SLMult)))
StopS = (SLS > 0.5)
BEL = (Close-Positionprice)
BrkEL = (BEL < 1)
BES = (Positionprice-Close)
BrkES = (BES < 1)
BCL = (Close - (Positionprice+(MyStop*BEMult)))
BeCalcL = (BCL > 1)
BCS = ((Positionprice-(Mystop*BEMult)) - Close)
BeCalcS = (BCS > 1)
BEL2 = (Close-(Positionprice+MyStop*1.25))
BrkEL2 = (BEL2 < 1)
BES2 = ((Positionprice-MyStop*1.25)-Close)
BrkES2 = (BES2 < 1)
BCL2 = (Close - (Positionprice+(MyStop*2)))
BeCalcL2 = (BCL2 > 1)
BCS2 = ((Positionprice-(Mystop*2)) - Close)
BeCalcS2 = (BCS2 > 1)
BEL3 = (Close-(Positionprice+(MyStop*2.5)))
BrkEL3 = (BEL3 < 1)
BES3 = ((Positionprice-(MyStop*2.5))-Close)
BrkES3 = (BES3 < 1)
BCL3 = (Close - (Positionprice+(MyStop*3)))
BeCalcL3 = (BCL3 > 1)
BCS3 = ((Positionprice-(Mystop*3)) - Close)
BeCalcS3 = (BCS3 > 1)
BEL4 = (Close-(Positionprice+(MyStop*3.5)))
BrkEL4 = (BEL4 < 1)
BES4 = ((Positionprice-(MyStop*3.5))-Close)
BrkES4 = (BES4 < 1)
BCL4 = (Close - (Positionprice+(MyStop*4)))
BeCalcL4 = (BCL4 > 1)
BCS4 = ((Positionprice-(Mystop*4)) - Close)
BeCalcS4 = (BCS4 > 1)
BEL5 = (Close-(Positionprice+(MyStop*4.5)))
BrkEL5 = (BEL5 < 1)
BES5 = ((Positionprice-(MyStop*4.5))-Close)
BrkES5 = (BES5 < 1)
BCL5 = (Close - (Positionprice+(MyStop*5)))
BeCalcL5 = (BCL5 > 1)
BCS5 = ((Positionprice-(Mystop*5)) - Close)
BeCalcS5 = (BCS5 > 1)
BEL6 = (Close-(Positionprice+(MyStop*5.5)))
BrkEL6 = (BEL6 < 1)
BES6 = ((Positionprice-(MyStop*5.5))-Close)
BrkES6 = (BES6 < 1)
BCL6 = (Close - (Positionprice+(MyStop*6)))
BeCalcL6 = (BCL6 > 1)
BCS6 = ((Positionprice-(Mystop*6)) - Close)
BeCalcS6 = (BCS6 > 1)
BEL7 = (Close-(Positionprice+(MyStop*6.5)))
BrkEL7 = (BEL7 < 1)
BES7 = ((Positionprice-(MyStop*6.5))-Close)
BrkES7 = (BES7 < 1)
BCL7 = (Close - (Positionprice+(MyStop*6)))
BeCalcL7 = (BCL7 > 1)
BCS7 = ((Positionprice-(Mystop*6)) - Close)
BeCalcS7 = (BCS7 > 1)
BEL8 = (Close-(Positionprice+(MyStop*7.5)))
BrkEL8 = (BEL8 < 1)
BES8 = ((Positionprice-(MyStop*7.5))-Close)
BrkES8 = (BES8 < 1)
BCL8 = (Close - (Positionprice+(MyStop*7)))
BeCalcL8 = (BCL8 > 1)
BCS8 = ((Positionprice-(Mystop*7)) - Close)
BeCalcS8 = (BCS8 > 1)
HalfPos = (Positionsize/2)
IF abs(CountOfPosition) = 0 THEN
CountSellHalf = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count2 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count3 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count4 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count5 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count6 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count7 = 0
endif
IF abs(CountOfPosition) = 0 THEN
Count8 = 0
endif
IF abs(CountOfPosition) = 0 THEN
HalfPos = 0
endif
IF LONGONMARKET THEN
IF SellHalf = 1 THEN
IF SellHalfL AND abs(CountOfPosition) = PositionSize THEN
SELL HalfPos CONTRACTS AT MARKET
endif
endif
IF SellHalf = 0 THEN
IF SellHalfL AND abs(CountOfPosition) = PositionSize THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF StopL THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BE1 = 1 THEN
IF BECalcL THEN
Count = Count + 1
endif
IF Count >= 1 and BrkEL THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE2 = 1 THEN
IF BECalcL2 THEN
Count2 = Count2 + 1
endif
IF Count2 >= 1 and BrkEL2 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE3 = 1 THEN
IF BECalcL3 THEN
Count3 = Count3 + 1
endif
IF Count3 >= 1 and BrkEL3 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE4 = 1 THEN
IF BECalcL4 THEN
Count4 = Count4 + 1
endif
IF Count4 >= 1 and BrkEL4 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BECalcL5 THEN
Count5 = Count5 + 1
endif
IF Count5 >= 1 and BrkEL5 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcL6 THEN
Count6 = Count6 + 1
endif
IF Count6 >= 1 and BrkEL6 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcL7 THEN
Count7 = Count7 + 1
endif
IF Count7 >= 1 and BrkEL7 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcL8 THEN
Count8 = Count8 + 1
endif
IF Count8 >= 1 and BrkEL8 THEN
SELL abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF SHORTONMARKET THEN
IF SellHalf = 1 THEN
IF SellHalfS AND abs(CountOfPosition) = PositionSize THEN
EXITSHORT HalfPos CONTRACTS AT MARKET
endif
endif
IF SellHalf = 0 THEN
IF SellHalfS AND abs(CountOfPosition) = PositionSize THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF StopS THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BE1 = 1 THEN
IF BECalcS THEN
Count = Count + 1
endif
IF Count >= 1 AND BrkES THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE2 = 1 THEN
IF BECalcS2 THEN
Count2 = Count2 + 1
endif
IF Count2 >= 1 AND BrkES2 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE3 = 1 THEN
IF BECalcS3 THEN
Count3 = Count3 + 1
endif
IF Count3 >= 1 AND BrkES3 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BE4 = 1 THEN
IF BECalcS4 THEN
Count4 = Count4 + 1
endif
IF Count4 >= 1 AND BrkES4 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
IF BECalcS5 THEN
Count5 = Count5 + 1
endif
IF Count5 >= 1 AND BrkES5 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcS6 THEN
Count6 = Count6 + 1
endif
IF Count6 >= 1 AND BrkES6 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcS7 THEN
Count7 = Count7 + 1
endif
IF Count7 >= 1 AND BrkES7 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
IF BECalcS8 THEN
Count8 = Count8 + 1
endif
IF Count8 >= 1 AND BrkES8 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
endif
If MyProfit > 0 then
SET TARGET PPROFIT MyProfit
endif
Issue #1: NO Long trades have been opened, despite trying with 1K, 5K, 10K, 15K, 50K, 100K and 200K units
Issue #2: Backtests seem to work correctly; it’s impossible to spot any real-time failure.
Thanks for supporting,
Issue #1: True, that is due to the fact that it has no levels that creates the buy signal for LONG. But i add those levels manually every week, and just now i did not have any long levels. But when it has an Long position, it scales out differently from short. Can that be due to that i have a certain sequence in the code?
Issue #2: Yes, i have the same conclusion the backtest looks just fine. But in real-time the code acts a bit differently by not respecting all sell calculations.
Is the code read from top to bottom or how does it work?
Br Anders
But when it has an Long position, it scales out differently from short. Can that be due to that i have a certain sequence in the code?
Hi Anders – Indeed it can. For example (and Yes, the code is executed from top to bottom) :
JustEntered = 0 // <<--
IF NOT ONMARKET THEN
If not JustEntered then // <<-- This one is for good habit.
IF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THEN
IF Z1 or Z2 or Z3 or Z7 or Z8 or Z9 or Z10 or Z11 or Z12 or Z13 or Z14 or Z15 or Z16 or Z17 or Z18 or Z19 or Z20 or Z21 or Z22 or Z23 or Z24 or Z25 THEN
MyStop = 8*ATRMult
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98
MinPositionSize = 1//Minsta size
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
BUY Positionsize CONTRACTS AT MARKET
JustEntered = 1 // <<--
endif
endif
endif
If not JustEntered then // <<-- See text in post below.**
IF Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 or Z11 or Z12 or Z13 or Z14 or Z15 or Z16 or Z17 or Z18 or Z19 or Z20 or Z21 or Z22 or Z23 or Z24 or Z25 THEN
MyStop = 8*ATRMult
WCS = MyStop
InitialEquity = 3000
Equity = InitialEquity+Strategyprofit
MyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98
MinPositionSize = 1//Minsta size
PositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))
SELLSHORT Positionsize CONTRACTS AT MARKET
JustEntered = 1 // <<--
endif
endif
endif
**) Without such an If, the SellShort will cancel out the Buy of the first section if the Buy happened as well.
You should NOT depend on the conditions for entering, no matter that you obviously think they will be mutually exclusive.
I only picked one example from your code, but it is full with these “mistakes”. So all your Sells and ExitShorts – same problem. For each of these commands, check whether you already executed them (e.g. If not JustExited).
IF Count8 >= 1 AND BrkES8 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET
endif
Maye the CountOfPosition is good habit (told by someone), but I would never do that. Your code will be vague (ambiguous) because of it, because you won’t be able to follow (ProRealCode itself won’t have a problem with it). Look at this example :
IF Count6 >= 1 AND BrkES6 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET // Suppose this one triggers ...
endif
IF BECalcS7 THEN
Count7 = Count7 + 1
endif
IF Count7 >= 1 AND BrkES7 THEN
EXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET // ... then CountOfPosition is unchanged here.
endif
Thus while you may think that when not OnMarket CountOfPosition will be 0 and nothing will happen anyway, you yourself won’t be able to check / follow what really will be happening. This is thus indeed because CountOfPosition will remain unchanged during all the lines of your code (this thus too may counteract an earlier command as per my example above). Only at the next call (when the current bar has passed / closed) these kind of “constants” will have been updated. Same with OnMarket and everything.
Notice that if you leave out the number of Contracts, it will also work for your example (not adding position to already existing position). Thus “ExitShort at Marlet” suffices.
When you apply my “hints” it will be an eyeopener for you how
– suddenly the results are wildly different
– you suddenly understand what’s happening everywhere.
Have fun !
Peter
Huge thanks Peter, i will most certainly start working on it. I actually got an new example today, i use the same code but with other levels on USTech, as seen in the image attached where i put the backtest above the actual trade, the position does the first move to halfen the size, but this should in my mind (before adding the wisedom from you Peter) also trigger my other condition to move the SL to BE according to my condition,
I will get on the task to correct my errors, please let me know if there is other vital mistakes i should consider 🙂
BR Anders
Division by zero in position sizing code error
This topic contains 15 replies,
has 3 voices, and was last updated by
robertogozzi
4 years, 2 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 11/08/2021 |
| Status: | Active |
| Attachments: | 5 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.