//+------------------------------------------------------------------+
//| Comment代替：四角＋テキストで情報パネル                          |
//+------------------------------------------------------------------+
#property strict

// 好みで調整
input ENUM_BASE_CORNER  InpCorner   = CORNER_LEFT_UPPER; // 配置コーナー
input int               InpX        = 10;                // 余白X(px)
input int               InpY        = 20;                // 余白Y(px)
input int               InpWidth    = 320;               // パネル幅(px)
input int               InpHeight   = 120;               // パネル高(px)
input color             InpBG       = clrBlack;          // 背景色
input color             InpBorder   = clrDimGray;        // 枠線色
input color             InpTextCol  = clrWhite;          // 文字色
input string            InpFont     = "Segoe UI";        // フォント
input int               InpTitleSz  = 14;                // タイトル文字サイズ
input int               InpBodySz   = 11;                // 本文文字サイズ

#define PANEL_BG   "info_panel_bg"
#define PANEL_TTL  "info_panel_title"
#define PANEL_BODY "info_panel_body"

// 便利関数: ある/ないで作成
bool EnsureRectLabel(const string name)
{
   if(ObjectFind(0,name)>=0) return true;
   return ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0);
}
bool EnsureLabel(const string name)
{
   if(ObjectFind(0,name)>=0) return true;
   return ObjectCreate(0,name,OBJ_LABEL,0,0,0);
}

void CreateInfoPanel()
{
   // 背景
   if(!EnsureRectLabel(PANEL_BG))
   {
      Print("Create failed: ",PANEL_BG," err=",GetLastError());
      return;
   }
   ObjectSetInteger(0,PANEL_BG,OBJPROP_CORNER,   InpCorner);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_XDISTANCE,InpX);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_YDISTANCE,InpY);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_XSIZE,    InpWidth);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_YSIZE,    InpHeight);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_BGCOLOR,  InpBG);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_COLOR,    InpBorder);   // 枠
   ObjectSetInteger(0,PANEL_BG,OBJPROP_BACK,     false);       // 前面に
   ObjectSetInteger(0,PANEL_BG,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,PANEL_BG,OBJPROP_HIDDEN,   true);

   // タイトル
   if(!EnsureLabel(PANEL_TTL))
   {
      Print("Create failed: ",PANEL_TTL," err=",GetLastError());
      return;
   }
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_CORNER,    InpCorner);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_XDISTANCE, InpX+12);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_YDISTANCE, InpY+10);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_COLOR,     InpTextCol);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_FONTSIZE,  InpTitleSz);
   ObjectSetString (0,PANEL_TTL,OBJPROP_FONT,      InpFont);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_BOLD,      true);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_ANCHOR,    ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,PANEL_TTL,OBJPROP_HIDDEN,    true);
   ObjectSetString (0,PANEL_TTL,OBJPROP_TEXT,      "EA Status");

   // 本文（複数行OK: \n で改行）
   if(!EnsureLabel(PANEL_BODY))
   {
      Print("Create failed: ",PANEL_BODY," err=",GetLastError());
      return;
   }
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_CORNER,    InpCorner);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_XDISTANCE, InpX+12);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_YDISTANCE, InpY+40);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_COLOR,     InpTextCol);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_FONTSIZE,  InpBodySz);
   ObjectSetString (0,PANEL_BODY,OBJPROP_FONT,      InpFont);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_BOLD,      false);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_ANCHOR,    ANCHOR_LEFT_UPPER);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_SELECTABLE,false);
   ObjectSetInteger(0,PANEL_BODY,OBJPROP_HIDDEN,    true);

   ChartRedraw(0);
}

void SetPanelText(const string title, const string body)
{
   if(ObjectFind(0,PANEL_TTL)>=0)  ObjectSetString(0,PANEL_TTL, OBJPROP_TEXT, title);
   if(ObjectFind(0,PANEL_BODY)>=0) ObjectSetString(0,PANEL_BODY,OBJPROP_TEXT, body);
   ChartRedraw(0);
}

int OnInit()
{
   CreateInfoPanel();

   // 表示例
   string body=
      "Symbol : "+_Symbol+"\n"+
      "TF     : "+EnumToString((ENUM_TIMEFRAMES)Period())+"\n"+
      "Spread : "+(string)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)+"\n"+
      "Equity : "+DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2);
   SetPanelText("MachineLearning EA", body);

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   // 片付け
   ObjectDelete(0,PANEL_BODY);
   ObjectDelete(0,PANEL_TTL);
   ObjectDelete(0,PANEL_BG);
}

void OnTick()
{
   // 動的更新の例
   static datetime last=0;
   if(TimeCurrent()!=last)
   {
      last=TimeCurrent();
      string body=
         "Time   : "+TimeToString(last,TIME_DATE|TIME_SECONDS)+"\n"+
         "Bid/Ask: "+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_BID),5)+
         " / "+DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_ASK),5)+"\n"+
         "Pos    : "+(string)PositionsTotal();
      SetPanelText("Live Monitor", body);
   }
}
