Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinkoff_trader-master/show_table.py
5918 views
1
import trader
2
import os
3
4
global g_trial
5
6
def get_table():
7
with open('trials.txt', 'r') as trials_file:
8
trials = [line.strip() for line in trials_file]
9
sold_pile = {}
10
days = []
11
res_table = {}
12
currencies = ['USD', 'RUB', 'EUR']
13
for trial in set(trials):
14
if not trial.rstrip(): #Skip empty rows
15
continue
16
trader.g_trial = trial
17
sold = trader.get_sold()
18
sold_pile[trial] = sold
19
for s in sold:
20
days.append(s['sell_time'].replace(hour=0, minute=0, second=0, microsecond=0))
21
days = set(days)
22
23
# Init empty table
24
for d in days:
25
empty_record = {}
26
for trial in sold_pile.keys():
27
trial_empty_record = {}
28
for c in currencies:
29
trial_empty_record[c] = 0
30
empty_record[trial] = trial_empty_record
31
res_table[d] = empty_record
32
33
# Fill table
34
for trial in sold_pile.keys():
35
for sell in sold_pile[trial]:
36
res_table[sell['sell_time'].replace(hour=0, minute=0, second=0, microsecond=0)][trial][sell['currency']] = res_table[sell['sell_time'].replace(hour=0, minute=0, second=0, microsecond=0)][trial][sell['currency']] + sell['profit']
37
return res_table
38
39
def out_put(p_str, p_target):
40
print(p_str)
41
if p_target != '':
42
with open(p_target, 'a') as f:
43
f.write(p_str)
44
45
46
def show_table(p_currency, p_target):
47
column_width = 15
48
49
if p_target:
50
try:
51
os.remove(p_target)
52
except FileNotFoundError:
53
None
54
55
t = get_table()
56
total = {}
57
for d in sorted(t.keys()):
58
trials = sorted(t[d].keys())
59
break
60
header = ''
61
for trial in trials:
62
header = header + trial.rjust(column_width, ' ') + ' '
63
total[trial] = 0
64
out_put(' '+header+'\n', p_target)
65
66
for d in sorted(t.keys()):
67
v_str = ''
68
for trial in trials:
69
v_str = v_str + str(round(t[d][trial][p_currency], 2)).rjust(column_width, ' ') + ' '
70
total[trial] = total[trial] + t[d][trial][p_currency]
71
out_put(d.strftime('%d.%m.%Y')+' '+v_str+'\n', p_target)
72
73
footer = ''
74
for trial in trials:
75
footer = footer + str(round(total[trial], 2)).rjust(column_width, ' ') + ' '
76
out_put('Total: '+footer+'\n', p_target)
77
78
79
show_table('USD', 'table_usd.txt')
80
show_table('RUB', 'table_rub.txt')
81
82
83