Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/report/Report.py
5931 views
1
from fpdf import FPDF
2
from abc import ABC, abstractmethod
3
from account.Account import Account
4
5
6
class ReportBuilder:
7
8
@classmethod
9
def buildSessionResultReport(cls, report_name, report_source):
10
return SessionResultReport(report_name=report_name, report_source=report_source)
11
12
13
class Report(ABC):
14
def __init__(self, report_name, report_source):
15
self._report_source = report_source
16
self._report_name = report_name
17
pass
18
19
@abstractmethod
20
def pdf_generator(self):
21
pass
22
23
def save_to_pdf(self, file_name) -> object:
24
"""
25
Save pdf
26
:param file_name:
27
:return:
28
"""
29
pdf = self.pdf_generator()
30
# save the pdf with name .pdf
31
out_file = "report/result/" + file_name + ".pdf"
32
pdf.output(out_file)
33
return out_file
34
35
36
class SessionResultReport(Report):
37
38
def pdf_generator(self) -> object:
39
"""
40
Generate pdf
41
:return:
42
"""
43
pdf = FPDF()
44
pdf.add_page()
45
family = "Sans"
46
family_name = "report/Open_Sans/static/OpenSans/OpenSans-Regular.ttf"
47
pdf.add_font(family, style="", fname=family_name, uni=True)
48
pdf.set_font(family, size=15)
49
w, h = 200, 10
50
pdf.cell(w, h, txt="RESULT OF TRADING SESSION", ln=1, align='C')
51
pdf.cell(w, h, txt="SESSION ID" + self._report_name, ln=1, align='C')
52
pdf.cell(200, 10, txt="", ln=2, align='L')
53
54
pdf.set_font(family, size=9)
55
56
pdf.cell(w, h,
57
txt="Всего выполнено ордеров алгоритмом: " + str(self._report_source['count_orders']),
58
ln=1, align='L')
59
pdf.cell(w, h,
60
txt="Всего выполнено ордеров на открытие лонг позиций: " +
61
str(self._report_source['count_long_opened']),
62
ln=1, align='L')
63
pdf.cell(w, h,
64
txt="Всего выполнено ордеров на закрытие лонг позиций: " +
65
str(self._report_source['count_long_closed']),
66
ln=1, align='L')
67
pdf.cell(200, 10, txt="", ln=2, align='L')
68
69
def __write_operation(operation_type, pdf_link, source):
70
idx = 0
71
for i in source[operation_type]:
72
idx += 1
73
payment = "(Сумма = " + str(Account.convert_out(i['payment']))
74
quantity = " количество = " + str(i['quantity'])
75
txt = str(idx) + ") " + str(i['name']) + payment + " " + i['currency'] + quantity + " )"
76
pdf_link.cell(w, h, txt, ln=1, align='L')
77
return pdf_link
78
79
pdf.cell(w, h, txt="Операции, зафиксированные брокером", ln=1, align='C')
80
81
pdf.cell(w, h, txt="Покупка", ln=1, align='C')
82
pdf = __write_operation(operation_type='buy', pdf_link=pdf, source=self._report_source)
83
pdf.cell(200, 10, txt="", ln=2, align='L')
84
85
pdf.cell(w, h, txt="Продажа", ln=1, align='C')
86
pdf = __write_operation(operation_type='sell', pdf_link=pdf, source=self._report_source)
87
pdf.cell(200, 10, txt="", ln=2, align='L')
88
89
pdf.cell(200, 10, txt="Комиссия брокера " + str(Account.convert_out(self._report_source['sum_fee'])),
90
ln=2, align='L')
91
92
return pdf
93
94