Help with increase/decrease in number of contracts in steps

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #151224 quote
    marcusstrom87
    Participant
    New

    Hi!

    I’m a rookie and I’m having trouble to create a code that increases and decreases the number of contracts based on profits/losses.

    I need a code that’s increasing contracts by 0.25 for every 350$ profit. So if a single trade profits 700$ that would mean that the system should increase with two steps immediately (increase with 0.5 contracts in this example).

    The same if it loses a single trade for  1050$ then it would need to decrease contracts with three steps (0.75 contracts in this example).

    I have searched a lot on the forum without finding anything that works the way I want it to. Everything I found does not take into account if a trade make a huge profit or a huge loss, that it need to increase/decrease several steps at once.

    If anyone could help me to create this code I would be extremely grateful!

    Regards

    The rookie

    #151227 quote
    robertogozzi
    Moderator
    Master

    There you go (not tested):

    Once MinSize = 0.5      //minimum sizecrequired
    Once LotSize = MinSize  //starting size (can be bigger than minimum)
    Once LotStep = 250      //increase/decrease this money step
    LotSize      = max(MinSize,MinSize + round(StrategyProfit / LotStep) //update LotSize
    #151229 quote
    robertogozzi
    Moderator
    Master

    Sorry, line 4 should read:

    LotSize = max(
    MinSize,MinSize + (MinSize * round(StrategyProfit / LotStep)))
    marcusstrom87 thanked this post
    #151236 quote
    marcusstrom87
    Participant
    New

    Thank you!! I’m gonna try it later tonight!

    #199043 quote
    MaoRai54
    Participant
    Master

    Hi to everybody here.

    Well, I found this topic and it seems interesting to me.  I tried to add it into a strategy running in demo and I got different results on long and short trades.

    Then I modified it as follows (just setting different quantities and profit amount for short and long trades) but I can’t find a way to obtain the strategyprofit only for long trades and/or for short ones.  Is there any way to know the profit generated by long or short trades ????  

    // for LONG trades
    Once MinSizeL = 1 //minimum sizecrequired
    Once LotSizeL = MinSizeL //starting size (can be bigger than minimum)
    Once LotStepL = 250 //increase/decrease this money step
    LotSizeL = max(MinSizeL,MinSizeL + (MinSizeL * round(StrategyProfit / LotStepL)))
    myLotL=LotSizeL

    // for SHORT trades
    Once MinSizeS = 1 //minimum sizecrequired
    Once LotSizeS = MinSizeS //starting size (can be bigger than minimum)
    Once LotStepS = 250 //increase/decrease this money step
    LotSizeS = max(MinSizeS,MinSizeS + (MinSizeS * round(StrategyProfit / LotStepS)))
    myLotS=LotSizeS

    Many thanks !!!!!!!!!!!!!

    #199054 quote
    robertogozzi
    Moderator
    Master

    This snippet should to, to split STRATEGYPROFIT into two parts, Long and Short performances (not tested):

    ONCE LongProfit  = 0
    ONCE ShortProfit = 0
    IF StrategyProfit <> StrategyProfit[1] THEN
       IF LongOnMarket[1] THEN
          LongProfit  = LongProfit  + (StrategyProfit - StrategyProfit[1])
       ELSIF ShortOnMarket[1] THEN
          ShortProfit = ShortProfit + (StrategyProfit - StrategyProfit[1])
       ENDIF
    ENDIF
    MaoRai54 thanked this post
    #199055 quote
    MaoRai54
    Participant
    Master

    @ rOBERTO

    many thanks for your prompt answer.

    I’m testing it but it seems to work unproperly.

    At the end of the backtest i’ll send here the complete system for your comments.

    Thanks !!

    #199079 quote
    robertogozzi
    Moderator
    Master

    There was an issue when a single-bar trade occurred, with this modification everything seems to work fine (tested on DAX, Daiky TF):

    ONCE LongProfit  = 0
    ONCE ShortProfit = 0
    IF StrategyProfit <> StrategyProfit[1] AND BarIndex > 0 THEN
       Profitto = StrategyProfit - StrategyProfit[1]
       IF LongOnMarket[1] THEN
          LongProfit  = LongProfit  + Profitto
       ELSIF ShortOnMarket[1] THEN
          ShortProfit = ShortProfit + Profitto
       ELSE
          p1 = 1
          p2 = 2
          IF OnMarket THEN
             p1 = 2
             p2 = 3
          ENDIF
          IF TradePrice(p1) > TradePrice(p2) THEN
             IF Profitto > 0 THEN
                LongProfit  = LongProfit  + Profitto
             ELSE
                ShortProfit = ShortProfit + Profitto
             ENDIF
          ELSIF TradePrice(p1) < TradePrice(p2) THEN
             IF Profitto < 0 THEN
                LongProfit  = LongProfit  + Profitto
             ELSE
                ShortProfit = ShortProfit + Profitto
             ENDIF
          ELSE
             LongProfit  = LongProfit  + (Profitto / 2)
             ShortProfit = ShortProfit + (Profitto / 2)
          ENDIF
       ENDIF
    ENDIF
    if close crosses over average[200,0](close) then
       buy at market
    elsif close crosses under average[200,0](close) then
       sellshort at market
    endif
    set target pprofit 600
    set stop   ploss   200
    //
    GRAPH StrategyProfit           coloured(0,0,0,255)   //Black
    GRAPH LongProfit               coloured(0,128,0,155) //Green
    GRAPH ShortProfit              coloured(255,0,0,255) //Red
    GRAPH LongProfit + ShortProfit coloured(0,0,255,255) //Blue
    MaoRai54 thanked this post
    #199082 quote
    MaoRai54
    Participant
    Master

    Dear ROBERTO,

    many thanks for spending your time about this issue.  I will try your new system but I think is missing the part regarding the definition of buy/sell number of contracts . Here below I added a part of your previous reply to let the system calculate the contract q.ty based on profit levels (SEE BOLD WORDS).   Do you think it’s OK in this way ????

    ONCE LongProfit  = 0

    ONCE ShortProfit = 0

    IF StrategyProfit <> StrategyProfit[1] AND BarIndex > 0 THEN

    Profitto = StrategyProfit – StrategyProfit[1]

    IF LongOnMarket[1] THEN

    LongProfit  = LongProfit  + Profitto

    ELSIF ShortOnMarket[1] THEN

    ShortProfit = ShortProfit + Profitto

    ELSE

    p1 = 1

    p2 = 2

    IF OnMarket THEN

    p1 = 2

    p2 = 3

    ENDIF

    IF TradePrice(p1) > TradePrice(p2) THEN

    IF Profitto > 0 THEN

    LongProfit  = LongProfit  + Profitto

    ELSE

    ShortProfit = ShortProfit + Profitto

    ENDIF

    ELSIF TradePrice(p1) < TradePrice(p2) THEN

    IF Profitto < 0 THEN

    LongProfit  = LongProfit  + Profitto

    ELSE

    ShortProfit = ShortProfit + Profitto

    ENDIF

    ELSE

    LongProfit  = LongProfit  + (Profitto / 2)

    ShortProfit = ShortProfit + (Profitto / 2)

    ENDIF

    ENDIF

    ENDIF

     

    // for LONG trades

    Once MinSizeL = 1      //minimum sizecrequired

    Once LotSizeL = MinSizeL  //starting size (can be bigger than minimum)

    Once LotStepL = LL200 //250      //increase/decrease this money step

    LotSizeL = max(MinSizeL,MinSizeL + (MinSizeL * round(LongProfit / LotStepL)))

    myLotL=LotSizeL

     

    // for SHORT trades

    Once MinSizeS = 1      //minimum sizecrequired

    Once LotSizeS = MinSizeS  //starting size (can be bigger than minimum)

    Once LotStepS = LS200 //250      //increase/decrease this money step

    LotSizeS = max(MinSizeS,MinSizeS + (MinSizeS * round(ShortProfit / LotStepS)))

    myLotS=LotSizeS

     

    if close crosses over average[200,0](close) then

    buy myLotL contract at market

    elsif close crosses under average[200,0](close) then

    sellshort myLotS contract at market

    endif

    set target pprofit 600

    set stop   ploss   200

    //

    #199084 quote
    robertogozzi
    Moderator
    Master

    Mine is just the way to split STRATEGYPROFIT into its two components, Short and Long.

    You seem to have used it correctly.

    MaoRai54 thanked this post
    #199086 quote
    MaoRai54
    Participant
    Master

    many thanks.!!

    It seems working well. Maybe I’ll add limits for the quantities.

    So you think it’s an interesting snippet for GRAHAL ????

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Help with increase/decrease in number of contracts in steps


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 10 replies,
has 3 voices, and was last updated by MaoRai54
3 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/22/2020
Status: Active
Attachments: No files
Logo Logo
Loading...