Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
from binance.client import Client
import glob
import pandas as pd
import pytz
import datetime
import urllib3
def convert_datetime_timezone(dt, tz1, tz2):
tz1 = pytz.timezone(tz1)
tz2 = pytz.timezone(tz2)
dt = datetime.datetime.strptime(dt,'%Y-%m-%d %H:%M:%S')
dt = tz1.localize(dt)
dt = dt.astimezone(tz2)
dt = dt.strftime('%Y-%m-%d %H:%M:%S')
return dt
def scrap_binance_price(sym, interval, start_date, OUTPUT_PATH):
"""
sym: Ticker symbol of the asset - e.g: BTCUSDT
interval: the interval range for candles - e.g. 1m 10m 4h
unit: start date
"""
start_date += " ACDT"
api_key = 'LKa422RMjzx2tsXESIoSQ5qJzqi7LaC7iDFf9qdSrYjhJrZ6a8CcvOdniJWtfX13'
api_secret = 'EvomPthpqwgTjeuqYFWBQM9HtE52Zeq6SuKrZSROqhd8g2tvbz086zVZqbbJGxsZ'
client = Client(api_key, api_secret, {"verify": False, "timeout": 20})
klines = client.get_historical_klines(sym, interval, start_date)[:-1]
bnb_klines = client.get_historical_klines("BNBUSDT", interval, start_date)[:-1]
data = pd.DataFrame()
tradingdate = []
openprice = []
highprice = []
lowprice = []
closeprice = []
bnbprice = []
for i in range(len(klines)):
time = datetime.datetime.utcfromtimestamp(klines[i][0] / 1000).strftime('%Y-%m-%d %H:%M:%S')
local_time = convert_datetime_timezone(time, 'UTC', 'Australia/Adelaide')
tradingdate.append(local_time)
openprice.append(float(klines[i][1]))
highprice.append(float(klines[i][2]))
lowprice.append(float(klines[i][3]))
closeprice.append(float(klines[i][4]))
bnbprice.append(float(bnb_klines[i][4]))
data['datetime'] = tradingdate
data['low'] = lowprice
data['high'] = highprice
data['open'] = openprice
data['close'] = closeprice
data['bnb_price'] = bnbprice
data.reset_index(drop=True, inplace=True)
data.to_csv(OUTPUT_PATH)
return data
# a = scrap_binance_price("BTCUSDT", "4h", "1 August, 2020", "a.csv")
import jovian
jovian.commit(project="bitbotboom-scrape-price-data", environment=None, filename="/home/ec2-user/SageMaker/Renko Simulation/scrape_price_data.ipynb", message="Added Adelaide Timezone support")
[jovian] Attempting to save notebook..