Path: blob/master/ invest-robot-contest_TradingCompetition2022-main/report/Report.py
5931 views
from fpdf import FPDF1from abc import ABC, abstractmethod2from account.Account import Account345class ReportBuilder:67@classmethod8def buildSessionResultReport(cls, report_name, report_source):9return SessionResultReport(report_name=report_name, report_source=report_source)101112class Report(ABC):13def __init__(self, report_name, report_source):14self._report_source = report_source15self._report_name = report_name16pass1718@abstractmethod19def pdf_generator(self):20pass2122def save_to_pdf(self, file_name) -> object:23"""24Save pdf25:param file_name:26:return:27"""28pdf = self.pdf_generator()29# save the pdf with name .pdf30out_file = "report/result/" + file_name + ".pdf"31pdf.output(out_file)32return out_file333435class SessionResultReport(Report):3637def pdf_generator(self) -> object:38"""39Generate pdf40:return:41"""42pdf = FPDF()43pdf.add_page()44family = "Sans"45family_name = "report/Open_Sans/static/OpenSans/OpenSans-Regular.ttf"46pdf.add_font(family, style="", fname=family_name, uni=True)47pdf.set_font(family, size=15)48w, h = 200, 1049pdf.cell(w, h, txt="RESULT OF TRADING SESSION", ln=1, align='C')50pdf.cell(w, h, txt="SESSION ID" + self._report_name, ln=1, align='C')51pdf.cell(200, 10, txt="", ln=2, align='L')5253pdf.set_font(family, size=9)5455pdf.cell(w, h,56txt="Всего выполнено ордеров алгоритмом: " + str(self._report_source['count_orders']),57ln=1, align='L')58pdf.cell(w, h,59txt="Всего выполнено ордеров на открытие лонг позиций: " +60str(self._report_source['count_long_opened']),61ln=1, align='L')62pdf.cell(w, h,63txt="Всего выполнено ордеров на закрытие лонг позиций: " +64str(self._report_source['count_long_closed']),65ln=1, align='L')66pdf.cell(200, 10, txt="", ln=2, align='L')6768def __write_operation(operation_type, pdf_link, source):69idx = 070for i in source[operation_type]:71idx += 172payment = "(Сумма = " + str(Account.convert_out(i['payment']))73quantity = " количество = " + str(i['quantity'])74txt = str(idx) + ") " + str(i['name']) + payment + " " + i['currency'] + quantity + " )"75pdf_link.cell(w, h, txt, ln=1, align='L')76return pdf_link7778pdf.cell(w, h, txt="Операции, зафиксированные брокером", ln=1, align='C')7980pdf.cell(w, h, txt="Покупка", ln=1, align='C')81pdf = __write_operation(operation_type='buy', pdf_link=pdf, source=self._report_source)82pdf.cell(200, 10, txt="", ln=2, align='L')8384pdf.cell(w, h, txt="Продажа", ln=1, align='C')85pdf = __write_operation(operation_type='sell', pdf_link=pdf, source=self._report_source)86pdf.cell(200, 10, txt="", ln=2, align='L')8788pdf.cell(200, 10, txt="Комиссия брокера " + str(Account.convert_out(self._report_source['sum_fee'])),89ln=2, align='L')9091return pdf929394