Code for partial close with percentage

Viewing 15 posts - 1 through 15 (of 62 total)
  • Author
    Posts
  • #153312 quote
    Fran55
    Participant
    Veteran

    Please, code for partial close with porcentage?

    #153313 quote
    robertogozzi
    Moderator
    Legend

    There you go (not tested):

    ONCE PerCent = 0.50            //0.50 = close half positions
    IF MyLongExitConditions AND LongOnMarket THEN
       SELL (abs(CountOfPosition) * PerCent) AT Market
    ELSIF MyLongExitConditions AND ShortOnMarket THEN
       EXITSHORT (abs(CountOfPosition) * PerCent) AT Market
    ENDIF
    Monochrome thanked this post
    #153326 quote
    Fran55
    Participant
    Veteran

    Really fast, thanks Roberto!

    #153428 quote
    AE
    Participant
    Senior

    There you go (not tested):

    This is already available with PRT 11? Because on PRT 10 it wasn’t possible partial closed.

     

    Thanks

    #153432 quote
    robertogozzi
    Moderator
    Legend

    Yes, it is available with v11.

    #154384 quote
    Tony40
    Participant
    Junior

    Help for code. I need that if the price falls 0.5% from the opening price, it partially closes 0.2% of the position. Thank you.

    #154392 quote
    robertogozzi
    Moderator
    Legend

    There you go (not tested):

    ONCE PerCent     = 0.002            //0.2% = positions to close
    ONCE FallPerCent = 0.005            //0.5% = price drop
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    RemainQuantity   = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - RemainQuantity
    IF close <= (PositionPrice * (1 - FallPerCent)) AND LongOnMarket THEN
       SELL CloseQuantity Contracts AT Market
    ELSIF close >= (PositionPrice * (1 + FallPerCent)) AND ShortOnMarket THEN
       EXITSHORT CloseQuantity Contracts AT Market
    ENDIF

    I added a check to make sure the quantity left open is not < to the Minimum Lot Size required (that you can set at line 3, after checking with the broker).

    My code in the previous post was missing the word Contracts.

    #154419 quote
    Tony40
    Participant
    Junior
    #154425 quote
    robertogozzi
    Moderator
    Legend

    You may either write lines 5-6 manually or try to replace them with:

    RemainQty   = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - RemainQty

    Using a different name.

    #154427 quote
    bullbear
    Participant
    Senior

    This with partial close is interesting.

    If you want to close 50% of the position when you have 20 points plus.

    What does that code look like?

    #154446 quote
    Fran55
    Participant
    Veteran

    Long, (1+fallpercent).

    Short, (1-fallpercent).

     

    You can change “ONCE percent” and “ONCE fallpercent” to others %.

     

     

    #154451 quote
    robertogozzi
    Moderator
    Legend

    There you go:

    ONCE PerCent     = 0.5              //50%  = positions to close
    ONCE Pips        = 20 * PipSize
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    RemainQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - RemainQty
    IF close >= (PositionPrice + Pips) AND LongOnMarket THEN
       SELL CloseQuantity Contracts AT Market
    ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket THEN
       EXITSHORT CloseQuantity Contracts AT Market
    ENDIF
    #154455 quote
    bullbear
    Participant
    Senior

    Thanks Roberto but I got an error “RemainQty” when I run your code.

    what am I doing wrong?

    I have position size = 4 and want to close 2 contracts when I am 20 points plus.

     

    Regards

    #154457 quote
    bullbear
    Participant
    Senior

    So how do I enter the values ​​in row 5 if I have 4 contracts from the beginning and want to close 2 contracts.

    #154538 quote
    bullbear
    Participant
    Senior

    You may either write lines 5-6 manually or try to replace them with:

    Using a different name.

     

    I’m probably pretty stupid.

    Is there an opportunity to explain in a simpler way how I do with “RemainQty”.

    Gets error no matter what I do!

Viewing 15 posts - 1 through 15 (of 62 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 close with percentage


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Fran55 @fran55 Participant
Summary

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

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