Skip to content

RSI Bot Example

This example demonstrates how to create a bot using the Relative Strength Index (RSI) strategy. The RSI is a momentum oscillator that measures the speed and change of price movements. It is used to identify overbought or oversold conditions in the market.

Bot Implementation

from traderion import TraderionBot
from traderion.strategies import RSI

class RSIBot(TraderionBot):
    """
    The bot uses the Relative Strength Index (RSI) strategy to make trading decisions.

    It identifies overbought and oversold conditions based on the RSI value.
    If the RSI value is above the overbought threshold, the bot sells.
    If the RSI value is below the oversold threshold, the bot buys.
    """

    def __init__(self, username, password, room_id):
        super().__init__(username, password, room_id)
        self.strategy = RSI(self.api, {
            'period': 14,
            'overbought': 70,
            'oversold': 30
        })

    def on_market_price_change(self, old_prices, new_prices):
        """
        Callback for market price changes.
        """
        # Implement RSI strategy logic
        self.strategy.execute(new_prices)

Running the Bot

if __name__ == "__main__":
    bot = RSIBot('username', 'password', room_id=123)
    bot.run()

Detailed Explanation

Initialization

  • __init__ Method:
  • The bot initializes with the provided username, password, and room_id.
  • It sets up the RSI strategy with the specified parameters:
    • period is set to 14, which is the number of periods used to calculate the RSI.
    • overbought threshold is set to 70, indicating that the market is overbought when the RSI value exceeds this threshold.
    • oversold threshold is set to 30, indicating that the market is oversold when the RSI value falls below this threshold.

Handling Market Price Changes

  • on_market_price_change Method:
  • This method is a callback that handles market price changes.
  • It calls the execute method of the RSI strategy to get the RSI value and make trading decisions.
  • If the RSI value is above the overbought threshold, the bot sells.
  • If the RSI value is below the oversold threshold, the bot buys.

Next Steps