Hi,
Using Quantbook I would like to use both Brain Sentiment 30 day indicator and price to filter the coarse universe but it prints the indicator twice instead of the price. This is my research code:
# QuantBook Analysis Tool
# For more information see [https://www.quantconnect.com/docs/v2/our-platform/research/getting-started]
qb = QuantBook()
# Requesting Data
aapl = qb.add_equity("AAPL", Resolution.DAILY).symbol
symbol = qb.add_data(BrainSentimentIndicator30Day, aapl).symbol
# Historical data
'''
history = qb.history(BrainSentimentIndicator30Day, symbol, 30, Resolution.DAILY)
for (symbol, time), row in history.iterrows():
print(f"{symbol} sentiment at {time}: {row['sentiment']}")
'''
# Add Universe Selection
def universe_selection(alt_coarse: List[BrainSentimentIndicatorUniverse]) -> List[Symbol]:
return [d.symbol for d in sorted([x for x in alt_coarse if x.sentiment_7_days],
key=lambda x: x.sentiment_7_days, reverse=True)[:10]]
universe = qb.add_universe(BrainSentimentIndicatorUniverse, universe_selection)
# Historical Universe data
universe_history = qb.universe_history(universe, qb.time-timedelta(5), qb.time)
for (_, time), sentiments in universe_history.items():
for sentiment in sentiments:
print(f"{sentiment.symbol} 7-day sentiment at {sentiment.end_time}: {sentiment.sentiment_7_days} price: {sentiment.price}")
And this is a sample of what I get:
UIHC VCCC90PCGF39 7-day sentiment at 2024-06-03 12:00:00: 0.7876 price: 0.7876
COCP WSRW88XX4PR9 7-day sentiment at 2024-06-03 12:00:00: 0.802 price: 0.802
DC XXE3335KR339 7-day sentiment at 2024-06-03 12:00:00: 0.8122 price: 0.8122
EGHT R735QTJ8XC9X 7-day sentiment at 2024-06-03 12:00:00: 0.8477 price: 0.8477
Any help will be greatly appreciated.
Cheers,
Andres
Mia Alissi
Hi Andres,
It seems like the issue you're encountering is that the sentiment value is being printed twice, once for the sentiment and once for the price. This is because the
sentiment.price
attribute is not the price of the asset but the value of the sentiment indicator.The
BrainSentimentIndicatorUniverse
object does not directly provide the price of the asset. To get the price of the asset, you would need to use theHistory
method with the symbol of the asset.Here is an example of how you can get the price of the asset:
Please note that this will give you the last price of the asset. If you want the price at a specific time, you would need to index the history DataFrame with the specific time.
For more information on how to use the
History
method, you can refer to the QuantConnect documentation.I hope this helps!
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.
Andres Arizpe
You're awesome Mia, thank you very much.
Mia Alissi
You're welcome, Andres! I'm glad I could help. 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.
Andres Arizpe
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!