Code for partial closures by percentage.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #185903 quote
    jhonjhon
    Participant
    New

    Good.
    I needed code for multiple partial closures within one operation.

    If the operation is positive and earns equal to or more than 0.25%, close 1/5 of the position.
    If the operation is positive and earns equal to or more than 0.5%, close 1/5 of the position.

    It’s an example, thanks.

    #185906 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    There you go (not tested):

    If not OnMarket then
       Tally = 0
    Endif
    ProfitPC = PositionPrice * PositionPerf * 100
    If ProfitPC >= 0.25 and Tally = 0 then
       Close PositionSize/5 contracts at Market
       Tally = Tally + 1
    Endif
    If ProfitPC >= 0.50 and Tally = 1 then
       Close PositionSize/5 contracts at Market
       Tally = Tally + 1
    Endif
    #185908 quote
    nonethelessnonetheless
    Participant
    Master

    Here’s another version (also coded by Roberto👍)

    ONCE partialclose = 1
    ONCE PerCent     = pc              //0.2 = 20%  positions to close
    ONCE PerCent2     = pc2            //0.2 = 20%  positions to close
    ONCE PerCentGain = pcg             //.0025 = 0.25% gain
    ONCE PerCentGain2 = pcg2            //.005 = 0.5% gain
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
     
    ExitQuantity2     = abs(CountOfPosition) * PerCent2
    LeftQty2          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity2)
    CloseQuantity2    = abs(CountOfPosition) - LeftQty2
     
    IF Not OnMarket THEN
    Flag = 1
    Flag2 = 1 
    ENDIF
     
    IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain))  AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
     
    IF partialclose AND shortOnMarket and  close <= (PositionPrice * (1 - PerCentGain2)) AND Flag2 THEN
    exitshort CloseQuantity2 Contracts AT Market
    Flag2 = 0
    endif
    robertogozzi thanked this post
    #185923 quote
    jhonjhon
    Participant
    New

    Thanks boys.

    #186018 quote
    jhonjhon
    Participant
    New

    I’m sorry, but in Roberto’s code, line 5, it doesn’t matter if I put 0.25, 0.5, or 1, he always wins the same in backtest.

    I think the code is not working properly.

    #186037 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    Sorry, there are two mistakes:

    • I typed CLOSE in line 6 and 10 instead of SELL (or SELLSHORT)
    • line 4 should read
    ProfitPC = PositionPerf * 100
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Code for partial closures by percentage.


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
jhon @jhon Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/19/2022
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...