Skip to main content

RiskClient

The RiskClient provides access to risk management features including limits, exposure, and pre-trade checks.

Risk Limits

const limits = await client.risk.getLimits();

console.log(limits);
// {
// maxOrderSize: { 'BTC': '10', 'ETH': '100' },
// maxPositionSize: { 'BTC': '50', 'ETH': '500' },
// maxDailyVolume: '1000000',
// maxOpenOrders: 100
// }

Pre-Trade Checks

Validate an order before submission:

const check = await client.risk.preTradeCheck({
side: 'buy',
asset: 'BTC',
quantity: 5.0,
price: 50000,
});

if (check.allowed) {
console.log('Order can be placed');
} else {
console.log(`Order rejected: ${check.reason}`);
}

// Check for warnings
if (check.warnings.length > 0) {
console.log('Warnings:', check.warnings);
}

// View limits
console.log(`Max quantity: ${check.limits.maxQuantity}`);
console.log(`Available margin: ${check.limits.availableMargin}`);

Risk Exposure

Get Total Exposure

const exposure = await client.risk.getExposure();

console.log(exposure);
// {
// totalExposure: '500000.00',
// netExposure: '250000.00',
// longExposure: '400000.00',
// shortExposure: '150000.00',
// byAsset: {
// 'BTC': { long: '300000.00', short: '50000.00', net: '250000.00' },
// 'ETH': { long: '100000.00', short: '100000.00', net: '0.00' }
// }
// }

Get Asset-Specific Exposure

const btcExposure = await client.risk.getAssetExposure('BTC');

console.log(`BTC Long: ${btcExposure.longExposure}`);
console.log(`BTC Short: ${btcExposure.shortExposure}`);
console.log(`BTC Net: ${btcExposure.netExposure}`);

Limit Utilization

const utilization = await client.risk.getUtilization();

console.log('Order Size Utilization:', utilization.orderSize);
// {
// current: '5.0',
// max: '10.0',
// utilization: '50.00'
// }

console.log('Position Utilization:', utilization.position);
console.log('Daily Volume Utilization:', utilization.dailyVolume);

Maximum Quantity

Get the maximum order quantity based on current limits and exposure:

const { maxQuantity } = await client.risk.getMaxQuantity({
side: 'buy',
asset: 'BTC',
price: 50000,
});

console.log(`Max BTC buy quantity: ${maxQuantity}`);

Risk Metrics

const metrics = await client.risk.getMetrics();

console.log(metrics);
// {
// var95: '25000.00',
// var99: '35000.00',
// sharpeRatio: '1.5',
// maxDrawdown: '15.2',
// beta: '0.85'
// }

Order Simulation

Simulate the impact of an order before placing it:

const simulation = await client.risk.simulateOrder({
side: 'buy',
asset: 'BTC',
quantity: 2.0,
price: 50000,
});

console.log(simulation);
// {
// newPosition: '7.0',
// newExposure: '350000.00',
// marginRequired: '35000.00',
// estimatedFee: '50.00',
// warnings: []
// }

if (simulation.warnings.length > 0) {
console.log('Order warnings:', simulation.warnings);
}

Types

import type {
RiskLimits,
PreTradeCheckParams,
PreTradeCheckResult,
RiskExposure,
LimitUtilization,
RiskMetrics,
SimulateOrderParams,
SimulateOrderResult,
} from 'zenotc';

RiskLimits

FieldTypeDescription
maxOrderSizeRecord<string, string>Max order size per asset
maxPositionSizeRecord<string, string>Max position per asset
maxDailyVolumestringMax daily trading volume
maxOpenOrdersnumberMax concurrent open orders

PreTradeCheckResult

FieldTypeDescription
allowedbooleanWhether order is allowed
reasonstring?Rejection reason if not allowed
warningsstring[]Warning messages
limits.maxQuantitystringMaximum allowed quantity
limits.availableMarginstringAvailable margin

RiskExposure

FieldTypeDescription
totalExposurestringTotal exposure (absolute)
netExposurestringNet exposure (long - short)
longExposurestringTotal long exposure
shortExposurestringTotal short exposure
byAssetRecord<string, {...}>Exposure breakdown by asset

SimulateOrderResult

FieldTypeDescription
newPositionstringResulting position size
newExposurestringResulting exposure
marginRequiredstringMargin required for order
estimatedFeestringEstimated trading fee
warningsstring[]Warning messages

Best Practices

Pre-Trade Validation

Always validate orders before submission in production:

async function placeOrderSafely(params: CreateOrderParams) {
const check = await client.risk.preTradeCheck({
side: params.side,
asset: params.asset,
quantity: params.quantity,
price: params.price,
});

if (!check.allowed) {
throw new Error(`Order rejected: ${check.reason}`);
}

if (check.warnings.length > 0) {
console.warn('Order warnings:', check.warnings);
}

return client.execution.createOrder(params);
}

Position Sizing

Use max quantity to determine optimal position size:

async function getOptimalQuantity(side: 'buy' | 'sell', asset: string, price: number) {
const { maxQuantity } = await client.risk.getMaxQuantity({ side, asset, price });

// Use a percentage of max for safety
const safeQuantity = parseFloat(maxQuantity) * 0.8;

return safeQuantity.toFixed(8);
}