Basic Trading
A complete walkthrough of placing, monitoring, and canceling orders.
Setup
typescript
import { HundredX } from '100x-sdk';
const sdk = await HundredX.create({
privateKey: process.env.PRIVATE_KEY!,
brokerId: Number(process.env.BROKER_ID!),
});Place a Limit Buy Order
typescript
const order = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: true,
amount: '1.0',
price: '2500.00',
orderType: 'LIMIT',
});
console.log(`Order ${order.id}: ${order.status} — ${order.side} ${order.amountx18} @ ${order.pricex18}`);Place a Market Order
typescript
const marketOrder = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: true,
amount: '0.1',
price: '0',
orderType: 'MARKET',
});Check Orders
typescript
// Get all orders via /api/v1/order
const allOrders = await sdk.info.getOrders();
for (const o of allOrders.orders) {
console.log(`[${o.status}] ${o.side} ${o.marketSymbol}: ${o.amountx18} @ ${o.pricex18} (id: ${o.id})`);
}
// Get open orders via /api/v1/user/orders
const openOrders = await sdk.info.getUserOrders();
for (const o of openOrders.orders) {
console.log(`[${o.OrderType}] ${o.operation} product=${o.productID}: ${o.amountx18} @ ${o.pricex18}`);
}
// Get a specific order
const order = await sdk.info.getOrder(12345);
console.log(`Order ${order.id}: ${order.status}`);Cancel a Single Order
typescript
const result = await sdk.exchange.cancelOrder(12345);
console.log(result.message);Cancel All Orders for a Market
typescript
const result = await sdk.exchange.cancelAllOrders(1); // Market ID
console.log('Cancelled:', result.cancelledOrderIds);Check Positions
typescript
const positions = await sdk.info.getPositions();
for (const pos of positions.positions) {
console.log(
`Product ${pos.productID}: amount=${pos.vBalanceAmount}, quote=${pos.vQuoteBalance}, leverage=${pos.leverage}`
);
}
// Recent trade history
for (const trade of positions.tradeHistory.slice(-5)) {
console.log(`${trade.type}: ${trade.amountx18} @ ${trade.pricex18}, pnl=${trade.pnl}`);
}Check Buying Power
typescript
const bp = await sdk.info.getBuyingPower();
console.log('Total buying power:', bp.totalBuyingPower);Place a Take-Profit Order
typescript
const tpOrder = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: false, // Sell to take profit on a long
amount: '1.0',
price: '2700.00',
orderType: 'LIMIT',
isReduce: true,
triggerPrice: '2700.00',
triggerCondition: 'TP',
});Place a Stop-Loss Order
typescript
const slOrder = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: false, // Sell to cut losses on a long
amount: '1.0',
price: '2300.00',
orderType: 'LIMIT',
isReduce: true,
triggerPrice: '2300.00',
triggerCondition: 'SL',
});Full Example: Open and Close a Position
typescript
import { HundredX, HundredXApiError } from '100x-sdk';
async function tradeRoundTrip() {
const sdk = await HundredX.create({
privateKey: process.env.PRIVATE_KEY!,
brokerId: Number(process.env.BROKER_ID!),
});
try {
// 1. Open a long position
console.log('Opening long position...');
const openOrder = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: true,
amount: '1.0',
price: '3000.00',
orderType: 'MARKET',
});
console.log('Order placed:', openOrder.id, openOrder.status);
// 2. Check position
const positions = await sdk.info.getPositions();
console.log('Positions:', positions.positions.length);
const buyingPower = await sdk.info.getBuyingPower();
console.log('Buying power:', buyingPower.totalBuyingPower);
// 3. Close the position
console.log('Closing position...');
const closeOrder = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: false,
amount: '1.0',
price: '2000.00',
orderType: 'MARKET',
isReduce: true,
});
console.log('Close order:', closeOrder.id, closeOrder.status);
} catch (error) {
if (error instanceof HundredXApiError) {
console.error(`API Error [${error.code}]: ${error.message}`);
} else {
throw error;
}
}
}
tradeRoundTrip();