diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..6d5a2da --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,9 @@ +version: 2 +jobs: + build: + docker: + - image: circleci/python:3.7.4-stretch + steps: + - checkout + - run: pip install --user . + parallelism: 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2beae2c --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +key.sh +__pycache__ +.vscode +*.egg-info +.pytest_cache +.ipynb_checkpoints +key.sh diff --git a/bybit/LICENSE b/LICENSE similarity index 100% rename from bybit/LICENSE rename to LICENSE diff --git a/README.md b/README.md index b44f324..940f5e5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ +bybit-exchange-python-sdk +========================= -# bybit-exchange-python-sdk -This is an unofficial Bybit Exchange API python implementation for automated trading. Find the documentation [here](https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html). +[![CircleCI](https://circleci.com/gh/rgalbo/bybit-exchange-python-sdk.svg?style=svg)](https://circleci.com/gh/rgalbo/bybit-exchange-python-sdk) +This is an unofficial Bybit Exchange API python implementation for automated trading. [API Documentation](https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html). Features -------- @@ -12,7 +14,6 @@ Features - Response exception handling - Historical Kline/Candle fetching function - Quick Start ----------- @@ -20,61 +21,72 @@ To register an account with Bybit [Click here](https://www.bybit.com/app/registe To Generate an API Key and assign relevant permissions [Click here](https://www.bybit.com/app/user/api-management) -**Terminal Installation** - - pip install bybit - - -**Python** - - from bybit import Bybit - client = Client(api_key, api_secret) +Installation +------------ +`$ pip install .` - # get exchange symbols - depth = Bybit().get_symbols() +or for my purposes: - - # get all symbol prices - prices = Bybit().get_klines('BTCUSD', '15', '1563367231') +``` +pyenv local 3.7.4 +mkvirtualenv futures +workon futures +pip install -r requirements.txt +cd bybit +python -m pip install -e . +cd .. +jupyter lab +``` - - # Create Market Buy Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) +KEY Env Variables +----------------- +``` +$ vi key_example.sh # add your api key from bybit in the parenthesis +$ cp key_example.sh key.sh +$ source key.sh # if this doesnt work, copy paste the two lines to your terminal +``` - +alternatively, in the disgust of all other failure you are likely to ask to just please god fucking hell make jupyter environments stop and run(see [jvp](https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/)): - - # Create Market Sell Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) +for ipython: - +```ipython +# follows conveintion %set_env var val: +%set_env BTKEY '[your_key_here]' +%set_env BTSECRET '[your_secret_here]' +``` - - # Create Market Buy Limit Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) +for last resort, (key.py): - +```python +import sys,os,os.path +sys.path.append(os.path.expanduser('~/code/eol_hsrl_python')) +os.environ['HSRL_INSTRUMENT']='gvhsrl' +os.environ['HSRL_CONFIG']=os.path.expanduser('~/hsrl_config') +``` - - # Create Market Sell Limit Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) +API Usage Examples +------------------ - +```python +import os +from bybit import Bybit - - # Cancelling an order - auth = Bybit().auth(key, secret) - prices = cancel_order(order_id) - +KEY = os.environ['BTKEY'] +SECRET = os.environ['BTSECRET'] +bt = Bybit() +bt.(api_key, api_secret) +# get exchange symbols +depth = bt.get_symbols() +# get all symbol prices +prices = bt.get_klines('BTCUSD', '15', '1563367231') -For more check out the documentation +# Create Market Buy Orders +orders = bt.buy('BTCUSD', 9550, 1.000) +``` - https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html +check out the [documentation](https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html)! diff --git a/bybit/__init__.py b/__init__.py similarity index 100% rename from bybit/__init__.py rename to __init__.py diff --git a/bybit/bybit.py b/bybit.py similarity index 97% rename from bybit/bybit.py rename to bybit.py index 6f5d49a..4bea48c 100644 --- a/bybit/bybit.py +++ b/bybit.py @@ -20,7 +20,7 @@ def order_link_id(self, stringLength): lettersAndDigits = string.ascii_letters + string.digits return ''.join(random.choice(lettersAndDigits) for i in range(stringLength)) - def public_request(self, method, api_url, params): + def public_request(self, method, api_url, params=''): '''Public Requests''' r_url = self.base_url + api_url try: @@ -37,10 +37,8 @@ def get_signed(self, sig_str): signature = base64.b64encode(hmac.new(self.secret, sig_str, digestmod=hashlib.sha1).digest()) return signature - def signed_request(self, method, api_url, params): + def signed_request(self, method, api_url, params=''): '''Handler for a signed requests''' - - param = '' if params: sort_pay = sorted(params.items()) # sort_pay.sort() diff --git a/bybit/README.md b/bybit/README.md deleted file mode 100644 index b44f324..0000000 --- a/bybit/README.md +++ /dev/null @@ -1,80 +0,0 @@ - -# bybit-exchange-python-sdk -This is an unofficial Bybit Exchange API python implementation for automated trading. Find the documentation [here](https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html). - - -Features --------- - -- Implementation of all General, Market Data and Account endpoints. -- Simple handling of authentication -- No need to generate timestamps yourself, the wrapper does it for you -- Response exception handling -- Historical Kline/Candle fetching function - - -Quick Start ------------ - -To register an account with Bybit [Click here](https://www.bybit.com/app/register?ref=00xv5) - -To Generate an API Key and assign relevant permissions [Click here](https://www.bybit.com/app/user/api-management) - -**Terminal Installation** - - pip install bybit - - -**Python** - - from bybit import Bybit - client = Client(api_key, api_secret) - - # get exchange symbols - depth = Bybit().get_symbols() - - - # get all symbol prices - prices = Bybit().get_klines('BTCUSD', '15', '1563367231') - - - # Create Market Buy Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) - - - - - # Create Market Sell Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) - - - - - # Create Market Buy Limit Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) - - - - - # Create Market Sell Limit Orders - auth = Bybit().auth(key, secret) - prices = buy('BTCUSD', 9550, 1.000) - - - - - # Cancelling an order - auth = Bybit().auth(key, secret) - prices = cancel_order(order_id) - - - - - -For more check out the documentation - - https://bybit-exchange.github.io/bybit-official-api-docs/en/index.html - diff --git a/bybit/bybit/__init__.py b/bybit/bybit/__init__.py deleted file mode 100644 index 91849b5..0000000 --- a/bybit/bybit/__init__.py +++ /dev/null @@ -1 +0,0 @@ -name = "bybit" \ No newline at end of file diff --git a/key_example.py b/key_example.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb479be --- /dev/null +++ b/requirements.txt @@ -0,0 +1,38 @@ +appdirs==1.4.3 +atomicwrites==1.3.0 +attrs==19.1.0 +backcall==0.1.0 +black==19.3b0 +certifi==2019.6.16 +chardet==3.0.4 +Click==7.0 +decorator==4.4.0 +idna==2.8 +importlib-metadata==0.19 +influxdb==5.2.2 +ipython==7.7.0 +ipython-genutils==0.2.0 +ipython-pytest==0.0.1 +jedi==0.15.1 +more-itertools==7.2.0 +packaging==19.1 +parso==0.5.1 +pexpect==4.7.0 +pickleshare==0.7.5 +pluggy==0.12.0 +prompt-toolkit==2.0.9 +ptyprocess==0.6.0 +py==1.8.0 +Pygments==2.4.2 +pyparsing==2.4.2 +pytest==5.1.1 +python-dateutil==2.8.0 +pytz==2019.2 +requests==2.22.0 +six==1.12.0 +toml==0.10.0 +traitlets==4.3.2 +urllib3==1.25.3 +wcwidth==0.1.7 +websockets==8.0.2 +zipp==0.5.2 diff --git a/bybit/setup.py b/setup.py similarity index 53% rename from bybit/setup.py rename to setup.py index 9ae596b..7cf6a20 100644 --- a/bybit/setup.py +++ b/setup.py @@ -1,21 +1,36 @@ -import setuptools +from setuptools import setup, find_packages +from distutils.command.sdist import sdist as sdist_orig +from distutils.errors import DistutilsExecError + +class keyadd(sdist_orig): + def run(self): + try: + self.spawn(['source', 'key.sh']) + except: + self.warn('adding key env variables failed') + super().run() + +# read in description with open("README.md", "r") as fh: long_description = fh.read() -setuptools.setup( +setup( name="bybit", version="0.0.1", - author="Muema", + author="Muemrga", author_email="hi@ssi.co.ke", description="unofficial Bybit Exchange API python implementation for automated trading", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/m-root/bybit-exchange-python-sdk", - packages=setuptools.find_packages(), + packages=find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: GNU GENERAL PUBLIC LICENSE", "Operating System :: OS Independent", ], + cmdclass={ + 'keyadd': keyadd + }, ) diff --git a/test_bybit.py b/test_bybit.py new file mode 100644 index 0000000..aacb70c --- /dev/null +++ b/test_bybit.py @@ -0,0 +1,22 @@ +import pytest +import os +from bybit import Bybit + +KEY = os.environ['BTKEY'] +SECRET = os.environ['BTSECRET'] + +bt = Bybit() +bt.auth(KEY, SECRET) + +@pytest.mark.parametrize("bt",[(bt)]) +def test_get_symbols(bt): + assert len(bt.get_symbols()) > 0 + +@pytest.mark.parametrize("bt",[(bt)]) +def test_get_klines(bt): + klines = bt.get_klines('BTCUSD', '15', '1563367231') + print(klines) + print(f'len klines: {len(klines)}') + +if __name__ == "__main__": + test_get_klines(bt) \ No newline at end of file