Skip to content

Instantly share code, notes, and snippets.

View sammchardy's full-sized avatar

Sam McHardy sammchardy

  • Geoscape
  • Australia
View GitHub Profile
@sammchardy
sammchardy / binance_get_historical_klines.py
Last active May 13, 2023 22:12
Get historical Klines from Binance
# uses the date_to_milliseconds and interval_to_milliseconds functions
# https://gist.github.com/sammchardy/3547cfab1faf78e385b3fcb83ad86395
# https://gist.github.com/sammchardy/fcbb2b836d1f694f39bddd569d1c16fe
from binance.client import Client
import time
def get_historical_klines(symbol, interval, start_str, end_str=None):
"""Get Historical Klines from Binance
@sammchardy
sammchardy / convert-binance-interval-to-milliseconds.py
Last active April 26, 2019 07:51
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
@sammchardy
sammchardy / convert-date-to-milliseconds.py
Last active August 15, 2018 12:46
Convert date to millisecond timestamp
# requires dateparser package
import dateparser
import pytz
from datetime import datetime
def date_to_milliseconds(date_str):
"""Convert UTC date to milliseconds
If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
@sammchardy
sammchardy / binance-depth-cache-notes.txt
Last active September 25, 2022 10:36
Binance Depth Cache Notes
Ninj0r admin, [Oct 20, 2017, 9:18:55 AM]:
It's a three step process:
1) Start listening to the stream and buffering the messages
2) Get a depth snapshot
3) replay the buffered messages and the live messges.
Depth updates have two variables, u and U
U is the initial updateId, and u is the final updateId. There can be multiple updates "compressed" into a single update that comes out via the web socket stream.