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
33.19k stars 7.55k forks source link

Bitfinex: create_order() failed Strings must be encoded before hashing #18196

Open biahoi opened 1 year ago

biahoi commented 1 year ago

Operating System

win10 + Jupyter

Programming Languages

python

CCXT Version

No response

Description

I used the code provided in /examples (minor revised to work on Jupyter

https://github.com/ccxt/ccxt/blob/master/examples/py/async-binance-create-margin-order.py

for Binance it works perfect. But the samecode while running for Bitfinex it returns error:

"create_order() failed Strings must be encoded before hashing "

Code

import os
import asyncio
import sys
import ccxt.async_support as ccxt  

import nest_asyncio
nest_asyncio.apply()

EXCHANGE_NAME = "bitfinex"

def exchange_init(exchange = EXCHANGE_NAME):
    if exchange == "bitfinex":
        api_key =  os.getenv('BITFINEX_API_KEY_230610')
        api_secret = os.getenv('BITFINEX_SECRET_KEY_230610')
        client = ccxt.bitfinex({
        'apiKey': api_key,
        'secret': api_secret,
        'verbose': False,  # switch it to False if you don't want the HTTP log
        })

    if exchange == "binance":
        api_key =  os.getenv('BINANCE_API_KEY_230422')
        api_secret = os.getenv('BINANCE_SECRET_KEY_230422')
        client = ccxt.binance({
        'apiKey': api_key,
        'secret': api_secret,
        'verbose': False,  # switch it to False if you don't want the HTTP log
        })

    return client

async def exchange_close(client):
    await client.close()
    print("Exchange client closed")

async def create_order(client):
    try:
            # change the values here
            symbol = 'BTC/USDT'
            price = 9000
            amount = 1
            type = 'limit'  # or market
            side = 'buy'

            order = await client.create_order(symbol, type, side, amount, price, {
                'type': 'margin',
            })
            #pprint(order)
            return order

    except ccxt.InsufficientFunds as e:
            print('create_order() failed – not enough funds')
            print(e)
    except Exception as e:
            print('create_order() failed')
            print(e)
  
async def main():
    CLIENT = exchange_init()
    placedOrder = await create_order(CLIENT)
    print("placed order = ",placedOrder)

    await exchange_close(CLIENT)

asyncio.run(main())
carlosmiei commented 1 year ago

Hello @biahoi, unfortunately, I don't think we have margin support in Bitfinex yet, so providing type = 'margin' inside params is not a valid combination.

biahoi commented 1 year ago

Hello @biahoi, unfortunately, I don't think we have margin support in Bitfinex yet, so providing type = 'margin' inside params is not a valid combination.

Hi Carlos, I tried manually put margin order on my bitfinex, and it works perfectly. So I dont think that is the case.. However I will to remove the "margin", see if it will affect.. Thank you..

SteCrypto commented 1 year ago

Hi biahoi, I got the same issue just asking for balance of Bitfinex using BidBalance = BidExchange.fetch_balance()

Did you find the way to fix it?

Thanks

Sosyetekadir commented 1 year ago

Sosyete

wajahat414 commented 6 months ago

I am having the same problem , let me know if found any fixes