Path: blob/master/ invest-robot-contest_tinkoff_trader-master/show_table.py
5918 views
import trader1import os23global g_trial45def get_table():6with open('trials.txt', 'r') as trials_file:7trials = [line.strip() for line in trials_file]8sold_pile = {}9days = []10res_table = {}11currencies = ['USD', 'RUB', 'EUR']12for trial in set(trials):13if not trial.rstrip(): #Skip empty rows14continue15trader.g_trial = trial16sold = trader.get_sold()17sold_pile[trial] = sold18for s in sold:19days.append(s['sell_time'].replace(hour=0, minute=0, second=0, microsecond=0))20days = set(days)2122# Init empty table23for d in days:24empty_record = {}25for trial in sold_pile.keys():26trial_empty_record = {}27for c in currencies:28trial_empty_record[c] = 029empty_record[trial] = trial_empty_record30res_table[d] = empty_record3132# Fill table33for trial in sold_pile.keys():34for sell in sold_pile[trial]:35res_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']36return res_table3738def out_put(p_str, p_target):39print(p_str)40if p_target != '':41with open(p_target, 'a') as f:42f.write(p_str)434445def show_table(p_currency, p_target):46column_width = 154748if p_target:49try:50os.remove(p_target)51except FileNotFoundError:52None5354t = get_table()55total = {}56for d in sorted(t.keys()):57trials = sorted(t[d].keys())58break59header = ''60for trial in trials:61header = header + trial.rjust(column_width, ' ') + ' '62total[trial] = 063out_put(' '+header+'\n', p_target)6465for d in sorted(t.keys()):66v_str = ''67for trial in trials:68v_str = v_str + str(round(t[d][trial][p_currency], 2)).rjust(column_width, ' ') + ' '69total[trial] = total[trial] + t[d][trial][p_currency]70out_put(d.strftime('%d.%m.%Y')+' '+v_str+'\n', p_target)7172footer = ''73for trial in trials:74footer = footer + str(round(total[trial], 2)).rjust(column_width, ' ') + ' '75out_put('Total: '+footer+'\n', p_target)767778show_table('USD', 'table_usd.txt')79show_table('RUB', 'table_rub.txt')80818283