OPEN-SOURCE SCRIPT

ICT Swing

478
//+------------------------------------------------------------------+
//| FractalLines.mq5 |
//| Ultimos 3 Fractales |
//+------------------------------------------------------------------+
#property copyright "Solicitud Personalizada"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0

//--- Parametros
input int LineExtension = 20; // Extension de la linea (velas a la derecha)
input int MaxFractales = 3; // Maximo numero de fractales a mostrar (3)
input color ColorClose = clrWhite; // Color linea del CLOSE
input color ColorAlcista = clrDodgerBlue;// Color linea del MINIMO (alcista)
input color ColorBajista = clrRed; // Color linea del MAXIMO (bajista)

//+------------------------------------------------------------------+
//| Detectar fractal ALCISTA |
//+------------------------------------------------------------------+
bool EsFractalAlcista(int pos, const double &low[])
{
if(pos < 2 || pos >= ArraySize(low) - 2) return false;
return (low[pos] < low[pos-1] && low[pos] < low[pos-2] &&
low[pos] < low[pos+1] && low[pos] < low[pos+2]);
}

//+------------------------------------------------------------------+
//| Detectar fractal BAJISTA |
//+------------------------------------------------------------------+
bool EsFractalBajista(int pos, const double &high[])
{
if(pos < 2 || pos >= ArraySize(high) - 2) return false;
return (high[pos] > high[pos-1] && high[pos] > high[pos-2] &&
high[pos] > high[pos+1] && high[pos] > high[pos+2]);
}

//+------------------------------------------------------------------+
//| Obtener segundos por timeframe |
//+------------------------------------------------------------------+
int GetSegundosPorVela()
{
switch(Period())
{
case PERIOD_M1: return 60;
case PERIOD_M5: return 300;
case PERIOD_M15: return 900;
case PERIOD_M30: return 1800;
case PERIOD_H1: return 3600;
case PERIOD_H4: return 14400;
case PERIOD_D1: return 86400;
case PERIOD_W1: return 604800;
case PERIOD_MN1: return 2592000;
default: return 86400;
}
}

//+------------------------------------------------------------------+
//| Dibujar linea |
//+------------------------------------------------------------------+
void DibujarLinea(string nombre, double nivel, datetime tiempo, color col)
{
ObjectDelete(0, nombre);
int seg = GetSegundosPorVela();
datetime futuro = tiempo + (LineExtension * seg);

if(ObjectCreate(0, nombre, OBJ_TREND, 0, tiempo, nivel, futuro, nivel))
{
ObjectSetInteger(0, nombre, OBJPROP_COLOR, col);
ObjectSetInteger(0, nombre, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, nombre, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, nombre, OBJPROP_BACK, false);
ObjectSetInteger(0, nombre, OBJPROP_RAY_RIGHT, false);
ChartRedraw();
}
}

//+------------------------------------------------------------------+
//| Limpiar todas las lineas de fractales |
//+------------------------------------------------------------------+
void LimpiarLineas()
{
for(int i = ObjectsTotal(0)-1; i >= 0; i--)
{
string nombre = ObjectName(0, i);
if(StringFind(nombre, "Fractal_Alcista_") == 0 ||
StringFind(nombre, "Fractal_Bajista_") == 0)
{
ObjectDelete(0, nombre);
}
}
}

//+------------------------------------------------------------------+
//| Main |
//+------------------------------------------------------------------+
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 < 10) return(0);

// Limpiar lineas anteriores
LimpiarLineas();

// --- COLECCIONAR FRACTALES ALCISTAS (los mas cercanos al precio) ---
int alcistas[];
int alcistasCount = 0;
ArrayResize(alcistas, rates_total);

for(int i = rates_total - 1; i >= 2; i--)
{
if(EsFractalAlcista(i, low))
{
alcistas[alcistasCount] = i;
alcistasCount++;
if(alcistasCount >= MaxFractales) break;
}
}

// --- DIBUJAR LINEAS ALCISTAS (CLOSE y MINIMO) ---
for(int j = 0; j < alcistasCount; j++)
{
int idx = alcistas[j];
double nivelClose = close[idx];
double nivelMinimo = low[idx];
datetime tiempo = time[idx];

// Linea en el CLOSE
DibujarLinea("Fractal_Alcista_Close_" + IntegerToString(idx), nivelClose, tiempo, ColorClose);
// Linea en el MINIMO
DibujarLinea("Fractal_Alcista_Min_" + IntegerToString(idx), nivelMinimo, tiempo, ColorAlcista);

Print("ALCISTA #", j+1, " | Vela: ", idx, " | Close: ", nivelClose, " | Min: ", nivelMinimo);
}

// --- COLECCIONAR FRACTALES BAJISTAS (los mas cercanos al precio) ---
int bajistas[];
int bajistasCount = 0;
ArrayResize(bajistas, rates_total);

for(int i = rates_total - 1; i >= 2; i--)
{
if(EsFractalBajista(i, high))
{
bajistas[bajistasCount] = i;
bajistasCount++;
if(bajistasCount >= MaxFractales) break;
}
}

// --- DIBUJAR LINEAS BAJISTAS (CLOSE y MAXIMO) ---
for(int j = 0; j < bajistasCount; j++)
{
int idx = bajistas[j];
double nivelClose = close[idx];
double nivelMaximo = high[idx];
datetime tiempo = time[idx];

// Linea en el CLOSE
DibujarLinea("Fractal_Bajista_Close_" + IntegerToString(idx), nivelClose, tiempo, ColorClose);
// Linea en el MAXIMO
DibujarLinea("Fractal_Bajista_Max_" + IntegerToString(idx), nivelMaximo, tiempo, ColorBajista);

Print("BAJISTA #", j+1, " | Vela: ", idx, " | Close: ", nivelClose, " | Max: ", nivelMaximo);
}

return(rates_total);
}
//+------------------------------------------------------------------+

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.