Skip to content

Instantly share code, notes, and snippets.

@normanlmfung
Last active January 12, 2022 05:54
Show Gist options
  • Save normanlmfung/62c975167f8263a0f9d7b7fa3a777c44 to your computer and use it in GitHub Desktop.
Save normanlmfung/62c975167f8263a0f9d7b7fa3a777c44 to your computer and use it in GitHub Desktop.
test_binance_derivatives_rounding
from decimal import Decimal
from typing import Dict
from dotmap import DotMap
import ccxt
class binanceperps(ccxt.binance):
def describe(self) -> Dict:
description = super().describe()
description["options"]["defaultType"] = "future"
return description
class binancedelivery(ccxt.binance):
def describe(self) -> Dict:
description = super().describe()
description["options"]["defaultType"] = "delivery"
return description
class huobiprolinearswap(ccxt.huobipro):
def describe(self):
rv = self.deep_extend(super().describe(), {
'id': 'huobilinearswap',
'name': 'Huobilenearswap',
'market_type': 'linearswap'
})
return rv
if __name__ == '__main__':
apiKey = ''
secret = ''
params = {
'apiKey' : apiKey,
'secret' : secret
}
exchange = binanceperps(params)
markets = exchange.load_markets()
target = [markets[market] for market in markets if market=='BTC/USDT'][0]
'''
target
[{'percentage': True, 'feeSide': 'get', 'tierBased': False, 'taker': 0.0004, 'maker': 0.0002, 'precision': {...}, 'limits': {...}, 'id': 'BTCUSDT', 'lowercaseId': 'btcusdt', ...}]
special variables:
function variables:
0: {'percentage': True, 'feeSide': 'get', 'tierBased': False, 'taker': 0.0004, 'maker': 0.0002, 'precision': {'base': 8, 'quote': 8, 'amount': 3, 'price': 2}, 'limits': {'amount': {...}, 'price': {...}, 'cost': {...}, 'market': {...}}, 'id': 'BTCUSDT', 'lowercaseId': 'btcusdt', 'symbol': 'BTC/USDT', 'base': 'BTC', 'quote': 'USDT', 'baseId': 'BTC', 'quoteId': 'USDT', ...}
special variables:
function variables:
'percentage': True
'feeSide': 'get'
'tierBased': False
'taker': 0.0004
'maker': 0.0002
'precision': {'base': 8, 'quote': 8, 'amount': 3, 'price': 2}
special variables:
function variables:
'base': 8
'quote': 8
'amount': 3 <--- number of decimal places
'price': 2 <---
len(): 4
'limits': {'amount': {'min': 0.001, 'max': 1000.0}, 'price': {'min': 556.72, 'max': 4529764.0}, 'cost': {'min': 5.0, 'max': None}, 'market': {'min': 0.001, 'max': 120.0}}
'id': 'BTCUSDT'
'lowercaseId': 'btcusdt'
'symbol': 'BTC/USDT'
'base': 'BTC'
'quote': 'USDT'
'baseId': 'BTC'
'quoteId': 'USDT'
'info': {
'symbol': 'BTCUSDT',
'pair': 'BTCUSDT',
'contractType': 'PERPETUAL',
'deliveryDate': '4133404800000',
'onboardDate': '1569398400000',
'status': 'TRADING',
'maintMarginPercent': '2.5000',
'requiredMarginPercent': '5.0000',
'baseAsset': 'BTC',
'quoteAsset': 'USDT',
'marginAsset': 'USDT',
'pricePrecision': '2', <---
'quantityPrecision': '3', <---
'baseAssetPrecision': '8', ...}
'spot': False
'type': 'future'
'margin': False
'future': True
'delivery': False
'linear': True
'inverse': False
'expiry': None
'expiryDatetime': None
'settleId': 'USDT'
'settle': 'USDT'
'active': True
'contractSize': '1'
len(): 28
len(): 1
'''
denormalized_symbol : str = target['id']
type : str = 'market'
side : str = 'buy'
amount : float = 1.001
d_amount = Decimal(str(amount))
quantity_precision = abs(d_amount.as_tuple().exponent)
price : float = 40000.01
d_price = Decimal(str(price))
price_precision = abs(d_price.as_tuple().exponent)
rounded_amount : float = exchange.amount_to_precision(symbol=denormalized_symbol, amount=amount)
rounded_price : float = exchange.price_to_precision(symbol=denormalized_symbol, price=price)
print(f"amount: {amount}, rounded_amount: {rounded_amount}, price: {price}, rounded_price: {rounded_price}")
# rv = exchange.create_order(symbol=denormalized_symbol, type=type, side=side, amount=amount)
amount : float = 1.0001
d_amount = Decimal(str(amount))
quantity_precision = abs(d_amount.as_tuple().exponent)
price : float = 40000.001
d_price = Decimal(str(price))
price_precision = abs(d_price.as_tuple().exponent)
rounded_amount : float = exchange.amount_to_precision(symbol=denormalized_symbol, amount=amount)
rounded_price : float = exchange.price_to_precision(symbol=denormalized_symbol, price=price)
print(f"amount: {amount}, rounded_amount: {rounded_amount}, price: {price}, rounded_price: {rounded_price}")
exchange = binancedelivery(params)
markets = exchange.load_markets()
target = [markets[market] for market in markets if market=='BTC/USD'][0]
'''
target
{'percentage': True, 'feeSide': 'get', 'tierBased': False, 'taker': 0.0005, 'maker': 0.0001, 'precision': {'base': 8, 'quote': 8, 'amount': 0, 'price': 1}, 'limits': {'amount': {...}, 'price': {...}, 'cost': {...}, 'market': {...}}, 'id': 'BTCUSD_PERP', 'lowercaseId': 'btcusd_perp', 'symbol': 'BTC/USD', 'base': 'BTC', 'quote': 'USD', 'baseId': 'BTC', 'quoteId': 'USD', ...}
special variables:
function variables:
'percentage': True
'feeSide': 'get'
'tierBased': False
'taker': 0.0005
'maker': 0.0001
'precision': {'base': 8, 'quote': 8, 'amount': 0, 'price': 1}
'limits': {'amount': {'min': 1.0, 'max': 1000000.0}, 'price': {'min': 1000.0, 'max': 4520958.0}, 'cost': {'min': None, 'max': None}, 'market': {'min': 1.0, 'max': 60000.0}}
'id': 'BTCUSD_PERP'
'lowercaseId': 'btcusd_perp'
'symbol': 'BTC/USD'
'base': 'BTC'
'quote': 'USD'
'baseId': 'BTC'
'quoteId': 'USD'
'info': {
'symbol': 'BTCUSD_PERP',
'pair': 'BTCUSD',
'contractType': 'PERPETUAL',
'deliveryDate': '4133404800000',
'onboardDate': '1597042800000',
'contractStatus': 'TRADING',
'contractSize': '100',
'marginAsset': 'BTC',
'maintMarginPercent': '2.5000',
'requiredMarginPercent': '5.0000',
'baseAsset': 'BTC',
'quoteAsset': 'USD',
'pricePrecision': '1', <---
'quantityPrecision': '0', <--- For inverse, it's rounded to Number of contracts.
...}
'spot': False
'type': 'delivery'
'margin': False
'future': False
'delivery': True
'linear': False
'inverse': True
'expiry': None
'expiryDatetime': None
'settleId': 'BTC'
'settle': 'BTC'
'active': True
'contractSize': '100'
len(): 28
'''
denormalized_symbol : str = target['id']
type : str = 'market'
side : str = 'buy'
amount : float = 3.1
price : float = 40000.1
rounded_amount : float = exchange.amount_to_precision(symbol=denormalized_symbol, amount=amount)
rounded_price : float = exchange.price_to_precision(symbol=denormalized_symbol, price=price)
print(f"amount: {amount}, rounded_amount: {rounded_amount}, price: {price}, rounded_price: {rounded_price}")
# rv = exchange.create_order(symbol=denormalized_symbol, type=type, side=side, amount=amount)
amount : float = 3.1
price : float = 40000.01
rounded_amount : float = exchange.amount_to_precision(symbol=denormalized_symbol, amount=amount)
rounded_price : float = exchange.price_to_precision(symbol=denormalized_symbol, price=price)
print(f"amount: {amount}, rounded_amount: {rounded_amount}, price: {price}, rounded_price: {rounded_price}")
exchange = huobiprolinearswap(params)
markets = exchange.load_markets()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment