Is there a way to close half of the position as soon as it triggers?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #134126 quote
    crolakstrading
    Participant
    Senior
    // --- settings
    amount = 2 //amount of contract/lot/shares to open for each order
    takeprofit = 30 //takeprofit in points 
    stoploss = 60 //stoploss in points 
    BreakevenAt = 25 //percent achieved of target to move stop to entry (breakeven)
    PointsToKeep = 1 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    Lot2Close = 1 //amount of contract/lot/shares quantity to close when breakeven occurs
    // --- end of settings
    
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
     
    startBreakeven =  takeprofit*(BreakevenAt/100)//how much pips/points in gain to activate the breakeven function?
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF LONGONMARKET AND breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    if countoflongshares=amount then 
    sell Lot2Close contract at market
    endif 
    ENDIF
    // --- end of BUY SIDE ---
     
    // --- SELL SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
     
    //place the new stop orders on market at breakevenLevel
    IF SHORTONMARKET AND breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    if countofshortshares=amount then
    exitshort Lot2Close contract at market
    endif
    ENDIF
    // --- end of SELL SIDE ---

    I have found the above coding from @nicolas which close half the position when breakeven.

    I just wanted to know if there a way to close half the position as soon as it triggered or 1pips in profit?? just to reduce risk straight away!!  appreciate if anyone could help.

    Thanks in advanced

    #134146 quote
    Nicolas
    Keymaster
    Master

    That could be coded but be aware that partial closure of position is still not possible through IG with ProOrder in live trading.

    #134157 quote
    crolakstrading
    Participant
    Senior

    Thank you for your response @Nicolas. above coding shows as its working on IG Pr0Order but i haven’t tried it!
    how can i make this work separately, please.. means breakeven and close half working separately

    let’s say.. 50pips stop loss, 100pips Target and once 50pips in profit stop loss to breakeven!! also when trade triggered and 2pips in profit close half of the position to reduce risk!! i have tried but really need some help.

    defparam cumulateorders = false
    // --- settings
    amount = 0.2 //amount of contract/lot/shares to open for each order
    takeprofit = 150 //takeprofit in points
    stoploss = 50 //stoploss in points
    Lot2Close = 0.1 //amount of contract/lot/shares quantity to close when breakeven occurs
    // --- end of settings
     
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND xxxxxx THEN
    BUY amount CONTRACTS AT EntryPrice STOP
    if countoflongshares=amount then
    sell Lot2Close contract at market
    endif
    ENDIF
     
    // Conditions to exit long positions
    If LongOnMarket AND short THEN
    SELL AT MARKET
    ENDIF
     
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND xxxxxxx THEN
    SELLSHORT amount CONTRACTS AT EntryPrice STOP
    if countofshortshares=amount then
    buy Lot2Close contract at market
    ENDIF
    ENDIF
     
    // Conditions to exit short positions
    IF ShortOnMarket AND long THEN
    EXITSHORT AT MARKET
    ENDIF
    
    
    // Stops and targets : Enter your protection stops and profit targets here
    // points based STOP LOSS and TRAILING STOP
    // initial STOP LOSS
    SET STOP pLOSS stoploss
    //set target of positions at 100 points
    SET TARGET PROFIT takeprofit
    
    startBreakeven = 50 //how much pips/points in gain to activate the breakeven function?
    PointsToKeep = 3  //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    // --- end of BUY SIDE ---
    
    // --- SELL SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    // --- end of SELL SIDE ---
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Is there a way to close half of the position as soon as it triggers?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by crolakstrading
5 years, 9 months ago.

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