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

  1. Price Thresholds: Define specific price levels (or thresholds) for each trading pair that will trigger buy or sell orders.

  2. Monitoring Prices: Continuously monitor the current market prices. When a price reaches a specified threshold, the bot executes the corresponding trade.

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

def fetch_prices(self):
    prices = {}
    for name, exchange in self.exchanges.items():
        prices[name] = {}
        for pair in self.trading_pairs:
            try:
                ticker = exchange.fetch_ticker(pair)
                prices[name][pair] = ticker['last']
            except Exception as e:
                print(f"Error fetching price from {name} for {pair}: {e}")
    return prices

def check_triggers(self, prices):
    for name, pairs in prices.items():
        exchange = self.exchanges[name]
        for pair, price in pairs.items():
            if pair in self.thresholds:
                buy_threshold = self.thresholds[pair]['buy']
                sell_threshold = self.thresholds[pair]['sell']

                # Check if the current price meets the buy trigger
                if price <= buy_threshold:
                    self.place_order(exchange, pair, 'buy', price)

                # Check if the current price meets the sell trigger
                elif price >= sell_threshold:
                    self.place_order(exchange, pair, 'sell', price)

def place_order(self, exchange, pair, side, price):
    try:
        # Place limit order
        order_type = 'limit'
        amount = 1  # Define the amount to trade
        if side == 'buy':
            order = exchange.create_limit_buy_order(pair, amount, price)
            print(f"Placed Buy Order on {exchange.id} for {pair}: {order}")
        else:
            order = exchange.create_limit_sell_order(pair, amount, price)
            print(f"Placed Sell Order on {exchange.id} for {pair}: {order}")

        # Store the order
        self.orders[pair] = order

    except Exception as e:
        print(f"Error placing order on {exchange.id} for {pair}: {e}")

def run(self):
    while True:
        prices = self.fetch_prices()
        if prices:
            self.check_triggers(prices)
        time.sleep(1)  # Sleep briefly to avoid busy waiting

if name == "main": exchanges = ['binance', 'kraken'] # List of exchanges to support trading_pairs = ['BTC/USDT', 'ETH/USDT', 'LTC/USDT'] # List of trading pairs

# Define buy and sell thresholds for each trading pair
thresholds = {
    'BTC/USDT': {'buy': 30000, 'sell': 35000},
    'ETH/USDT': {'buy': 2000, 'sell': 2500},
    'LTC/USDT': {'buy': 100, 'sell': 150}
}

bot = PriceBasedMarketMakingBot(exchanges, trading_pairs, thresholds)
bot.run()

▎Explanation of the Code

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

  2. Fetching Prices:

    • The fetch_prices method retrieves the latest prices for all specified trading pairs from each exchange.

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

  4. Placing Orders:

    • The place_order method places a limit order (either buy or sell) based on the specified action and logs the order.

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