Pair Trading Lab > Questions & Answers

Z-score from EMA difference with Ratio Model

(1/1)

svkuz:
Hello!

Firtly, congratulate with all work done, it's really helpful.

I have done some own tests to do backtests, and I found that your z-score its different than mine, using this backtest as a reference: https://www.pairtradinglab.com/backtests/Y-33ItOP-XTOENSI

Details:

* I am using Python to calculate all necessary values
* I am using library pandas_ta library to do the calculations of EMA and STD DEV (https://github.com/twopirllc/pandas-ta)
What could be wrong in my calculations of z-score to be near of yours?

I attached my dataframe exported to excel to compare.

Thank you again,
Best Regards.

admin:
Probably the unstable period of EMA. EMA is an infinite-response filter, so you have to set the unstable period to "cut" the history.
https://ta-lib.org/d_api/ta_setunstableperiod.html

svkuz:
Thank you for your answer.

I have tried this with 34 or 100 unstable period and it gives me the same z-score result.

Also, I tried changing compability and no way: https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_EMA.c#L285

What else can be?

Thank you again,
Best Regards.

admin:
You can take a look to PTL Trader implementation: https://github.com/quantverse/ptltrader

svkuz:
After reviewing the implementation. I found that the Zscore it is calculated here: method getZScore in PairTradingModelRatio.java

But, I cannot return the same results as on the backtest. Please, correct me if I understood bad on the calculation of the zscore:

1. Calculate the min and max ratios from closing ASK and BID prices of the current date for both symbols. To do this, it is used MultiRatio class

2. Calculate historial ratio using closing historical data (not bid or ask) and do: stock1 close price / stock2 close prince

3. Calculate current EMA with 21 periods using historical Ratio calculated on previous step

4. Calculate STD DEV with 21 periods using historical Ratio calculated on step 2

5. Calculate zscoremin:  (minratio - EMA of step 3) / STD DEV of step 4
    Calculate zscoremax: (maxratio - EMA of step 3) / STD DEV of step 4

6. Finally, calculate the mean of zscoremin and zscoremax: (zscoremin + zscoremax) / 2 and this is the final zscore which appear on the trade log

What am I doing wrong?

Many thanks in advance,
Best Regards.

Navigation

[0] Message Index

Go to full version