Triggers on price
The "Triggers on Price" feature allows a market-making bot to execute trades based on specific price movements or thresholds. This can be particularly useful for responding to market volatility, capturing arbitrage opportunities, or adjusting orders when prices reach certain levels.
▎How It Works
Price Thresholds: Define specific price levels (or thresholds) for each trading pair that will trigger buy or sell orders.
Monitoring Prices: Continuously monitor the current market prices. When a price reaches a specified threshold, the bot executes the corresponding trade.
Execution Logic: The bot can place limit orders, market orders, or adjust existing orders based on the price triggers.
▎Example Code
Below is an example implementation of the "Triggers on Price" feature using Python and the ccxt library.
▎Sample Code
import ccxt import time
class PriceBasedMarketMakingBot: def init(self, exchanges, trading_pairs, thresholds): self.exchanges = {name: ccxt.getattribute(name)() for name in exchanges} self.trading_pairs = trading_pairs self.thresholds = thresholds # Dictionary of thresholds for each pair self.orders = {} # Store active orders
if name == "main": exchanges = ['binance', 'kraken'] # List of exchanges to support trading_pairs = ['BTC/USDT', 'ETH/USDT', 'LTC/USDT'] # List of trading pairs
▎Explanation of the Code
Initialization:
The bot initializes with a list of exchanges and trading pairs.
It defines thresholds for buy and sell actions for each trading pair.
An empty dictionary orders is created to store active orders.
Fetching Prices:
The fetch_prices method retrieves the latest prices for all specified trading pairs from each exchange.
Checking Triggers:
The check_triggers method checks if the current market price meets any predefined thresholds.
If the price is below the buy threshold, it places a buy order.
If the price is above the sell threshold, it places a sell order.
Placing Orders:
The place_order method places a limit order (either buy or sell) based on the specified action and logs the order.
Running the Bot:
The run method continuously fetches prices and checks triggers in an infinite loop.
The bot sleeps briefly (1 second) to avoid busy waiting.
▎Considerations
Dynamic Thresholds: You may want to implement logic to adjust thresholds dynamically based on market conditions or user input.
Error Handling: Ensure robust error handling for API calls and order placements.
Performance Monitoring: Implement logging to monitor trades and performance over time.
Last updated