-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_public.py
112 lines (86 loc) · 2.98 KB
/
func_public.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
from constants import RESOLUTION
from func_utils import get_ISO_times
import pandas as pd
import numpy as np
import time
from pprint import pprint
# Get relevant time periods from ISO from and to
ISO_TIMES = get_ISO_times()
#pprint(ISO_TIMES)
# Get Candles recent
def get_candles_recent(client, market):
# Define output
close_prices = []
# Protect API
time.sleep(0.2)
# Get data
candles = client.public.get_candles(
market=market,
resolution=RESOLUTION,
limit=100
)
# Structure data
for candle in candles.data["candles"]:
close_prices.append(candle["close"])
# Construct and return close price series
close_prices.reverse()
prices_result = np.array(close_prices).astype(np.float)
return prices_result
# Get Candles Historical
def get_candles_histoical(client, market):
# Define output
close_prices = []
# Extract historical price data for each timeframe
for timeframe in ISO_TIMES.keys():
# Confirm times needed
tf_ojb = ISO_TIMES[timeframe]
from_iso = tf_ojb["from_iso"]
to_iso = tf_ojb["to_iso"]
# Protect rate limits
time.sleep(0.2)
# Get data
candles = client.public.get_candles(
market=market,
resolution=RESOLUTION,
from_iso=from_iso,
to_iso=to_iso,
limit=100
)
# Structure data
for candle in candles.data["candles"]:
close_prices.append({"datetime": candle["startedAt"], market: candle["close"]})
# Construct and return DataFrame
close_prices.reverse()
return close_prices
# Construct market prices
def construct_market_prices(client):
# Declare variables
tradeable_markets = []
markets = client.public.get_markets()
# Find tradeable pairs
for market in markets.data["markets"].keys():
market_info = markets.data["markets"][market]
if market_info["status"] == "ONLINE" and market_info["type"] == "PERPETUAL":
tradeable_markets.append(market)
# Send initial DataFrame
close_prices = get_candles_histoical(client, tradeable_markets[0])
#pprint(close_prices)
df = pd.DataFrame(close_prices)
df.set_index("datetime", inplace=True)
# Append other prices to DataFrame
# You an limit the amount to loop through here to save time in development
# Change second number after :. Leave blank to include everything
for market in tradeable_markets[1:]:
close_prices_add = get_candles_histoical(client, market)
df_add = pd.DataFrame(close_prices_add)
df_add.set_index("datetime", inplace=True)
df = pd.merge(df, df_add, how="outer", on="datetime", copy=False)
del df_add
# Check any columns with NaNs
nans = df.columns[df.isna().any()].tolist()
if len(nans) > 0:
print("Dropping columns: ")
print(nans)
df.drop(columns=nans, inplace=True)
# Return result
return df