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.44k stars 7.46k forks source link

fapiPrivate_post_leverage on mexc #18530

Closed jejoj closed 1 year ago

jejoj commented 1 year ago

Operating System

windows

Programming Languages

python

CCXT Version

4.0.18

Description

the error is : AttributeError: 'mexc' object has no attribute 'fapiPrivate_post_leverage'

i made script in python for Futures mexc with a leverage of 100 x the purchase order and the termination of the deal Market taking profits on a profit of 0.01% as an example In the event that I entered number 1 in the terminal script, I will execute a purchase order or wait to take profits, or enter number 3 to end the transaction with a market order

the error is : AttributeError: 'mexc' object has no attribute 'fapiPrivate_post_leverage'

and if you have anything to edit or improve script im need help

Code

import ccxt

api_key = 'api_key' secret = 'secret' leverage = 100 symbol = 'BTC/USDT' # Bitcoin trading pair take_profit_percentage = 0.01 # 0.01% profit

exchange = ccxt.mexc({ # Assuming exchange is Binance 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } })

def create_long_order(): data = exchange.fapiPrivate_post_leverage({ 'symbol': symbol.replace('/', ''), 'leverage': leverage })

market_price = exchange.fetch_ticker(symbol)['last']
balance_usdt = exchange.fetch_balance()['total']['USDT']  # Fetch your USDT balance
amount_btc = balance_usdt / market_price  # Use your total USDT to buy BTC
order = exchange.create_market_buy_order(symbol, amount_btc)
print('Buy order created at market price:', market_price)
return order

def close_order_at_market(): balance = exchange.fetch_balance()['total']['BTC'] order = exchange.create_market_sell_order(symbol, balance) print('Order closed at market price') return order

while True: action = input("Enter 1 to execute a buy order, 3 to close the order at market price: ") if action == '1': order = create_long_order() while True: order_info = exchange.fetch_order(order['id'], symbol) if order_info['status'] == 'closed': print('Order closed with a profit of 0.01%') break elif float(exchange.fetch_ticker(symbol)['last']) >= float(order['info']['price']) * (1 + take_profit_percentage): # Fetch the latest price close_order = close_order_at_market() break elif action == '3': close_order = close_order_at_market() break

carlosmiei commented 1 year ago

Hello @jejoj, fapiPrivate_post_leverage is not an implicit endpoint from Mexc., it belongs to Binance so I think you might be mixing some different things in your script.

carlosmiei commented 1 year ago

@jejoj If you want to set the leverage at Mexc you can use the unified set_leverage method.

jejoj commented 1 year ago

@jejoj If you want to set the leverage at Mexc you can use the unified set_leverage method.

oh ok i will try now does my script good or not help me fo fix it pleas

jejoj commented 1 year ago

my code after editing `import ccxt

api_key = 'XXXXX' secret = 'YYYYY' leverage = 100 symbol = 'BTC/USDT' # Bitcoin trading pair take_profit_percentage = 0.01 # 0.01% profit

exchange = ccxt.mexc({ # Assuming exchange is Binance 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } })

def create_long_order(): data = exchange.set_leverage({ 'symbol': symbol.replace('/', ''), 'leverage': leverage, 'openType': 1, # Use isolated margin 'positionType': 1, # Use long position })

market_price = exchange.fetch_ticker(symbol)['last']
balance_usdt = exchange.fetch_balance()['total']['USDT']  # Fetch your USDT balance
amount_btc = balance_usdt / market_price  # Use your total USDT to buy BTC
order = exchange.create_market_buy_order(symbol, amount_btc)
print('Buy order created at market price:', market_price)
return order

def close_order_at_market(): balance = exchange.fetch_balance()['total']['BTC'] order = exchange.create_market_sell_order(symbol, balance) print('Order closed at market price') return order

while True: action = input("Enter 1 to execute a buy order, 3 to close the order at market price: ") if action == '1': order = create_long_order() while True: order_info = exchange.fetch_order(order['id'], symbol) if order_info['status'] == 'closed': print('Order closed with a profit of 0.01%') break elif float(exchange.fetch_ticker(symbol)['last']) >= float(order['info']['price']) * (1 + take_profit_percentage): # Fetch the latest price close_order = close_order_at_market() break elif action == '3': close_order = close_order_at_market() break `

now error changed to Enter 1 to execute a buy order, 3 to close the order at market price: 1 Traceback (most recent call last): File "c:\Users\Jehad\Desktop\import ccxt test 4.py", line 42, in <module> order = create_long_order() ^^^^^^^^^^^^^^^^^^^ File "c:\Users\Jehad\Desktop\import ccxt test 4.py", line 19, in create_long_order data = exchange.set_leverage({ ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\mexc.py", line 3275, in set_leverage raise ArgumentsRequired(self.id + ' setLeverage() requires a positionId parameter or a symbol argument with openType and positionType parameters, use openType 1 or 2 for isolated or cross margin respectively, use positionType 1 or 2 for long or short positions') ccxt.base.errors.ArgumentsRequired: mexc setLeverage() requires a positionId parameter or a symbol argument with openType and positionType parameters, use openType 1 or 2 for isolated or cross margin respectively, use positionType 1 or 2 for long or short positions PS C:\Users\Jehad>

kroitor commented 1 year ago

@jejoj first and foremost, never post your keys publicly, keep them in secret.

Please, read this carefully, literally to a word:

Second, please, read the signature of the set_leverage() method, it accepts a number of arguments instead of a dictionary:

Check the implementation of MEXC for more info:

For MEXC that would look like:

    data = exchange.set_leverage(leverage, symbol, {
        'openType': 1,  # Use isolated margin
        'positionType': 1,  # Use long position
    })

We highly recommend reading the entire Manual from top to bottom, it will really save your time (we cannot write the code for you and we cannot fix all the code in the world since this library is used by thousands of users). The key to using this library efficiently is reading the documentation very carefully (you have to get used to a lot of reading as noted in the FAQ):

Also, check the examples here:

jejoj commented 1 year ago

@jejoj first and foremost, never post your keys publicly, keep them in secret.

Please, read this carefully, literally to a word:

Second, please, read the signature of the set_leverage() method, it accepts a number of arguments instead of a dictionary:

Check the implementation of MEXC for more info:

For MEXC that would look like:

    data = exchange.set_leverage(leverage, symbol, {
        'openType': 1,  # Use isolated margin
        'positionType': 1,  # Use long position
    })

We highly recommend reading the entire Manual from top to bottom, it will really save your time (we cannot write the code for you and we cannot fix all the code in the world since this library is used by thousands of users). The key to using this library efficiently is reading the documentation very carefully (you have to get used to a lot of reading as noted in the FAQ):

Also, check the examples here:

ok thanks i will search on it

kroitor commented 1 year ago

@jejoj feel free to reopen this issue or just ask further questions here if any, closing it for now.

jejoj commented 1 year ago

ok

after many edits . in returned to this `import ccxt

api_key = 'api_key' secret = 'secret' leverage = 10 symbol = 'SOL/USDT' # Bitcoin trading pair take_profit_percentage = 0.01 # 0.01% profit

exchange = ccxt.mexc({ # Assuming exchange is Binance 'apiKey': api_key, 'secret': secret, 'enableRateLimit': True, 'options': { 'defaultType': 'future' } })

def create_long_order(): openType = 1 positionType = 1 data = exchange.set_leverage(leverage, symbol, { 'openType': 1, # Use isolated margin 'positionType': 1, # Use long position })

market_price = exchange.fetch_ticker(symbol)['last']
balance_usdt = exchange.fetch_balance()['total']['USDT']  # Fetch your USDT balance
amount_btc = balance_usdt / market_price  # Use your total USDT to buy BTC
order = exchange.create_market_buy_order(symbol, amount_btc)
print('Buy order created at market price:', market_price)
return order

def close_order_at_market(): balance = exchange.fetch_balance()['total']['BTC'] order = exchange.create_market_sell_order(symbol, balance) print('Order closed at market price') return order

while True: action = input("Enter 1 to execute a buy order, 3 to close the order at market price: ") if action == '1': order = create_long_order() while True: order_info = exchange.fetch_order(order['id'], symbol) if order_info['status'] == 'closed': print('Order closed with a profit of 0.01%') break elif float(exchange.fetch_ticker(symbol)['last']) >= float(order['info']['price']) * (1 + take_profit_percentage): # Fetch the latest price close_order = close_order_at_market() break elif action == '3': close_order = close_order_at_market() break `

now main error: ccxt.base.errors.ExchangeError: mexc {"success":false,"code":1001,"message":"Contract does not exist!"}

File "c:\Users\Jehad\Desktop\import ccxt test 4.py", line 51, in <module> order = create_long_order() ^^^^^^^^^^^^^^^^^^^ File "c:\Users\Jehad\Desktop\import ccxt test 4.py", line 32, in create_long_order data = exchange.set_leverage(leverage, symbol, { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\mexc.py", line 3282, in set_leverage return self.contractPrivatePostPositionChangeLeverage(self.extend(request, params)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\base\exchange.py", line 507, in inner return entry(_self, **inner_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\base\exchange.py", line 2889, in request return self.fetch2(path, api, method, params, headers, body, config, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\base\exchange.py", line 2886, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\base\exchange.py", line 669, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "C:\Users\Jehad\AppData\Local\Programs\Python\Python311\Lib\site-packages\ccxt\mexc.py", line 4713, in handle_errors raise ExchangeError(feedback) ccxt.base.errors.ExchangeError: mexc {"success":false,"code":1001,"message":"Contract does not exist!"}

kroitor commented 1 year ago

@jejoj if you want to trade contracts instead of spot markets, then you have to specify a correct contract symbol which is SOL/USDT:USDT (Not SOL/USDT which is for spot markets). Please, read the manual once again: