Creating Trading Bots¶
Bot Architecture¶
graph LR
A[Market Data] --> B[Strategy]
B --> C{Decision}
C -->|Buy| D[Execute Buy]
C -->|Sell| E[Execute Sell]
C -->|Hold| F[Wait]
D --> G[Update Position]
E --> G
Basic Bot Structure¶
from traderion import TraderionBot, Dashboard
class BasicBot(TraderionBot):
def __init__(self, username, password, room_id, dashboard=None):
super().__init__(username, password, room_id)
self.dashboard = dashboard
if self.dashboard:
self.dashboard.update_core_data('status', 'Initializing')
def on_market_price_change(self, old_prices, new_prices):
# Your logic here
if self.dashboard:
self.dashboard.update_core_data('market_prices', new_prices)
self.dashboard.add_market_event('Price Change',
f"From {old_prices} to {new_prices}")
print(f"Price changed from {old_prices} to {new_prices}")
Key Event Handlers¶
| Event | Description |
|---|---|
on_market_price_change |
Triggered when market prices change |
on_new_client_call |
Triggered when a new client request arrives |
on_position_change |
Triggered when your position changes |
on_orders_change |
Triggered when your orders change |
Dashboard Integration¶
The Traderion dashboard provides real-time monitoring of your bot's activities. Here's how to integrate it:
Initialization¶
from traderion import Dashboard
# Create dashboard instance
dashboard = Dashboard(title="My Trading Bot")
# Pass dashboard to your bot
bot = BasicBot(username, password, room_id, dashboard=dashboard)
# Run bot with dashboard
dashboard.run_dashboard(bot.run, ())
Updating the Dashboard¶
Update the dashboard in your event handlers:
def on_position_change(self, old_position, new_position):
if self.dashboard:
self.dashboard.update_core_data('position', {
'amount': new_position['amount'],
'pnl': new_position['pnl'],
'rate': new_position.get('rate', 0)
})
self.dashboard.add_performance_metric('Position Change',
new_position['pnl'])
def on_new_client_call(self, client_call):
if self.dashboard:
self.dashboard.add_bot_event('New Client Call',
f"Direction: {'BID' if client_call['direction'] == 0 else 'ASK'}")
Custom Panels¶
Add custom panels for strategy-specific data:
def my_custom_panel(stdscr, y, x, height, width):
stdscr.addstr(y, x, "My Strategy", curses.A_BOLD)
stdscr.addstr(y + 1, x + 2, "Data 1: Value")
stdscr.addstr(y + 2, x + 2, "Data 2: Value")
self.dashboard.add_custom_panel(my_custom_panel)
Adding Strategies (Optional)¶
Strategies are optional modular components that can help organize your trading logic. You can:
- Implement trading logic directly in your bot class
- Use pre-built strategies
- Create custom strategies
- Combine multiple strategies
from traderion.strategies import EMA
class StrategyBot(TraderionBot):
def __init__(self, username, password, room_id):
super().__init__(username, password, room_id)
# Strategies are optional - you can implement logic directly in event handlers
self.strategy = EMA(self.api, {
'small_ema_period': 5,
'big_ema_period': 20
})
Best Practices¶
- Keep event handlers short and efficient
- Use separate methods for different trading logic
- Implement proper error handling
- Use logging to track bot decisions