Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting Phemex python CCXT script from USD to USDT #22478

Closed
BingBong1234567898765321 opened this issue May 13, 2024 · 6 comments
Closed

Converting Phemex python CCXT script from USD to USDT #22478

BingBong1234567898765321 opened this issue May 13, 2024 · 6 comments
Assignees
Labels

Comments

@BingBong1234567898765321

Operating System

Windows 11 - PythonAnywhere.com

Programming Languages

Python

CCXT Version

4.3.21

Description

Hello, Phemex has recently announced they are delisting the perpetual contracts trading pair BTCUSD. I am trying to convert my script to BTCUSDT to adapt to this change. However, it appears the commands that have always worked for BTCUSD do not work for BTCUSDT and I do not know why. For example, when running this code:
exchange.set_leverage(leverage = leverage,symbol = symbol)

I get this error:
{"code":20004,"msg":"TE_ERR_INCONSISTENT_POS_MODE","data":null}

I've tried using exchange.set_position_mode to set to merged, and hedged, and this does not work. I also get similar errors when running:
exchange.create_limit_order(**new_contract_params)

Am I missing required params? Are the commands different for USDT vs. USD?

Code

    try:
        exchange.set_leverage(leverage = leverage,symbol = symbol)
        print("Leverage set")
        new_order_params={'type':'swap','code':'USDT'}
        # Determine order type based on the signal
        if new_signal == 'Buy':
            new_order_side = 'long'
            new_order_action = 'buy'
        elif new_signal == 'Sell':
            new_order_side = 'short'
            new_order_action = 'sell'
        # Get the current market price
        ticker = exchange.fetch_ticker(symbol)
        live_price = ticker['last']
        if new_signal == 'Buy':
            new_order_price = live_price + 20
        elif new_signal == 'Sell':
            new_order_price = live_price - 20
        entry_price = {"Entry": new_order_price}
        with open('entry_price.json', 'w') as entry_price_file:
            json.dump(entry_price, entry_price_file)
        new_order_params={'type':'swap','code':'USDT'}
        available_balance = exchange.fetch_balance(params = new_order_params)
        usd_balance = float(available_balance['total']['USDT'])
        prior_balance = {"Value": usd_balance}
        with open('usd_balance.json', 'w') as prior_balance_file:
            json.dump(prior_balance, prior_balance_file)

        new_order_contract_size = round(((((usd_balance * percent_of_holdings_to_enter) * leverage) / new_order_price)*1000),1)

        # Create a limit order to open a new position
        new_contract_params = {
            'symbol': symbol,
            'amount': new_order_contract_size,
            'side': new_order_action,
            'price': new_order_price,
        }
        new_order = exchange.create_limit_order(**new_contract_params)
        new_order_id = new_order['id']  

@sc0Vu sc0Vu self-assigned this May 13, 2024
@sc0Vu sc0Vu added the question label May 13, 2024
@sc0Vu
Copy link
Contributor

sc0Vu commented May 13, 2024

@BingBong1234567898765321 I think you can use this symbol instead BTC/USDT:USDT.

@BingBong1234567898765321
Copy link
Author

BingBong1234567898765321 commented May 13, 2024

@BingBong1234567898765321 I think you can use this symbol instead BTC/USDT:USDT.

Hi @sc0Vu, BTC/USDT:USDT is the symbol I am using, I just mentioned BTCUSDT in the question for simplicity. I am attempting to switch from BTC/USD:USD to BTC/USDT:USDT

@ttodua
Copy link
Member

ttodua commented May 14, 2024

@BingBong1234567898765321 have you tried set_margin_mode to 'cross' or 'isolated' to see if that has any effect? or set_position_mode with true (means hedged) or false (means one-way)

@BingBong1234567898765321
Copy link
Author

BingBong1234567898765321 commented May 14, 2024

@ttodua when I run:

exchange.set_margin_mode("cross", symbol = symbol)
#Note, "symbol" is predefined as "BTC/USDT:USDT"
I get this error:
Error: phemex setMarginMode() supports swap(non USDT based) contracts only

When I run:

exchange.set_position_mode(False, symbol = symbol)
It progresses further than I've been so far, so thank you! But when it gets to the section to create the limit order (code outlined above), I get this error:
Error: phemex {"code":11058,"msg":"TE_QTY_TOO_SMALL","data":null}

Is the contract size (amount) calculated differently for USDT contracts vs. USD? With the code I have above:
new_order_contract_size = round(((((usd_balance * percent_of_holdings_to_enter) * leverage) / new_order_price)*1000),1)
this code works properly for USD but seems to be giving the QTY_TOO_SMALL error with USDT. For example, my available balance being multiplied by .95 (because 100% does not allow enough room for fees) and then multiplied by leverage (ex. 20), divided by the price to place limit order at, times 1000 (I think because USD contracts are in Ep), and then rounded to the first decimal.

#Note, I have tried multiplying by 1000, 10000, and 100000 and they all get the same error.

Thank you for your support.

@BingBong1234567898765321
Copy link
Author

@ttodua any update on this topic?

Thank you.

@BingBong1234567898765321
Copy link
Author

Update: I discovered the solution. Since BTCUSD is settled in USD and BTCUSDT is settled in BTC and the notation is different, the below contract size formula has fixed the issue:

new_order_contract_size = round((((((usd_balance * percent_of_holdings_to_enter)/new_order_price) * leverage))),5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants