-
Notifications
You must be signed in to change notification settings - Fork 4
Getting Started
poloniex-sdk edited this page Sep 22, 2022
·
1 revision
Required Python version 3.7+
Installation steps via pypi coming for now just checkout repo.
If you do not have a Poloniex account yet, use the link to sign up.
Once you are verified and have an account, you can create an API Key.
Enabling IP address restrictions for API keys is strongly recommended. Withdrawals are disabled by default and must be enabled on a per key basis.
As the name implies, your secret must remain private! If you suspect your key has been compromised, immediately disable that key and generate a new one.
from polosdk import RestClient, RequestError
def main():
client = RestClient()
# get server timestamp
try:
response = client.get_timestamp()
print(f'Server Timestamp:\n{response}\n')
except RequestError as request_error:
print(f'Server Timestamp:\n{request_error}\n')
# get all supported currencies
try:
response = client.get_currencies()
print(f'Supported Currencies:\n{response}\n')
except RequestError as request_error:
print(f'Supported Currencies:\n{request_error}\n')
# get all supported markets
try:
response = client.get_markets()
print(f'Supported Markets:\n{response}\n')
except RequestError as request_error:
print(f'Supported Markets:\n{request_error}\n')
# get 24h ticker for BTC_USDT
try:
response = client.markets().get_ticker24h('BTC_USDT')
print(f'Supported Markets:\n{response}\n')
except RequestError as request_error:
print(f'Supported Markets:\n{request_error}\n')
if __name__ == '__main__':
main()
import asyncio
from polosdk import WsClientPublic
async def ws_public_example():
def on_message(msg):
print(msg)
def on_error(err):
print(err)
ws_client_public = WsClientPublic(on_message, on_error=on_error)
await ws_client_public.connect()
await ws_client_public.subscribe(['ticker'], ['ETH_USDT'])
await ws_client_public.subscribe(['book', 'trades'], ['TRX_USDT', 'BTC_USDT'])
await ws_client_public.list_subscriptions()
await asyncio.sleep(5)
await ws_client_public.unsubscribe(['book'], ['TRX_USDT', 'BTC_USDT'])
await ws_client_public.unsubscribe(['ticker'], ['ETH_USDT'])
await ws_client_public.list_subscriptions()
await asyncio.sleep(5)
await ws_client_public.unsubscribe_all()
await ws_client_public.list_subscriptions()
await asyncio.sleep(1)
await ws_client_public.disconnect()
def main():
asyncio.run(ws_public_example())
if __name__ == '__main__':
main()
Set environment variables that contain your API Key values: POLO_API_KEY and POLO_API_SECRET.
import os
from polosdk import RestClient, RequestError
api_key = os.environ['POLO_API_KEY']
api_secret = os.environ['POLO_API_SECRET']
def main():
client = RestClient(api_key, api_secret)
# get all account balances
try:
response = client.accounts().get_balances()
print(f'Account Balances:\n{response}\n')
except RequestError as request_error:
print(f'Account Balances:\n{request_error}\n')
# create limit order
order_id = None
try:
response = client.orders().create(price='20000',
quantity='0.00025',
side='BUY',
symbol='BTC_USDT',
type='LIMIT',
client_order_id='1234Abc')
print(f'Create Limit Order:\n{response}\n')
order_id = response['id']
except RequestError as request_error:
print(f'Create Limit Order:\n{request_error}\n')
return
# get order by id
try:
response = client.orders().get_by_id(order_id)
print(f'Get Order By Id:\n{response}\n')
except RequestError as request_error:
print(f'Get Order By Id:\n{request_error}\n')
# cancel open order by id (WARNING: If order is filled it can't be cancelled)
try:
response = client.orders().cancel_by_id(order_id)
print(f'Cancel Order By Id:\n{response}\n')
except RequestError as request_error:
print(f'Cancel Order By Id:\n{request_error}\n')
if __name__ == '__main__':
main()
import asyncio
import os
from polosdk import WsClientAuthenticated
api_key = os.environ['POLO_API_KEY']
api_secret = os.environ['POLO_API_SECRET']
async def ws_authenticated_example():
def on_message(msg):
print(msg)
def on_error(err):
print(err)
ws_client_authenticated = WsClientAuthenticated(on_message, on_error=on_error)
await ws_client_authenticated.connect(api_key, api_secret)
await asyncio.sleep(1)
await ws_client_authenticated.subscribe(['orders', 'balances'], ['TRX_USDT'])
await ws_client_authenticated.list_subscriptions()
await asyncio.sleep(1)
await ws_client_authenticated.unsubscribe(['orders'], ['TRX_USDT'])
await ws_client_authenticated.list_subscriptions()
await asyncio.sleep(1)
await ws_client_authenticated.disconnect()
def main():
asyncio.run(ws_authenticated_example())
if __name__ == '__main__':
main()