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)!!!!

  • This topic has 7 replies, 3 voices, and was last updated 3 minutes ago by avatarJS.
Viewing 8 posts - 1 through 8 (of 8 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.

    #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
    #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/

Viewing 8 posts - 1 through 8 (of 8 total)

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