-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfundingrates.py
90 lines (77 loc) · 2.94 KB
/
fundingrates.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
from datetime import datetime
import sys
import mariadb
import click
import funclib
from classlib import ConfigFile as cfg
from classlib import FundingRates as fundingRatesClass
@click.command()
@click.option('--exchange', default='all_exchange', help='A derivative exchange')
@click.option('--window', default='day', help='support day, hour, and min')
@click.option('--from_date', default='',
help='If window=day is used, it can also be formatted as YYYYMMDD (date)')
@click.option('--to_date', default='',
help='If window=day is used, it can also be formatted as YYYYMMDD (date)')
@click.option('--limit', default='1',
help='The maximum number of records to return before the latest data point (or before to if specified)')
@click.option('--return_format', default='json',
help='A format type about return message type. Supported formats are json, csv')
def main(exchange, window, from_date, to_date, limit, return_format):
print(f"exchange={exchange}")
print(f"window={window}")
print(f"from_date={from_date}")
print(f"to_date={to_date}")
print(f"limit={limit}")
print(f"return_format={return_format}")
config_file = 'config.ini'
# Read database section from config.ini
config = cfg()
config.filename = config_file
try:
config.section = "database"
dbConfig = config.readConfig()
except Exception as ex:
# print(f"Could not read config file")
funclib.log_traceback(ex)
sys.exit(1)
# Connect to MariaDB Platform
try:
dbConnection = mariadb.connect(
user=dbConfig["user"],
password=dbConfig["password"],
host=dbConfig["host"],
port=int(dbConfig["port"]),
database=dbConfig["database"],
autocommit=True
)
except mariadb.Error as ex:
# print(f"Error connecting to MariaDB Platform: {ex}")
funclib.log_traceback(ex)
sys.exit(1)
# Get Cursor
cursor = dbConnection.cursor()
# Read cryptoquant section from config.ini
try:
config.section = "cryptoquant"
quantConfig = config.readConfig()
except Exception as ex:
# print(f"Could not read config file")
funclib.log_traceback(ex)
sys.exit(1)
# print(f"{quantConfig}")
url = quantConfig["url_funding_rates"]
accessToken = quantConfig["access_token"]
funding = fundingRatesClass()
funding.cursor = cursor
records = funding.getData(url, accessToken, exchange, window, from_date, to_date, limit, return_format)
if (records is not None) and (len(records) > 0):
print(f"{records}")
print(f"{len(records)}")
for record in records:
dt = datetime.strptime(record["date"], "%Y-%m-%d").date()
funding.indexDate = dt
funding.fundingRates = record["funding_rates"]
funding.addData()
dbConnection.commit()
dbConnection.close()
main()