Skip to content

Commit

Permalink
fix(okx): set leverage
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Apr 20, 2023
1 parent dcd701d commit d46711c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/exchanges/okx/okx.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,24 +516,29 @@ export class OKXExchange extends BaseExchange {

setLeverage = async (symbol: string, inputLeverage: number) => {
const market = this.store.markets.find((m) => m.symbol === symbol);
const position = this.store.positions.find((p) => p.symbol === symbol);

if (!market) throw new Error(`Market ${symbol} not found on OKX`);
if (!position) throw new Error(`Position ${symbol} not found on OKX`);

const leverage = Math.min(
Math.max(inputLeverage, market.limits.leverage.min),
market.limits.leverage.max
);

await this.xhr.post(ENDPOINTS.SET_LEVERAGE, {
instId: market.id,
lever: `${leverage}`,
mgnMode: 'cross',
});
if (position.leverage !== leverage) {
await this.xhr.post(ENDPOINTS.SET_LEVERAGE, {
instId: market.id,
lever: `${leverage}`,
mgnMode: 'cross',
});

this.leverageHash[market.id] = leverage;
this.store.updatePositions([
[{ symbol, side: PositionSide.Long }, { leverage }],
[{ symbol, side: PositionSide.Short }, { leverage }],
]);
this.leverageHash[market.id] = leverage;
this.store.updatePositions([
[{ symbol, side: PositionSide.Long }, { leverage }],
[{ symbol, side: PositionSide.Short }, { leverage }],
]);
}
};

fetchPositionMode = async () => {
Expand Down Expand Up @@ -713,7 +718,7 @@ export class OKXExchange extends BaseExchange {
side,
entryPrice: contracts ? parseFloat(p.avgPx) : 0,
notional: contracts ? Math.abs(parseFloat(p.notionalUsd)) : 0,
leverage: parseFloat(p.lever) || this.leverageHash[market.id] || 0,
leverage: this.leverageHash[market.id] || 0,
unrealizedPnl: contracts
? adjust(parseFloat(p.upl), market.precision.price)
: 0,
Expand Down

0 comments on commit d46711c

Please sign in to comment.