Skip to content

Instantly share code, notes, and snippets.

@uneasyguy
Last active January 7, 2022 20:24
Show Gist options
  • Save uneasyguy/bc60b18664ad601f1352874d33a845fa to your computer and use it in GitHub Desktop.
Save uneasyguy/bc60b18664ad601f1352874d33a845fa to your computer and use it in GitHub Desktop.
binance basic rounding example
from math import floor
def get_purchase_quantity( raw_quantity:float, step_size:str ) -> float:
if float( step_size ) % 1 == 0:
return round( raw_quantity / float( step_size ) ) * float( step_size )
return round( raw_quantity, step_size.index( '1' ) - 1 )
def get_sale_quantity( raw_quantity:float, step_size:str ) -> float:
if float( step_size ) % 1 == 0:
return floor( raw_quantity / float( step_size ) ) * float( step_size )
multiplier = 10 ** ( step_size.index( '1' ) - 1 )
return floor( raw_quantity * multiplier ) / multiplier
def main():
step_sizes = [
'1.00',
'1.00000000',
'0.10000000',
'0.01000000',
'0.00100000',
'0.00010000',
'0.00001000',
'0.00000100',
'10.00000000'
]
raw_quantity = 38.9365461
print( f'When raw quantity equals: { raw_quantity }' )
for step_size in step_sizes:
purchase_quantity = get_purchase_quantity( raw_quantity, step_size )
sale_quantity = get_sale_quantity( raw_quantity, step_size )
print( f'\n\tstep size:{step_size}, {purchase_quantity=},{sale_quantity=}\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment