Giter VIP home page Giter VIP logo

cuongitl / python-bitget Goto Github PK

View Code? Open in Web Editor NEW
30.0 1.0 6.0 59 KB

An unofficial Python3 library to interact with bitget.com - full support for all API's endpoints. Buy me a coffee: https://buymeacoffee.com/cuongitl

Home Page: https://python-bitget.readthedocs.io/en/latest/index.html

Python 100.00%
bitget bitget-api bitget-client python python3 bitget-api-client bitget-api-sdk bitget-websocket

python-bitget's Introduction

Python bitget API Library

License python-bitget Version python-bitget Python Versions python-bitget Downloads Per Day python-bitget Downloads Per Week python-bitget Downloads Per Month Downloads

bitget is a cryptocurrency derivatives exchange.

This is a wrapper around the Bitget API as described on Bitget, including all features the API provides using clear and readable objects, both for the REST as the websocket API.

I am in no way affiliated with Bitget, use at your own risk.

If you think something is broken, something is missing or have any questions, please open an Issue

Get Started and Documentation

If you're new to Bitget, use the following link to save 10% on all of your trade fees, and can get rewards up to $5005.

Install

pip install python-bitget

Usage

Change your API KEY and your SECRET KEY.

Restful Api Sample Code

from pybitget import Client

api_key = "your-api-key"
api_secret = "your-secret-key"
api_passphrase = "your-api-passphrase"

client = Client(api_key, api_secret, passphrase=api_passphrase)
result = client.mix_get_accounts(productType='UMCBL')
print(result)

Websocket Sample Code

from pybitget.stream import BitgetWsClient, SubscribeReq, handel_error

from pybitget.enums import *
from pybitget import logger

api_key = "your-api-key"
api_secret = "your-secret-key"
api_passphrase = "your-api-passphrase"

def on_message(message):
    logger.info(message)


# Auth subscribe
client = BitgetWsClient(api_key=api_key,
                        api_secret=api_secret,
                        passphrase=api_passphrase,
                        verbose=True) \
    .error_listener(handel_error) \
    .build()

# multi subscribe  - Public Channels
channels = [SubscribeReq("mc", "ticker", "BTCUSD"), SubscribeReq("SP", "candle1W", "BTCUSDT")]
client.subscribe(channels, on_message)

# single subscribe -     # multi subscribe  Public Channels
channels = [SubscribeReq("mc", "ticker", "BTCUSD")]
client.subscribe(channels, on_message)

# single subscribe - Order Channel - Private Channels
channels = [SubscribeReq(WS_CHANNEL_INSTTYPE, WS_PRIVATE_ORDERS_CHANNEL, WS_CHANNEL_INSTID)]
client.subscribe(channels, on_message)

Donate / Sponsor

I develop and maintain this package on my own for free in my spare time. Donations are greatly appreciated. If you prefer to donate any other currency please contact me.

  • Buy me a coffee: https://buymeacoffee.com/cuongitl

  • BTC: 19rvFC79eVoCMHxJ44hB3GLhiD2HHsMJoZ

  • BTC: 0xab4686635a02dff0babedddfee813d325f56cfb8 (BEP20)

  • USDT: 0xab4686635a02dff0babedddfee813d325f56cfb8 (BEP20)

  • USDT: TCdyZs2mRhwYpCB5dZqiXEUoYRfiVJuq1H (TRC20)

  • BGB: 0x3ee4ca7cc911ad4e423dec2ae8f2846e9a6a0a77 (ERC-20)

Communities - Telegram

Release Notes

The release notes can be found here.

Contribution

  • Fork this repository.
  • Make pull requests with proper commit message.

python-bitget's People

Contributors

cuongitl avatar cuongitlb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-bitget's Issues

Typo in stream.py

Line 150 in stream.py has chanel and it should be channel

    def unsubscribe(self, channels):
        try:
            for channel in channels:
                if channel in self.__scribe_map:
                    del self.__scribe_map[chanel] <-

            for channel in channels:
                if channel in self.__all_suribe:
                    self.__all_suribe.remove(channel)

            self.send_message(WS_OP_UNSUBSCRIBE, channels)
        except Exception as e:
            logger.error(e)
            pass

Error setting Margin Type & Leverage

Hello

First thing first, thank you for this wrapper. It's fantastic

I am experiencing a problem when I try to change the Margin Mode and Leverage in the same go. I am getting this error: "API Request Error(code=40409): wrong format"

The code that I am executing is this:

 try:
       print("Changing Margin Type")
       margin = session.mix_adjust_margintype(symbol="SBTCSUSDT_SUMCBL",marginCoin="SUSDT",marginMode="fixed")
       print(margin)
 except Exception as inst:
 print(inst)

 try:
       print("Changink Leverage")
       apal = session.mix_adjust_leverage(symbol="SBTCSUSDT_SUMCBL", marginCoin="SUSDT", leverage="3", holdSide="long")
       print(apal)
 except Exception as inst:
 print(inst)

If I execute that code a second time I don´t get any error

Thnak you very much for your hard work

Error client.mix_get_all_open_orders

When you call the function mix_get_all_open_orders(productType) it returns the following error with the parameter 'orderId' which is not needed for this request.
name 'orderId' is not defined
pybitget/client.py:

def mix_cancel_all_orders(self, productType, marginCoin):
        """ Cancel All Order
        https://bitgetlimited.github.io/apidoc/en/mix/#cancel-all-order
        Limit rule: 10 times/1s (uid)

        Required: productType, marginCoin
        """
        params = {}
        if productType and marginCoin:
            params["productType"] = productType
            params["marginCoin"] = marginCoin
            return self._request_with_params(POST, MIX_ORDER_V1_URL + '/cancel-all-orders', params)
        else:
            logger.error("pls check args")
            return False

Can't connect to the websocket

Thank you for writing the code for this.

Everything else works but I cannot get the websocket to connect. Stuck at

2024-01-28 20:51:04.449 | INFO | pybitget.stream:build:73 - start connecting...wss://ws.bitget.com/spot/v1/stream
2024-01-28 20:51:05.450 | INFO | pybitget.stream:build:73 - start connecting...wss://ws.bitget.com/spot/v1/stream

Thank you

Can't find history-position

Get History Position

Only supports Query within 3 months

Limit rule: 20c/1s (uid)

HTTP Request

GET /api/mix/v1/position/history-position

Seems needed to calculated true closed PNL because other method as mix_get_productType_history_orders return totalProfits and fee data BUT show different final data than the Closed PNL section in Bitget app and UI.

Could you add ? :)

Websocket doesn't re-subscribe upon disconnect

Thanks for your great work on this.

The websocket re-connects on a disconnect but doesn't seem to re-subscribe to channels.

2024-02-14 04:48:48.092 | ERROR | pybitget.stream:__on_error:235 - Connection to remote host was lost.
2024-02-14 04:48:48.093 | INFO | pybitget.stream:__re_connect:252 - start reconnection ...
2024-02-14 04:48:48.095 | INFO | pybitget.stream:__on_close:241 - ws is closeing ......close_status:None,close_msg:None
2024-02-14 04:48:49.505 | INFO | pybitget.stream:__on_open:179 - connection is success....
2024-02-14 04:48:49.566 | INFO | pybitget.stream:__on_open:179 - connection is success....

I have edited stream.py as below in to close the connection and then wait in case it needs time until properly logged in before trying to re-subscribe

def __re_connect(self):
    if not self.__keyboard_interrupt_flag:
        self.__reconnect_status = True
        logger.info("start reconnection ...")
        # Close the current connection
        self.__close()
        # Rebuild the connection
        self.build()
        # Wait for the connection to be established before resubscribing
        while not self.has_connect():
            time.sleep(1)
        for channel in self.__all_suribe:
            self.subscribe([channel])            
        #pass
        logger.info("Resubscribed to channels: {}".format(self.__all_suribe))

Could ideally do with the script catching KeyboardException (I have tried to implement within stream.py but isn't error free) and a close and reconnect if no messages received for a period of time.

Thanks

ModuleNotFoundError: No module named 'pybitget'

Hi there, I am trying to get this code to run but without any success. I have installed the module: pip3 install python-bitget

from pybitget import Client

api_key = "your-api-key"
api_secret = "your-secret-key"
api_passphrase = "your-api-passphrase"

client = Client(api_key, api_secret, passphrase=api_passphrase)
result = client.mix_get_accounts(productType='UMCBL')
print(result)

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.