Closing half a position

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #136576 quote
    teapot2002
    Participant
    Average

    If I’m in a trade and price moves favourably, I am trying to close half the position and set a stop so that the trade can’t lose. I’ve written this simple code to help me debug it. It does’t seem to work on my spread betting platform. Could someone please tell me whether it should be possible and I’ve done something wrong in the code, or if there’s another way to do it? If it’s not possible on a spread betting platform, is it possible with CFDs?

    My screenshot shows that the code runs; Stopmoved = 1 and Soldhalf = 1, but there is no indication that the buy is executed and COUNTOFSHARES never changes to 1.

    IF NOT ONMARKET AND close < close[1] THEN    // conditions to enter a short trade
    SELLSHORT 2 perpoint at market
    ENDIF
     
    IF SHORTONMARKET AND close < tradeprice - 10 THEN // if the price moves 10pts in my favour...  
    breakevenLevel = tradeprice - 5 //...calculate the level for a stop 5pts below the trade open price...
    SELL AT breakevenLevel STOP     //...and place a new stop order on market at breakevenLevel, ie 5pts below the trade open price
    Stopmoved = 1
    if countofshortshares = 2 then
    buy 1 PERPOINT at market  //close half of the trade
    SoldHalf = 1
    endif
    ENDIF
    graph stopmoved
    graph soldhalf
    graph countofshortshares
    
    IF SHORTONMARKET AND CLOSE > TRADEPRICE + 2 THEN
    EXITSHORT AT MARKET
    ENDIF
    
    
    

    Many thanks in advance.

    Screenshot-2020-06-19-at-16.20.00.png Screenshot-2020-06-19-at-16.20.00.png
    #136583 quote
    robertogozzi
    Moderator
    Master

    Partial closure of positions is not allowed in automated trading, as of this writing. We all hope it will be in the future.

    Still, it is allowed in backtesting.

    #136601 quote
    Vonasi
    Moderator
    Master

    Partial closure is possible in backtesting even if IG don’t allow it in live trading but also your code at line 10 is

    buy 1 PERPOINT at market  //close half of the trade

    If you want to partially close a short position then you must use EXITSHORT and not BUY. BUY will reverse the position and all short positions will be closed as it is not possible to be long and short at the same time in a ProOrder strategy.

    #136885 quote
    teapot2002
    Participant
    Average

    Thanks, robertogozzi and Vonasi, and I take the point that I should have used EXITSHORT.

    Nicolas’ post here made me think that I could close half the position: https://www.prorealcode.com/topic/bollinger-band-coding-help-please/#post-80959

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

Closing half a position


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
teapot2002 @teapot2002 Participant
Summary

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

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