Different stop losses for longs and shorts

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #57248 quote
    Nobody
    Participant
    Veteran

    Just wondering if its possible to have 2 different stop loss strategies in a trading strat for longs and shorts and what is the syntax to acheive this  . lets say this set of parameters for longs

    SET STOP pLOSS 10
    SET TARGET pPROFIT 15
    SET STOP pTRAILING 20

    and this set of parameters for shorts

    SET STOP pLOSS 20
    SET TARGET pPROFIT 30
    SET STOP pTRAILING 25

    Ive had a few shots at it but cant get it working  in anyway

    #57252 quote
    Vonasi
    Moderator
    Master

    You can just set variables say TP and SL within your IF THEN BUY SELL like this:

    IF your conditions THEN
    BUY 1 Contract AT MARKET
    TP = 10
    SL = 5
    ENDIF
    
    IF your conditions THEN
    SELLSHORT 1 Contract AT MARKET
    TP = 20
    SL = 10
    ENDIF
    
    SET TARGET pPROFIT TP
    SET STOP pLOSS SL
    Nobody thanked this post
    #57268 quote
    Nobody
    Participant
    Veteran

    You can just set variables say TP and SL within your IF THEN BUY SELL like this:

    Awesome thanks for that , would appear to be perfect for the job at hand , I really appreciate the level of knowledge and generosity in these forums

    #57271 quote
    robertogozzi
    Moderator
    Master

    Brisvegas, ProOrder won’t allow you to use both STOP orders as in your lines 2-3, line 2 will be immediately overridden by line 3 (since it comes last) and it will not be reported as an error.

    Vonasi‘s code is the correct way to accomplish different SLs and TPs for LONG and SHORT trades.

    Despite not being 100% sure I want to point out that your code

    IF your conditions THEN
       BUY 1 Contract AT MARKET
       TP = 10
       SL = 5
    ENDIF

    might modify TP and SL values even when OnMarket, because if your conditions are true the BUY won’t be executed in case you forbade this from happening with DEFPARAM ComulateOrders = FALSE, but all lines will be evaluated and variables set accordingly. So, in case you have some trailing stop code that has previously changed those value, they might be restored to the original ones, thus producing unwanted results.

    I suggest that the above code be changed to

    IF your conditions AND Not OnMarket THEN
       BUY 1 Contract AT MARKET
       TP = 10
       SL = 5
    ENDIF

    to make sure that when OnMarket the whole IF…ENDIF block is skipped.

    GraHal and Nobody thanked this post
    #57274 quote
    Vonasi
    Moderator
    Master

    Thanks for spotting that Robertogozzi. I always use AND NOT OnMarket but left is out for simplification as I always assume that everyone else knows what they are doing!

    Many strategies can be confused by having variables set every time entry conditions are met even if the actual BUY and SELL does not take place due to CumulateOrders = false.

    #57308 quote
    Nobody
    Participant
    Veteran

    I do know what im doing but i got to say the resources for actually learning this code are very skinny outside this forum  , I am a trader first and then a coder , i have the maths of all this down pat , its just half a line of syntax that slows me down and for the life of me i could find no documentation to assist me  , I only ask here as a last resort unlike some , working it out for yourself is the best way if you can , trust me i have tried to work on some syntax for weeks before asking in here  ..

    #57309 quote
    Nobody
    Participant
    Veteran

    LOL would be great if the people who build the website were as talented as the coders here  . This should be front and centre and in a drop down menu on every page in this site  . I am actually starting a bookmark folder for this place as there is little rhyme or reason to the cataloguing of content  😉       https://www.prorealcode.com/documentation/

    #57312 quote
    Vonasi
    Moderator
    Master

    Sorry Brisvegas I was not trying to imply that you do not know what you are doing. Sometimes the written word is not the best form of communication. I was actually criticising myself for always assuming too much. As my girlfriend always says Assume makes an ASS out of U and ME.

    Six months ago I spent a week of my life trying to fathom out how SET STOP and SET TARGET worked within a code which is why I happily answered your opening post.

    #57320 quote
    Nicolas
    Keymaster
    Master

    LOL would be great if the people who build the website were as talented as the coders here  .

    Sorry, I do my best…

    #57325 quote
    Vonasi
    Moderator
    Master

    LOL would be great if the people who build the website were as talented as the coders here .

    Sorry, I do my best…

    ….and your best is very very good even when you are supposed to be on holiday!

    #57338 quote
    jebus89
    Participant
    Master

    is it possible to write the code like this?

    if longonmarket then
    set stop ploss 50
    set target pprofit 50
    endif
    #57353 quote
    Vonasi
    Moderator
    Master

    is it possible to write the code like this?

    Yes. The SET instruction just places an order on the market that is in place until the position is closed or until the SET value is set to zero. I just find that having the SET instructions at the end of a code with a variable allows more elegant adjustment of stops and targets.

    #57354 quote
    jebus89
    Participant
    Master

    Thats what i thought Vonasi, thanks for confirming.

    #57462 quote
    Nobody
    Participant
    Veteran

    LOL would be great if the people who build the website were as talented as the coders here .

    Sorry, I do my best…

    Sorry Nicolas you do a great job overall no doubt

    #57599 quote
    MaoRai54
    Participant
    Master

    Hi everybody,

     

    after many trials of placing the different set for loss&profit in long and short positions, I’ve done the following way and it seems to give backtests with less tick/tick values.

    what do you think?

     

    IF LONGONMARKET AND CLOSE>=(TRADEPRICE+(TPL*PIPSIZE)) OR CLOSE<=(TRADEPRICE-(SL*PIPSIZE)) THEN

    SELL AT MARKET

    ENDIF

     

    IF SHORTONMARKET AND CLOSE<=(TRADEPRICE-(TP1*PIPSIZE)) OR CLOSE>=(TRADEPRICE+(SL1*PIPSIZE)) THEN

    EXITSHORT AT MARKET

    ENDIF

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

Different stop losses for longs and shorts


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Nobody @brisvegas Participant
Summary

This topic contains 18 replies,
has 7 voices, and was last updated by Vonasi
8 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/04/2018
Status: Active
Attachments: No files
Logo Logo
Loading...