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, androom_id. - It sets up the
RSIstrategy with the specified parameters:periodis set to 14, which is the number of periods used to calculate the RSI.overboughtthreshold is set to 70, indicating that the market is overbought when the RSI value exceeds this threshold.oversoldthreshold 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_changeMethod:- This method is a callback that handles market price changes.
- It calls the
executemethod of theRSIstrategy 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.