Trades API

Query historical trade data for prediction markets

Trades API

The Trades API provides access to historical trade data for prediction markets. Relays requests directly to Kalshi API.

Access

const dflow = new DFlowClient();
const trades = dflow.trades;

Methods

getTrades

Get historical trades with optional filters.

const { trades, cursor } = await dflow.trades.getTrades({
  ticker: 'BTCD-25DEC0313-T92749.99',
  limit: 100,
});
 
for (const trade of trades) {
  console.log(`${trade.side} @ ${trade.price} - ${trade.count} shares`);
}

Parameters:

NameTypeRequiredDescription
tickerstringNoFilter by market ticker
minTsnumberNoFilter trades after this Unix timestamp
maxTsnumberNoFilter trades before this Unix timestamp
limitnumberNoMaximum trades to return (1-1000, default 100)
cursorstringNoPagination cursor (trade ID) to start from

getTradesByMint

Get trades by outcome token mint address.

const { trades } = await dflow.trades.getTradesByMint('EPjFWdd5...', {
  limit: 50,
});
 
// Filter by timestamp
const recentTrades = await dflow.trades.getTradesByMint('EPjFWdd5...', {
  minTs: Date.now() / 1000 - 86400,  // Last 24 hours
});

Parameters:

NameTypeRequiredDescription
mintAddressstringYesMint address (ledger or outcome mint)
params.minTsnumberNoFilter trades after this Unix timestamp
params.maxTsnumberNoFilter trades before this Unix timestamp
params.limitnumberNoMaximum trades to return (1-1000, default 100)
params.cursorstringNoPagination cursor (trade ID) to start from

Trade Object

interface Trade {
  tradeId: string;
  ticker: string;
  side: string;
  yesPrice: number;
  noPrice: number;
  count: number;
  takerSide: string;
  createdTime: string;
}
 
interface TradesResponse {
  trades: Trade[];
  cursor?: string;
}

Examples

Get Recent Trades

const { trades } = await dflow.trades.getTrades({
  ticker: 'BTCD-25DEC0313-T92749.99',
  limit: 20,
});
 
console.log('Recent Trades:');
for (const trade of trades) {
  const time = new Date(trade.createdTime).toLocaleTimeString();
  console.log(`[${time}] ${trade.takerSide.toUpperCase()} ${trade.count} @ YES=$${trade.yesPrice}`);
}

Filter by Timestamp Range

// Get trades from a specific time range
const { trades } = await dflow.trades.getTrades({
  ticker: 'BTCD-25DEC0313-T92749.99',
  minTs: 1704067200,  // Jan 1, 2024
  maxTs: 1704153600,  // Jan 2, 2024
});
 
console.log(`Found ${trades.length} trades in range`);

Paginate Through All Trades

async function getAllTrades(ticker: string): Promise<Trade[]> {
  const allTrades: Trade[] = [];
  let cursor: string | undefined;
 
  do {
    const response = await dflow.trades.getTrades({
      ticker,
      limit: 100,
      cursor,
    });
    
    allTrades.push(...response.trades);
    cursor = response.cursor;
  } while (cursor);
 
  return allTrades;
}
 
const trades = await getAllTrades('BTCD-25DEC0313-T92749.99');
console.log(`Total trades: ${trades.length}`);

Real-time Trades

For real-time trade updates, use the WebSocket API:

await dflow.ws.connect();
 
dflow.ws.subscribeTrades(['BTCD-25DEC0313-T92749.99']);
 
dflow.ws.onTrade((trade) => {
  console.log(`New trade: ${trade.side} @ ${trade.price}`);
});

See the WebSocket API for more details.

On this page