Trigger Trailing SL if target is met

Viewing 15 posts - 16 through 30 (of 69 total)
  • Author
    Posts
  • #198335 quote
    Mr.Mjau
    Participant
    New

    Ok ,but how would I then go about if I want a trailing stop to start at just that level where   indicator3<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]</span> <span class=”crayon-st”>CROSSES OVER</span> indicator4<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>] </span>? Is it possible to  store this value in a constant that I can later use in “trailingstarts?

    #198336 quote
    GraHal
    Participant
    Master

    Is it possible to  store this value

    Value of what … price at crossing or what?

    #198408 quote
    Mr.Mjau
    Participant
    New

    Yes exactly, and have the price at crossing to be where trailing starts

    #198411 quote
    GraHal
    Participant
    Master

    My code does what you state above, but you still need a start and step value, for example 30, in the code so when price drops by >= 30 the trade will exit.

    If you have start value = 0 then if price drops by 1 point then trade exits.

    You are imagining that Trail starts at crossover and then price only goes up from there … what if you are Long and price goes down after crossover??

    You need to do loads of tests using your Demo Platform to see how all this hangs together else you will forever be working on ‘how you think PRT works’. 

    Mr.Mjau thanked this post
    #198440 quote
    Mr.Mjau
    Participant
    New
    // Conditions to enter long positions
    indicator1 = Average[20](close)
    indicator2 = Average[100](close)
    c1 = (indicator1 CROSSES UNDER indicator2)
    
    IF c1 THEN
    BUY 5 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    indicator3 = Average[20](close)
    indicator4 = Average[100](close)
    c2 = (indicator3[2] CROSSES OVER indicator4[2])
    
    IF c2 THEN
    SELL AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 500
    

    1y backtesting gave 50 trades and 8.8% time in market. Stoploss was never triggerd during this year.

    // Conditions to enter long positions
    indicator1 = Average[20](close)
    indicator2 = Average[100](close)
    c1 = (indicator1 CROSSES UNDER indicator2)
    
    IF c1 THEN
    BUY 5 CONTRACT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    indicator3 = Average[20](close)
    indicator4 = Average[100](close)
    c2 = (indicator3[2] CROSSES OVER indicator4[2])
    
    If c2 Then
    // Stops and targets
    trailingstart = 1   //trailing will start @trailinstart points profit
    trailingstep  = 1     //trailing step to move the "stoploss"
     
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    Endif
    SET STOP pLOSS 500
    

    This is with the trailing stoploss triggerd at c2. Trailingstart and trailingstep set to 1 should give a similar result as to the one without trailing stop.

    1y backtesting gave 23 trades and 61% time in market. Stoploss was  triggerd 2 during this year.

    I tried with different values for trailingstart and trailing step, but nothing works. As you can see the trailing stop does not work as it should and does almost never sell where it should, and that is “why time in market” goes from 8% to 61%. The results are day and night and they should be almost the same.

     

    After alot of detective work. The second one with trailing, will only trigger “IF indicator3[2] CROSSES OVER indicator4[2] AND (now this is strange) if 2 candles after the cross over the candle goes lower then the close of the one before”

    Somehow the trailing stop gets bound with what happends 2 candles after C2 and works ONLY in this candle and ONLY if this cadles low is lower then the previous candles close. And if it dont happend this candle, the bot needs to wait for next C2 bore it can try again. This almost never happends and this is why time in market exploded.

    (Yes I have a delay on the sale with 2 candles, but I have the same problem with my bots without it)

    I hope I make some sence, and if you have time can help me from here

    Without-trailing.jpg Without-trailing.jpg With-trailing.jpg With-trailing.jpg
    #198443 quote
    phoentzs
    Participant
    Master

    The logic sounds familiar to me. What time frame?

    #198445 quote
    Mr.Mjau
    Participant
    New

    30 min

    Yes somehow the trailing stop gets bound to this and only this candle and only work here.

     

    Now we just need to set it free 🙂

    #198446 quote
    phoentzs
    Participant
    Master

    trailingstart = 1 and trailingstep =1 is nonsense.
    Just run it through the optimizer.

    trailingstart from 20 to 200 in increments of 10 and trailingstep from 5 to 50 in increments of 5. Should be quick.

    #198449 quote
    Mr.Mjau
    Participant
    New

    Yes 1 and 1 is nonsence. But they should give the same result as the one without trailing stop, and it does not.

    Have tried manually alot of differnet numbers and can ofc get a better reslut then when I use 1 and 1.

    But no matter what numbers I use, It still only works in just that 1 candle and this is the problem.

    (Don’t know that optimizer is, mb you can link smt?)

    #198450 quote
    phoentzs
    Participant
    Master

    No, can’t give the same results at all. In your case the system would try to secure a point if you win one point. which is bullshit. Can someone please show him the basics? I don’t have a laptop here.

    #198451 quote
    GraHal
    Participant
    Master

    Why are you using below … this means the cross occured 2 bars ago?

    c2 = (indicator3[2] CROSSES OVER indicator4[2])
    

    Why not use …

    c2 = (indicator3 CROSSES OVER indicator4)
    #198452 quote
    GraHal
    Participant
    Master

    Re settings for Start and Step … it be very useful to get your head around how a Trailing Stop works …

    https://www.investopedia.com/terms/t/trailingstop.asp

    https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/

    #198453 quote
    Mr.Mjau
    Participant
    New

    Yes it would close out almost immediately, just like if diden’t even have it.

    But the problem is not if I use 1-1 or 1-30

    The problem is the trailing stop ONLY work in one candle.

    #198454 quote
    Mr.Mjau
    Participant
    New

    Yes I want it to sell on delay from cross.

    But the problem remains indicator3(2) or indicator3, the trailing stop ONLY works in just that one candle.

    #198467 quote
    GraHal
    Participant
    Master

    trailing stop ONLY works in just that one candle.

    It will with a Start of 1 and a Step of 1 or 30 ?

    Post your latest code and I will test it on my Platform.

    Mr.Mjau thanked this post
Viewing 15 posts - 16 through 30 (of 69 total)
  • You must be logged in to reply to this topic.

Trigger Trailing SL if target is met


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Mr.Mjau @mr-mjau Participant
Summary

This topic contains 68 replies,
has 5 voices, and was last updated by Mr.Mjau
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/30/2022
Status: Active
Attachments: 17 files
Logo Logo
Loading...