Previous Days/Weeks/Months Highs and Lows Indicator
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;
}
}
}
}
Replies
hannanmumtaz01
04 May 2024, 04:52
( Updated at: 04 May 2024, 06:37 )
RE: Previous Days/Weeks/Months Highs and Lows Indicator
Didnt work for me at all, did some cleanup / bugfixes, seems to be working now.
using cAlgo.API;
using System;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class PreviousDayHighLow : 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 PreHue { get; set; }
[Parameter("Start Hour", Group = "Global", DefaultValue = 12, MinValue = 0, MaxValue = 23, Step = 1)]
public int StartTimeHour { get; set; }
[Parameter("UTC adjustment (-/+)", Group = "Global", DefaultValue = 8, 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()
{
System.Diagnostics.Debugger.Launch();
_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)
{
var indexOpenTime = Bars.OpenTimes[index];
CalculateDailyHighLows(index, indexOpenTime);
CalculateWeeklyHighLows(index, indexOpenTime);
CalculateMonthlyHighLows(index, indexOpenTime);
}
private void CalculateMonthlyHighLows(int index, DateTime indexOpenTime)
{
if (!PreMonthHigh && !PreMonthLow) return;
var monthlyIndex = _month.OpenTimes.GetIndexByTime(indexOpenTime);
_indexM[index] = monthlyIndex > 0 ? monthlyIndex : _indexM[index - 1];
var nameHigh = "Month High" + _indexM[index];
var nameLow = "Month Low" + _indexM[index];
var indexStart = (int)_indexM[index];
var start = _month.OpenTimes[indexStart - 1];
var stop = Bars.OpenTimes[index];
var high = _month.HighPrices[indexStart - 1];
var low = _month.LowPrices[indexStart - 1];
var clr = GetColor(PreMonthLineColor, PreHue);
var lns = GetLineStyle(PreMonthLineStyle);
if (double.IsNaN(high) || double.IsNaN(low)) return;
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 void CalculateWeeklyHighLows(int index, DateTime indexOpenTime)
{
if (!PreWeekHigh && !PreWeekLow) return;
var weeklyIndex = _week.OpenTimes.GetIndexByTime(indexOpenTime);
_indexW[index] = weeklyIndex > 0 ? weeklyIndex : _indexW[index - 1];
var nameHigh = "Week High" + _indexW[index];
var nameLow = "Week Low" + _indexW[index];
var indexStart = (int)_indexW[index];
var start = _week.OpenTimes[indexStart - 1];
var stop = Bars.OpenTimes[index];
var high = _week.HighPrices[indexStart - 1];
var low = _week.LowPrices[indexStart - 1];
var clr = GetColor(PreWeekLineColor, PreHue);
var lns = GetLineStyle(PreWeekLineStyle);
if (double.IsNaN(high) || double.IsNaN(low)) return;
if (PreWeekHigh)
Chart.DrawTrendLine(nameHigh, start, high, stop, high, clr, PreWeekThickness, lns);
if (PreWeekLow)
Chart.DrawTrendLine(nameLow, start, low, stop, low, clr, PreWeekThickness, lns);
}
private void CalculateDailyHighLows(int index, DateTime indexOpenTime)
{
if (!PreDayHigh && !PreDayLow) return;
var dailyIndex = _day.OpenTimes.GetIndexByTime(indexOpenTime);
_indexD[index] = dailyIndex > 0 ? dailyIndex : _indexD[index - 1];
var nameHigh = "Day High" + _indexD[index];
var nameLow = "Day Low" + _indexD[index];
var indexStart = (int)_indexD[index];
var startHour = StartTimeHour - Utc;
DateTime lineStart;
switch (startHour)
{
case >= 24:
startHour -= 24;
lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
break;
case <= 0:
startHour += 24;
lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
break;
default:
lineStart = new DateTime(Bars.OpenTimes[index].Year, Bars.OpenTimes[index].Month, Bars.OpenTimes[index].Day, startHour, 0, 0);
break;
}
// line length
var timeSpan = new TimeSpan(23, 59, 59);
var lineStop = lineStart + timeSpan;
// line height
var dayHighPrice = _day.HighPrices[indexStart - 1];
var dayLowPrice = _day.LowPrices[indexStart - 1];
// line style
var color = GetColor(PreDayLineColor, PreHue);
var lineStyle = GetLineStyle(PreDayLineStyle);
if (double.IsNaN(dayHighPrice) || double.IsNaN(dayLowPrice)) return;
if (PreDayHigh)
Chart.DrawTrendLine(nameHigh, lineStart, dayHighPrice, lineStop, dayHighPrice, color, PreDayThickness, lineStyle);
if (PreDayLow)
Chart.DrawTrendLine(nameLow, lineStart, dayLowPrice, lineStop, dayLowPrice, color, PreDayThickness, lineStyle);
}
private Color GetColor(ColorSelector color, HueSelector hue)
{
Color result;
switch (color)
{
case ColorSelector.Black:
{
result = Color.Black;
}
break;
case ColorSelector.White:
{
result = Color.White;
}
break;
case ColorSelector.Gray:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkGray;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightGray;
}
else
{
result = Color.Gray;
}
}
break;
case ColorSelector.Blue:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkBlue;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightBlue;
}
else
{
result = Color.Blue;
}
}
break;
case ColorSelector.Cyan:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkCyan;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightCyan;
}
else
{
result = Color.Cyan;
}
}
break;
case ColorSelector.Red:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkRed;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.OrangeRed;
}
else
{
result = Color.Red;
}
}
break;
case ColorSelector.Pink:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkMagenta;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightPink;
}
else
{
result = Color.Pink;
}
}
break;
case ColorSelector.Green:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkGreen;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightGreen;
}
else
{
result = Color.Green;
}
}
break;
case ColorSelector.Orange:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkOrange;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.Orange;
}
else
{
result = Color.Orange;
}
}
break;
case ColorSelector.Yellow:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.Gold;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.LightYellow;
}
else
{
result = Color.Yellow;
}
}
break;
case ColorSelector.Purple:
{
if (hue.Equals(HueSelector.Dark))
{
result = Color.DarkViolet;
}
else if (hue.Equals(HueSelector.Light))
{
result = Color.Violet;
}
else
{
result = Color.Purple;
}
}
break;
case ColorSelector.Default:
{
result = Application.ColorTheme.Equals(ColorTheme.Dark) ? Color.White : Color.Black;
}
break;
default:
{
result = Application.ColorTheme.Equals(ColorTheme.Dark) ? Color.White : Color.Black;
}
break;
}
return result;
}
private static LineStyle GetLineStyle(LineSelector line) =>
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
};
}
}
@hannanmumtaz01
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