Back

Previous High (in Python)

Hello,

I am very new to QuantConnect and have basic question.

How do I get for example previous high of traded stock?

I tried something like this:

def Rebalance(self):
history = self.History("SPY",2, Resolution.Daily)
history.loc["SPY"]['high'][0]

That works in Research but not in API.

What is correct way?

Thank you,

Peter

Update Backtest








There are a few idiosyncrasies with the ‘History’ method. In this specific case, the method doesn’t like a single symbol and wants a list of symbols (even if the list only has a single symbol). Put brackets around the symbol to turn it into a list. The following code should work.

# Get history (notice the brackets to make a list)
history = self.History([“SPY”], 2, Resolution.Daily)


Also, your reference works but I’m fond of the approach below. Also, maybe check out the following post for some more on the 'History' idiosyncrasies.

# Get history (notice the brackets to make a list)
history = self.History([“SPY”], 2, Resolution.Daily)


# Extract just the high prices and use the 'unstack' method to make a column for each equity

high_prices = history.high.unstack(level=0)

# Get the high for just AAPL and take the first value using 'head'
high_aapl = high_prices['AAPL'].head(1)
https://www.quantconnect.com/forum/discussion/2641/history-method-not-returning-a-dataframe-in-python
1

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