Backtesting and the ‘correct’ use of stops (Code that is)!!!!

Forums ProRealTime English forum ProOrder support Backtesting and the ‘correct’ use of stops (Code that is)!!!!

Viewing 13 posts - 1 through 13 (of 13 total)
  • #247095

    Hiya all coders,

    This one has had my head pickled of late!!!

    PLEASE NOTE: I only ever use the daily: 1 day / 1 bar.

    Background:
    —————
    To get a backtest benchmark, I use a fixed stop loss.
    Once the fixed stop loss backtest has been validated,
    I then move to a training stop system.

    I have found a way to validate the exit type (Separate panel)
    1. Stop loss hit SL (-1)
    2. Opposite signal triggered OS (+1)
    This is working and a good visual for said exit types.
    And provides a useful visual of the trade entry system, and its exit.

    Issue:
    ——–
    Currently, my code is not detecting the stop loss hits, and thus the trade continues!
    When the SL is detected, it can be days / bars later.
    I also notice that there are occurrences where the trade exit is nowhere near a stop loss level or an opposite signal !

    I have used both the system code => SET STOP PRICE
    And a manual check, ‘high or low’ values of the latest bar.
    The opposite signal trigger is working – and for some reason, the OS check is occurring before the SL!!!

    Subjectively:
    —————-
    The stop loss SL check should be performed before the opposite signal OS check.
    If, during the day, the price has been higher or lower than the SL level, then the SL should be triggered before the next bar / next day.
    if no stop loss is triggered, then the OS check should be performed, and if triggered the open trade is closed, and the opposite trade is opened (on the same bar)

    I hope this explanation makes sense.

    I appreciate any code guidance that provides a working solution.

    My thanks always.
    NT

    #247098
    JS

    Hi NT,
    A few comments regarding your code:

    • The logic in the signal definitions is incorrect…

    • Some variables are defined multiple times in different ways…

    • Misuse of “BarsSince”…

    • Certain variables are never reset…

    • myReverseToLong always remains zero…

    • myReverseToShort always remains zero…

    1 user thanked author for this post.
    #247103

    Hi NT,
    A few comments regarding your code…..

    Hey JS,

    My my, that sounds like a code mess to untangle…
    Would you be kind enough to help me, by showing the correction is code form?

    Is that possible ?
    With thanks in anticipation,

    NT

    #247105
    JS

    Hi NT,

    I would like to help you, but this is going to take too much time to get the code right…

    Line 22 and 23 you define myLongSignal and myShortSignal but in line 30 and 31 you redefine them…

    When you are “Long” then BarsSince(LongOnMarket) is always equal to zero so myPreviousEquity will be redefined repeatedly…

    myReverseToLong is reset to zero once but does not change after that…

    Same with myReverseToShort…

    Maybe better to start with a simpler strategy to understand the PRT logic…

    1 user thanked author for this post.
    #247108

    Also myExitType looks like it is always = 0?

    Beats me how you can author / amend / add to 148 lines of code Neo … but you can’t debug it?

    Idea so you can progress … run the code past AI / ChatGPT … you will get suggested fixes in a few seconds.

    1 user thanked author for this post.
    #247111

    Hi NT,

    I would like to help you, but this is going to take too much time to get the code right…

    Hi JS,
    Oh, okay….

    Can I ask that you send me a link to code, that can send me in the right direction….
    For the FIXED stop loss for instance….

    I ask this, as I am not sure if others may look in on this matter, as you have commented…. or shall I repost requesting more defined code guidance ??

    In the meantime, I’ll strip out the stop loss and opposite signal processes, and start again…

    My regards

    NT

    #247113

    Is below of any help

    https://www.prorealcode.com/documentation/ploss-2/

     

    2 users thanked author for this post.
    #247114
    JS

    Article about a “trailing stop”…
    https://www.prorealcode.com/blog/learning/kinds-trailing-stop-proorder/

    Article about “Breakeven” coding…
    https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/

    Article about using an indicator for the “stop loss”…
    https://www.prorealcode.com/blog/learning/moving-stoploss-dynamic-informations-proorder/

    I recommend checking the “Blog” section, which contains many interesting articles (including videos)…
    https://www.prorealcode.com/category/blog/

    2 users thanked author for this post.
    #247116

    Is below of any help

    https://www.prorealcode.com/documentation/ploss-2/

    Hey GraHal,
    Not sure how ploss is related to the stop loss solution I am looking for…

    Any ‘stop loss’ links you might know…?
    Aside, I’ll keep searching,

    Cheers,

    NT

    #247122

    Article about a “trailing stop”… <<< looking for fixed stop loss, not trailing stop 🙂
    https://www.prorealcode.com/blog/learning/kinds-trailing-stop-proorder/

    Article about “Breakeven” coding… <<< Can this be used in backtest, it states its for automated trading ?
    https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/

    Article about using an indicator for the “stop loss”… <<< Digesting this one, lovely, thank you 
    https://www.prorealcode.com/blog/learning/moving-stoploss-dynamic-informations-proorder/

    I recommend checking the “Blog” section, which contains many interesting articles (including videos)…
    https://www.prorealcode.com/category/blog/ 
    Reviewed said blog, but no specific finds for ‘fixed stop loss’

    I’ll strip out the code, and have a go refactoring the code for a simplistic fixed stop loss. Hopefully, that moves me in the right direction

    Thanks for the pointers

    NT.

    #247125

    Also myExitType looks like it is always = 0?

    Beats me how you can author / amend / add to 148 lines of code Neo … but you can’t debug it? <<< AI assisted, and the code was also provided by support at PRT.

    Idea so you can progress … run the code past AI / ChatGPT … you will get suggested fixes in a few seconds.

    Yep.
    Ive been running the code past several (5 no.) AIM’s for the past few hours…

    So far, they have made the code worse, even when I provide the explicit probacktest code syntax !!!

    I have trawled so much of the site, looking for FIXED stop loss backtest code samples, examples…. and so far, I am not seeing any answers on this one….

    Aside, I appreciate your involvement GraHal

    NT

    #247127

    looking for FIXED stop loss backtest code

    What do you mean by above?
    The link I provided (a few posts above) was for a FIXED stop loss of 50 points.
    You can optimise the points value or use a % instead of points.

    I use fixed stop loss in most of my Algos (example below) along with a coded Trailing Stop function.
    Plenty of coded TS’s on this link …

    Snippet Link Library

     

     

    2 users thanked author for this post.
    #247128

    What do you mean by above?……

    That is what I get for doing too many things at once….

    Indeed GraHal,

    You link does apply to a ‘fixed stop loss’.

    I am just setting some time aside to sort this code pickle out.
    Thank you, for the correction, and making sure I am on the right track.

    I’ll post my updated code.

    With thanks

    NT

    1 user thanked author for this post.
Viewing 13 posts - 1 through 13 (of 13 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login