Division by zero in position sizing code error

Forums ProRealTime English forum ProOrder support Division by zero in position sizing code error

Viewing 15 posts - 1 through 15 (of 16 total)
  • #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
    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

    #181261

    Append these lines to visually detect any cause:

    1 user thanked author for this post.
    #181264

    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

    #181269

    Attached also the values for the different graphs.

    Br Anders

    #181272

    What value should MyStop have?

     

    #181335

    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

    #181394

    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.

    #181408

    Thanks, i’ve added the full code.

     

    Br Anders

    #181445

    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.

     

    1 user thanked author for this post.
    #181463

    Big THANKS! 😀

    #183656

    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

     

    #183664

    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.

     

    #183708

    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

    #183721

    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) :

     

    **) 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).

    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 :

    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

    1 user thanked author for this post.
    #183773

    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

     

Viewing 15 posts - 1 through 15 (of 16 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login