Back

Merge Prices and Indicators in One Single Dataframe in Research Environment

Hi All,

I love QC and I'm getting more and more into it, but I really hope the learning curve flattens out at some point because the most basic stuff takes ages to figure out! I guess the effort will pay off!

I'm just trying to join historical OHLC with some indicators in a single dataframe. I gave up after a long time trying.

The below code runs but the result is wrong as the index is not being kept properly.

Any help please?

df = qb.History(spy.Symbol, startDate, endDate, resolution)

mom = Momentum(11)
momdf = qb.Indicator(mom, "SPY", startDate, endDate, resolution)

results = pd.concat([df, momdf])
results

Update Backtest







Thanks for submitting the code used. The indexing issue comes primarily due to the fact that the historical data is multi-indexed. Since only one asset is being analyzed, it made sense to remove the symbol index from the OHLC DataFrame. This can be done via .unstack() or .reset_index(). The notebook below has the code above running and another indicator added was to show how multiple indicators can be created and implemented into DataFrames together.

0

Thank you!! I understand now :)

Emilio

0

Update Backtest





0

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.


Loading...

This discussion is closed