Binance DataBase

class BinanceWatch.storage.BinanceDataBase.BinanceDataBase(name: str = 'binance_db')[source]

Bases: BinanceWatch.storage.DataBase.DataBase

Handles the recording of the binance account in a local database

__init__(name: str = 'binance_db')[source]

Initialise a binance database instance

Parameters

name (str) – name of the database

add_deposit(tx_id: str, insert_time: int, amount: float, asset: str, auto_commit=True)[source]

Add a deposit to the database

Parameters
  • tx_id (str) – transaction id

  • insert_time (int) – millistamp when the deposit arrived on binance

  • amount (float) – amount of token deposited

  • asset (str) – name of the token

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_dividend(div_id: int, div_time: int, asset: str, amount: float, auto_commit: bool = True)[source]

Add a dividend to the database

Parameters
  • div_id (int) – dividend id

  • div_time (int) – millistamp of dividend reception

  • asset (str) – name of the dividend unit

  • amount (float) – amount of asset distributed

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_isolated_transfer(transfer_id: int, transfer_type: str, transfer_time: int, isolated_symbol: str, asset: str, amount: float, auto_commit: bool = True)[source]

Add a universal transfer to the database

Parameters
  • transfer_id (int) – id of the transfer

  • transfer_type (str) – enum of the transfer type (ex: ‘MAIN_MARGIN’)

  • transfer_time (int) – millistamp of the operation

  • isolated_symbol (str) – isolated symbol that received or sent the transfer

  • asset (str) – asset that got transferred

  • amount (float) – amount transferred

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_lending_interest(time: int, lending_type: str, asset: str, amount: float, auto_commit: bool = True)[source]

Add an lending interest to the database

Parameters
  • time (int) – millitstamp of the operation

  • lending_type (str) – either ‘DAILY’, ‘ACTIVITY’ or ‘CUSTOMIZED_FIXED’

  • asset (str) – asset that was received

  • amount (float) – amount of asset received

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_lending_purchase(purchase_id: int, purchase_time: int, lending_type: str, asset: str, amount: float, auto_commit: bool = True)[source]

Add a lending purchase to the database

Parameters
  • purchase_id (int) – id of the purchase

  • purchase_time (int) – millitstamp of the operation

  • lending_type (str) – either ‘DAILY’, ‘ACTIVITY’ or ‘CUSTOMIZED_FIXED’

  • asset (str) – asset lent

  • amount (float) – amount of asset lent

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_lending_redemption(redemption_time: int, lending_type: str, asset: str, amount: float, auto_commit: bool = True)[source]

Add a lending redemption to the database

Parameters
  • redemption_time (int) – millitstamp of the operation

  • lending_type (str) – either ‘DAILY’, ‘ACTIVITY’ or ‘CUSTOMIZED_FIXED’

  • asset (str) – asset lent

  • amount (float) – amount of asset redeemed

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_loan(tx_id: int, loan_time: int, asset: str, principal: float, isolated_symbol: Optional[str] = None, auto_commit: bool = True)[source]

Add a loan to the database

Parameters
  • tx_id (int) – binance id for the transaction (uniqueness?)

  • loan_time (int) – millitstamp of the operation

  • asset (str) – asset that got loaned

  • principal (float) – amount of loaned asset

  • isolated_symbol (Optional[str]) – for isolated margin, provided the trading symbol otherwise it will be counted a cross margin data

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_margin_interest(interest_time: int, asset: str, interest: float, interest_type: str, isolated_symbol: Optional[str] = None, auto_commit: bool = True)[source]

Add a margin interest to the database

Parameters
  • interest_time (int) – millistamp of the operation

  • asset (str) – asset that got repaid

  • interest (float) – amount of interest accrued

  • interest_type (str) – one of (PERIODIC, ON_BORROW, PERIODIC_CONVERTED, ON_BORROW_CONVERTED)

  • isolated_symbol (Optional[str]) – for isolated margin, provided the trading symbol otherwise it will be counted a cross margin data

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_repay(tx_id: int, repay_time: int, asset: str, principal: float, interest: float, isolated_symbol: Optional[str] = None, auto_commit: bool = True)[source]

Add a repay to the database

Parameters
  • tx_id (int) – binance id for the transaction (uniqueness?)

  • repay_time (int) – millitstamp of the operation

  • asset (str) – asset that got repaid

  • principal (float) – principal amount repaid for the loan

  • interest (float) – amount of interest repaid for the loan

  • isolated_symbol (Optional[str]) – for isolated margin, provided the trading symbol otherwise it will be counted a cross margin data

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_spot_dust(tran_id: str, time: int, asset: str, asset_amount: float, bnb_amount: float, bnb_fee: float, auto_commit: bool = True)[source]

Add dust operation to the database

Parameters
  • tran_id (str) – id of the transaction (non unique)

  • time (int) – millitstamp of the operation

  • asset (str) – asset that got converted to BNB

  • asset_amount (float) – amount of asset that got converted

  • bnb_amount (float) – amount received from the conversion

  • bnb_fee (float) – fee amount in BNB

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_trade(trade_type: str, trade_id: int, trade_time: int, asset: str, ref_asset: str, qty: float, price: float, fee: float, fee_asset: str, is_buyer: bool, symbol: Optional[str] = None, auto_commit: bool = True)[source]

Add a trade to the database

Parameters
  • trade_type (string, must be one of {'spot', 'cross_margin', 'isolated_margin'}) – type trade executed

  • trade_id (int) – id of the trade (binance id, unique per trading pair)

  • trade_time (int) – millistamp of the trade

  • asset (string) – name of the asset in the trading pair (ex ‘BTC’ for ‘BTCUSDT’)

  • ref_asset (string) – name of the reference asset in the trading pair (ex ‘USDT’ for ‘BTCUSDT’)

  • qty (float) – quantity of asset exchanged

  • price (float) – price of the asset regarding the ref_asset

  • fee (float) – amount kept by the exchange

  • fee_asset (str) – token unit for the fee

  • is_buyer (bool) – if the trade is a buy or a sell

  • symbol (Optional[str]) – trading symbol, mandatory if thr trade_type is isolated margin

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_universal_transfer(transfer_id: int, transfer_type: str, transfer_time: int, asset: str, amount: float, auto_commit: bool = True)[source]

Add a universal transfer to the database

Parameters
  • transfer_id (int) – id of the transfer

  • transfer_type (str) – enum of the transfer type (ex: ‘MAIN_MARGIN’)

  • transfer_time (int) – millistamp of the operation

  • asset (str) – asset that got transferred

  • amount (float) – amount transferred

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

add_withdraw(withdraw_id: str, tx_id: str, apply_time: int, asset: str, amount: float, fee: float, auto_commit: bool = True)[source]

Add a withdraw to the database

Parameters
  • withdraw_id (str) – binance if of the withdraw

  • tx_id (str) – transaction id

  • apply_time (int) – millistamp when the withdraw was requested

  • asset (str) – name of the token

  • amount (float) – amount of token withdrawn

  • fee (float) – amount of the asset paid for the withdraw

  • auto_commit (bool) – if the database should commit the change made, default True

Returns

None

Return type

None

get_isolated_transfers(isolated_symbol: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return isolated transfers stored in the database. isolated_symbol and time filters can be used

Parameters
  • isolated_symbol (Optional[str]) – for isolated margin, provided the trading symbol otherwise it will be counted a cross margin data

  • start_time (Optional[int]) – fetch only transfers after this millistamp

  • end_time (Optional[int]) – fetch only transfers before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (1206491332,        # transfer id
    'IN',               # transfer type (IN or OUT)
    1589121841000,      # time
    'BTCBUSD',          # isolated symbol
    'BTC',              # asset
    10.594112),         # amount
]
get_last_isolated_transfer_time(isolated_symbol: str)int[source]

Return the latest time when a isolated margin transfer was made If None, return the millistamp corresponding to 2017/01/01

Parameters

isolated_symbol (str) – isolated symbol that received or sent the transfers

Returns

millistamp

Return type

int

get_last_lending_interest_time(lending_type: Optional[str] = None)int[source]

Return the latest time when an interest was received. If None, return the millistamp corresponding to 2017/01/01

Parameters

lending_type (str) – type of lending

Returns

millistamp

Return type

int

get_last_lending_purchase_time(lending_type: Optional[str] = None)int[source]

Return the latest time when an lending purchase was made. If None, return the millistamp corresponding to 2017/01/01

Parameters

lending_type (str) – type of lending

Returns

millistamp

Return type

int

get_last_lending_redemption_time(lending_type: Optional[str] = None)int[source]

Return the latest time when an lending redemption was made. If None, return the millistamp corresponding to 2017/01/01

Parameters

lending_type (str) – type of lending

Returns

millistamp

Return type

int

get_last_loan_time(asset: str, isolated_symbol: Optional[str] = None)int[source]

Return the latest time when an loan was made on a defined asset If None, return the millistamp corresponding to 2017/01/01

Parameters
  • asset (str) – name of the asset loaned

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol (otherwise cross data are returned)

Returns

millistamp

Return type

int

get_last_margin_interest_time(asset: Optional[str] = None, isolated_symbol: Optional[str] = None)int[source]

Return the latest time when a margin interest was accured on a defined asset or on all assets If None, return the millistamp corresponding to 2017/01/01

Parameters
  • asset (Optional[str]) – name of the asset charged as interest

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol (otherwise cross data are returned)

Returns

millistamp

Return type

int

get_last_repay_time(asset: str, isolated_symbol: Optional[str] = None)int[source]

Return the latest time when a repay was made on a defined asset If None, return the millistamp corresponding to 2017/01/01

Parameters
  • asset (str) – name of the asset repaid

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol (otherwise cross data are returned)

Returns

millistamp

Return type

int

get_last_spot_deposit_time()int[source]

Fetch the latest time a deposit has been made on the spot account. If None is found, return the millistamp corresponding to 2017/1/1

Returns

last deposit millistamp

Return type

int

get_last_spot_dividend_time()int[source]

Fetch the latest time a dividend has been distributed on the spot account. If None is found, return the millistamp corresponding to 2017/1/1

Returns

get_last_spot_withdraw_time()int[source]

Fetch the latest time a withdraw has been made on the spot account. If None is found, return the millistamp corresponding to 2017/1/1

Returns

get_last_universal_transfer_time(transfer_type: str)int[source]

Return the latest time when a universal transfer was made If None, return the millistamp corresponding to 2017/01/01

Parameters

transfer_type (str) – enum of the transfer type (ex: ‘MAIN_MARGIN’)

Returns

millistamp

Return type

int

get_lending_interests(lending_type: Optional[str] = None, asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return lending interests stored in the database. Asset type and time filters can be used

Parameters
  • lending_type (Optional[str]) – fetch only interests from this lending type

  • asset (Optional[str]) – fetch only interests from this asset

  • start_time (Optional[int]) – fetch only interests after this millistamp

  • end_time (Optional[int]) – fetch only interests before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (1619846515000,     # time
    'DAILY',            # lending type
    'DOT',              # asset
    0.00490156)         # amount
]
get_lending_purchases(lending_type: Optional[str] = None, asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return lending purchases stored in the database. Asset type and time filters can be used

Parameters
  • lending_type (Optional[str]) – fetch only purchases from this lending type

  • asset (Optional[str]) – fetch only purchases from this asset

  • start_time (Optional[int]) – fetch only purchases after this millistamp

  • end_time (Optional[int]) – fetch only purchases before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (58516828,          # purchase id
    1612841562000,      # time
    'DAILY',            # lending type
    'LTC',              # asset
    1.89151684),        # amount
]
get_lending_redemptions(lending_type: Optional[str] = None, asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return lending redemptions stored in the database. Asset type and time filters can be used

Parameters
  • lending_type (Optional[str]) – fetch only redemptions from this lending type

  • asset (Optional[str]) – fetch only redemptions from this asset

  • start_time (Optional[int]) – fetch only redemptions after this millistamp

  • end_time (Optional[int]) – fetch only redemptions before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    1612841562000,      # time
    'DAILY',            # lending type
    'LTC',              # asset
    1.89151684),        # amount
]
get_loans(margin_type: str, asset: Optional[str] = None, isolated_symbol: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return loans stored in the database. Asset type and time filters can be used

Parameters
  • margin_type – either ‘cross’ or ‘isolated’

  • asset (Optional[str]) – fetch only loans of this asset

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol

  • start_time (Optional[int]) – fetch only loans after this millistamp

  • end_time (Optional[int]) – fetch only loans before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

# cross margin
[
    (8289451654,        # transaction id
    1559415215400,      # time
    'USDT',             # asset
    145.5491462),       # amount
]

# isolated margin
[
    (8289451654,        # transaction id
    1559415215400,      # time
    'BTCUSDT',          # symbol
    'USDT',             # asset
    145.5491462),       # amount
]
get_margin_interests(margin_type: str, asset: Optional[str] = None, isolated_symbol: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return margin interests stored in the database. Asset type and time filters can be used

Parameters
  • margin_type – either ‘cross’ or ‘isolated’

  • asset (Optional[str]) – fetch only interests in this asset

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol (otherwise cross data are returned)

  • start_time (Optional[int]) – fetch only interests after this millistamp

  • end_time (Optional[int]) – fetch only interests before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

# cross margin
[
    1559415215400,             # time
    'BNB',                     # asset
    0.51561,                   # interest
    'PERIODIC_CONVERTED'),     # interest type
]

# isolated margin
[
    1559415215400,             # time
    'BTCBUSD',                 # symbol
    'BUSD',                    # asset
    0.51561,                   # interest
    'PERIODIC'),               # interest type
]
get_max_trade_id(asset: str, ref_asset: str, trade_type: str)int[source]

Return the latest trade id for a trading pair. If none is found, return -1

Parameters
  • asset (string) – name of the asset in the trading pair (ex ‘BTC’ for ‘BTCUSDT’)

  • ref_asset (string) – name of the reference asset in the trading pair (ex ‘USDT’ for ‘BTCUSDT’)

  • trade_type (string, must be one of {'spot', 'cross_margin'}) – type trade executed

Returns

latest trade id

Return type

int

get_repays(margin_type: str, asset: Optional[str] = None, isolated_symbol: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return repays stored in the database. Asset type and time filters can be used

Parameters
  • margin_type (str) – either ‘cross’ or ‘isolated’

  • asset (Optional[str]) – fetch only repays of this asset

  • isolated_symbol (Optional[str]) – only for isolated margin, provide the trading symbol

  • start_time (Optional[int]) – fetch only repays after this millistamp

  • end_time (Optional[int]) – fetch only repays before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

# cross margin
[
    (8289451654,        # transaction id
    1559415215400,      # time
    'USDT',             # asset
    145.5491462,        # principal
    0.51561),           # interest
]

# isolated margin
[
    (8289451654,        # transaction id
    1559415215400,      # time
    'BTCUSDT',          # isolated symbol
    'USDT',             # asset
    145.5491462,        # principal
    0.51561),           # interest
]
get_spot_deposits(asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return deposits stored in the database. Asset type and time filters can be used

Parameters
  • asset (Optional[str]) – fetch only deposits of this asset

  • start_time (Optional[int]) – fetch only deposits after this millistamp

  • end_time (Optional[int]) – fetch only deposits before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    ('azdf5e6a1d5z',    # transaction id
    1589479004000,      # deposit time
    'LTC',              # asset
    14.25),             # amount
]
get_spot_dividends(asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return dividends stored in the database. Asset type and time filters can be used

Parameters
  • asset (Optional[str]) – fetch only dividends of this asset

  • start_time (Optional[int]) – fetch only dividends after this millistamp

  • end_time (Optional[int]) – fetch only dividends before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (8945138941,         # dividend id
    1594513589000,       # time
    'TRX',               # asset
    0.18745654),         # amount
]
get_spot_dusts(asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return dusts stored in the database. Asset type and time filters can be used

Parameters
  • asset (Optional[str]) – fetch only dusts from this asset

  • start_time (Optional[int]) – fetch only dusts after this millistamp

  • end_time (Optional[int]) – fetch only dusts before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (82156485284,       # transaction id
    1605489113400,      # time
    'TRX',              # asset
    102.78415879,       # asset amount
    0.09084498,         # bnb amount
    0.00171514),        # bnb fee
]
get_spot_withdraws(asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return withdraws stored in the database. Asset type and time filters can be used

Parameters
  • asset (Optional[str]) – fetch only withdraws of this asset

  • start_time (Optional[int]) – fetch only withdraws after this millistamp

  • end_time (Optional[int]) – fetch only withdraws before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    ('84984dcqq5z11gyjfa',  # withdraw id
    'aazd8949vredqs56dz',   # transaction id
    1599138389000,          # withdraw time
    'XTZ',                  # asset
    57.0194,                # amount
    0.5),                   # fee
]
get_trades(trade_type: str, start_time: Optional[int] = None, end_time: Optional[int] = None, asset: Optional[str] = None, ref_asset: Optional[str] = None)[source]

Return trades stored in the database. asset type, ref_asset type and time filters can be used

Parameters
  • trade_type (string, must be one of ('spot', 'cross_margin', 'isolated_margin')) – type trade executed

  • start_time (Optional[int]) – fetch only trades after this millistamp

  • end_time (Optional[int]) – fetch only trades before this millistamp

  • asset (Optional[str]) – fetch only trades with this asset

  • ref_asset (Optional[str]) – fetch only trades with this ref_asset

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

Return for spot and cross margin:

[
    (384518832,         # trade_id
    1582892988052,      # trade time
    'BTC',              # asset
    'USDT',             # ref asset
    0.0015,             # asset quantity
    9011.2,             # asset price to ref asset
    0.01425,            # fee
    'USDT',             # fee asset
    0),                 # is_buyer
]

Return for isolated margin:

[
    (384518832,         # trade_id
    1582892988052,      # trade time
    'BTCUSDT',          # symbol
    'BTC',              # asset
    'USDT',             # ref asset
    0.0015,             # asset quantity
    9011.2,             # asset price to ref asset
    0.01425,            # fee
    'USDT',             # fee asset
    0),                 # is_buyer
]
get_universal_transfers(transfer_type: Optional[str] = None, asset: Optional[str] = None, start_time: Optional[int] = None, end_time: Optional[int] = None)[source]

Return universal transfers stored in the database. Transfer type, Asset type and time filters can be used

Parameters
  • transfer_type (Optional[str]) – enum of the transfer type (ex: ‘MAIN_MARGIN’)

  • asset (Optional[str]) – fetch only interests in this asset

  • start_time (Optional[int]) – fetch only interests after this millistamp

  • end_time (Optional[int]) – fetch only interests before this millistamp

Returns

The raw rows selected as saved in the database

Return type

List[Tuple]

[
    (1206491332,        # transfer id
    'MAIN_MARGIN',      # transfer type
    1589121841000,      # time
    'BNB',              # asset
    10.594112),         # amount
]