Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why use natural date instead of bar date? #460

Open
none2003 opened this issue Nov 21, 2024 · 0 comments
Open

Why use natural date instead of bar date? #460

none2003 opened this issue Nov 21, 2024 · 0 comments

Comments

@none2003
Copy link

Hi

I'm testing WeighInvVol algo,and I found it uses natural date instead of bar date, for example, I have following data

2023-12-25 1.042 1.117 1.064 4.634 2.299 106.870
2023-12-26 1.041 1.121 1.105 4.647 2.308 106.873
2023-12-27 1.038 1.128 1.072 4.660 2.335 106.806
2023-12-28 1.034 1.128 1.058 4.662 2.310 106.627
2023-12-29 1.030 1.130 1.051 4.648 2.268 106.562
2024-01-02 1.030 1.136 1.064 4.666 2.299 106.678
2024-01-03 1.023 1.146 1.047 4.663 2.286 106.835

the strategy is

s1 = bt.Strategy('Inv Vol',
                 [bt.algos.RunMonthly(run_on_first_date=False, run_on_end_of_period=False, run_on_last_date=False),
                  bt.algos.SelectAll(),
                  bt.algos.WeighInvVol(lookback=pd.DateOffset(days=5), lag=pd.DateOffset(days=1)),
                  bt.algos.Rebalance()])

according to code of WeighInvVol:

prc = target.universe.loc[t0 - self.lookback : t0, selected]

When t0 is '2024-01-01', the data range will be '2023-12-27':'2024-01-01', so the prc will be:

2023-12-27 1.038 1.128 1.072 4.660 2.335 106.806
2023-12-28 1.034 1.128 1.058 4.662 2.310 106.627
2023-12-29 1.030 1.130 1.051 4.648 2.268 106.562

clearly, it uses natural date.

If use bar date, since '2024-01-02' is the first available date of January, so 't0' should be '2024-01-02', and the prc calculation could be:

prc = target.universe[target.universe.index < t0][selected].tail(self.lookback)

then prc will be:

2023-12-25 1.042 1.117 1.064 4.634 2.299 106.870
2023-12-26 1.041 1.121 1.105 4.647 2.308 106.873
2023-12-27 1.038 1.128 1.072 4.660 2.335 106.806
2023-12-28 1.034 1.128 1.058 4.662 2.310 106.627
2023-12-29 1.030 1.130 1.051 4.648 2.268 106.562

I think this make more sense.

Another problem of using natural date is, the data range of every period varies, "t0 - self.lookback : t0" could give range from 3 to 5, this inconsistency is detrimental to subsequent calculations of inverse volatility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant