code for limiting the number of cumulative orders

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #147183 quote
    nonetheless
    Participant
    Master

    I’m using the following code to limit cumulative orders to a max of 8:

    Once MaxPositionsAllowed = 8
    // Conditions to enter long positions
    IF Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a and abs(CountOfPosition) < MaxPositionsAllowed THEN
    BUY positionsize CONTRACT AT MARKET
    SET STOP %LOSS 1.3
    SET TARGET %PROFIT 2.2
    ENDIF

    but in practice (demo) the strategy is taking more than 8, see attached pic. Is there anything wrong with the above code?

    cum-order.jpg cum-order.jpg
    #147185 quote
    JC_Bywan
    Moderator
    Master

    I don’t know how your positionsize is calculated and how your countofposition usually evolves, but let’s say as an example you have abs(countposition)=7 ( is lower than 8) and positionsize is 3, the “if” statement will conclude that 7 being lower than 8 (and your other conditions being respected), it’s ok to add 3 to this, and 7+3=10…

    In other words, from quick read without the rest of the code, this piece of code is not preventing you from going above 8 at all, because you made your check before adding to the position, so I’d try with :    abs(countofposition)<maxpositionallowed-positionsize   to see there’s enough room left below the max before increasing the position

    nonetheless thanked this post
    #147188 quote
    robertogozzi
    Moderator
    Master

    I think line 3 could be written as:

    IF Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a and (abs(CountOfPosition)  + PositionSize) <= MaxPositionsAllowed THEN
    nonetheless and Paul thanked this post
    #147189 quote
    nonetheless
    Participant
    Master

    Thanks for that, but perhaps I am missing something. Surely countofposition and positionsize are 2 different things, no?

    In this case, positionsize is .4 on the DJ and I want to have a max of 8 different entries of .4 each. Would the MaxPositionsAllowed then need to be 3.2 ?

    #147190 quote
    swedshare
    Participant
    Senior

    Yes, 3.2. I also had to think twice on DJI.

    #147193 quote
    nonetheless
    Participant
    Master

    But how will that work with MM then? when the positionsize increases? Maybe

    MaxPositionsAllowed = (positionsize x 8)

    ???

    #147194 quote
    robertogozzi
    Moderator
    Master

    COUNTOFPOSITION is the number (negative for Short positions) of total positions accumulated so far, while PositionSize is the number of positions you want to trade at that very moment. After opening additional PositionSize contracts/shares, COUNTOFPOSITION will be increased by PositionSize units.

    #147206 quote
    nonetheless
    Participant
    Master

    Sorry Roberto, if my positionsize = .4 and the strategy has cumulated 7 orders then the countofposition is 7  or 2.8 ?

    #147208 quote
    robertogozzi
    Moderator
    Master

    2.8

    #147209 quote
    nonetheless
    Participant
    Master

    Ok, so countofposition is actually the total number of contracts. In this case I would want that to max at 3.2 and if positionsize didn’t change then there’s no problem. But what happens when the MM  changes the positionsize? Is there no way of specifying the number of instances, rather than the number of contracts?

    #147211 quote
    fifi743
    Participant
    Master

    hi nonetheless

    Try this

    round(COUNTOFPOSITION /positionsize)
    
    nonetheless thanked this post
    #147212 quote
    nonetheless
    Participant
    Master

    Hi Fifi, comment ca va?

    Do you mean

    #147217 quote
    nonetheless
    Participant
    Master

    that is supposed to say

    Once MaxPositionsAllowed = 8
    
    // Conditions to enter long positions
    IF Ctime and myconditions and round(CountOfPosition/positionsize) <= MaxPositionsAllowed THEN
    #147220 quote
    robertogozzi
    Moderator
    Master

    Why using ROUND()?

    MaxPositionsAllowed is a limit. If your current COUNTOFPOSITION is 7.4 and you wanna add 0.8 the code won’t allow that.

    Rounding that number will count it as 7, so you’ll be allow to add 0.8 PositionSize contracts, but doing this actually will raise COUNTOFPOSITION to 8.2, which is not what you asked with MaxPositionsAllowed.

    #147221 quote
    robertogozzi
    Moderator
    Master

    Sorry, I just realized I misunderstood.

    You want MaxPositionsAllowed to set the number of times you open positions, no matter how large they are.

    In that case your code is correct.

    nonetheless and Kovit thanked this post
Viewing 15 posts - 1 through 15 (of 30 total)
  • You must be logged in to reply to this topic.

code for limiting the number of cumulative orders


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 29 replies,
has 8 voices, and was last updated by nonetheless
5 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/12/2020
Status: Active
Attachments: 5 files
Logo Logo
Loading...