//+------------------------------------------------------------------+
//|                                                  ChartTextEA.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <ChartText\CanvasBlock.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input int  InpEveryTicks = 50;     // refresh the status block every N ticks
input int  InpWidth      = 620;    // widest line of the block, pixels
input bool InpCanvas     = false;  // draw with CCanvas instead of labels
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
CLabelBlock  g_labels;           // the label version of the status block
CCanvasBlock g_canvas;           // the canvas version of the status block
long         g_ticks=0;          // ticks seen
long         g_updates=0;        // times the status block was written
long         g_verify_bad=0;     // problems that Verify() reported, see the class of the block
int          g_max_lines=0;      // most lines the block needed
int          g_max_units=0;      // longest status text, in UTF-16 code units
int          g_raw_stored=0;     // code units a plain label kept
long         g_set_failed=0;     // Set() calls that returned a negative number
long         g_overwide=0;       // lines wider than the block, see CTextWrap::Overwide()
//+------------------------------------------------------------------+
//| StatusText                                                       |
//| A status text of the kind an EA keeps on the chart. It is far    |
//| longer than 63 characters.                                       |
//+------------------------------------------------------------------+
string StatusText(void)
  {
   return(StringFormat("%s %s | server time %s | bid %s, ask %s, spread %d points | ticks seen %I64d | "
                       "equity %.2f, balance %.2f, margin level %.2f%% | open positions %d | "
                       "this panel has refreshed %I64d times since the start of the run.",
                       _Symbol,EnumToString(_Period),TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),
                       DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits),DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits),
                       (int)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD),g_ticks,AccountInfoDouble(ACCOUNT_EQUITY),AccountInfoDouble(ACCOUNT_BALANCE),
                       AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),PositionsTotal(),g_updates));
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   bool ok=InpCanvas ? g_canvas.Init("CTEA_block",10,30,InpWidth) : g_labels.Init("CTEA_block",10,30,InpWidth);
   if(!ok)
      return(INIT_FAILED);
   ResetLastError();
   bool made=ObjectCreate(0,"CTEA_raw",OBJ_LABEL,0,0,0);
   made=made && ObjectSetInteger(0,"CTEA_raw",OBJPROP_YDISTANCE,6);
   made=made && ObjectSetString(0,"CTEA_raw",OBJPROP_TEXT,StatusText());
   int err=GetLastError();
   g_raw_stored=StringLen(ObjectGetString(0,"CTEA_raw",OBJPROP_TEXT));
   PrintFormat("CTEA init build=%d tester=%s raw_label_set=%s err=%d raw_label_sent=%d raw_label_stored=%d",TerminalInfoInteger(TERMINAL_BUILD),
               MQLInfoInteger(MQL_TESTER) ? "yes" : "no",made ? "true" : "false",err,StringLen(StatusText()),g_raw_stored);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   PrintFormat("CTEA summary block=%s ticks=%I64d updates=%I64d %s=%I64d set_failed=%I64d overwide=%I64d max_lines=%d max_units=%d raw_label_stored=%d object_calls=%d",
               InpCanvas ? "canvas" : "labels",g_ticks,g_updates,InpCanvas ? "canvas_verify_bad" : "readback_mismatch",g_verify_bad,
               g_set_failed,g_overwide,g_max_lines,g_max_units,g_raw_stored,InpCanvas ? 0 : g_labels.ObjectCalls());
   g_labels.Clear();
   g_canvas.Clear();
   ObjectDelete(0,"CTEA_raw");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   g_ticks++;
   if(g_ticks%InpEveryTicks!=0)
      return;
   string text=StatusText();
   int lines=InpCanvas ? g_canvas.Set(text) : g_labels.Set(text);
   if(lines<0)
     {
      g_set_failed++;
      PrintFormat("CTEA Set() returned %d, last error %d",lines,InpCanvas ? g_canvas.LastError() : g_labels.LastError());
      return;
     }
   g_updates++;
   g_overwide+=CTextWrap::Overwide();
   g_max_lines=MathMax(g_max_lines,lines);
   g_max_units=MathMax(g_max_units,StringLen(text));
   g_verify_bad+=InpCanvas ? g_canvas.Verify() : g_labels.Verify();
  }
//+------------------------------------------------------------------+
