ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
32.08k stars 7.4k forks source link

Binance fapiPrivate_post_positionmargin method not working #18930

Closed Sekharonline4u closed 11 months ago

Sekharonline4u commented 11 months ago

Operating System

Mac OS

Programming Languages

Python

CCXT Version

CCXT Version: 4.0.65

Description

I was trying to create a Market Buy Order for Isolated Trade and Trying to Add the Margin Amount for handling the Liquidation , while executing the below code am getting the error response = exchange.fapiPrivate_post_positionmargin( AttributeError: 'binance' object has no attribute 'fapiPrivate_post_positionmargin' how to resolve this error. I know there is a API available at binance ""/fapi/v1/positionMargin" if i use the API directly with the support of requests it is working but when i use CCXT and the method fapiPrivate_post_positionmargin mentioned in documentation it is not working. Kindly advise

Code

  import ccxt
import config
import requests
import time
import hashlib
import hmac
from urllib.parse import urlencode

api_key = config.BINANCE_FUTURE_API_KEY_TESTNET
secret_key = config.BINANCE_FUTURE_SECRET_KEY_TESTNET

print("CCXT Version:", ccxt.__version__)

exchange = ccxt.binance(
    {
        "apiKey": api_key,
        "secret": secret_key,
        "enableRateLimit": True,
        "options": {
            "defaultType": "future",
        },
    }
)

enable_sandbox_mode = True
exchange.set_sandbox_mode(enable_sandbox_mode)
symbol = "ETH/USDT"
side = "buy"  # or 'sell'
quantity = 1.0
price = None  # Use None for market orders
margin_amount = 1000.0  # Replace with your desired margin amount

# Executing BUY Trade
order = exchange.create_order(
    symbol,
    "market",
    "buy",
    quantity,
    params={"isIsolated": "TRUE"},
)
print(order)
# # for ISOLATED positions Adding Margin
print("Modifying your ISOLATED", symbol, "position margin:")
response = exchange.fapiPrivate_post_positionmargin(
    {
        "symbol": symbol,
        "amount": 1234.45,  # ←--------------  AMOUNT HERE
        "positionSide": "BOTH",  #  BOTH for One-way positions, LONG or SHORT for Hedge Mode
        "type": 1,  # 1 = add position margin, 2 = reduce position margin
    }
)
print(response)
Sekharonline4u commented 11 months ago

I have used the following link code: https://github.com/ccxt/ccxt/blob/master/examples/py/binance-futures-margin.py as reference

ttodua commented 11 months ago

I'll take a look into this soon

ttodua commented 11 months ago

what error did you get? please paste it. also, please mention which market do you want to trade, spot>margin or swap ("binance futures") ? in case you want to trade futures, then you have to use ETH/USDT:USDT (not ETH/USDT) because CCXT uses https://docs.ccxt.com/#/README?id=contract-naming-conventions symbol names for derivtives.

so, you might have several issues: 1) you have used:

2) you have passed symbol ETH/USDT to raw non-unified method, which is not what exchange might expect. (as opposed to createOrder which is unified method and expects unified symbol).

3) I advise you used native CCXT method for adding / removing margin with exchange.add_margin ('ETH/USDT:USDT', 1.2345);. similarly, ccxt has .remove_margin method.

4) in case you meant spot>margin trading, then you should just use .transfer method to transfer from spot to isolated account. you can look through a full example file named "margin-loan-borrow" in folder https://github.com/ccxt/ccxt/tree/master/examples

let us know if that does not solve your issue.

Sekharonline4u commented 11 months ago

Thanks for your Update and quick response, just checked my code with fapiPrivate_post_positionmargin and fapi_private_post_position_margin for these two methods am getting the following error: ### response = exchange.fapi_private_post_position_margin( AttributeError: 'binance' object has no attribute 'fapi_private_post_position_margin' but the native CCXT method which you have referred exchange.add_margin ('ETH/USDT:USDT', 1.2345); or if i use this way too exchange.add_margin ('ETH/USDT', 1.2345);is working like charm Thanks a ton