Code for partial close with percentage

Viewing 15 posts - 16 through 30 (of 62 total)
  • Author
    Posts
  • #154549 quote
    robertogozzi
    Moderator
    Master

    As to my code, Vonasi realized that the error is due to the fact the the name of that variable starts with REM which is a reserved word for COMMENTS (like double slashes).

    Just replace Rem with MyRem and it should work.

    #154551 quote
    bullbear
    Participant
    Senior

    Thanks Roberto😀

    Now it works!!!

    #161613 quote
    s00071609
    Participant
    Senior

    Hi Roberto

    How do i run this code only once. That is, exit 50% based on the condition and stop running this code. For remaining position, I have another condition.

    #161621 quote
    robertogozzi
    Moderator
    Master

    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
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
    IF close >= (PositionPrice + Pips) AND LongOnMarket THEN
       SELL CloseQuantity Contracts AT Market
       QUIT
    ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket THEN
       EXITSHORT CloseQuantity Contracts AT Market
       QUIT
    ENDIF

    QUIT will exit the strategy from AutoTrading. Any open position will either be kept open or closed according to the option you have selected in the platform.

    #161628 quote
    s00071609
    Participant
    Senior

    thanks Roberto, but I meant – once 50% position is closed, then no further 50% close even when condition is met.

    To clarify,

    I want 50% of positions to close when Condtion1 is met, the above code already does this, but the code closes 50% position each time the condition is met – I need it to run 50% close only once.

    For the remaining 50% I have another set of condition. This 50% remaining position is intended for larger trend ride.

     

    I have this code for closing 50%, but the conter concept isn’t working

    Counter=0
    ONCE Percent = 0.50 //0.50 = close half positions
    CloseQuantity=(abs(CountOfPosition)*Percent)
    IF STUExitCondition and counter<=1 AND LongOnMarket THEN
    Sell CloseQuantity Contract AT MARKET
    ELSIF STUExitCondition AND ShortOnMarket THEN
    EXITSHORT CloseQuantity Contract AT Market
    counter=counter+1
    ENDIF

     

    #161631 quote
    robertogozzi
    Moderator
    Master

    Bob’s your uncle:

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

    this code didnt work, it kept closing the position by half each time condition was met, I have this code but the counter is not working,

    See image below, I need the last two orders ie. Buy 233 contract then sell 116.5 contract. Once this is done, I don’t need the below code to run as I will close the remaining order based on another set of codes.

    Hope it makes sense

    Once Counter=0
    ONCE Percent = 0.50 //0.50 = close half positions
    CloseQuantity=(abs(CountOfPosition)*Percent)
    IF STUExitCondition and counter<=1 AND LongOnMarket THEN
    Sell CloseQuantity Contract AT MARKET
    ELSIF STUExitCondition AND ShortOnMarket THEN
    EXITSHORT CloseQuantity Contract AT Market
    counter=counter+1
    ENDIF
    PRT111.png PRT111.png
    #161645 quote
    robertogozzi
    Moderator
    Master

    It works like a charm for me:

    ONCE PerCent     = 0.5              //50%  = positions to close
    ONCE Pips        = 20 * PipSize
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
    IF Not OnMarket THEN
       Flag = 1
    ENDIF
    IF close crosses over highest[20](high[1]) and not OnMarket THEN
       buy 1 contract at market
    endif
    IF close crosses under lowest[20](low[1]) and not OnMarket THEN
       sellshort 1 contract at market
    endif
    IF close >= (PositionPrice + Pips) AND LongOnMarket AND Flag THEN
       SELL CloseQuantity Contracts AT Market
       Flag = 0
    ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag THEN
       EXITSHORT CloseQuantity Contracts AT Market
       Flag = 0
    ENDIF
    set target pprofit 600
    set stop   ploss   100
    graph abs(countofposition)
    graph Flag
    graph positionperf*positionprice
    graph positionprice

    once half position is closed, no further action is taken.

    #161654 quote
    s00071609
    Participant
    Senior

    I tried it. With BUY 1 Contract as in code above, it works.

    But when I change the 1 contract to variable size of PositionSize Contract, the code closes 50% position multiple times. This makes no sense – it should work exactly same with Postitionsize contract as well I think

    #161656 quote
    robertogozzi
    Moderator
    Master

    It’s weird, it trades when FLAG=1 which is set when NOT OnMarket.

    Once a position is partially closed, FLAG is set to 0 and can’t be set again to 1 until ALL positions are closed.

    Try using this at lines 16 and 19:

    AND Flag = 1
    #161657 quote
    s00071609
    Participant
    Senior

    Looks like the code was working only because the buy quantity was set to 1. If I change it to anything else it does not work. I tried flag=1, no difference See below the exact code,’

     

    ONCE PerCent     = 0.5              //50%  = positions to close
    ONCE Pips        = 20 * PipSize
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
    IF close crosses over highest[20](high[1]) and not OnMarket THEN
    buy 10 contract at market
    Set Stop Loss SL
    endif
    IF close crosses under lowest[20](low[1]) and not OnMarket THEN
    sellshort Positionsize contract at market
    endif
    IF close >= (PositionPrice + Pips) AND LongOnMarket AND Flag=1 THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag=1 THEN
    EXITSHORT CloseQuantity Contracts AT Market
    Flag = 0
    ENDIF
    #161676 quote
    robertogozzi
    Moderator
    Master

    It works perfectly for me, with 10 positions.

    I noticed that you are using POSITIONSIZE.

    If you set POSITIONSIZE to an inituial value, than halve it without restoring its initial value when not onmarket, then the next trade it will be halved once more. I guess this might be the cause.

    Simply add this line after line 7:

    PositionSize = X     //restore initial value
    #161679 quote
    s00071609
    Participant
    Senior

    I tried with 10 position size, it doesn’t work. See the image, this is the code I ran

    What am I doing wrong? When I change to quantity to 1, it works. Neigher a variable position size nor anything above 1 is working for me

    ONCE PerCent     = 0.5              //50%  = positions to close
    ONCE Pips        = 20 * PipSize
    ONCE MinLotSize  = 0.5              //0.5  lots minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
    IF Not OnMarket THEN
    
    Flag = 1
    ENDIF
    
    IF  NOT OnMarket And ENTRY and OrderAllowed>0  THEN
    BUY 10 CONTRACT AT MARKET
    //set stop Loss sl
    //set target $profit 50
    endif
    
    
    IF close >= (PositionPrice + Pips) AND LongOnMarket AND Flag=1   THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    ELSIF close <= (PositionPrice - Pips) AND ShortOnMarket AND Flag=1 THEN
    EXITSHORT CloseQuantity Contracts AT Market
    Flag = 0
    ENDIF
    PRT2.png PRT2.png
    #161687 quote
    robertogozzi
    Moderator
    Master

    Use the GRAPH instructions that I posted, it’s better to spot any weird change in values, candle by candle.

    #161741 quote
    s00071609
    Participant
    Senior

    Please see the image

    The code is closing order despite flag value being zero at the bar the partial close occured

    PRTFLAG.png PRTFLAG.png
Viewing 15 posts - 16 through 30 (of 62 total)
  • You must be logged in to reply to this topic.

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
4 years, 10 months ago.

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