Author Topic: Z-score from EMA difference with Ratio Model  (Read 2318 times)

svkuz

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Z-score from EMA difference with Ratio Model
« on: March 01, 2023, 05:53:01 am »
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:

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

  • Administrator
  • Sr. Member
  • *****
  • Posts: 336
  • Karma: +13/-0
    • Pair Trading Lab
Re: Z-score from EMA difference with Ratio Model
« Reply #1 on: March 01, 2023, 04:12:33 pm »
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

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Z-score from EMA difference with Ratio Model
« Reply #2 on: March 02, 2023, 05:19:09 pm »
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

  • Administrator
  • Sr. Member
  • *****
  • Posts: 336
  • Karma: +13/-0
    • Pair Trading Lab
Re: Z-score from EMA difference with Ratio Model
« Reply #3 on: March 03, 2023, 11:26:06 am »
You can take a look to PTL Trader implementation: https://github.com/quantverse/ptltrader

svkuz

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
Re: Z-score from EMA difference with Ratio Model
« Reply #4 on: June 05, 2023, 05:22:59 pm »
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.