Dear Colleagues,
I am new to QC and coding. I am trying to get daily close price in “research environment” for different futures. But due to closing times of them are different I face a problem to compare daily closing prices. For example. XX futures closing timedate is 19-00, and ZZ futures close at 20-00. When I unstack I get 2 raws:
ZZ XX
YY-DD-MM 19-00 Nan | 100.23
YY-DD-MM 20-00 98.15 | Nan
What is proper solution for that?
Mia Alissi
Hello Emil,
In the research environment, you can use the
pandaslibrary'sresamplemethod to resample the data to daily frequency and then use thelastmethod to get the last price of the day which would be the closing price. Here is an example:This will give you the closing price for each day for your futures data. The 'D' parameter in the
resamplemethod stands for 'Daily'. Thelastmethod will get the last non-NaN value of the day which should be the closing price.Please note that this will still give you NaN values for the days when there is no trading activity. If you want to fill these NaN values, you can use the
fillnamethod with the 'ffill' method which propagates the last valid observation to fill the gaps:This will fill the NaN values with the last valid observation. Please be aware that this might introduce lookahead bias into your model if the last valid observation is from a future date.
I hope this helps! If you have any more questions, feel free to ask.
Best,
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Emil Mammadsadikhov
Dear Mia. Let me add some clarifications with code:
qb.tickers = ["GC", "ZS", "ZM"]
for x in qb.tickers:
qb.AddFuture(x)
history = qb.History(qb.Securities.Keys, 100, Resolution.Daily)
history.index = history.index.droplevel([0]) #to remove meaningless data
# after that I get multi index dataframe. index 0 = ticker, Index 1 = time in YY-DD-MM hh-mm-ss format.
df = history['close'].unstack(level = 0)
# I unstack at level 0 and get ['close'] prices. as a result I get the mentioned frame (1st column is index now)
ZZ XX
YY-DD-MM 19-00 Nan | 100.23
YY-DD-MM 20-00 98.15 | Nan
At what stage I should use df = df.resample('D').last() ?
Mia Alissi
Hello Emil,
You should use the
resamplemethod after you have unstacked the DataFrame. Here is how you can do it:This will resample your DataFrame to daily frequency and get the last price of the day which should be the closing price. The 'D' parameter in the
resamplemethod stands for 'Daily'. Thelastmethod will get the last non-NaN value of the day.Please note that this will still give you NaN values for the times when there is no trading activity. If you want to fill these NaN values, you can use the
fillnamethod with the 'ffill' method which propagates the last valid observation to fill the gaps:This will fill the NaN values with the last valid observation. Please be aware that this might introduce lookahead bias into your model if the last valid observation is from a future date.
I hope this helps! If you have any more questions, feel free to ask.
Best,
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Emil Mammadsadikhov
Thank you. That helped
Mia Alissi
I'm glad to hear that you found the information helpful, Emil! If you have any more questions or need further clarification, don't hesitate to ask. Happy coding!
Best,
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Emil Mammadsadikhov
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!