//+------------------------------------------------------------------+
//|                                           RiskReportPrinter.mqh  |
//+------------------------------------------------------------------+
#ifndef RISKREPORTPRINTER_MQH
#define RISKREPORTPRINTER_MQH

#include "RiskTypes.mqh"

//+------------------------------------------------------------------+
//| CRiskReportPrinter                                               |
//| Prints the streak probability table, the historical worst streak |
//| with its own probability, and the current risk-of-ruin figure    |
//| to the terminal log.                                             |
//+------------------------------------------------------------------+
class CRiskReportPrinter
  {
private:
   string                PadRight(string value, int width) const;

public:
                     CRiskReportPrinter(void);
                    ~CRiskReportPrinter(void);

   void                  Print(const CTradeStats &stats, const CStreakProbability &table[],
                               int table_count, double historical_streak_probability,
                               const CRiskOfRuinResult &current_ror) const;
  };

//+------------------------------------------------------------------+
//| Constructor: no member state to initialize.                      |
//+------------------------------------------------------------------+
CRiskReportPrinter::CRiskReportPrinter(void)
  {
  }

//+------------------------------------------------------------------+
//| Destructor: no dynamic resources to release.                     |
//+------------------------------------------------------------------+
CRiskReportPrinter::~CRiskReportPrinter(void)
  {
  }

//+------------------------------------------------------------------+
//| Print                                                            |
//| Prints trade statistics, the streak probability table, the       |
//| account's own historical worst streak and its probability, and   |
//| the current risk-of-ruin figure.                                 |
//+------------------------------------------------------------------+
void CRiskReportPrinter::Print(const CTradeStats &stats, const CStreakProbability &table[],
                               int table_count, double historical_streak_probability,
                               const CRiskOfRuinResult &current_ror) const
  {
   ::PrintFormat("RiskAnalyzer: %d trades, win rate %.1f%%, avg win %.2f, avg loss %.2f",
                 stats.trade_count, stats.win_rate * 100.0, stats.avg_win, stats.avg_loss);
//--- print the streak probability table
   string header = PadRight("Streak", 10) + PadRight("Probability", 14);
   ::Print(header);
   for(int i = 0; i < table_count; i++)
     {
      string row = PadRight(::IntegerToString(table[i].streak_length), 10) +
                   PadRight(::DoubleToString(table[i].probability * 100.0, 3) + "%", 14);
      ::Print(row);
     }
//--- put the account's own worst streak in the same probability context
   ::PrintFormat("RiskAnalyzer: historical worst streak was %d losses (P = %.3f%%)",
                 stats.max_loss_streak, historical_streak_probability * 100.0);
//--- print the current risk-of-ruin figure
   ::PrintFormat("RiskAnalyzer: at %.1f%% risk-per-trade, edge %.4f, risk of ruin %.4f%%",
                 current_ror.risk_percent, current_ror.edge, current_ror.ror * 100.0);
  }

//+------------------------------------------------------------------+
//| PadRight                                                         |
//| Pads a string with trailing spaces up to the given width, or     |
//| returns the original string unchanged if it already meets or     |
//| exceeds that width.                                              |
//+------------------------------------------------------------------+
string CRiskReportPrinter::PadRight(string value, int width) const
  {
   int need = width - ::StringLen(value);
   if(need <= 0)
      return(value);
   string result = value;
   for(int i = 0; i < need; i++)
      result += " ";
   return(result);
  }

#endif // RISKREPORTPRINTER_MQH
//+------------------------------------------------------------------+