Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can I shift the date to the nearest trading day?

I want to close all my position before er earnings to lower the volatility.
My data looks like:

date symbol
2010/1/10 mu
2010/1/12 aapl
....

so, I want to close all position on 2010/1/9 if I have mu, and close all position on 2010/1/11 if I have aapl.
I am trying to shift the dataframe by shift(1,freq='D'), but if 2010/1/9 is Sat. I want to shift to 2010/1/8,which
is a trading day.
Can someone help me on this?

Best

1 response

sorry, I attached the wrong algorithm