This website uses cookies to enhance site navigation, analyze site usage, and assist in our marketing efforts. By clicking “Accept All” you are providing your consent to our use of all cookies. Alternatively, please provide your choice by pressing “Customize Cookies”. For more information, please read our Privacy policy
eacm8691
24 Mar 2025, 15:47 ( Updated at: 26 Mar 2025, 06:26 )
RE: RE: Previous Days/Weeks/Months Highs and Lows Indicator
hannanmumtaz01 said:
fix this one there where some visual errors:
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class PreviousDayHighLow : Indicator
{
// ---------------------------------
// Parámetros para el DÍA ANTERIOR
// ---------------------------------
[Parameter("Show High", Group = "Previous Day H/L", DefaultValue = true)]
public bool PreDayHigh { get; set; }
[Parameter("Show Low", Group = "Previous Day H/L", DefaultValue = true)]
public bool PreDayLow { get; set; }
[Parameter("Color", Group = "Previous Day H/L", DefaultValue = ColorSelector.Pink)]
public ColorSelector PreDayLineColor { get; set; }
[Parameter("Style", Group = "Previous Day H/L", DefaultValue = LineSelector.Dots)]
public LineSelector PreDayLineStyle { get; set; }
[Parameter("Thickness", Group = "Previous Day H/L", MinValue = 1, DefaultValue = 1)]
public int PreDayThickness { get; set; }
// ---------------------------------
// Parámetros para la SEMANA ANTERIOR
// ---------------------------------
[Parameter("Show High", Group = "Previous Week H/L", DefaultValue = true)]
public bool PreWeekHigh { get; set; }
[Parameter("Show Low", Group = "Previous Week H/L", DefaultValue = true)]
public bool PreWeekLow { get; set; }
[Parameter("Color", Group = "Previous Week H/L", DefaultValue = ColorSelector.Yellow)]
public ColorSelector PreWeekLineColor { get; set; }
[Parameter("Style", Group = "Previous Week H/L", DefaultValue = LineSelector.LinesDots)]
public LineSelector PreWeekLineStyle { get; set; }
[Parameter("Thickness", Group = "Previous Week H/L", MinValue = 1, DefaultValue = 1)]
public int PreWeekThickness { get; set; }
// ---------------------------------
// Parámetros para el MES ANTERIOR
// ---------------------------------
[Parameter("Show High", Group = "Previous Month H/L", DefaultValue = true)]
public bool PreMonthHigh { get; set; }
[Parameter("Show Low", Group = "Previous Month H/L", DefaultValue = true)]
public bool PreMonthLow { get; set; }
[Parameter("Color", Group = "Previous Month H/L", DefaultValue = ColorSelector.Purple)]
public ColorSelector PreMonthLineColor { get; set; }
[Parameter("Style", Group = "Previous Month H/L", DefaultValue = LineSelector.Solid)]
public LineSelector PreMonthLineStyle { get; set; }
[Parameter("Thickness", Group = "Previous Month H/L", MinValue = 1, DefaultValue = 1)]
public int PreMonthThickness { get; set; }
// ---------------------------------
// Parámetros Globales
// ---------------------------------
[Parameter("Hue", Group = "Global", DefaultValue = HueSelector.Main)]
public HueSelector PreHue { get; set; }
[Parameter("Start Hour (Daily)", Group = "Global", DefaultValue = 12, MinValue = 0, MaxValue = 23, Step = 1)]
public int StartTimeHour { get; set; }
[Parameter("UTC adjustment (Daily)", Group = "Global", DefaultValue = -5, MinValue = -12, MaxValue = 14, Step = 1)]
public int Utc { get; set; }
private Bars _day;
private Bars _week;
private Bars _month;
// Enums para color y estilo
public enum HueSelector
{
Main,
Light,
Dark
}
public enum LineSelector
{
Solid,
Lines,
LinesDots,
Dots,
DotsRare,
DotsVeryRare
}
public enum ColorSelector
{
Default,
White,
Black,
Gray,
Blue,
Cyan,
Red,
Pink,
Green,
Orange,
Yellow,
Purple
}
protected override void Initialize()
{
// Obtenemos la serie diaria, semanal y mensual
_day = MarketData.GetBars(TimeFrame.Daily);
_week = MarketData.GetBars(TimeFrame.Weekly);
_month = MarketData.GetBars(TimeFrame.Monthly);
}
public override void Calculate(int index)
{
// Mostramos solo el día anterior más reciente
CalculateDailyHighLows(index);
// Mostramos solo la semana anterior más reciente
CalculateWeeklyHighLows(index);
// Mostramos solo el mes anterior más reciente
CalculateMonthlyHighLows(index);
}
// --------------------------------------------------------------------
// 1) CÁLCULO DEL DÍA ANTERIOR (SOLO el más reciente)
// --------------------------------------------------------------------
private void CalculateDailyHighLows(int index)
{
if (!PreDayHigh && !PreDayLow)
return;
DateTime indexOpenTime = Bars.OpenTimes[index];
int dailyIndex = _day.OpenTimes.GetIndexByTime(indexOpenTime);
// Se necesita al menos 1 día anterior
if (dailyIndex < 1)
return;
int prevDayIndex = dailyIndex - 1;
// Borramos líneas anteriores para no acumular
Chart.RemoveObject("Day High");
Chart.RemoveObject("Day Low");
// Ajustamos la hora de inicio según parámetros
DateTime prevDayOpenTime = _day.OpenTimes[prevDayIndex];
int startHour = StartTimeHour - Utc;
// Normalizamos startHour si sale del rango 0-23
if (startHour >= 24) startHour -= 24;
else if (startHour < 0) startHour += 24;
DateTime lineStart = new DateTime(prevDayOpenTime.Year, prevDayOpenTime.Month, prevDayOpenTime.Day, startHour, 0, 0);
// Duración de un día completo
TimeSpan timeSpan = new TimeSpan(23, 59, 59);
DateTime lineStop = lineStart + timeSpan;
// Altos y bajos del día anterior
double dayHighPrice = _day.HighPrices[prevDayIndex];
double dayLowPrice = _day.LowPrices[prevDayIndex];
if (double.IsNaN(dayHighPrice) || double.IsNaN(dayLowPrice))
return;
// Dibujamos
Color color = GetColor(PreDayLineColor, PreHue);
LineStyle lineStyle = GetLineStyle(PreDayLineStyle);
if (PreDayHigh)
Chart.DrawTrendLine("Day High", lineStart, dayHighPrice, lineStop, dayHighPrice, color, PreDayThickness, lineStyle);
if (PreDayLow)
Chart.DrawTrendLine("Day Low", lineStart, dayLowPrice, lineStop, dayLowPrice, color, PreDayThickness, lineStyle);
}
// --------------------------------------------------------------------
// 2) CÁLCULO DE LA SEMANA ANTERIOR (SOLO la más reciente)
// --------------------------------------------------------------------
private void CalculateWeeklyHighLows(int index)
{
if (!PreWeekHigh && !PreWeekLow)
return;
DateTime indexOpenTime = Bars.OpenTimes[index];
int weeklyIndex = _week.OpenTimes.GetIndexByTime(indexOpenTime);
// Se necesita al menos 1 semana anterior
if (weeklyIndex < 1)
return;
int prevWeekIndex = weeklyIndex - 1;
// Borramos líneas anteriores para no acumular
Chart.RemoveObject("Week High");
Chart.RemoveObject("Week Low");
// Obtenemos los altos y bajos de la semana anterior
double weekHighPrice = _week.HighPrices[prevWeekIndex];
double weekLowPrice = _week.LowPrices[prevWeekIndex];
if (double.IsNaN(weekHighPrice) || double.IsNaN(weekLowPrice))
return;
// Definimos el inicio de la semana anterior
DateTime prevWeekOpenTime = _week.OpenTimes[prevWeekIndex];
// Definimos el final de esa semana (justo antes de la apertura de la siguiente)
DateTime nextWeekOpenTime = _week.OpenTimes[weeklyIndex];
DateTime lineStop = nextWeekOpenTime.AddSeconds(-1);
// Dibujamos
Color color = GetColor(PreWeekLineColor, PreHue);
LineStyle lineStyle = GetLineStyle(PreWeekLineStyle);
if (PreWeekHigh)
Chart.DrawTrendLine("Week High", prevWeekOpenTime, weekHighPrice, lineStop, weekHighPrice, color, PreWeekThickness, lineStyle);
if (PreWeekLow)
Chart.DrawTrendLine("Week Low", prevWeekOpenTime, weekLowPrice, lineStop, weekLowPrice, color, PreWeekThickness, lineStyle);
}
// --------------------------------------------------------------------
// 3) CÁLCULO DEL MES ANTERIOR (SOLO el más reciente)
// --------------------------------------------------------------------
private void CalculateMonthlyHighLows(int index)
{
if (!PreMonthHigh && !PreMonthLow)
return;
DateTime indexOpenTime = Bars.OpenTimes[index];
int monthlyIndex = _month.OpenTimes.GetIndexByTime(indexOpenTime);
// Se necesita al menos 1 mes anterior
if (monthlyIndex < 1)
return;
int prevMonthIndex = monthlyIndex - 1;
// Borramos líneas anteriores para no acumular
Chart.RemoveObject("Month High");
Chart.RemoveObject("Month Low");
// Obtenemos los altos y bajos del mes anterior
double monthHighPrice = _month.HighPrices[prevMonthIndex];
double monthLowPrice = _month.LowPrices[prevMonthIndex];
if (double.IsNaN(monthHighPrice) || double.IsNaN(monthLowPrice))
return;
// Inicio del mes anterior
DateTime prevMonthOpenTime = _month.OpenTimes[prevMonthIndex];
// Fin del mes anterior (un segundo antes de la apertura del siguiente)
DateTime nextMonthOpenTime = _month.OpenTimes[monthlyIndex];
DateTime lineStop = nextMonthOpenTime.AddSeconds(-1);
// Dibujamos
Color color = GetColor(PreMonthLineColor, PreHue);
LineStyle lineStyle = GetLineStyle(PreMonthLineStyle);
if (PreMonthHigh)
Chart.DrawTrendLine("Month High", prevMonthOpenTime, monthHighPrice, lineStop, monthHighPrice, color, PreMonthThickness, lineStyle);
if (PreMonthLow)
Chart.DrawTrendLine("Month Low", prevMonthOpenTime, monthLowPrice, lineStop, monthLowPrice, color, PreMonthThickness, lineStyle);
}
// ----------------------------------------------------
// Métodos auxiliares para colores y estilos de línea
// ----------------------------------------------------
private Color GetColor(ColorSelector color, HueSelector hue)
{
switch (color)
{
case ColorSelector.Black:
return Color.Black;
case ColorSelector.White:
return Color.White;
case ColorSelector.Gray:
if (hue == HueSelector.Dark) return Color.DarkGray;
if (hue == HueSelector.Light) return Color.LightGray;
return Color.Gray;
case ColorSelector.Blue:
if (hue == HueSelector.Dark) return Color.DarkBlue;
if (hue == HueSelector.Light) return Color.LightBlue;
return Color.Blue;
case ColorSelector.Cyan:
if (hue == HueSelector.Dark) return Color.DarkCyan;
if (hue == HueSelector.Light) return Color.LightCyan;
return Color.Cyan;
case ColorSelector.Red:
if (hue == HueSelector.Dark) return Color.DarkRed;
if (hue == HueSelector.Light) return Color.OrangeRed;
return Color.Red;
case ColorSelector.Pink:
if (hue == HueSelector.Dark) return Color.DarkMagenta;
if (hue == HueSelector.Light) return Color.LightPink;
return Color.Pink;
case ColorSelector.Green:
if (hue == HueSelector.Dark) return Color.DarkGreen;
if (hue == HueSelector.Light) return Color.LightGreen;
return Color.Green;
case ColorSelector.Orange:
if (hue == HueSelector.Dark) return Color.DarkOrange;
if (hue == HueSelector.Light) return Color.Orange;
return Color.Orange;
case ColorSelector.Yellow:
if (hue == HueSelector.Dark) return Color.Gold;
if (hue == HueSelector.Light) return Color.LightYellow;
return Color.Yellow;
case ColorSelector.Purple:
if (hue == HueSelector.Dark) return Color.DarkViolet;
if (hue == HueSelector.Light) return Color.Violet;
return Color.Purple;
case ColorSelector.Default:
default:
return Application.ColorTheme == ColorTheme.Dark ? Color.White : Color.Black;
}
}
private static LineStyle GetLineStyle(LineSelector line)
{
return line switch
{
LineSelector.Solid => LineStyle.Solid,
LineSelector.Lines => LineStyle.Lines,
LineSelector.LinesDots => LineStyle.LinesDots,
LineSelector.Dots => LineStyle.Dots,
LineSelector.DotsRare => LineStyle.DotsRare,
LineSelector.DotsVeryRare => LineStyle.DotsVeryRare,
_ => LineStyle.Solid,
};
}
}
}
@eacm8691