Add a mood filter to a trading bot
A bot can skip or shrink entries when the market mood is at an extreme. Here is how to read the index once a day and test the rule on your own price data before you use it.
You need: a bot or a backtest that you already run, your own price data and an API key (opens in a new tab).
Get an API key
Create a key and keep it in an environment variable, not in your code. Every request uses credits.


Read the index once a day
Call
get_market_indexfor the last 30 or 90 days. The value for a day is calculated once, for the previous UTC day, so one call a day is enough.Write the rule
Decide what an extreme is, for example a value above or below a level you choose, and what the bot does then: skip the entry or use a smaller size.
Test it on your own history
Run the rule on your price data for the same days and compare the result with and without it. Try other levels and other periods before you rely on it.
What you will see
A rule that uses the daily index, and a backtest that shows how your own results change with and without it.
Data used
get_market_indexthrough the API (opens in a new tab) with one key.Limits
- A rule that fits the past may fail later. The index describes mood, not prices.
- The latest value is for the previous day.
More use cases
Look for crowded moods
When almost everyone is bullish or bearish, a portfolio may be more exposed to a sudden reversal.
Send a message when the mood hits an extreme
A small script that tells you only when the market mood goes beyond limits you chose.
Write a weekly market mood note
Summarize how the market mood changed during the week for your team or your clients.
Test whether mood leads returns
Check whether changes in the index come before changes in prices in your own data.