Previous Days/Weeks/Months Highs and Lows Indicator
10 Mar 2024, 04:54
Hello everyone,
I have here an indicator that I had someone program for me just over a year ago. It was working fine when I first tried it. I haven't used Ctrader for some time and now back to using it.
As the subject states, it's a previous day/week/month high and low indicator that draws lines for previous days. issue that I'm having is that the lines that are being drawn out are from the second previous day (2 days before) instead of the previous day. I paid for this indicator to be made (I can't find the person who made it originally), and I'm willing to share it free with you all, but can someone fix this ?? I have only found 1 other indicator that's kind of the same idea, but it doesn't display properly, the one I had made is much better (when it shows properly). I like it the way it is with the discontinued lines, the other one I found has continuous lines and makes the chart not as clean.
below is the code for it. please, if someone can have a look and fix it, I would be grateful. thanks in advance!
my email: fcarabat@gmail.com
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class OHCL : Indicator
{
[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; }
[Parameter("Show High", Group = "Previous Week H/L", DefaultValue = false)]
public bool PreWeekHigh { get; set; }
[Parameter("Show Low", Group = "Previous Week H/L", DefaultValue = false)]
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; }
[Parameter("Show High", Group = "Previous Month H/L", DefaultValue = false)]
public bool PreMonthHigh { get; set; }
[Parameter("Show Low", Group = "Previous Month H/L", DefaultValue = false)]
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; }
[Parameter("Hue", Group = "Global", DefaultValue = HueSelector.Main)]
public HueSelector Hue { get; set; }
[Parameter("Start Hour", Group = "Global", DefaultValue = 15, MinValue = 0, MaxValue = 23, Step = 1)]
public int StartTimeHour { get; set; }
[Parameter("UTC adjustment (-/+)", Group = "Global", DefaultValue = -6, MinValue = -12, MaxValue = 14, Step = 1)]
public int UTC { get; set; }
private Bars week, day, month;
private IndicatorDataSeries indexM, indexD, indexW;
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()
{
week = MarketData.GetBars(TimeFrame.Weekly);
day = MarketData.GetBars(TimeFrame.Daily);
month = MarketData.GetBars(TimeFrame.Monthly);
indexW = CreateDataSeries();
indexD = CreateDataSeries();
indexM = CreateDataSeries();
}
public override void Calculate(int index)
{
int index1 = index;
int indxM = month.OpenTimes.GetIndexByExactTime(Bars.OpenTimes[index]);
int indxW = week.OpenTimes.GetIndexByExactTime(Bars.OpenTimes[index]);
int indxD = day.OpenTimes.GetIndexByExactTime(Bars.OpenTimes[index]);
indexW[index] = indxW > 0 ? indxW : indexW[index - 1];
indexD[index] = indxD > 0 ? indxD : indexD[index - 1];
indexM[index] = indxM > 0 ? indxM : indexM[index - 1];
if (PreDayHigh || PreDayLow)
{
string nameHigh = "Day High" + indexD[index].ToString();
string nameLow = "Day Low" + indexD[index].ToString();
int indexStart = (int)indexD[index];
//DateTime start = day.OpenTimes[indexStart - 1];
TimeSpan timeSP = new TimeSpan(23, 59, 59);
int startHour = StartTimeHour - UTC;
DateTime start = new DateTime();
if (startHour >= 24)
{
startHour = startHour - 24;
start = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
}
else if (startHour <= 0)
{
startHour = startHour + 24;
start = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
}
else
{
start = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
}
DateTime stop = start + timeSP;
double high = day.HighPrices[indexStart - 1];
double low = day.LowPrices[indexStart - 1];
Color clr = GetColor(PreDayLineColor, Hue);
LineStyle lns = GetLineStyle(PreDayLineStyle);
if (PreDayHigh)
Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreDayThickness, lns);
if (PreDayLow)
Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreDayThickness, lns);
}
if (PreWeekHigh || PreWeekLow)
{
string nameHigh = "Week High" + indexW[index].ToString();
string nameLow = "Week Low" + indexW[index].ToString();
int indexStart = (int)indexW[index];
DateTime start = week.OpenTimes[indexStart - 1];
DateTime stop = Bars.OpenTimes[index1];
double high = week.HighPrices[indexStart - 1];
double low = week.LowPrices[indexStart - 1];
Color clr = GetColor(PreWeekLineColor, Hue);
LineStyle lns = GetLineStyle(PreWeekLineStyle);
if (PreWeekHigh)
Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreWeekThickness, lns);
if (PreWeekLow)
Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreWeekThickness, lns);
}
if (PreMonthHigh || PreMonthLow)
{
string nameHigh = "Month High" + indexM[index].ToString();
string nameLow = "Month Low" + indexM[index].ToString();
int indexStart = (int)indexM[index];
DateTime start = month.OpenTimes[indexStart - 1];
DateTime stop = Bars.OpenTimes[index1];
double high = month.HighPrices[indexStart - 1];
double low = month.LowPrices[indexStart - 1];
Color clr = GetColor(PreMonthLineColor, Hue);
LineStyle lns = GetLineStyle(PreMonthLineStyle);
if (PreMonthHigh)
Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreMonthThickness, lns);
if (PreMonthLow)
Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreMonthThickness, lns);
}
}
private Color GetColor(ColorSelector color, HueSelector hue)
{
switch (color)
{
case ColorSelector.Black:
{
return Color.Black;
}
break;
case ColorSelector.White:
{
return Color.White;
}
break;
case ColorSelector.Gray:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkGray;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightGray;
}
else
{
return Color.Gray;
}
}
break;
case ColorSelector.Blue:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkBlue;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightBlue;
}
else
{
return Color.Blue;
}
}
break;
case ColorSelector.Cyan:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkCyan;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightCyan;
}
else
{
return Color.Cyan;
}
}
break;
case ColorSelector.Red:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkRed;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.OrangeRed;
}
else
{
return Color.Red;
}
}
break;
case ColorSelector.Pink:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkMagenta;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightPink;
}
else
{
return Color.Pink;
}
}
break;
case ColorSelector.Green:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkGreen;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightGreen;
}
else
{
return Color.Green;
}
}
break;
case ColorSelector.Yellow:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.Gold;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.LightYellow;
}
else
{
return Color.Yellow;
}
}
break;
case ColorSelector.Purple:
{
if (hue.Equals(HueSelector.Dark))
{
return Color.DarkViolet;
}
else if (hue.Equals(HueSelector.Light))
{
return Color.Violet;
}
else
{
return Color.Purple;
}
}
break;
case ColorSelector.Default:
{
if (Application.ColorTheme.Equals(ColorTheme.Dark))
return Color.White;
else
return Color.Black;
}
break;
default:
{
if (Application.ColorTheme.Equals(ColorTheme.Dark))
return Color.White;
else
return Color.Black;
}
break;
}
}
private LineStyle GetLineStyle(LineSelector line)
{
switch (line)
{
case LineSelector.Solid:
return LineStyle.Solid;
break;
case LineSelector.Lines:
return LineStyle.Lines;
break;
case LineSelector.LinesDots:
return LineStyle.LinesDots;
break;
case LineSelector.Dots:
return LineStyle.Dots;
break;
case LineSelector.DotsRare:
return LineStyle.DotsRare;
break;
case LineSelector.DotsVeryRare:
return LineStyle.DotsVeryRare;
break;
default:
return LineStyle.Solid;
break;
}
}
}
}
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);
// -------------------------------------------------------------------- // 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);
// -------------------------------------------------------------------- // 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);
PanagiotisCharalampous
11 Mar 2024, 08:29
Hi there,
If you would like to get a quote for a fix, feel free to contact me at development@clickalgo.com
@PanagiotisCharalampous