DEV Community

TateLyman
TateLyman

Posted on • Originally published at devtools-site-delta.vercel.app

Limit Orders and Stop-Loss on Solana DEX — How to Build Them

The Problem

Solana DEXs like Jupiter don't natively support limit orders or stop-losses. You can only do market swaps.

The Solution: Background Price Monitoring

Build limit orders by polling prices and executing when conditions are met.

const orders = [];

async function checkOrders() {
  for (const order of orders) {
    const currentPrice = await getTokenPrice(order.token);
    if (order.type === 'limit_buy' && currentPrice <= order.targetPrice) {
      await executeSwap(order);
      notifyUser(order.userId, `Limit buy at $${currentPrice}`);
    }
    if (order.type === 'stop_loss' && currentPrice <= order.targetPrice) {
      await executeSell(order);
      notifyUser(order.userId, `Stop-loss triggered at $${currentPrice}`);
    }
  }
}
setInterval(checkOrders, 30000);
Enter fullscreen mode Exit fullscreen mode

Price Feeds

  1. Jupiter Price APIprice.jup.ag/v4/price?ids=<mint>
  2. DexScreener API — real-time DEX prices
  3. Pyth Network — oracle prices

Ready-Made Solution

My Telegram trading bot has all of this built in:

/limit <token> <price> <amount>
/stoploss <token> <price>
/orders
Enter fullscreen mode Exit fullscreen mode

Full source (4,500 lines): devtools-site-delta.vercel.app/sol-bot-source

Top comments (0)