﻿//+------------------------------------------------------------------+
//|                                        DynamicVerticalPrice.mq5  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0

// Paramètres d'entrée
input int      InpLookback      = 100;    // Décalage en nombre de barres
input bool     InpShowHLine     = true;   // Afficher la ligne horizontale et le prix ?
input color    InpVertColor     = clrRed; // Couleur ligne verticale
input ENUM_LINE_STYLE InpVertStyle = STYLE_SOLID; // Style ligne verticale
input color    InpHorizColor    = clrBlue;// Couleur ligne horizontale
input ENUM_LINE_STYLE InpHorizStyle = STYLE_DOT;  // Style ligne horizontale

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], 
                const double &open[], const double &high[], const double &low[], 
                const double &close[], const long &tick_volume[], const long &volume[], 
                const int &spread[]) 
{
   if(rates_total <= InpLookback) return 0;
   int targetIndex = rates_total - 1 - InpLookback;

   // 1. Ligne verticale
   string vName = "DynamicVLine";
   if(ObjectFind(0, vName) < 0) 
      ObjectCreate(0, vName, OBJ_VLINE, 0, time[targetIndex], 0);
   else 
      ObjectMove(0, vName, 0, time[targetIndex], 0);
      
   ObjectSetInteger(0, vName, OBJPROP_COLOR, InpVertColor);
   ObjectSetInteger(0, vName, OBJPROP_STYLE, InpVertStyle);

   // 2. Ligne horizontale et Texte
   string hName = "DynamicHLine";
   string tName = "DynamicPriceText";
   
   if(InpShowHLine) {
      double closePrice = close[targetIndex];
      
      // Ligne horizontale
      if(ObjectFind(0, hName) < 0) 
         ObjectCreate(0, hName, OBJ_HLINE, 0, 0, closePrice);
      else 
         ObjectMove(0, hName, 0, 0, closePrice);
         
      ObjectSetInteger(0, hName, OBJPROP_COLOR, InpHorizColor);
      ObjectSetInteger(0, hName, OBJPROP_STYLE, InpHorizStyle);
      
      // Texte en bas
      if(ObjectFind(0, tName) < 0) 
         ObjectCreate(0, tName, OBJ_TEXT, 0, 0, 0);
         
      ObjectMove(0, tName, 0, time[targetIndex], 0); 
      ObjectSetString(0, tName, OBJPROP_TEXT, "Prix : " + DoubleToString(closePrice, _Digits));
      ObjectSetInteger(0, tName, OBJPROP_COLOR, InpHorizColor);
      ObjectSetInteger(0, tName, OBJPROP_ANCHOR, ANCHOR_BOTTOM);
      ObjectSetInteger(0, tName, OBJPROP_ALIGN, ALIGN_CENTER);
   } else {
      ObjectDelete(0, hName);
      ObjectDelete(0, tName);
   }

   return(rates_total);
}

//+------------------------------------------------------------------+
//| Nettoyage                                                        |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
   ObjectsDeleteAll(0, "DynamicVLine");
   ObjectsDeleteAll(0, "DynamicHLine");
   ObjectsDeleteAll(0, "DynamicPriceText");
}