-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolss.py
More file actions
89 lines (77 loc) · 2.32 KB
/
toolss.py
File metadata and controls
89 lines (77 loc) · 2.32 KB
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
import mysql.connector as db
import json
import os
import os.path
import pandas as pd
import pwds
MySQL_HOST = pwds.MySQL_HOST
MySQL_USER = pwds.MySQL_USER
MySQL_PASSWORD = pwds.MySQL_PASSWORD
MySQL_DBNAME = pwds.MySQL_DBNAME
MySQL_PORT = pwds.MySQL_PORT
mydb = db.connect(
host=MySQL_HOST,
user=MySQL_USER,
password=MySQL_PASSWORD,
db=MySQL_DBNAME,
port=MySQL_PORT,
auth_plugin='mysql_native_password'
)
mycursor = mydb.cursor()
def download_size_information():
"""
download_size_information downloads the price tick and volume multiple from server if not downloaded before
Returns
-------
A tuple of dictionaries
first being dictionary of vol multiple, second is dict of tick size
"""
if not os.path.isfile('./tick_multiplier.json'):
vol = {}
tick = {}
sql = 'SELECT name, price_tick, volume_multiple from future_data.future_info'
mycursor.execute(sql)
data = mycursor.fetchall()
for element in data:
contract = element[0]
price_tick = element[1]
volume = element[2]
vol[contract] = volume
tick[contract] = price_tick
with open('volume.json', 'w+') as fp:
json.dump(vol,fp)
with open('tick.json', 'w+') as fp:
json.dump(tick,fp)
return vol, tick
else:
return load_size_information()
def load_size_information():
"""load_size_information load volume and tick data from local file
Returns
-------
A tuple of dictionaries
first being dictionary of vol multiple, second is dict of tick size
"""
vol = {}
tick = {}
with open('volume.json', 'r') as fp:
vol = json.load(fp)
with open('tick.json', 'r') as fp:
tick = json.load(fp)
return vol, tick
def data_export_csv(data: dict):
"""data_export_csv export a dict-like dataframe to csv in the same folder
Parameters
----------
data : dict
Returns
-------
A dataframe that has been written to csv
"""
result = pd.DataFrame.from_dict(data, orient = 'index')
result.index = pd.to_datetime(result.index, format = '%Y-%m-%d-%H', errors='ignore')
result.to_csv('export.csv')
return result
if __name__ == "__main__":
a = download_size_information()
b = download_size_information()