Z-Score to improve strategies

Forums ProRealTime English forum ProOrder support Z-Score to improve strategies

  • This topic has 18 replies, 6 voices, and was last updated 1 month ago by avatarBrad.

Tagged: ,

Viewing 15 posts - 1 through 15 (of 19 total)
  • #174383

    I am posting my version of the code to calculate Z-Score and manage it, in order to embed it into your strategies. It also comes with a simple test strategy.

    What is Z-score?

    Z-score measures the distance between the mean of some set of the statistical results and the given observation.

    A trader might suspect a dependency if he or she experiences a run of several consecutive profitable trades, or a run of several consecutive unprofitable trades. “Obviously, there was some kind of dependency or serial correlation among your trades [in this case], where winners were followed by winners and losers were followed by more losers,” Thomas Stridsman wrote in the April 1998 issue of Futures. “If this happens again, you’ll want to exploit the good times and perhaps avoid trading altogether in bad times.”

    In Forex systems, traders are interested in Z-score not of a trade’s return (profit/loss size) but rather in a Z-score of the outcome — was it a profitable one or a losing one?

    Z-score, calculated using winning and losing streaks, measures the dependence between the outcome of the previous position and the outcome of the next position.

    If we consider profitable positions as positive results and losing positions as negative statistical results, counting the total number of all wins, losses, overall trades, as well as the number of win and loss streaks, we can calculate a Z-score for a given

    trading strategy.

    Usually, Z-score fluctuates between -3 to +3, but sometimes, can go above and below these “limits”. A Z-score value of 0 means that we are dealing with completely random results.

    Each Z-score value has also a probability of dependence (P) associated with it, which informs us of how probable dependence between the trades is. P values below -2 and above +2 have high (>95%) probability of dependence between trades:

    • a positive Z-score means that:
      – a profitable position is likely to be followed by a losing one
      – a losing position should probably be followed by a winning one.
      Basically, it means that the probability of long winning and losing
    • a negative Z-score means that:
      – profitable positions are likely to be followed by more profitable positions
      – losing positions are to be followed by more losing positions.
      Basically, it means that winning or losing streaks are probable.

    It should also be noted that Z-score calculation makes sense only for sufficiently

    large samples. Math literature suggests a sample size of no less than 51 (some talk about 30) to get a reliable Z-score value.

    One of the attached pics shows correspondance between these calculations and PRT’s.

    I tested it on DAX, h1, 200K units.

    In the attached PIC about the formula, PRT stands for “the above formula in PRT Language”, not “PRT Formula”.

    6 users thanked author for this post.
    #174413

    While the computation is working fine, I had to slightly change the Z-Score management and the strategy, as follows (you can copy & past it or you can download the correct version of the ITF file):

    #174418

    Link to above added as Log 295 here …

    Snippet Link Library

    1 user thanked author for this post.
    #174462

    Roberto, I copy-pasted your Z-score code (partly from your 2nd post), but something seems to go wrong / off ? The strategy is my own.

    -1.13 (PRT) vs +11.2 (yours). Notice the ever increase throughout the chart (of ~15 trading days).

    The difference I see is that I am dealing with Brokerage Fees (which are significant) which influence the amount of gain.

    Thanks !

     

    PS: The one but last attachment is redundant.

     

     

     

    #174469

    Fees, spread etc… do not affect the results.

    Only accumulation of positions and the three conditions stated in the comments (before the code starts) can affect Z-Score.

     

    #174474

    a new trade is opened (usually due to a Stop & Reverse)

    That happens (happened) 3 times, and I figured it unimportant. After switching off that feature, no changes except for the results somewhat. The Z-Score PRT reports also changed a little (1st attachment).

     

    a trade is closed

    That does not happen as far as I am aware of.

     

    the new trade hits TP

    Impossible because I control this myself (no Set Target $Profit, etc.).


    I assed a part of the chart which better shows what happens (2nd attachment) . The red graph is obviously your Z-Score.

    I will try to look into it later. But if you have more hints, please don’t hesitate.

    #174477

    It should be something with the streaks. You probably can tell (I so far can not);

    The previous post showed a winning streak of 4, which increases the Z-Score multiple times. In the attachment below there’s a losing streak of 2, and this decreases the Z-Score.
    I obviously have more winning streaks than losing streaks (also see 2nd attachment in my first post from yesterday), zo net the Z-Score increases. … Without looking into the code, it seems that a 3rd win (or loss) in a streak again increases the Z-Score, which should not be ? (because it is the same streak). Probably …

    More later.

     

     

    #174482

    With my own method of using Twins (instead of Runs) I end up with the same “negative values only” as you, Roberto (exact same outcome).

    #174486

    And if I apply my own method of tallying Runs, I again arrive at your exact number.

    Something else is, that the feeling of it all should tell that I never would have such a low Z-Score as PRT is telling me. Thus Roberto, your remark that PRT would be using the Runs method could be correct, but the way PRT is calculating that should be (way ?) off.

    What remains is why your calculated Z-Score *is* the same as PRT’s …

    #174492

    It’s worth while testing it with several strategies on different TF’s. I have, and I think the formula is the same one.

    PRT, on the other side, can overcome my limitations (as stated in the comments), as they can exactly tell if more than one trade was exited on the same bar, which we can’t, and manage accumulation (though accumulation is discouraged as a general rule according to some websites).

    Anyway, being able to manage Z-Score and also drawdown (https://www.prorealcode.com/topic/when-to-stop-a-strategy-and-money-management-code-snippet/#post-174460) may help improving strategies.

    Neither is the Holy Grail, a poor strategy is likely to remain as such, but a good enough strategy could be improved a bit.

     

    #174495

    I would love to have it readily available in my strategies because it would in real time show “what’s next”. Actually as how the real Fx guys apply it. So I guess I won’t rest until I have it going as you intended it. It intrigues !

    Thank you again for making this available. 🙂 It appears not all that difficult, but someone has/had to do it (invest the time).

    #174521

    I think I found the culprit.

    It goes wrong at that line (or because of that line used as a trigger);
    The first attachment shows a small drain of money at the entry (this happens at each entry). The difference you see reported as -60.
    The second attachment shows the used BrokerFee (this is IB). It is 60 … (counts for entry and counts for exit again – this latter does not bother regarding the issue).
    And so each entry is seen as a “Streak” and counts as such. Also, it will count as a loss and it confuses everything.
    When I eliminate the BrokerFee, all is fine (fourth attachment) – I’ll grant PRT the small difference (Reverse Orders is inactive though measurement is over 157 trades so maybe rounding errors are in place somewhere.

    Remedy : Let trigger the Z-Score code by a means which does not incorporate the entry. It is not easy (for me) to see how to do that, knowing that we’d want general code for that (like Roberto’s example which nicely has it all at the top of the program code). I myself probably would dare to code something like :

    I hope this addition helps someone (BackTesting with PRT-IB or other brokers who charge commission).

    1 user thanked author for this post.
    #212191

    P              = 2 * TotalWin * TotalLose

    Do you see a problem changing line 67 to “P              = max(1, 2 * TotalWin * TotalLose)” ?

    There’s a Division by zero error after the first trade since only win or lose has a 1 and the other is 0. Figured this way was more appropriate than “IF Streaks > 0 AND TotalWin > 0 AND TotalLose > 0 THEN”.

    1 user thanked author for this post.
    #212243

    Well spotted paisantrader. Thank you 🙂

    #226771

    Hi Robertogozzi,

    I am stumbling upon this interesting idea;
    But I cannot see how I can use it concretly in an ON OFF strategy management;

    Can you explain it on this example drawn directly from your code and strat.

    Thanks

Viewing 15 posts - 1 through 15 (of 19 total)

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