bhive.market module

class bhive.market.Market(base=None, quote=None, hive_instance=None)

Bases: dict

This class allows to easily access Markets on the blockchain for trading, etc.

Parameters
  • hive_instance (Hive) – Hive instance

  • base (Asset) – Base asset

  • quote (Asset) – Quote asset

Returns

Blockchain Market

Return type

dictionary with overloaded methods

Instances of this class are dictionaries that come with additional methods (see below) that allow dealing with a market and its corresponding functions.

This class tries to identify two assets as provided in the parameters in one of the following forms:

  • base and quote are valid assets (according to bhive.asset.Asset)

  • base:quote separated with :

  • base/quote separated with /

  • base-quote separated with -

Note

Throughout this library, the quote symbol will be presented first (e.g. HIVE:HBD with HIVE being the quote), while the base only refers to a secondary asset for a trade. This means, if you call bhive.market.Market.sell() or bhive.market.Market.buy(), you will sell/buy only quote and obtain/pay only base.

accountopenorders(account=None, raw_data=False)

Returns open Orders

Parameters
  • account (Account) – Account name or instance of Account to show orders for in this market

  • raw_data (bool) – (optional) returns raw data if set True, or a list of Order() instances if False (defaults to False)

static btc_usd_ticker(verbose=False)

Returns the BTC/USD price from bitfinex, gdax, kraken, okcoin and bitstamp. The mean price is weighted by the exchange volume.

buy(price, amount, expiration=None, killfill=False, account=None, orderid=None, returnOrderId=False)

Places a buy order in a given market

Parameters
  • price (float) – price denoted in base/quote

  • amount (number) – Amount of quote to buy

  • expiration (number) – (optional) expiration time of the order in seconds (defaults to 7 days)

  • killfill (bool) – flag that indicates if the order shall be killed if it is not filled (defaults to False)

  • account (string) – Account name that executes that order

  • returnOrderId (string) – If set to “head” or “irreversible” the call will wait for the tx to appear in the head/irreversible block and add the key “orderid” to the tx output

Prices/Rates are denoted in ‘base’, i.e. the HBD_HIVE market is priced in HIVE per HBD.

Example: in the HBD_HIVE market, a price of 300 means a HBD is worth 300 HIVE

Note

All prices returned are in the reversed orientation as the market. I.e. in the HIVE/HBD market, prices are HBD per HIVE. That way you can multiply prices with 1.05 to get a +5%.

Warning

Since buy orders are placed as limit-sell orders for the base asset, you may end up obtaining more of the buy asset than you placed the order for. Example:

  • You place and order to buy 10 HBD for 100 HIVE/HBD

  • This means that you actually place a sell order for 1000 HIVE in order to obtain at least 10 HBD

  • If an order on the market exists that sells HBD for cheaper, you will end up with more than 10 HBD

cancel(orderNumbers, account=None, **kwargs)

Cancels an order you have placed in a given market. Requires only the “orderNumbers”.

Parameters

orderNumbers (int, list) – A single order number or a list of order numbers

get_string(separator=':')

Return a formated string that identifies the market, e.g. HIVE:HBD

Parameters

separator (str) – The separator of the assets (defaults to :)

static hive_btc_ticker()

Returns the HIVE/BTC price from bittrex, binance, huobi and upbit. The mean price is weighted by the exchange volume.

hive_usd_implied()

Returns the current HIVE/USD market price

market_history(bucket_seconds=300, start_age=3600, end_age=0, raw_data=False)

Return the market history (filled orders).

Parameters
  • bucket_seconds (int) – Bucket size in seconds (see returnMarketHistoryBuckets())

  • start_age (int) – Age (in seconds) of the start of the window (default: 1h/3600)

  • end_age (int) – Age (in seconds) of the end of the window (default: now/0)

  • raw_data (bool) – (optional) returns raw data if set True

Example:

{
    'close_hbd': 2493387,
    'close_hive': 7743431,
    'high_hbd': 1943872,
    'high_hive': 5999610,
    'id': '7.1.5252',
    'low_hbd': 534928,
    'low_hive': 1661266,
    'open': '2016-07-08T11:25:00',
    'open_hbd': 534928,
    'open_hive': 1661266,
    'sbd_volume': 9714435,
    'seconds': 300,
    'steem_volume': 30088443
}
market_history_buckets()
orderbook(limit=25, raw_data=False)

Returns the order book for HBD/HIVE market.

Parameters

limit (int) – Limit the amount of orders (default: 25)

Sample output (raw_data=False):

{
    'asks': [
        380.510 HIVE 460.291 HBD @ 1.209669 HBD/HIVE,
        53.785 HIVE 65.063 HBD @ 1.209687 HBD/HIVE
    ],
    'bids': [
        0.292 HIVE 0.353 HBD @ 1.208904 HBD/HIVE,
        8.498 HIVE 10.262 HBD @ 1.207578 HBD/HIVE
    ],
    'asks_date': [
        datetime.datetime(2018, 4, 30, 21, 7, 24, tzinfo=<UTC>),
        datetime.datetime(2018, 4, 30, 18, 12, 18, tzinfo=<UTC>)
    ],
    'bids_date': [
        datetime.datetime(2018, 4, 30, 21, 1, 21, tzinfo=<UTC>),
        datetime.datetime(2018, 4, 30, 20, 38, 21, tzinfo=<UTC>)
    ]
}

Sample output (raw_data=True):

{
    'asks': [
        {
            'order_price': {'base': '8.000 HIVE', 'quote': '9.618 HBD'},
            'real_price': '1.20225000000000004',
            'hive': 4565,
            'hbd': 5488,
            'created': '2018-04-30T21:12:45'
        }
    ],
    'bids': [
        {
            'order_price': {'base': '10.000 HBD', 'quote': '8.333 HIVE'},
            'real_price': '1.20004800192007677',
            'hive': 8333,
            'hbd': 10000,
            'created': '2018-04-30T20:29:33'
        }
    ]
}

Note

Each bid is an instance of class:bhive.price.Order and thus carries the keys base, quote and price. From those you can obtain the actual amounts for sale

recent_trades(limit=25, raw_data=False)

Returns the order book for a given market. You may also specify “all” to get the orderbooks of all markets.

Parameters
  • limit (int) – Limit the amount of orders (default: 25)

  • raw_data (bool) – when False, FilledOrder objects will be returned

Sample output (raw_data=False):

[
    (2018-04-30 21:00:54+00:00) 0.267 HIVE 0.323 HBD @ 1.209738 HBD/HIVE,
    (2018-04-30 20:59:30+00:00) 0.131 HIVE 0.159 HBD @ 1.213740 HBD/HIVE,
    (2018-04-30 20:55:45+00:00) 0.093 HIVE 0.113 HBD @ 1.215054 HBD/HIVE,
    (2018-04-30 20:55:30+00:00) 26.501 HIVE 32.058 HBD @ 1.209690 HBD/HIVE,
    (2018-04-30 20:55:18+00:00) 2.108 HIVE 2.550 HBD @ 1.209677 HBD/HIVE,
]

Sample output (raw_data=True):

[
    {'date': '2018-04-30T21:02:45', 'current_pays': '0.235 HBD', 'open_pays': '0.194 HIVE'},
    {'date': '2018-04-30T21:02:03', 'current_pays': '24.494 HBD', 'open_pays': '20.248 HIVE'},
    {'date': '2018-04-30T20:48:30', 'current_pays': '175.464 HIVE', 'open_pays': '211.955 HBD'},
    {'date': '2018-04-30T20:48:30', 'current_pays': '0.999 HIVE', 'open_pays': '1.207 HBD'},
    {'date': '2018-04-30T20:47:54', 'current_pays': '0.273 HBD', 'open_pays': '0.225 HIVE'},
]

Note

Each bid is an instance of bhive.price.Order and thus carries the keys base, quote and price. From those you can obtain the actual amounts for sale

sell(price, amount, expiration=None, killfill=False, account=None, orderid=None, returnOrderId=False)

Places a sell order in a given market

Parameters
  • price (float) – price denoted in base/quote

  • amount (number) – Amount of quote to sell

  • expiration (number) – (optional) expiration time of the order in seconds (defaults to 7 days)

  • killfill (bool) – flag that indicates if the order shall be killed if it is not filled (defaults to False)

  • account (string) – Account name that executes that order

  • returnOrderId (string) – If set to “head” or “irreversible” the call will wait for the tx to appear in the head/irreversible block and add the key “orderid” to the tx output

Prices/Rates are denoted in ‘base’, i.e. the HBD_HIVE market is priced in HIVE per HBD.

Example: in the HBD_HIVE market, a price of 300 means a HBD is worth 300 HIVE

Note

All prices returned are in the reversed orientation as the market. I.e. in the HIVE/HBD market, prices are HBD per HIVE. That way you can multiply prices with 1.05 to get a +5%.

ticker(raw_data=False)

Returns the ticker for all markets.

Output Parameters:

  • latest: Price of the order last filled

  • lowest_ask: Price of the lowest ask

  • highest_bid: Price of the highest bid

  • sbd_volume: Volume of HBD

  • steem_volume: Volume of HIVE

  • percent_change: 24h change percentage (in %)

Note

Market is HIVE:HBD and prices are HBD per HIVE!

Sample Output:

 {
    'highest_bid': 0.30100226633322913,
    'latest': 0.0,
    'lowest_ask': 0.3249636958897082,
    'percent_change': 0.0,
    'sbd_volume': 108329611.0,
    'steem_volume': 355094043.0
}
trade_history(start=None, stop=None, intervall=None, limit=25, raw_data=False)

Returns the trade history for the internal market

This function allows to fetch a fixed number of trades at fixed intervall times to reduce the call duration time. E.g. it is possible to receive the trades from the last 7 days, by fetching 100 trades each 6 hours.

When intervall is set to None, all trades are received between start and stop. This can take a while.

Parameters
  • start (datetime) – Start date

  • stop (datetime) – Stop date

  • intervall (timedelta) – Defines the intervall

  • limit (int) – Defines how many trades are fetched at each intervall point

  • raw_data (bool) – when True, the raw data are returned

trades(limit=100, start=None, stop=None, raw_data=False)

Returns your trade history for a given market.

Parameters
  • limit (int) – Limit the amount of orders (default: 100)

  • start (datetime) – start time

  • stop (datetime) – stop time

volume24h(raw_data=False)

Returns the 24-hour volume for all markets, plus totals for primary currencies.

Sample output:

{
    "HIVE": 361666.63617,
    "HBD": 1087.0
}