Code Monkey home page Code Monkey logo

polo-futures-sdk-python's Introduction

polo-futures


Python 3 Wrapper for Poloniex Futures Exchange

DISCLAIMER:

USE AT YOUR OWN RISK. You should not use this code in production unless you fully understand its limitations. 
Even if you understand the code and its limitations, you may incur losses when using this code for trading. 
When used in combination with your own code, the combination may not function as intended, and as a result you may incur losses. 
Poloniex is not responsible for any losses you may incur when using this code.

Features

  • Support for REST and websocket endpoints
  • Simple handling of authentication
  • Response exception handling

Getting Started

Get the code files with git.

Clone the repo into the path you will be using

git clone https://github.com/poloniex/polo-futures-sdk-python

Samples of the wrappers' usage are found in rest_sample.py and ws_sample.py. These can be run directly from python. With,

python rest_sample.py

OR

python ws_sample.py

Code Samples

REST API

import os

from polofutures import RestClient


# Account Keys
API_KEY = os.environ['PF_API_KEY']
SECRET = os.environ['PF_SECRET']
API_PASS = os.environ['PF_PASS']

rest_client = RestClient(API_KEY, SECRET, API_PASS)

SYMBOL = 'BTCUSDTPERP'

# Fetch MarketData
market = rest_client.market_api()

server_time = market.get_server_timestamp()
l3_depth = market.get_l3_order_book(SYMBOL)
l2_depth = market.get_l2_order_book(SYMBOL)
klines = market.get_ticker(SYMBOL)

# Trade Functions
trade = rest_client.trade_api()

order_id = trade.create_limit_order(SYMBOL, 'buy', '1', '30', '8600')
cancel_id = trade.cancel_order(order_id['orderId'])
order_id = trade.create_limit_order(SYMBOL, 'buy', '1', '30', '8600')
cancel_all = trade.cancel_all_limit_orders(SYMBOL)


# User Account Functions
user = rest_client.user_api()

account_overview = user.get_account_overview()

Websockets

import asyncio
import os

from polofutures import WsClient


# Account Keys
API_KEY = os.environ['PF_API_KEY']
SECRET = os.environ['PF_SECRET']
API_PASS = os.environ['PF_PASS']

SYMBOL = 'BTCUSDTPERP'

async def ws_stream():
    def on_message(msg):
        if msg['topic'] == f'/contract/instrument:{SYMBOL}':
            print(f'Get {SYMBOL} Index Price: {msg["data"]}')
        elif msg['topic'] == f'/contractMarket/execution:{SYMBOL}':
            print(f'Last Execution: {msg["data"]}')
        elif msg['topic'] == f'/contractMarket/level2:{SYMBOL}':
            print(f'Get {SYMBOL} Level 2 :{msg["data"]}')

    ws_client = WsClient(on_message, API_KEY, SECRET, API_PASS)

    await ws_client.connect()

    await ws_client.subscribe(f'/contract/instrument:{SYMBOL}')
    await ws_client.subscribe(f'/contractMarket/execution:{SYMBOL}')
    await ws_client.subscribe(f'/contractMarket/level2:{SYMBOL}')

    await asyncio.sleep(30)

    await ws_client.disconnect()


if __name__ == "__main__":
    asyncio.run(ws_stream())

polo-futures-sdk-python's People

Contributors

chrisrut avatar wonniesim avatar

Stargazers

Jussi Haverinen avatar  avatar  avatar Camap6789 avatar Rafal Pieniazek avatar

Watchers

Cory Farinella avatar charles.liang avatar  avatar Maura Wilder avatar

Forkers

mik-patient

polo-futures-sdk-python's Issues

Market order returns error on the RS side: 'The price parameter cannot be empty.'

Hello, I'm trying to use your SDK I'm raising this error:

order = trade.create_market_order('BTCUSDTPERP','buy',1,1)
File "C:\Users\Papa\AppData\Local\Programs\Python\Python310\lib\site-packages\polofutures\rest\trade.py", line 197, in create_market_order return self._request('POST', '/api/v1/orders', params, True)
File "C:\Users\Papa\AppData\Local\Programs\Python\Python310\lib\site-packages\polofutures\rest\core.py", line 83, in __call__raise RuntimeError(payload)
RuntimeError: {'code': '100001', 'msg': 'The price parameter cannot be empty.'}

Best regards

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.