|
22 | 22 | from nautilus_trader.adapters.binance.common.schemas.account import BinanceOrder
|
23 | 23 | from nautilus_trader.adapters.binance.common.schemas.account import BinanceStatusCode
|
24 | 24 | from nautilus_trader.adapters.binance.common.symbol import BinanceSymbol
|
| 25 | +from nautilus_trader.adapters.binance.futures.enums import BinanceFuturesMarginType |
25 | 26 | from nautilus_trader.adapters.binance.futures.schemas.account import BinanceFuturesAccountInfo
|
26 | 27 | from nautilus_trader.adapters.binance.futures.schemas.account import BinanceFuturesDualSidePosition
|
27 | 28 | from nautilus_trader.adapters.binance.futures.schemas.account import BinanceFuturesLeverage
|
| 29 | +from nautilus_trader.adapters.binance.futures.schemas.account import BinanceFuturesMarginTypeResponse |
28 | 30 | from nautilus_trader.adapters.binance.futures.schemas.account import BinanceFuturesPositionRisk
|
29 | 31 | from nautilus_trader.adapters.binance.http.account import BinanceAccountHttpAPI
|
30 | 32 | from nautilus_trader.adapters.binance.http.client import BinanceHttpClient
|
31 | 33 | from nautilus_trader.adapters.binance.http.endpoint import BinanceHttpEndpoint
|
| 34 | +from nautilus_trader.adapters.binance.http.error import BinanceClientError |
32 | 35 | from nautilus_trader.common.component import LiveClock
|
33 | 36 | from nautilus_trader.common.config import PositiveInt
|
34 | 37 | from nautilus_trader.core.nautilus_pyo3 import HttpMethod
|
@@ -393,6 +396,64 @@ async def post(
|
393 | 396 | return self._resp_decoder.decode(raw)
|
394 | 397 |
|
395 | 398 |
|
| 399 | +class BinanceFuturesMarginTypeHttp(BinanceHttpEndpoint): |
| 400 | + """ |
| 401 | + Margin type |
| 402 | +
|
| 403 | + `POST /fapi/v1/marginType` |
| 404 | +
|
| 405 | + References |
| 406 | + ---------- |
| 407 | + https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Change-Margin-Type |
| 408 | +
|
| 409 | + """ |
| 410 | + |
| 411 | + def __init__( |
| 412 | + self, |
| 413 | + client: BinanceHttpClient, |
| 414 | + base_endpoint: str, |
| 415 | + ): |
| 416 | + methods = { |
| 417 | + HttpMethod.POST: BinanceSecurityType.TRADE, |
| 418 | + } |
| 419 | + url_path = base_endpoint + 'marginType' |
| 420 | + super().__init__(client, methods, url_path) |
| 421 | + self._resp_decoder = msgspec.json.Decoder(BinanceFuturesMarginTypeResponse) |
| 422 | + |
| 423 | + class PostParameters(msgspec.Struct, omit_defaults=True, frozen=True): |
| 424 | + """ |
| 425 | + Margin type POST endpoint parameters. |
| 426 | +
|
| 427 | + Parameters |
| 428 | + ---------- |
| 429 | + symbol : BinanceSymbol |
| 430 | + marginType : str |
| 431 | + ISOLATED or CROSSED |
| 432 | + timestamp : str |
| 433 | + The millisecond timestamp of the request. |
| 434 | + recvWindow : str, optional |
| 435 | + The response receive window in milliseconds for the request. |
| 436 | +
|
| 437 | + """ |
| 438 | + |
| 439 | + symbol: BinanceSymbol |
| 440 | + marginType: str |
| 441 | + timestamp: str |
| 442 | + recvWindow: str | None = None |
| 443 | + |
| 444 | + async def post( |
| 445 | + self, |
| 446 | + params: PostParameters, |
| 447 | + ) -> BinanceFuturesMarginTypeResponse: |
| 448 | + try: |
| 449 | + raw = await self._method(HttpMethod.POST, params) |
| 450 | + except BinanceClientError as e: |
| 451 | + if e.message['msg'] == 'No need to change margin type.': |
| 452 | + return BinanceFuturesMarginTypeResponse(code=200, msg='success') |
| 453 | + raise |
| 454 | + return self._resp_decoder.decode(raw) |
| 455 | + |
| 456 | + |
396 | 457 | class BinanceFuturesAccountHttpAPI(BinanceAccountHttpAPI):
|
397 | 458 | """
|
398 | 459 | Provides access to the Binance Futures Account/Trade HTTP REST API.
|
@@ -444,6 +505,10 @@ def __init__(
|
444 | 505 | v2_endpoint_base,
|
445 | 506 | )
|
446 | 507 | self._endpoint_futures_leverage = BinanceFuturesLeverageHttp(client, self.base_endpoint)
|
| 508 | + self._endpoint_futures_margin_type = BinanceFuturesMarginTypeHttp( |
| 509 | + client, |
| 510 | + self.base_endpoint, |
| 511 | + ) |
447 | 512 |
|
448 | 513 | async def query_futures_hedge_mode(
|
449 | 514 | self,
|
@@ -477,6 +542,29 @@ async def set_leverage(
|
477 | 542 | ),
|
478 | 543 | )
|
479 | 544 |
|
| 545 | + async def set_margin_type( |
| 546 | + self, |
| 547 | + symbol: BinanceSymbol, |
| 548 | + margin_type: BinanceFuturesMarginType, |
| 549 | + recv_window: str | None = None, |
| 550 | + ) -> BinanceFuturesMarginTypeResponse: |
| 551 | + """ |
| 552 | + Change symbol level margin type |
| 553 | +
|
| 554 | + :param symbol : BinanceSymbol |
| 555 | + :param margin_type : BinanceFuturesMarginType |
| 556 | + :param recv_window : str, optional |
| 557 | + :return: BinanceFuturesMarginTypeResponse |
| 558 | + """ |
| 559 | + return await self._endpoint_futures_margin_type.post( |
| 560 | + self._endpoint_futures_margin_type.PostParameters( |
| 561 | + symbol=symbol, |
| 562 | + marginType=margin_type.value, |
| 563 | + timestamp=self._timestamp(), |
| 564 | + recvWindow=recv_window, |
| 565 | + ) |
| 566 | + ) |
| 567 | + |
480 | 568 | async def set_futures_hedge_mode(
|
481 | 569 | self,
|
482 | 570 | dual_side_position: bool,
|
@@ -565,3 +653,5 @@ async def query_futures_position_risk(
|
565 | 653 | recvWindow=recv_window,
|
566 | 654 | ),
|
567 | 655 | )
|
| 656 | + |
| 657 | + |
0 commit comments