Skip to content

Documentation

gokulk04 edited this page Sep 4, 2018 · 14 revisions

About

Welcome to the py-crypto wiki! Here, you can find further documentation of the py-crypto library to better understand its usage and implementation.

Table of Contents

Installation

This part of the documentation covers the installation of py-crypto, which can be done using pip, as follows:

pip install py-crypto

Getting Started

To get started with using the py-crypto library, we will begin by importing the py-crypto module:

import pycrypto.client as pyc

Connecting to an Exchange

This section will cover connecting to a cryptocurrency exchange. This is done by using the Client object.

Parameters

Parameter Data Type Required/Optional Notes
Exchange Name String Required Used to specify which exchange to connect to. Exchange names listed as Constants. (ie. pyc.Exchange.BINANCE, pyc.Exchange.BITTREX, ... )
API Key String Required
API Secret String Required

Usage

client = pyc.Client([exchange-name], [your-api-key], [your-api-secret])

For example, you can connect to your Binance account as follows:

BINANCE_API_KEY = "MY_BINANCE_API_KEY"
BINANCE_API_SECRET = "MY_BINANCE_API_SECRET"

binance_client = pyc.Client(pyc.Exchange.BINANCE, BINANCE_API_KEY, BINANCE_API_SECRET)

You are now connected to an exchange and are ready to interact with it.

Account Information

The py-crypto library offers several methods to retrieve metadata about your account, all of which can be accessed through your Client object.

get_all_balances()

The get_all_balances() method retrieves your account balance, in terms of quantity and equivalent dollar value, of each currency offered by the exchange. The method takes no parameters and can be used as follows:

Usage

client.get_all_balances()

get_balance()

The get_balance() method retrieves the asset balance, in terms of quantity and and equivalent dollar value, of the currency specified by the user as an input parameter.

Parameters

Parameter Data Type Required/Optional Notes
Currency String Required ie. "BTC", "ETH", "LTC", etc ...

Usage

client.get_balance([currency])

For example, you can retrieve your Bitcoin (BTC) balance as follows:

client.get_balance("BTC")

Market Information

get_ticker()

The get_ticker() method retrieves current price metadata of a specific ticker specified as an input parameter.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Required A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.get_ticker([ticker-symbol])

For example, you can retrieve the current price metadata of Bitcoin (BTC) with respect to US Dollar Tether (USDT) as follows:

client.get_ticker("USDT-BTC")

get_current_price()

The get_ticker() method retrieves current price of a specific ticker, represented as a float data type.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Required A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.get_current_price([ticker-symbol])

For example, you can retrieve the current price metadata of Bitcoin (BTC) with respect to US Dollar Tether (USDT) as follows:

client.get_current_price("USDT-BTC")

Trade Execution

The py-crypto library enables users to execute trades through connected exchanges. Users are able to create and cancel market and limit orders on all available tickers.

create_market_order()

The create_market_order() method is used to buy or sell a security at the best-available price in the current market. For more information on how market orders work, visit this article.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Required A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)
Action String Required Used to specify whether to buy or sell a security. Action types listed as Constants. (ie. pyc.Trade.ORDER_ACTION_SELL, pyc.Trade.ORDER_ACTION_BUY)
Quantity Float Required Float value specifying number of units to be bought or sold. (ie. 1.0, 2.5, 3.14, etc...)

Usage

client.create_market_order([ticker-symbol], [action], [quantity])

For example, you can place a market order to buy 5 units of Bitcoin (BTC) against US Dollar Tether (USDT) as shown below:

ticker = "USDT-BTC"
action = pyc.Trade.ORDER_ACTION_BUY
quantity = 15.0 

client.create_market_order(ticker, action, quantity)

create_limit_order()

The create_limit_order() method is used to buy or sell a security at a specified limit price or better. For more information on how limit orders work, visit this article.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Required A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)
Action String Required Used to specify whether to buy or sell a security. Action types listed as Constants. (ie. pyc.Trade.ORDER_ACTION_SELL, pyc.Trade.ORDER_ACTION_BUY)
Quantity Float Required Float value specifying number of units to be bought or sold. (ie. 1.0, 2.5, 3.14, etc...)
Price Float Required Float value specifying limit price at which to execute order. (ie. 50.0, 1234.5, 99999.9, etc...)

Usage

client.create_limit_order([ticker-symbol], [action], [quantity], [price])

For example, you can place a market order to buy 10 units of Litecoin (LTC) against US Dollar Tether (USDT) only when the price reaches or goes below $68.00, as shown below:

ticker = "USDT-LTC"
action = pyc.Trade.ORDER_ACTION_BUY
quantity = 10.0 
price = 68.00

client.create_market_order(ticker, action, quantity, price)

cancel_order()

The cancel_order() method is used to cancel an open order given its unique order id as an input parameter. The order id can be retrieved from the response given from executing either the create_market_order() or create_limit_order() methods.

Parameters

Parameter Data Type Required/Optional Notes
Order ID String Required A unique string given as a response from executing a trade order.
Ticker Symbol String Optional A string literal with the market name followed by the currency name, separated by a hyphen. Only required for certain exchanges. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.cancel_order([order-id], [ticker])

For example, the following code can be used to cancel an order with an order id of 1.

order_id = 1

client.cancel_order(1)

Order Information

The py-crypto library enables users to retrieve metadata about order that were executed in the past through the following methods.

get_order_history()

The get_order_history() method returns a list of all orders executed by an account. The method takes an optional ticker symbol as an input parameter should the user only want to return all orders executed for a specific market.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Optional A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.get_order_history([ticker])

For example, the following code can be used to retrieve a list of all orders executed within the "BTC-ETH" market:

ticker_symbol = "BTC-ETH"

client.get_order_history(ticker_symbol)

get_open_orders()

The get_open_orders() method returns a list of all orders that are still open and un-filled. The method takes an optional ticker symbol as an input parameter should the user only want to return all open orders for a specific market.

Parameters

Parameter Data Type Required/Optional Notes
Ticker Symbol String Optional A string literal with the market name followed by the currency name, separated by a hyphen. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.get_open_orders([ticker])

For example, the following code can be used to retrieve a list of all open orders within the "BTC-ETH" market:

ticker_symbol = "BTC-ETH"

client.get_open_orders(ticker_symbol)

get_order_status()

The get_order_status() method returns metadata regarding the status of an order given its unique order id.

Parameters

Parameter Data Type Required/Optional Notes
Order ID String Required A unique string given as a response from executing a trade order.
Ticker Symbol String Optional A string literal with the market name followed by the currency name, separated by a hyphen. Only required for certain exchanges. (ie. "BTC-LTC", "BTC-ETH", "USDT-BTC", etc ...)

Usage

client.get_order_status([order-id], [ticker-symbol])

For example, the following code can be used to retrieve a the status of a specific order:

order_id = 1

client.get_order_status(order_id)