VerticalLinesFixedInterval-.../Indicators/VerticalLinesFixedInterval-Deep.mq5

178 lines
14 KiB
MQL5
Raw Permalink Normal View History

2025-06-23 22:26:54 -03:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| VerticalLinesFixedInterval.mq5 |
//| Copyright 2023, DeepSeek |
//| Raphael Bloch https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, DeepSeek"
#property link "https://www.mql5.com"
#property version "1.04"
#property indicator_chart_window
#property indicator_plots 0
input int IntervalMinutes = 5; // Intervalo entre linhas (minutos)
input color LineColor = clrDeepSkyBlue; // Cor das linhas
input color LineColor = clrDeepSkyBlue; // Cor das linhas
double Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Indicator buffer
SetIndexBuffer(0, Buffer);
// Set the color of the indicator line
PlotIndexSetInteger(0, PLOT_LINE_COLOR, LineColor);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
// Simple example: copy close prices to the buffer
for(int i = 0; i < rates_total; i++)
{
Buffer[i] = close[i];
}
return(rates_total);
}
//+------------------------------------------------------------------+
//
// In this example, the indicator will plot the close prices of a symbol in a separate window using the color specified by `LineColor`. The user can change the color from the indicator's properties dialog in the MetaTrader platform.
//
input ENUM_LINE_STYLE LineStyle = STYLE_SOLID; // Estilo da linha
input int LineWidth = 1; // Largura da linha
input int HistoryLines = 100; // N<EFBFBD>mero de linhas a manter no hist<EFBFBD>rico
input bool ShowTimeDescription = true; // Mostrar descri<EFBFBD><EFBFBD>o na linha
input color DescColor = clrBlack; // Cor da descri<EFBFBD><EFBFBD>o
input int DescSize = 10; // Tamanho da fonte
input string DescFont = "Arial"; // Fonte do texto
//+------------------------------------------------------------------+
//| Fun<EFBFBD><EFBFBD>o de inicializa<EFBFBD><EFBFBD>o do indicador |
//+------------------------------------------------------------------+
int OnInit()
{
if(IntervalMinutes <= 0)
{
// Certainly! The code snippet you've provided is written in MQL5, which is a programming language used for developing trading robots, scripts, and custom technical indicators for the MetaTrader 5 platform. Let's break it down:
//
Print("Erro: Intervalo deve ser maior que 0");
return(INIT_PARAMETERS_INCORRECT);
/
Print("Erro: Intervalo deve ser maior que 0");
return(INIT_PARAMETERS_INCORRECT);
}
DrawVerticalLines();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Fun<EFBFBD><EFBFBD>o de itera<EFBFBD><EFBFBD>o do indicador |
//+------------------------------------------------------------------+
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(prev_calculated != rates_total)
{
CleanOldLines();
DrawVerticalLines();
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Desenha as linhas verticais nos intervalos especificados |
//+------------------------------------------------------------------+
void DrawVerticalLines()
{
datetime currentTime = TimeCurrent();
MqlDateTime currentTimeStruct;
TimeToStruct(currentTime, currentTimeStruct);
int minutesToSubtract = currentTimeStruct.min % IntervalMinutes;
datetime referenceTime = currentTime - (minutesToSubtract * 60);
for(int i = 0; i < HistoryLines; i++)
{
datetime lineTime = referenceTime - (i * IntervalMinutes * 60);
string lineName = "VLine_" + IntegerToString(lineTime);
if(ObjectFind(0, lineName) < 0)
{
// Cria a linha vertical
ObjectCreate(0, lineName, OBJ_VLINE, 0, lineTime, 0);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, LineColor);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, LineStyle);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, LineWidth);
ObjectSetInteger(0, lineName, OBJPROP_BACK, true);
// Adiciona descri<EFBFBD><EFBFBD>o diretamente na linha
if(ShowTimeDescription)
{
string descText = "Linha: " + TimeToString(lineTime, TIME_MINUTES);
ObjectSetString(0, lineName, OBJPROP_TEXT, descText);
ObjectSetInteger(0, lineName, OBJPROP_FONTSIZE, DescSize);
ObjectSetString(0, lineName, OBJPROP_FONT, DescFont);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, DescColor);
}
}
}
}
//+------------------------------------------------------------------+
//| Remove linhas antigas al<EFBFBD>m do hist<EFBFBD>rico especificado |
//+------------------------------------------------------------------+
void CleanOldLines()
{
datetime currentTime = TimeCurrent();
datetime oldestTime = currentTime - (HistoryLines * IntervalMinutes * 60);
int total = ObjectsTotal(0, 0, -1);
for(int i = total-1; i >= 0; i--)
{
string name = ObjectName(0, i, 0, -1);
if(StringFind(name, "VLine_") == 0)
{
datetime lineTime = (datetime)ObjectGetInteger(0, name, OBJPROP_TIME);
if(lineTime < oldestTime)
{
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Fun<EFBFBD><EFBFBD>o de desinicializa<EFBFBD><EFBFBD>o do indicador |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, "VLine_");
}