Stop moved to entry when in profit

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #75795 quote
    Dave_the_trader
    Participant
    New

    Needing help to code so that the stop moves to entry (0) when the trade is in profit by a number of points?

    // indicators and parameters
    defparam cumulateorders = false
    
    
    defparam flatbefore = 083000
    defparam flatafter =170000
    
    possize = 10
    
    IF TIME = 090000 THEN //include the 0910 candle
    Top = highest[6](high) // go back 8 candles
    Bottom = lowest[6](low) // go back 8 candles
    LongsForDay = 0
    MaxPos = 1
    ENDIF
    
    IF TradeIndex = BarIndex THEN
    LongsForDay = LongsForDay +1
    ENDIF
    
    IF TIME >=090000 AND LongsForDay < MaxPos THEN
    BUY possize CONTRACTS AT Top +11 STOP
    SELLSHORT possize CONTRACTS AT Bottom -11 stop
    ENDIF
    
    set target profit 50
    set stop loss 170
    
    IF CLOSE-TradePrice>25 THEN
    set stop loss 0
    ENDIF
    
    #75797 quote
    robertogozzi
    Moderator
    Master

    I suggest to replace

    IF CLOSE-TradePrice>25 THEN

    with

    IF CLOSE-TradePrice>25*pipsize THEN

    to make sure it works with alla instruments.

    Your code seems fine, the only correction to make is line 30, since I don’t think setting a SL to 0 works, you’d better replace thet line with

    SELLSHORT possize CONTRACTS AT Tradeprice stop
    #75800 quote
    Vonasi
    Moderator
    Master

    You have long and short in the code so your stoploss change will not work anyway for both. Also Roberts suggestion of changing just the one line won’t work as if the price drops under the 25 pip gap the order will no longer be sent to market. You need something like this I think:

    if not onmarket then
    breakevenflag = 0
    endif
    
    IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
    breakevenflag = 1
    endif
    
    if breakevenflag = 1 then
    sell at positionprice stop
    exitshort at positionprice stop
    ENDIF
    
    
    robertogozzi thanked this post
    #75807 quote
    Dave_the_trader
    Participant
    New

    Thanks, Ive made the changes however I get a syntax error, please help?

     

    // indicators and parameters
    defparam cumulateorders = false
    
    defparam flatbefore = 083000
    defparam flatafter =170000
    
    possize = 10
    
    IF TIME = 090000 THEN //include the 0910 candle
    Top = highest[6](high) // go back 8 candles
    Bottom = lowest[6](low) // go back 8 candles
    LongsForDay = 0
    MaxPos = 1
    ENDIF
    
    IF TradeIndex = BarIndex THEN
    LongsForDay = LongsForDay +1
    ENDIF
    
    IF TIME >=090000 AND LongsForDay < MaxPos THEN
    BUY possize CONTRACTS AT Top +11 STOP
    SELLSHORT possize CONTRACTS AT Bottom -11 stop
    ENDIF
    
    set target profit 50
    set stop loss 170
    
    IF not onmarket then
    breakevenflag = 0
    ENDIF
    
    IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
    breakevenflag = 1
    ENDIF
    
    IF breakevenflag = 1 THEN
    sellshort possize contracts at positionPrice stop
    sell possize contracts at positionprice stop
    endif
    
    #75808 quote
    Dave_the_trader
    Participant
    New

    Sorry I see it didnt copy it all

    // indicators and parameters
    defparam cumulateorders = false
    
    defparam flatbefore = 083000
    defparam flatafter =170000
    
    possize = 10
    
    IF TIME = 090000 THEN //include the 0910 candle
    Top = highest[6](high) // go back 8 candles
    Bottom = lowest[6](low) // go back 8 candles
    LongsForDay = 0
    MaxPos = 1
    ENDIF
    
    IF TradeIndex = BarIndex THEN
    LongsForDay = LongsForDay +1
    ENDIF
    
    IF TIME >=090000 AND LongsForDay < MaxPos THEN
    BUY possize CONTRACTS AT Top +11 STOP
    SELLSHORT possize CONTRACTS AT Bottom -11 stop
    ENDIF
    
    set target profit 50
    set stop loss 170
    
    IF not onmarket then
    breakevenflag = 0
    ENDIF
    
    IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
    breakevenflag = 1
    ENDIF
    
    IF breakevenflag = 1 THEN
    sellshort possize contracts at positionprice stop
    sell possize contracts at positionprice stop
    endif
    
    //IF CLOSE-TradePrice>25 THEN
    //set stop loss 0
    ENDIF
    #75809 quote
    Dave_the_trader
    Participant
    New

    OK so clearly you can see I’m not a coder!

    #75810 quote
    Vonasi
    Moderator
    Master

    Please use the ‘Insert PRT Code’ button when posting code. I have tidied up your posts as best as I can but there was a lot of lines that were not PRT code so I had to guess a little.

    I think your syntax error is because you have an ENDIF at the end of the code that you do not need.

    #75811 quote
    Dave_the_trader
    Participant
    New

    Thank you so much, lets see how it behaves live tomorrow!

    #75841 quote
    Dave_the_trader
    Participant
    New

    Ok, so it doesn’t work live

    #75842 quote
    robertogozzi
    Moderator
    Master

    Line 32 does set BREAKEVENFLAG whenever you reach 25 pips, no matter whether you are in profit or loss, since it doesn’t tell between LONG and SHORT trades, I suggest replacing lines 32-34 with

    IF longonmarket and (CLOSE-TradePrice) > 25 THEN
       breakevenflag = 1
    ELSIF shortonmarket and (TradePrice-close) > 25 THEN
       breakevenflag = 1
    ENDIF

    you could actually combine the two IFs in just one line, but that would make the code much less clear.

    #75843 quote
    Vonasi
    Moderator
    Master

    Thanks for spotting that Robert – you can tell that I never trade short – so my brain does not work very well in that direction!

    #75844 quote
    Dave_the_trader
    Participant
    New
    // indicators and parameters
    defparam cumulateorders = false
    
    defparam flatbefore = 083000
    defparam flatafter =170000
    
    possize = 2
    
    IF TIME = 090000 THEN //include the 0910 candle
    Top = highest[6](high) // go back 8 candles
    Bottom = lowest[6](low) // go back 8 candles
    LongsForDay = 0
    MaxPos = 1
    ENDIF
    
    IF TradeIndex = BarIndex THEN
    LongsForDay = LongsForDay +1
    ENDIF
    
    IF TIME >=090000 AND LongsForDay < MaxPos THEN
    BUY possize CONTRACTS AT Top +11 STOP
    SELLSHORT possize CONTRACTS AT Bottom -11 stop
    ENDIF
    
    set target profit 50
    set stop loss 170
    
    IF not onmarket then
    breakevenflag = 0
    ENDIF
    
    IF longonmarket and (CLOSE-TradePrice) > 25 THEN
    breakevenflag = 1
    ELSIF shortonmarket and (CLOSE-TradePrice-CLOSE) > 25 THEN
    breakevenflag = 1
    ENDIF
    
    //IF breakevenflag = 1 THEN
    //sellshort possize contracts at positionprice stop
    //sell possize contracts at positionprice stop
    //ENDIF
    #75845 quote
    Dave_the_trader
    Participant
    New

    Is the above correct?

    When I try activate it for automatic trading I get:

    – Code is invalid. Please correct it.

    #75849 quote
    robertogozzi
    Moderator
    Master

    To write code, please use the <> “insert PRT code” button to make code easier to read and understand. Thank you.

    #75853 quote
    robertogozzi
    Moderator
    Master

    Is the above correct?

    When I try activate it for automatic trading I get:

    – Code is invalid. Please correct it.

    The only warning I get is that the variable BREAKEVENFLAG is not used. Why keeping it if you don’t use it?

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

Stop moved to entry when in profit


ProOrder support

New Reply
Author
Summary

This topic contains 25 replies,
has 4 voices, and was last updated by Vonasi
7 years, 6 months ago.

Topic Details
Forum: ProOrder support
Language: English
Started: 07/11/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...