Skip to content

Reporting CSV

ReportingCSV class

__init__(self, target_folder='reports') special

Create a reporting instance.

Parameters:

Name Type Description Default
target_folder str

relative target folder.

'reports'
Source code in estrade/reporting/csv.py
def __init__(self, target_folder: str = "reports") -> None:
    """
    Create a reporting instance.

    Arguments:
        target_folder: relative target folder.

    """
    self.target_folder = target_folder

report(self, strategies, trade_details=True)

Create a CSV reporting all trades of an Epic instance.

This method creates:

- A CSV file with input strategy result
- (Optionally) A CSV file per strategy with the detailed list of trades.

Parameters:

Name Type Description Default
strategies List[BaseStrategy]

List of strategies to report.

required
trade_details bool

create files with the detailed list of trades of each strategy.

True
Source code in estrade/reporting/csv.py
def report(
    self, strategies: List["BaseStrategy"], trade_details: bool = True
) -> None:
    """
    Create a CSV reporting all trades of an Epic instance.

    This method creates:

        - A CSV file with input strategy result
        - (Optionally) A CSV file per strategy with the detailed list of trades.

    Arguments:
        strategies: List of strategies to report.
        trade_details: create files with the detailed list of trades of each
            strategy.

    """
    logger.info("Report as CSV")
    dt = arrow.utcnow().format("YYYY-MM-DD_HH:mm:ss")

    strategies_report = []
    for strategy in strategies:
        strategy_trades = list(strategy.get_trades())
        strategies_report.append(
            {
                "ref": strategy.ref,
                "nb_trades": len(strategy_trades),
                "result": strategy.result(),
                "profit_factor": strategy.profit_factor(),
            }
        )
        if trade_details:
            target_filename = f"{dt}_{strategy.ref}_trades.csv"
            trades = []
            headers: List[str] = []
            for trade in strategy_trades:
                trade_dict = trade.asdict()
                if not headers:
                    headers = list(trade_dict.keys())
                trades.append(trade_dict)

            CSVWriter.dict_to_csv(
                path=self.target_folder,
                filename=target_filename,
                dict_list=trades,
                headers=headers,
            )

    CSVWriter.dict_to_csv(
        path=self.target_folder,
        filename=f"{dt}_strategies.csv",
        dict_list=strategies_report,
        headers=["ref", "nb_trades", "result", "profit_factor"],
    )