Giter Site home page Giter Site logo

niewiemczego / python-bingx Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 5.0 101 KB

Unofficial Python3 API connector for BingX's HTTP and WebSockets APIs.

Home Page: https://pypi.org/project/python-bingx/

License: MIT License

Python 100.00%
api cryptocurrency python3 websockets bingx python exchange

python-bingx's Introduction

Python-BingX

Python-bingx is a powerful and flexible Python package that allows you to easily interact with the BingX API. The package currently supports the Spot API, Standard API, Perpetual Swap API Reference V1, and Perpetual Swap API Reference V2. With python-bingx, you can retrieve real-time market data, manage your account, place trades, and more...

Installation

To install python-bingx, you can use pip:

pip install python-bingx

Usage

There are multiple ways to use python-bingx, depending on your needs and preferences.

Using BingX

The most straightforward way to use python-bingx is by importing the BingX class and initializing it with your API key and secret key:

from bingX import BingX

bingx_client = BingX(api_key="api_key", secret_key="secret_key")

Once you have initialized the client, you can call any of the available APIs, for example:

# Call the Trade API of Perpetual V2
bingx_client.perpetual_v2.trade.trade_order()

Using PerpetualV2

If you prefer to work with a specific API or version, you can import the relevant class and initialize it with your API key and secret key:

from bingX.perpetual.v2 import PerpetualV2

bingx_client = PerpetualV2(api_key="api_key", secret_key="secret_key")

Once you have initialized the client, you can call any of the available APIs, for example:

# Call the Trade API of Perpetual V2
bingx_client.trade.trade_order()

Handling Responses

Python-bingx uses requests library to communicate with the API and returns the response in JSON format. You can easily handle the response by accessing the relevant key(s) in the dictionary, for example:

# Get the symbol and last price of BTC/USDT
response = bingx_client.perpetual_v2.market.get_ticker("BTC-USDT")
symbol = response["symbol"]
last_price = response["lastPrice"]

Error Handling

In case of errors or exceptions, python-bingx will raise relevant exceptions with error message and error code. You can catch and handle the exceptions accordingly, for example:

from bingX import ClientError, ServerError

try:
    response = bingx_client.perpetual_v2.trade.create_order()
except (ClientError, ServerError) as e:
    error_code = e.error_code
    error_message = e.error_message

Contributing

Python-bingx welcomes contributions from the community! If you'd like to contribute, please fork the repository, create a feature branch, make your changes, and submit a pull request. Before submitting, please ensure that your code follows the PEP 8 style guide and includes appropriate tests.

License

Python-bingx is licensed under the MIT License. See the LICENSE file for more information.

python-bingx's People

Contributors

niewiemczego avatar slotfi909 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-bingx's Issues

Installation Issue

Hello,

I'm getting the following error when trying to import python-Bingx module. I've tried both python 3.8 and 3.9 so far and haven't had any luck. Any ideas?

Error:
Traceback (most recent call last):

File ~\anaconda3\envs\asdf\lib\site-packages\IPython\core\interactiveshell.py:3505 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

Cell In[1], line 1
import bingX

File ~\anaconda3\envs\asdf\lib\site-packages\bingX_init_.py:2
from bingX.main import BingX

File ~\anaconda3\envs\asdf\lib\site-packages\bingX\main.py:2
from bingX.perpetual.v2 import PerpetualV2

File ~\anaconda3\envs\asdf\lib\site-packages\bingX\perpetual\v2_init_.py:1
from .perpetual import PerpetualV2

File ~\anaconda3\envs\asdf\lib\site-packages\bingX\perpetual\v2\perpetual.py:1
from bingX.perpetual.v2.account import Account

File ~\anaconda3\envs\asdf\lib\site-packages\bingX\perpetual\v2\account.py:3
from bingX._http_manager import _HTTPManager

File ~\anaconda3\envs\asdf\lib\site-packages\bingX_http_manager.py:56
match method:
^
SyntaxError: invalid syntax

Order size error

i dont know what is the problm, please help:
`from bingX.perpetual.v2 import PerpetualV2
from bingX.perpetual.v2.types import (
ForceOrder,
HistoryOrder,
MarginType,
Order,
PositionSide,
Side,
OrderType
)
bingx_client = PerpetualV2(api_key="", secret_key="")
def create__order(symbol):

bingx_client.trade.create_order(Order(symbol,Side.BUY,PositionSide.LONG,quantity=float(1),type=OrderType.MARKET))

coins='HBAR-USDT'
create__order(coins)`

and response :

File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\bingX\_http_manager.py", line 78, in _request raise ClientError(req_json.get("code"), req_json.get("msg")) bingX.exceptions.ClientError: Order size error

python-bingx 1.0.1 problem

Dear niewiemczego
Please change the name of the primary folder from binx to bingX.
I faced with bellow error when I installed and used python-bingx 1.0.1

bingX

BR

Error in order command

Hello
I use the below commands to create an order:

order = Order(symbol="DOGE-USDT", side="BUY", position_side="LONG", type="MARKET", quantity=100)
Trade.create_order(order)

but I faced with below Error: (the error in attached
Error Bingx1
)

ClientError Traceback (most recent call last)
c:\Users\Administrator\Desktop\BingX Perpetual Commands.ipynb Cell 21 line 2
1 order = Order(symbol="DOGE-USDT", side=Side.BUY, position_side=PositionSide.LONG, quantity=100.0)
----> 2 Trade.create_order(order)

c:\Users\Administrator\Desktop\BingX Perpetual Commands.ipynb Cell 21 line 2
19 endpoint = "/openApi/swap/v2/trade/order"
20 payload = order.to_dict()
---> 22 response = self.__http_manager.post(endpoint, payload)
23 return response.json()["data"]

File c:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\bingX_http_manager.py:103, in _HTTPManager.post(self, endpoint, payload, headers)
93 def post(self, endpoint: str, payload: dict[str, Any] = {}, headers: dict[str, Any] = {}) -> requests.Response:
94 """
95 It makes a POST request to the given endpoint with the given payload and headers
96
(...)
100 :return: A response object
101 """
--> 103 return self._request("POST", endpoint, payload, headers)

File c:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\bingX_http_manager.py:78, in _HTTPManager._request(self, method, endpoint, payload, headers)
75 else:
76 #TODO: Check if this is the correct way to check for errors
77 if req_json.get("code") is not None and req_json.get("code") != 0:
---> 78 raise ClientError(req_json.get("code"), req_json.get("msg"))
79 return req

ClientError: Invalid parameters, err:Key: 'PlaceOrderRequest.ReqOrderData.PositionSide' Error:Field validation for 'PositionSide' failed on the 'required' tag

create_order problem

          Hey make sure to use PositionSide Enum.
from bingX.perpetual.v2.types import (
    Side,
    Order,
    PositionSide,
)

examples:
  - create long: Order(symbol="DOGE-USDT", side=Side.BUY, positionSide=PositionSide.LONG, quantity=100.0)
  - create short: Order(symbol="DOGE-USDT", side=Side.SELL, positionSide=PositionSide.SHORT, quantity=100.0)

Originally posted by @niewiemczego in #8 (comment)

'PerpetualV2' object has no attribute 'perpetual_v2'

Hello
I faced the error (AttributeError: 'PerpetualV2' object has no attribute 'perpetual_v2') when inputting the below command in Python 3.11:
response = bingx_client.perpetual_v2.market.get_ticker("BTC-USDT") ![Bing](https://github.com/niewiemczego/python-bingx/assets/129304303/88aa37f6-a260-49e3-9010-6d74444f36fb)
please guide me.
BR,

sl & tp error

Hi
I got a LONG position and tried to set take-profit and stop-loss for the position. I used the bellow code but faced with stop loss price and taker profit price less then or equal 0.0 error from bingX.

order = Order(symbol='TRB-USDT', side="SELL", positionSide="LONG", type="TAKE_PROFIT_MARKET", quantity=0.1, price=78.2)
Perpetual.trade.create_order(order)

The evidence is attached. Please guide me.

BR,
Robin Dala

price

create_order problem

I used the following command (create long: Order(symbol="DOGE-USDT", side=Side.BUY, positionSide=PositionSide.LONG, quantity=100.0)) and got bellow error:
TypeError: Order.init() got an unexpected keyword argument 'positionSide'

python-bingx 1.0.1 Issue

Dear niewiemczego
In now I have one problem with python-bingx 1.0.1
When I using ( from bingx.perpetual.v2.types import (Order)),
I facing with error (No module named 'bingX')
The evidence is attached.

bingX

create_order problem

Hello
Kindly, I am faced with the below error when I want to create an order, please guide me to solving it.

my code:
order = Order(symbol="LTC-USDT", side="BUY", position_side="LONG", type="MARKET", quantity=0.01)
Trade.create_order(order)

ClientError: Invalid parameters, err:Key: 'PlaceOrderRequest.ReqOrderData.PositionSide' Error: Field validation for 'PositionSide' failed on the 'required' tag

Can't open Short Position *help needed*

@niewiemczego

-Hi, thank you for this project but I have a problem with position opening. (V2 Perpetual). Long Position is not a problem - is working but not Short position. When I try to open short postion I get:

Order details: Order(symbol='AXS-USDT', side=<Side.SELL: 'SELL'>, position_side=<PositionSide.SHORT: 'SHORT'>, quantity=1.0, type=<OrderType.MARKET: 'MARKET'>, price=None, stop_price=None, recv_window=None)
Client error: No position to close

I want to open a short position and not close any position.

Can you help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.