Stop Loss Placement

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #105944 quote
    jpgcreatestrading
    Participant
    Average

    Hi, i am wanting to know whether setting a “stop loss”, or “target profit” within an if statement along with the “buy order” has the same results as placing the “stop loss” / “target profit” at the end of the code?

    i have been working on a system and seemed to get different results.

    when i used an if statement to check for “buycondition” and then placed a “buy x shares at market” and ” “stop loss” and “target profit” all within the same if statement, i had very different results as when i ran the exact same code but instead:
    used an if statement to check for “buycondition” and then placed a “buy x shares at market”. then placed the “stop loss” and “target profit” at the end of the code… what is the reason for this please?

    thank you

    #105962 quote
    GraHal
    Participant
    Master

    It would be easier to help if you posted the code? Even just a snippet will do.

    Do you mean using Set Stop pLoss and Set Stop pProfit?

    #105989 quote
    jpgcreatestrading
    Participant
    Average

    Hi sorry, that would be a good idea. i will add the code here.

    the code below has the stop loss within the if statement

    defparam cumulateorders = false
    
    if intradaybarindex = 0 then
    startprice = 0
    finishprice = 0
    endif
    
    if time = 090000 then
    startprice = close
    startlength = intradaybarindex
    endif
    
    if time = 203000 then
    finishprice = close
    finishlength = intradaybarindex
    y = 1
    endif
    
    if y = 1 then
    xlength = finishlength - startlength
    xprice = abs(finishprice - startprice)
    arctan = atan(xprice / xlength)
    highprice = highest[xlength](close)
    lowprice = lowest[xlength](close)
    endif
    
    minsl = 5     // the minimum points allowed for a stop loss on Instrument
    
    positionsize = 1
    SL = max(0.5 * xprice, minsl)
    TP = max(0.5 * xprice, minsl)
    
    // -----  >>> BUY CRITERIA  <<<  ----- //
    buycriteria = y = 1 and arctan > 50
    
    // ---  Buy  --- //
    if buycriteria then
    buy positionsize Shares at highprice stop
    set stop loss SL
    set target profit TP
    endif
    
    // -----  >>> SHORT CRITERIA  <<<  ----- //
    shortcriteria =  y = 1 and arctan > 50
    
    // ---  Short  --- //
    if shortcriteria then
    sellshort positionsize Shares at lowprice stop
    set stop loss SL
    set target profit TP
    endif
    
    if onmarket then
    y = 0
    endif
    

     

     

    the code below has the stop loss at the end of the code.

    defparam cumulateorders = false
    
    if intradaybarindex = 0 then
    startprice = 0
    finishprice = 0
    endif
    
    if time = 090000 then
    startprice = close
    startlength = intradaybarindex
    endif
    
    if time = 203000 then
    finishprice = close
    finishlength = intradaybarindex
    y = 1
    endif
    
    if y = 1 then
    xlength = finishlength - startlength
    xprice = abs(finishprice - startprice)
    arctan = atan(xprice / xlength)
    highprice = highest[xlength](close)
    lowprice = lowest[xlength](close)
    endif
    
    minsl = 5     // the minimum points allowed for a stop loss on Instrument
    
    positionsize = 1
    SL = max(0.5 * xprice, minsl)
    TP = max(0.5 * xprice, minsl)
    
    // -----  >>> BUY CRITERIA  <<<  ----- //
    buycriteria = y = 1 and arctan > 50
    
    // ---  Buy  --- //
    if buycriteria then
    buy positionsize Shares at highprice stop
    endif
    
    // -----  >>> SHORT CRITERIA  <<<  ----- //
    shortcriteria =  y = 1 and arctan > 50
    
    // ---  Short  --- //
    if shortcriteria then
    sellshort positionsize Shares at lowprice stop
    endif
    
    if onmarket then
    y = 0
    endif
    
    set stop loss SL
    set target profit TP
    

    please bare in mind, these are not good strategies, i am just playing around as new to this and am wanting to iron out a few of my misunderstandings.

    i dont understand how the placement of the stop loss / target profit effects the code / strategy as i thought once the “set stop loss SL” / “set target profit TP” has been set they were set in stone and therefore could not be changed?

     

    Thank you in advance

    #105991 quote
    robertogozzi
    Moderator
    Master

    in the latter case move lines 49-51 to 18, so that Y, even if set to 1 the following day, will be immediately cleared when onmarket.

    As you have put it lines 13-16 will set Y to 1 the following day and before you clear it again at the end it will have changed your SL in the meantime.

    #105992 quote
    jpgcreatestrading
    Participant
    Average

    How come the codes are effected differently though? In both cases, I would have assumed the stoploss and target profit to be altered as in both cases currently the SL and TP values are chaging,  yet only in one it seems to make a difference.

     

    Is it that, if the stop loss / target profit are written at the bottom they can only be set once and cannot be reset until the trade closes? However when they are placed in an if code along with a buy /sell then they are not set in stone, and can be changed even if the trade continues.

     

    It seems that in one circumstance the stop loss / target profit are trailing values and in the other case they are constant like I would expect. Is this the case?

     

    I understand that changing lines 49 – 51 would resolve this, in this instance but I am wanting to know the difference and how the software runs the code depending on where the SL and TP are placed. As I will probably have this problem again but it wouldn’t necessarily be an easy fix next time.

    #105993 quote
    robertogozzi
    Moderator
    Master

    Whenever a SET STOP or SET TARGET is encountered, ProOrder will execute it. If the SL & TP values are the same, your trade won’t be affected, but if those values change while OnMarket your trades will be affected!

    You may decide to either never change SL & TP when OnMarket or place a SET instruction in a place that ProOrder cannot reach while OnMarket.

    I also suggest to add AND NOT ONMARKET to your conditions because DEFPARAM CUMULATEORDERS=FALSE grants you another trade won’t be opened, but SET instructions would be executed nonetheless.

    #106000 quote
    jpgcreatestrading
    Participant
    Average

    Ah OK thank you for the explanation I am understanding much more now.

     

    I still struggle to understand how the two sections of code above are giving me different results.

    If the SL and TP values are changing due to reasons you stated, how come they don’t change equally in both different instances in each strategy.

    In both the strategies above I would assume the SL and TP values change as the y = 1 to reset the xprice and so both strategies should continue along the same path. But for some reason one of the strategies seems to close a trade soon after (next day or so) and the other strategy seems to keep a trade open for a long time (upto 6 months in one case 200,000 bars ago). This is the bit I don’t understand as I would assume they both should perform the same as the SL TP should change equally in both situations.

     

    So that brings me to the question, does placing a set stop loss within the if statement change how it would perform when compared to placing it at the end? Even if all the SL values change and would effect both placements….?

     

    Sorry if I’m not making sense

    #106001 quote
    jpgcreatestrading
    Participant
    Average

    Is there a difference between:

    If buycondition then
    Buy x shares at market
    Set stop loss y
    Set target profit y
    Endif

    And this:

    If buycondition then
    Buy x shares at market
    Endif
    
    Set stop loss y
    Set target profit y
    

    As i thought both the examples would be the same but when i use them in the strategies above its this difference that delivers different results

    #106003 quote
    Nicolas
    Keymaster
    Master

    Code is read from top to bottom, at each candle Close. If you calculate the value of y on each candle, it is obvious that your TP/SL will change accordingly. If you want to set it only one time, use the way you did in your first example.

    #106006 quote
    Vonasi
    Moderator
    Master

    Because in the first code they are only read if your buy conditions are true and in the second they are read at the close of every single bar. so the first code only changes the TP and SL to value y some of the time and the second code does it all of the time.

    jpgcreatestrading thanked this post
    #106011 quote
    Nicolas
    Keymaster
    Master

    You can also add a “not onmarket” condition to prevent the code to be read even if you are already in position and if the buycondition happen again:

    If buycondition AND NOT LONGONMARKET then
     Buy x shares at market
     Set stop loss y
     Set target profit y
    Endif
    jpgcreatestrading thanked this post
    #106016 quote
    jpgcreatestrading
    Participant
    Average

    Ah I think I finally understand. Because in the first strategy there was a condition of arctan > 50, new “set stop” “set target” were only set if y=1 AND arctan > 50, whereas in second strategy new “set stop” “set target” were set if y=1 only

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

Stop Loss Placement


ProOrder: Automated Strategies & Backtesting

New Reply
Summary

This topic contains 11 replies,
has 5 voices, and was last updated by jpgcreatestrading
6 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/30/2019
Status: Active
Attachments: No files
Logo Logo
Loading...