-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetPriceData.py
122 lines (109 loc) · 4.92 KB
/
getPriceData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
#IMPORTS
import pandas as pd
from bitmex import bitmex
import requests, json
import pytz # $ pip install pytz
from dotenv import load_dotenv
import math
import os.path
import time
from datetime import timedelta, datetime
from dateutil import parser
from tqdm import tqdm_notebook #(Optional, used for progress-bars)
#load constants
load_dotenv()
#create the bitmex client object
bitmex_client = None
########### Methods worthy of noting ###########
##CONVERTING TIME TO TZ AWARE OBJECT
#convert the time string to a datetime object
#dt_str = "5/30/2020 4:05:03:10:10"
#unaware_est = datetime.strptime(dt_str,"%m/%d/%Y %H:%M:%S+00:00")
# make it a timezone-aware datetime object
#est_time = pytz.timezone('US/Eastern').localize(unaware_est, is_dst=None)
##Public method to fetch price data
#ether = requests.get("https://testnet.bitmex.com/api/v1/orderBook/L2?symbol=ETHUSD&depth=1").json()
#xbt = requests.get("https://testnet.bitmex.com/api/v1/orderBook/L2?symbol=xbt&depth=1").json()
#ether_ask_price = ether[0]['price']
#ether_bid_price = ether[1]['price']
#print(ether_ask_price)
#print(ether_bid_price)
################################################
######### Example of creating an order #########
#symbol = 'XBTUSD'
#qty = -1
#price = ether[1]['price']
#order_result = bitmex_client.Order.Order_new(symbol=symbol, orderQty=qty, price=price).result()
#print(order_result)
##Storing existing orders
#orders = bitmex_client.Order.Order_getOrders().result()[0]
#for order in orders:
#print(order)
#processed_order = {}
#processed_order["symbol"] = order["symbol"]
#processed_order["amount"] = str(order["orderQty"]).split("L")[0]
#processed_order["price"] = order["price"]
#processed_order["side"] = order["side"]
#processed_order["status"] = order["ordStatus"]
#print(processed_order)
################################################
#converts start_time and end_time to datetime objects
def get_datetimes(symbol, kline_size, start_time, end_time, data, source):
#if len(data) > 0: old = parser.parse(data["timestamp"].iloc[-1]).replace(tzinfo=None)
if source == "binance":
if start_time == None:
old = datetime.strptime('1 Jan 2017', '%d %b %Y')
else:
old = datetime.strptime(start_time, '%d %b %Y')
elif source == "bitmex":
if start_time == None:
old = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=1, reverse=False).result()[0][0]['timestamp']
old = old.replace(tzinfo=None)
else:
old = datetime.strptime(start_time, '%d %b %Y')
print("GOT HEERERE")
print("old time", old)
if source == "binance":
if end_time == 'PRESENT':
new = pd.to_datetime(binance_client.get_klines(symbol=symbol, interval=kline_size)[-1][0], unit='ms')
else:
new = datetime.strptime(end_time, '%d %b %Y')
elif source == "bitmex":
if end_time == 'PRESENT':
new = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=1, reverse=True).result()[0][0]['timestamp']
new = new.replace(tzinfo=None)
else:
new = datetime.strptime(end_time, '%d %b %Y')
return old, new
def get_all_bitmex(symbol, kline_size, start_time, end_time, save = False):
filename = '%s-%s-data.csv' % (symbol, kline_size)
print(filename)
if os.path.isfile(filename): data_df = pd.read_csv(filename)
else: data_df = pd.DataFrame()
oldest_point, newest_point = get_datetimes(symbol,kline_size, start_time, end_time, data_df, source = "bitmex")
print("getting data from", oldest_point, " to ", newest_point)
count_minutes = int(divmod((newest_point - oldest_point).total_seconds(),60)[0])
ranges = []
for i in range(0,count_minutes,1000):
if(i+1000 > count_minutes):
ranges.append([i,count_minutes])
else: ranges.append([i, i+999])
#MAXIMUM count is 1000 so we need to loop until we hit the value, adding 1000
# We can only get 1000 values at a time, so we go from newest to oldest point by intervals of 1000 or less
for r in ranges:
start = oldest_point + timedelta(minutes=r[0])
end = oldest_point + timedelta(minutes=r[1])
data = bitmex_client.Trade.Trade_getBucketed(symbol=symbol, binSize=kline_size, count=r[1] - r[0], startTime = start, endTime = end).result()[0]
temp_df = pd.DataFrame(data)
data_df = data_df.append(temp_df)
data_df.set_index('timestamp', inplace=True)
if save : data_df.to_csv(filename)
print('All caught up..!')
return data_df
def getData():
global bitmex_client
bitmex_client = bitmex(test=os.getenv("TESTNET"), api_key=os.getenv("bitmex_api_key"), api_secret=os.getenv("bitmex_api_secret"))
#Get timeseries data
data = get_all_bitmex(symbol="XBTUSD", kline_size="1m", start_time='1 Jan 2019', end_time='PRESENT', save=False)
return data