Skip to content

Instantly share code, notes, and snippets.

@sammchardy
Last active April 26, 2019 07:51
Show Gist options
  • Save sammchardy/3547cfab1faf78e385b3fcb83ad86395 to your computer and use it in GitHub Desktop.
Save sammchardy/3547cfab1faf78e385b3fcb83ad86395 to your computer and use it in GitHub Desktop.
Convert Binance interval to milliseconds
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