Forked from sammchardy/convert-binance-interval-to-milliseconds.py
Created
April 29, 2018 01:24
-
-
Save liquidgenius/9b1e9d79ff6c2f2bfeb3480d634b4541 to your computer and use it in GitHub Desktop.
Convert Binance interval to milliseconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def interval_to_milliseconds(interval): | |
"""Convert a Binance interval string to milliseconds | |
:param interval: Binance interval string 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w | |
:type interval: str | |
:return: | |
None if unit not one of m, h, d or w | |
None if string not in correct format | |
int value of interval in milliseconds | |
""" | |
ms = None | |
seconds_per_unit = { | |
"m": 60, | |
"h": 60 * 60, | |
"d": 24 * 60 * 60, | |
"w": 7 * 24 * 60 * 60 | |
} | |
unit = interval[-1] | |
if unit in seconds_per_unit: | |
try: | |
ms = int(interval[:-1]) * seconds_per_unit[unit] * 1000 | |
except ValueError: | |
pass | |
return ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment