close half position when reach half limit and move stoploss to breakeven

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #163347 quote
    kevin12345
    Participant
    Average

    hi,

    can someone help me out with this? i want to close half my position when the market has reach have my limit and then move stoploss to breakeven.

    ONCE PerCent = 0.50            //0.50 = close half positions
    IF positionperf = 0.5 AND LongOnMarket THEN
    SELL (abs(CountOfPosition) * PerCent) AT Market
    set stop ploss 0 
    ELSIF positionperf = 0.5 AND ShortOnMarket THEN
    EXITSHORT (abs(CountOfPosition) * PerCent) AT Market
    set stop ploss 0
    ENDIF
    

    here’s what i got so far

    thank you

    #163348 quote
    Vonasi
    Moderator
    Master

    You cannot expect POSITIONPERF to equal 0.5% exactly. Change it to >=

    Also SET STOP pLOSS 0 does not set any stop loss – it simply cancels any stop loss on the market. Use SELL or EXITSHORT AT POSITIONPRICE STOP and place this order at every bar.

    KumoNoJuzza thanked this post
    #163349 quote
    Vonasi
    Moderator
    Master

    Something like: (not tested)

    ONCE PerCent = 0.50            //0.50 = close half positions
    
    if not onmarket and (your long entry conditions) then
    buy 1 contract at market
    flag = 0
    endif
    
    if not onmarket and (your short entry conditions) then
    sellshort 1 contract at market
    flag = 0
    endif
    
    IF positionperf >= 0.5 AND LongOnMarket THEN
    SELL (abs(CountOfPosition) * PerCent) AT Market
    flag = 1
    endif
    
    IF positionperf >= 0.5 AND ShortOnMarket THEN
    EXITSHORT (abs(CountOfPosition) * PerCent) AT Market
    flag = 1
    ENDIF
    
    if flag then
    sell at positionprice stop
    exitshort at positionprice stop
    endif
    icharttop thanked this post
    #163350 quote
    kevin12345
    Participant
    Average

    im getting an error for line

    SELL (abs(CountOfPosition) * PerCent) AT Market

    at the “AT”

    #163351 quote
    Vonasi
    Moderator
    Master

    Sorry – I cut and pasted your code! Add CONTRACTS before AT.

    #163352 quote
    kevin12345
    Participant
    Average

    does not seem to work. i got picture. if it worked then it should sell half and then breakeven with this example.

    dgda.png dgda.png
    #163358 quote
    Vonasi
    Moderator
    Master

    Yes it needs a little tweeking:

    ONCE PerCent = 0.50            //0.50 = close half positions
    
    if not onmarket and (your long entry conditions) then
    buy 1 contract at market
    flag = 0
    endif
    
    if not onmarket and (your short entry conditions) then
    sellshort 1 contract at market
    flag = 0
    endif
    
    IF positionperf >= 0.5 AND LongOnMarket and not flag THEN
    SELL (abs(CountOfPosition)) * PerCent contracts AT Market
    flag = 1
    endif
    
    IF positionperf >= 0.5 AND ShortOnMarket and not flag THEN
    EXITSHORT (abs(CountOfPosition) * PerCent) contracts AT Market
    flag = 1
    ENDIF
    
    if flag then
    sell at positionprice stop
    exitshort at positionprice stop
    endif
    
    #163397 quote
    kevin12345
    Participant
    Average

    still the same result.

    i tried changing the codes but the results are still the same.

    if not onmarket and longconditions and buyentry then
    buy 1 contracts at market
    flag = 0
    endif
    
    ONCE PerCent = 0.50            //0.50 = close half positions
    IF ((close - positionprice) >= 0.5*longstoploss) AND LongOnMarket and flag = 0 THEN
    SELL (abs(CountOfPosition) * PerCent) contracts AT Market
    flag = 1
    ENDIF
    
    if flag = 1 then
    sell at positionprice stop
    endif
    #163398 quote
    kevin12345
    Participant
    Average

    think i got it

    if not onmarket and longconditions and buyentry then
    buy 1 contracts at market
    flag = 0
    endif
    
    ONCE PerCent = 0.50            //0.50 = close half positions
    IF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and flag=0 THEN
    SELL (abs(CountOfPosition) * PerCent) contracts AT Market
    flag = 1
    ENDIF
    
    if flag=1 then
    sell at positionprice stop
    endif
    #163415 quote
    Vonasi
    Moderator
    Master

    My last code worked just fine when I tested it. You seem to have added a variable called longstoploss which is not declared at any value in your code!

    #163562 quote
    kevin12345
    Participant
    Average

    my codes seems to work half the time. half being it close half and move stoploss to breakeven and the other one is when profit reaches half the limi, it closes half 2 times resulting closing the full trade. anyone got any ideas why?

    #163566 quote
    Vonasi
    Moderator
    Master

    As I said – I tested my code and it worked just fine.

    You changed the exit price calculation so that it is no longer a percentage using POSITIONPERF – why?

    It is highly likely that your problem is being created by the code that you are choosing not to show us – do you have any other stop orders in the code? Without all the information such as the value of longstoploss the instrument and time frame and the rest of the code it is very difficult for anyone to help you or see what you are saying is happening.

    A screenshot is worth a thousand words.

    #163569 quote
    kevin12345
    Participant
    Average

    heres the screen shot  of what my current codes are doing on prorealtime.

    defparam CUMULATEORDERS = false
    timeframe(15mn)
    ema37 = ExponentialAverage[37](close)
    ema50 = ExponentialAverage[50](close)
    bar15mn = barindex
    //conditions to go long
    longc1 = ema37 - ema50 > 2*pipsize
    longc2 = high[1] < high[2] and high[2] < high[3]
    longc3 = (low[1] crosses under ema37[1]) or (low[1] crosses under ema50[1])
    longc4 = (close[1] > ema50[1]) and (close[2] > ema50[2]) and (close[3] > ema50[3])
    longconditions = longc1 and longc2 and longc3 and longc4
    
    if not onmarket and (longconditions ) then
    prehigh15mn = high[1]
    prelow15mn = low[1]
    endif
    
    timeframe(default)
    
    timeframe(default)
    once tradeON = 1
    mybar = bar15mn
    if mybar <>mybar[1] then
    tradeon = 1
    endif
    
    //long entry trigger
    buyentry = (close >= prehigh15mn)
    spreadave = 2.5
    minstop = 5
    if not onmarket and longconditions and buyentry and tradeon then
    buy 1 contracts at market
    tradeon = 0
    flag = 0
    if (close - prelow15mn+spreadave*pipsize)/pipsize > minstop and (close - prelow15mn+spreadave*pipsize)/pipsize < 50 then
    longstoploss = (close - prelow15mn+spreadave*pipsize)/pipsize
    elsif (close - prelow15mn+spreadave*pipsize)/pipsize <= minstop then
    longstoploss = minstop
    elsif (close - prelow15mn+spreadave*pipsize)/pipsize >= 50 then
    longstoploss = 50
    endif
    endif
    
    longtarget = longstoploss
    
    
    set stop ploss longstoploss
    set target pprofit longtarget
    
    ONCE PerCent = 0.50            //0.50 = close half positions
    
    IF ((close - positionprice)/pipsize >= 0.5*longstoploss) AND LongOnMarket and not flag THEN
    SELL (abs(CountOfPosition) * PerCent) contracts AT Market
    flag = 1
    ENDIF
    
    
    if flag then
    sell at positionprice stop
    
    endif
    

    this is only for going long.

    dhjh.png dhjh.png
    #163572 quote
    kevin12345
    Participant
    Average

    however, on ig trading the results r different. when it reached half my limit, it close half and another half. resulting in closing full.

    #163573 quote
    kevin12345
    Participant
    Average

    this is the screenshot with the codes u suggested. line 52 to line 55 replaced with these

    IF positionperf >= 0.5 AND LongOnMarket and not flag THEN
    SELL (abs(CountOfPosition)) * PerCent contracts AT Market
    flag = 1
    endif
    sfdgsdf.png sfdgsdf.png
Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

close half position when reach half limit and move stoploss to breakeven


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kevin12345 @kevin12345 Participant
Summary

This topic contains 21 replies,
has 5 voices, and was last updated by GraHal
2 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/07/2021
Status: Active
Attachments: 3 files
Logo Logo
Loading...