btfx_trader package

Submodules

btfx_trader.private module

class btfx_trader.private.Trader(key, secret)[source]

Bases: btfx_trader.private._Account

Class interface for trading on the Bitfinex Exchange :param key: str: Bitfinex api-key :param secret: str: Bitfinex api-secret

cancel(_id)[source]

Cancel an order :param _id: int: order id :return: None

cancel_all(older_than=0)[source]

Cancel all orders older than a certain time :param older_than: int/float:

age of order that should be cancelled (in seconds)
Returns:None
close()[source]

Close the connection to the Bitfinex websocket

connect()[source]

Open a connection to a Bitfinex websocket

order(symbol, price, *, dollar_amount=None, ratio=None, value_ratio=None, trade_type='limit', pad_price=None, return_id=True)[source]

Make an exchange order :param symbol: str:

ticker symbol to order (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Parameters:
  • price – float: Price at which to submit order You can also pass “market” as an argument for price to order at current market value e.g. Trader.order(‘BTCUSD’, ‘market’, dollar_amount=5)
  • dollar_amount

    float: Dollar equivalent value of trade amount (negative for selling) e.g. ordering 10 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, dollar_amount=50)
  • ratio

    float: Ratio of available balance for requested symbol (negative for sell) e.g. With $1000 USD in wallet, ordering 100 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, ratio=0.5)
  • value_ratio

    float: Ratio of Trader.value (negative for selling) e.g. With $500 in USD and $500 in BTCUSD (at $5) in wallet,

    ordering 100 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, value_ratio=0.5)

  • trade_type – str: (optional) Bitfinex api trade type - one of {‘market’, ‘limit’} see https://www.bitfinex.com/features for details
  • pad_price – float: (optional) Ratio based price padding - used to undercut or overshoot price If buying then pad_price * price is added to submitted price If selling then pad_price * price is subtracted from price
  • return_id – bool: (optional) If true, Trader.order blocks until order_id is returned from Bitfinex, otherwise returns immediately
Returns:

int: If return_id is True, then order id is returned, otherwise None

Raises:

AssertionError: If values passed are non-consistent

subscribe(symbol)[source]

Subscribe to a symbol for price watching :param symbol: str: symbol to subscribe to (e.g. ‘BTCUSD’, ‘ETHUSD’…)

wait_execution(_id, seconds=1000000000.0)[source]

Waits for an order to execute :param _id: int: id of order to wait for :param seconds: int/float: time to wait before raising error :return: dict: order in json format :raises: TimeoutError (if order is not executed in given time)

btfx_trader.public module

class btfx_trader.public.PublicData(types=None, symbols=None)[source]

Bases: object

Wrapper for BtfxWss for access to public bitfinex data :param types: list:

(optional) data types to filter data by (tickers, trades, candles)
Parameters:symbols – list: (optional) symbol to filter data by (e.g. [‘BTCUSD’, ‘ETHUSD’])
candle_periods = ['1m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '1D', '7D', '14D', '1M']
close()[source]

Close the connection to the Bitfinex websocket

connect()[source]

Open a connection to a Bitfinex websocket

get(_type, symbol)[source]

Gets next available data (blocks until new datum is received) :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Returns:dict: requested data in json format
Raises:AssertionError: if either of the provided type and symbol are unrecognized
get_nowait(_type, symbol)[source]

Gets currently available data with no blocking :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Returns:

dict: requested data in json format

Raises:
AssertionError:

if either of the provided type and symbol are unrecognized

Empty:

if no new data is available (same as queue.Queue api)

subscribe(_type, symbol)[source]

Subscribe to a stream of data :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
btfx_trader.public.get_symbols()[source]
btfx_trader.public.get_symbols_as_updated(check_every=43200)[source]

Module contents

btfx_trader.get_symbols()[source]
btfx_trader.get_symbols_as_updated(check_every=43200)[source]
class btfx_trader.PublicData(types=None, symbols=None)[source]

Bases: object

Wrapper for BtfxWss for access to public bitfinex data :param types: list:

(optional) data types to filter data by (tickers, trades, candles)
Parameters:symbols – list: (optional) symbol to filter data by (e.g. [‘BTCUSD’, ‘ETHUSD’])
candle_periods = ['1m', '5m', '15m', '30m', '1h', '3h', '6h', '12h', '1D', '7D', '14D', '1M']
close()[source]

Close the connection to the Bitfinex websocket

connect()[source]

Open a connection to a Bitfinex websocket

get(_type, symbol)[source]

Gets next available data (blocks until new datum is received) :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Returns:dict: requested data in json format
Raises:AssertionError: if either of the provided type and symbol are unrecognized
get_nowait(_type, symbol)[source]

Gets currently available data with no blocking :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Returns:

dict: requested data in json format

Raises:
AssertionError:

if either of the provided type and symbol are unrecognized

Empty:

if no new data is available (same as queue.Queue api)

subscribe(_type, symbol)[source]

Subscribe to a stream of data :param _type: str: type of data to get (e.g. ‘tickers’, ‘candles’…) :param symbol: str:

symbol for which to get data (e.g. ‘BTCUSD’, ‘ETHUSD’…)
class btfx_trader.Trader(key, secret)[source]

Bases: btfx_trader.private._Account

Class interface for trading on the Bitfinex Exchange :param key: str: Bitfinex api-key :param secret: str: Bitfinex api-secret

cancel(_id)[source]

Cancel an order :param _id: int: order id :return: None

cancel_all(older_than=0)[source]

Cancel all orders older than a certain time :param older_than: int/float:

age of order that should be cancelled (in seconds)
Returns:None
close()[source]

Close the connection to the Bitfinex websocket

connect()[source]

Open a connection to a Bitfinex websocket

order(symbol, price, *, dollar_amount=None, ratio=None, value_ratio=None, trade_type='limit', pad_price=None, return_id=True)[source]

Make an exchange order :param symbol: str:

ticker symbol to order (e.g. ‘BTCUSD’, ‘ETHUSD’…)
Parameters:
  • price – float: Price at which to submit order You can also pass “market” as an argument for price to order at current market value e.g. Trader.order(‘BTCUSD’, ‘market’, dollar_amount=5)
  • dollar_amount

    float: Dollar equivalent value of trade amount (negative for selling) e.g. ordering 10 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, dollar_amount=50)
  • ratio

    float: Ratio of available balance for requested symbol (negative for sell) e.g. With $1000 USD in wallet, ordering 100 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, ratio=0.5)
  • value_ratio

    float: Ratio of Trader.value (negative for selling) e.g. With $500 in USD and $500 in BTCUSD (at $5) in wallet,

    ordering 100 BTCUSD at $5 per coin

    -> Trader.order(‘BTCUSD’, 5, value_ratio=0.5)

  • trade_type – str: (optional) Bitfinex api trade type - one of {‘market’, ‘limit’} see https://www.bitfinex.com/features for details
  • pad_price – float: (optional) Ratio based price padding - used to undercut or overshoot price If buying then pad_price * price is added to submitted price If selling then pad_price * price is subtracted from price
  • return_id – bool: (optional) If true, Trader.order blocks until order_id is returned from Bitfinex, otherwise returns immediately
Returns:

int: If return_id is True, then order id is returned, otherwise None

Raises:

AssertionError: If values passed are non-consistent

subscribe(symbol)[source]

Subscribe to a symbol for price watching :param symbol: str: symbol to subscribe to (e.g. ‘BTCUSD’, ‘ETHUSD’…)

wait_execution(_id, seconds=1000000000.0)[source]

Waits for an order to execute :param _id: int: id of order to wait for :param seconds: int/float: time to wait before raising error :return: dict: order in json format :raises: TimeoutError (if order is not executed in given time)