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).

  1. Get an API key

    Create a key and keep it in an environment variable, not in your code. Every request uses credits.

    An example request with the API key in a header
  2. Read the index once a day

    Call get_market_index for 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.

  3. 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.

  4. 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_index through 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.