Division by zero in position sizing code error
Forums › ProRealTime English forum › ProOrder support › Division by zero in position sizing code error
- This topic has 15 replies, 3 voices, and was last updated 3 years ago by
robertogozzi.
-
-
11/08/2021 at 7:17 PM #181256
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
endifIF 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
endifI would be happy with suggestions that i could try,
Best regards Anders
11/08/2021 at 8:47 PM #181261Append these lines to visually detect any cause:
1234graph ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))graph (Equity-(Equity*MyPercRisk))graph (MinPositionSize*WCS)graph WCS //this line could retain 01 user thanked author for this post.
11/08/2021 at 9:32 PM #181264Thanks 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
11/08/2021 at 10:03 PM #181269Attached also the values for the different graphs.
Br Anders
11/08/2021 at 10:26 PM #181272What value should MyStop have?
11/09/2021 at 8:27 PM #181335Ciao,
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
endifBr Anders
11/10/2021 at 4:55 PM #181394Where 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.11/10/2021 at 7:06 PM #181408Code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135Defparam cumulateorders = FalseWCS = MyStopInitialEquity = 3000Equity = InitialEquity+StrategyprofitMyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98MinPositionSize = 1//Minsta sizePositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))ATRMult = 5SLMult = 1 //1 = ingen multiplier, starta alltid med 1, försämrad risk/reward vid multiplierMyProfit = 0 //0 är standardMyStartTrade = 090000MyEndTrade = 173000DaytoFilter = 0Timeframe (Default)Z1H = 2204Z1L = 2191Z2H = 2250Z2L = 2245Z3H = 2284Z3L = 2277Z4H = 2308Z4L = 2300Z5H = 2362Z5L = 2356Z6H = 2413Z6L = 2404Z7H = 2383Z7L = 2376Z8H = 4620Z8L = 4615Z9H = 4665Z9L = 4670Z10H = 4698Z10L = 4704Z1 = Close > Z1L and Close < Z1HZ2 = Close > Z2L and Close < Z2HZ3 = Close > Z3L and Close < Z3HZ4 = Close > Z4L and Close < Z4HZ5 = Close > Z5L and Close < Z5HZ6 = Close > Z6L and Close < Z6HZ7 = Close > Z7L and Close < Z7HZ8 = Close > Z8L and Close < Z8HZ9 = Close > Z9L and Close < Z9HZ10 = Close > Z10L and Close < Z10HLong = Average[9](close) < Average[20](close)Short = Average[9](close) > Average[20](close)Timeframe (5 minutes)IF NOT ONMARKET THENIF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THENIF Long THENIF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THENMyStop = AverageTrueRange[10](close[1])*ATRMultBUY Positionsize CONTRACTS AT MARKETendifendifIF Short THENIF Z1 or Z2 or Z3 or Z4 or Z5 or Z6 or Z7 or Z8 or Z9 or Z10 THENMyStop = AverageTrueRange[10](close[1])*ATRMultSELLSHORT Positionsize CONTRACTS AT MARKETendifendifendifendifTimeframe (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 THENCount = 0endifIF LONGONMARKET THENIF SellHalfL AND abs(CountOfPosition) = PositionSize THENSELL HalfPos CONTRACTS AT MARKETelsif StopL THENSELL abs(CountOfPosition) CONTRACTS AT MARKETelsif BECalcL THENCount = Count + 1IF Count >= 1 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifendifIF SHORTONMARKET THENIF SellHalfS AND abs(CountOfPosition) = PositionSize THENEXITSHORT HalfPos CONTRACTS AT MARKETelsif StopS THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETelsif BECalcS THENCount = Count + 1IF Count = 1 AND BrkES THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifendifIf MyProfit > 0 thenSET TARGET PPROFIT MyProfitendifThanks, i’ve added the full code.
Br Anders
11/11/2021 at 11:25 AM #181445It’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.
1 user thanked author for this post.
11/11/2021 at 7:09 PM #18146312/20/2021 at 9:04 PM #183656Ciao 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
Updated code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381Defparam cumulateorders = FalseATRMult = 1//4SellHalf = 1 // 0 = säljer poss till 2:1 RR, 1 säljer halva vid 2:1BE1 = 1 // 1= BE lyfts in vid 1:1BEMult = 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:1BE3 = 1 // 1= BE lyfts in till 2.5:1 vid 3:1BE4 = 1 // 1= BE lyfts in till 3.75:1 vid 4:1SLMult = 1 //1 = ingen multiplier, starta alltid med 1, försämrad risk/reward vid multiplierMyProfit = 0 //0 är standardMyStartTrade = 060000MyEndTrade = 173000DaytoFilter = 0Timeframe (Default)Z1H = 23080 //LONGZ1L = 23030 //LONGZ2H = 23000 //LONGZ2L = 22290 //LONGZ3H = 22870 //LONGZ3L = 22770 //LONGZ4H = 2315 // SHORTZ4L = 2308 // SHORTZ5H = 2342 // SHORTZ5L = 2338 // SHORTZ6H = 2371 // SHORTZ6L = 2365 // SHORTZ7H = 15955Z7L = 15955Z8H = 15955Z8L = 15955Z9H = 15955Z9L = 15955Z10H = 15955Z10L = 15955Z11H = 15955Z11L = 15955Z12H = 15955Z12L = 15955Z13H = 15955Z13L = 15955Z14H = 15955Z14L = 15945Z15H = 16010Z15L = 15995Z16H = 16055Z16L = 16045Z17H = 16110Z17L = 16090Z18H = 16155Z18L = 16135Z19H = 16235Z19L = 16215Z20H = 16295Z20L = 16280Z21H = 16360Z21L = 16345Z22H = 16420Z22L = 16400Z23H = 16525Z23L = 16500Z24H = 16610Z24L = 16595Z25H = 16775Z25L = 16775Z1 = Close > Z1L and Close < Z1HZ2 = Close > Z2L and Close < Z2HZ3 = Close > Z3L and Close < Z3HZ4 = Close > Z4L and Close < Z4HZ5 = Close > Z5L and Close < Z5HZ6 = Close > Z6L and Close < Z6HZ7 = Close > Z7L and Close < Z7HZ8 = Close > Z8L and Close < Z8HZ9 = Close > Z9L and Close < Z9HZ10 = Close > Z10L and Close < Z10HZ11 = Close > Z11L and Close < Z11HZ12 = Close > Z12L and Close < Z12HZ13 = Close > Z13L and Close < Z13HZ14 = Close > Z14L and Close < Z14HZ15 = Close > Z15L and Close < Z15HZ16 = Close > Z16L and Close < Z16HZ17 = Close > Z17L and Close < Z17HZ18 = Close > Z18L and Close < Z18HZ19 = Close > Z19L and Close < Z19HZ20 = Close > Z20L and Close < Z20HZ21 = Close > Z21L and Close < Z21HZ22 = Close > Z22L and Close < Z22HZ23 = Close > Z23L and Close < Z23HZ24 = Close > Z24L and Close < Z24HZ25 = Close > Z25L and Close < Z25HIF NOT ONMARKET THENIF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THENIF 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 THENMyStop = 8*ATRMultWCS = MyStopInitialEquity = 3000Equity = InitialEquity+StrategyprofitMyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98MinPositionSize = 1//Minsta sizePositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))BUY Positionsize CONTRACTS AT MARKETendifendifIF 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 THENMyStop = 8*ATRMultWCS = MyStopInitialEquity = 3000Equity = InitialEquity+StrategyprofitMyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98MinPositionSize = 1//Minsta sizePositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))SELLSHORT Positionsize CONTRACTS AT MARKETendifendifTimeframe (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 THENCountSellHalf = 0endifIF abs(CountOfPosition) = 0 THENCount = 0endifIF abs(CountOfPosition) = 0 THENCount2 = 0endifIF abs(CountOfPosition) = 0 THENCount3 = 0endifIF abs(CountOfPosition) = 0 THENCount4 = 0endifIF abs(CountOfPosition) = 0 THENCount5 = 0endifIF abs(CountOfPosition) = 0 THENCount6 = 0endifIF abs(CountOfPosition) = 0 THENCount7 = 0endifIF abs(CountOfPosition) = 0 THENCount8 = 0endifIF abs(CountOfPosition) = 0 THENHalfPos = 0endifIF LONGONMARKET THENIF SellHalf = 1 THENIF SellHalfL AND abs(CountOfPosition) = PositionSize THENSELL HalfPos CONTRACTS AT MARKETendifendifIF SellHalf = 0 THENIF SellHalfL AND abs(CountOfPosition) = PositionSize THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF StopL THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifIF BE1 = 1 THENIF BECalcL THENCount = Count + 1endifIF Count >= 1 and BrkEL THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE2 = 1 THENIF BECalcL2 THENCount2 = Count2 + 1endifIF Count2 >= 1 and BrkEL2 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE3 = 1 THENIF BECalcL3 THENCount3 = Count3 + 1endifIF Count3 >= 1 and BrkEL3 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE4 = 1 THENIF BECalcL4 THENCount4 = Count4 + 1endifIF Count4 >= 1 and BrkEL4 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BECalcL5 THENCount5 = Count5 + 1endifIF Count5 >= 1 and BrkEL5 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcL6 THENCount6 = Count6 + 1endifIF Count6 >= 1 and BrkEL6 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcL7 THENCount7 = Count7 + 1endifIF Count7 >= 1 and BrkEL7 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcL8 THENCount8 = Count8 + 1endifIF Count8 >= 1 and BrkEL8 THENSELL abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF SHORTONMARKET THENIF SellHalf = 1 THENIF SellHalfS AND abs(CountOfPosition) = PositionSize THENEXITSHORT HalfPos CONTRACTS AT MARKETendifendifIF SellHalf = 0 THENIF SellHalfS AND abs(CountOfPosition) = PositionSize THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF StopS THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifIF BE1 = 1 THENIF BECalcS THENCount = Count + 1endifIF Count >= 1 AND BrkES THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE2 = 1 THENIF BECalcS2 THENCount2 = Count2 + 1endifIF Count2 >= 1 AND BrkES2 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE3 = 1 THENIF BECalcS3 THENCount3 = Count3 + 1endifIF Count3 >= 1 AND BrkES3 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BE4 = 1 THENIF BECalcS4 THENCount4 = Count4 + 1endifIF Count4 >= 1 AND BrkES4 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIF BECalcS5 THENCount5 = Count5 + 1endifIF Count5 >= 1 AND BrkES5 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcS6 THENCount6 = Count6 + 1endifIF Count6 >= 1 AND BrkES6 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcS7 THENCount7 = Count7 + 1endifIF Count7 >= 1 AND BrkES7 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifIF BECalcS8 THENCount8 = Count8 + 1endifIF Count8 >= 1 AND BrkES8 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifendifIf MyProfit > 0 thenSET TARGET PPROFIT MyProfitendif12/21/2021 at 6:32 AM #183664Issue #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.
12/21/2021 at 7:08 PM #183708Thanks 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
12/22/2021 at 5:14 AM #183721But 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) :
1234567891011121314151617181920212223242526272829303132333435JustEntered = 0 // <<--IF NOT ONMARKET THENIf not JustEntered then // <<-- This one is for good habit.IF CurrentDayOfWeek <> DaytoFilter and Time > MyStartTrade And Time < MyEndTrade THENIF 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 THENMyStop = 8*ATRMultWCS = MyStopInitialEquity = 3000Equity = InitialEquity+StrategyprofitMyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98MinPositionSize = 1//Minsta sizePositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))BUY Positionsize CONTRACTS AT MARKETJustEntered = 1 // <<--endifendifendifIf 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 THENMyStop = 8*ATRMultWCS = MyStopInitialEquity = 3000Equity = InitialEquity+StrategyprofitMyPercRisk = 0.995 // Max risk, bör ej vara över 2% = 0.98MinPositionSize = 1//Minsta sizePositionSize = MinPositionSize*ROUND((Equity-(Equity*MyPercRisk))/(((MinPositionSize*WCS))))SELLSHORT Positionsize CONTRACTS AT MARKETJustEntered = 1 // <<--endifendifendif**) 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).123IF Count8 >= 1 AND BrkES8 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKETendifMaye 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 :
123456789IF Count6 >= 1 AND BrkES6 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET // Suppose this one triggers ...endifIF BECalcS7 THENCount7 = Count7 + 1endifIF Count7 >= 1 AND BrkES7 THENEXITSHORT abs(CountOfPosition) CONTRACTS AT MARKET // ... then CountOfPosition is unchanged here.endifThus 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 !
Peter1 user thanked author for this post.
12/22/2021 at 7:19 PM #183773Huge 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
-
AuthorPosts
Find exclusive trading pro-tools on