Skip to content

Commit

Permalink
Merge pull request #18723 from carlosmiei/add-position-type
Browse files Browse the repository at this point in the history
feat(base): add position type
  • Loading branch information
kroitor committed Jul 31, 2023
2 parents 1ee4a2b + 8c80664 commit 1e752f2
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 25 deletions.
29 changes: 29 additions & 0 deletions python/ccxt/base/types.py
Expand Up @@ -44,6 +44,7 @@ class Balance(TypedDict):


IndexType = Union[str, int]
Numeric = Union[None, str, float, int]


class Trade(TypedDict):
Expand All @@ -60,3 +61,31 @@ class Trade(TypedDict):
takerOrMaker: str
cost: Union[None, str, float]
fee: TypedDict


class Position(TypedDict):
symbol: str
id: str
timestamp: int
datetime: str
contracts: Numeric
contractsSize: Numeric
side: str
notional: Numeric
leverage: Numeric
unrealizedPnl: Numeric
realizedPnl: Numeric
collateral: Numeric
entryPrice: Numeric
markPrice: Numeric
liquidationPrice: Numeric
hedged: bool
maintenanceMargin: Numeric
initialMargin: Numeric
initialMarginPercentage: Numeric
marginMode: str
marginRatio: Numeric
lastUpdateTimestamp: int
lastPrice: Numeric
percentage: Numeric
info: TypedDict
2 changes: 1 addition & 1 deletion ts/src/ascendex.ts
Expand Up @@ -2571,7 +2571,7 @@ export default class ascendex extends Exchange {
result.push (this.parsePosition (position[i]));
}
symbols = this.marketSymbols (symbols);
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePosition (position, market = undefined) {
Expand Down
27 changes: 18 additions & 9 deletions ts/src/base/Exchange.ts
Expand Up @@ -139,7 +139,7 @@ import { OrderBook as WsOrderBook, IndexedOrderBook, CountedOrderBook } from './
//

// import types
import { Market, Trade, Fee, Ticker, OHLCV, OHLCVC, Order, OrderBook, Balance, Balances, Dictionary, Transaction, DepositAddressResponse, Currency, MinMax, IndexType, Int, OrderType, OrderSide } from './types';
import { Market, Trade, Fee, Ticker, OHLCV, OHLCVC, Order, OrderBook, Balance, Balances, Dictionary, Transaction, DepositAddressResponse, Currency, MinMax, IndexType, Int, OrderType, OrderSide, Position } from './types';
export {Market, Trade, Fee, Ticker} from './types'

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -2832,7 +2832,7 @@ export default class Exchange {
return this.markets;
}

safePosition (position) {
safePosition (position): Position {
// simplified version of: /pull/12765/
const unrealizedPnlString = this.safeString (position, 'unrealisedPnl');
const initialMarginString = this.safeString (position, 'initialMargin');
Expand All @@ -2845,18 +2845,18 @@ export default class Exchange {
const percentageString = Precise.stringMul (Precise.stringDiv (unrealizedPnlString, initialMarginString, 4), '100');
position['percentage'] = this.parseNumber (percentageString);
}
return position;
return position as any;
}

parsePositions (positions, symbols: string[] = undefined, params = {}) {
parsePositions (positions, symbols: string[] = undefined, params = {}): Position[] {
symbols = this.marketSymbols (symbols);
positions = this.toArray (positions);
const result = [];
for (let i = 0; i < positions.length; i++) {
const position = this.extend (this.parsePosition (positions[i], undefined), params);
result.push (position);
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parseAccounts (accounts, params = {}) {
Expand Down Expand Up @@ -3074,11 +3074,11 @@ export default class Exchange {
throw new NotSupported (this.id + ' fetchPermissions() is not supported yet');
}

async fetchPosition (symbol: string, params = {}): Promise<any> {
async fetchPosition (symbol: string, params = {}): Promise<Position> {
throw new NotSupported (this.id + ' fetchPosition() is not supported yet');
}

async fetchPositionsBySymbol (symbol: string, params = {}): Promise<any> {
async fetchPositionsBySymbol (symbol: string, params = {}): Promise<Position[]> {
/**
* @method
* @name exchange#fetchPositionsBySymbol
Expand All @@ -3090,11 +3090,11 @@ export default class Exchange {
throw new NotSupported (this.id + ' fetchPositionsBySymbol() is not supported yet');
}

async fetchPositions (symbols: string[] = undefined, params = {}): Promise<any> {
async fetchPositions (symbols: string[] = undefined, params = {}): Promise<Position[]> {
throw new NotSupported (this.id + ' fetchPositions() is not supported yet');
}

async fetchPositionsRisk (symbols: string[] = undefined, params = {}) {
async fetchPositionsRisk (symbols: string[] = undefined, params = {}): Promise<Position[]> {
throw new NotSupported (this.id + ' fetchPositionsRisk() is not supported yet');
}

Expand Down Expand Up @@ -4407,6 +4407,15 @@ export default class Exchange {
throw new NotSupported (this.id + ' fetchTransactions () is not supported yet');
}
}

filterByArrayPositions (objects, key: IndexType, values = undefined, indexed = true): Position[] {
/**
* @ignore
* @method
* @description Typed wrapper for filterByArray that returns a list of positions
*/
return this.filterByArray (objects, key, values, indexed) as Position[];
}
}

export {
Expand Down
29 changes: 29 additions & 0 deletions ts/src/base/types.ts
Expand Up @@ -189,6 +189,35 @@ export interface DepositAddressResponse {
tag?: string;
}

export interface Position {
symbol: string;
id: string;
timestamp?: number;
datetime: string;
contracts?: number;
contractsSize?: number;
side: string;
notional?: number;
leverage?: number;
unrealizedPnl?: number;
realizedPnl?: number;
collateral?: number;
entryPrice?: number;
markPrice?: number;
liquidationPrice?: number;
hedged?: boolean;
maintenanceMargin?: number;
maintenanceMarginPercentage?: number;
initialMargin?: number;
initialMarginPercentage?: number;
marginMode: string;
marginRatio?: number;
lastUpdateTimestamp?: number;
lastPrice?: number;
percentage?: number;
info: any;
}

/** [ timestamp, open, high, low, close, volume ] */
export type OHLCV = [number, number, number, number, number, number];

Expand Down
6 changes: 3 additions & 3 deletions ts/src/binance.ts
Expand Up @@ -7212,7 +7212,7 @@ export default class binance extends Exchange {
for (let i = 0; i < response.length; i++) {
result.push (this.parsePosition (response[i], market));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePosition (position, market = undefined) {
Expand Down Expand Up @@ -7326,7 +7326,7 @@ export default class binance extends Exchange {
const account = await this[method] (query);
const result = this.parseAccountPositions (account);
symbols = this.marketSymbols (symbols);
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

async fetchPositionsRisk (symbols: string[] = undefined, params = {}) {
Expand Down Expand Up @@ -7422,7 +7422,7 @@ export default class binance extends Exchange {
result.push (parsed);
}
symbols = this.marketSymbols (symbols);
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

async fetchFundingHistory (symbol: string = undefined, since: Int = undefined, limit: Int = undefined, params = {}) {
Expand Down
3 changes: 2 additions & 1 deletion ts/src/bitget.ts
Expand Up @@ -4023,7 +4023,7 @@ export default class bitget extends Exchange {
result.push (this.parsePosition (position[i]));
}
symbols = this.marketSymbols (symbols);
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePosition (position, market = undefined) {
Expand Down Expand Up @@ -4141,6 +4141,7 @@ export default class bitget extends Exchange {
'liquidationPrice': liquidationPrice,
'entryPrice': this.parseNumber (entryPrice),
'unrealizedPnl': this.parseNumber (unrealizedPnl),
'realizedPnl': this.safeNumber (position, 'pnl'),
'percentage': this.parseNumber (percentage),
'contracts': contracts,
'contractSize': contractSizeNumber,
Expand Down
4 changes: 2 additions & 2 deletions ts/src/bybit.ts
Expand Up @@ -7179,7 +7179,7 @@ export default class bybit extends Exchange {
}
results.push (this.parsePosition (rawPosition));
}
return this.filterByArray (results, 'symbol', symbols, false);
return this.filterByArrayPositions (results, 'symbol', symbols, false);
}

async fetchUSDCPositions (symbols: string[] = undefined, params = {}) {
Expand Down Expand Up @@ -7254,7 +7254,7 @@ export default class bybit extends Exchange {
}
results.push (this.parsePosition (rawPosition, market));
}
return this.filterByArray (results, 'symbol', symbols, false);
return this.filterByArrayPositions (results, 'symbol', symbols, false);
}

async fetchDerivativesPositions (symbols: string[] = undefined, params = {}) {
Expand Down
2 changes: 1 addition & 1 deletion ts/src/coinex.ts
Expand Up @@ -3018,7 +3018,7 @@ export default class coinex extends Exchange {
for (let i = 0; i < position.length; i++) {
result.push (this.parsePosition (position[i], market));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

async fetchPosition (symbol: string, params = {}) {
Expand Down
4 changes: 2 additions & 2 deletions ts/src/digifinex.ts
Expand Up @@ -3218,7 +3218,7 @@ export default class digifinex extends Exchange {
for (let i = 0; i < positions.length; i++) {
result.push (this.parsePosition (positions[i], market));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

async fetchPosition (symbol: string, params = {}) {
Expand Down Expand Up @@ -3309,7 +3309,7 @@ export default class digifinex extends Exchange {
return this.extend (position, {
'collateral': this.safeNumber (response, 'margin'),
'marginRatio': this.safeNumber (response, 'margin_rate'),
});
}) as any;
}
}

Expand Down
2 changes: 1 addition & 1 deletion ts/src/huobi.ts
Expand Up @@ -6771,7 +6771,7 @@ export default class huobi extends Exchange {
'datetime': this.iso8601 (timestamp),
}));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

async fetchPosition (symbol: string, params = {}) {
Expand Down
2 changes: 1 addition & 1 deletion ts/src/krakenfutures.ts
Expand Up @@ -1754,7 +1754,7 @@ export default class krakenfutures extends Exchange {
// }
//
const result = this.parsePositions (response);
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePositions (response, symbols: string[] = undefined, params = {}) {
Expand Down
2 changes: 1 addition & 1 deletion ts/src/kucoinfutures.ts
Expand Up @@ -1077,7 +1077,7 @@ export default class kucoinfutures extends kucoin {
'unrealizedPnl': this.parseNumber (unrealisedPnl),
'contracts': this.parseNumber (Precise.stringAbs (size)),
'contractSize': this.safeValue (market, 'contractSize'),
// realisedPnl: position['realised_pnl'],
'realizedPnl': this.safeNumber (position, 'realised_pnl'),
'marginRatio': undefined,
'liquidationPrice': this.safeNumber (position, 'liquidationPrice'),
'markPrice': this.safeNumber (position, 'markPrice'),
Expand Down
4 changes: 2 additions & 2 deletions ts/src/okx.ts
Expand Up @@ -4816,7 +4816,7 @@ export default class okx extends Exchange {
const data = this.safeValue (response, 'data', []);
const position = this.safeValue (data, 0);
if (position === undefined) {
return position;
return undefined;
}
return this.parsePosition (position);
}
Expand Down Expand Up @@ -4904,7 +4904,7 @@ export default class okx extends Exchange {
for (let i = 0; i < positions.length; i++) {
result.push (this.parsePosition (positions[i]));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePosition (position, market = undefined) {
Expand Down
2 changes: 1 addition & 1 deletion ts/src/phemex.ts
Expand Up @@ -3419,7 +3419,7 @@ export default class phemex extends Exchange {
const position = positions[i];
result.push (this.parsePosition (position));
}
return this.filterByArray (result, 'symbol', symbols, false);
return this.filterByArrayPositions (result, 'symbol', symbols, false);
}

parsePosition (position, market = undefined) {
Expand Down

0 comments on commit 1e752f2

Please sign in to comment.