Prior Day High Low Indicator: How do I Ignore Sundays?
Prior Day High Low Indicator: How do I Ignore Sundays?
07 May 2023, 18:57
Hi all
I am using the indicator below which shows the high and low levels for the prior day but it picks up Sunday's short session on a Monday where I want Monday to show Friday's high and low (effectively ignoring Sunday).
I'd be grateful if someone could confirm how I do this.
Much appreciated.
David
using System;
using System.Globalization;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Windows.Forms;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DailyHighLow : Indicator
{
[Parameter("Start Time", DefaultValue = "2:00 am", Group = "Indicator")]
public string startTime { get; set; }
[Parameter("End Time", DefaultValue = "8:00 am", Group = "Indicator")]
public string endTime { get; set; }
[Parameter("Alerts", DefaultValue = false, Group = "Alerts")]
public bool alertOn { get; set; }
[Parameter("Alerts text", DefaultValue = "Alert", Group = "Alerts")]
public string alert_text { get; set; }
//[Parameter("Email Alert On", DefaultValue = false, Group = "Alerts")]
//public bool emailAlertOn { get; set; }
//[Parameter("Email Adress", DefaultValue = "", Group = "Alerts")]
//public string email_adress { get; set; }
//[Parameter("Email Subject", DefaultValue = "", Group = "Alerts")]
//public string email_subject { get; set; }
//[Parameter("Email Text", DefaultValue = "", Group = "Alerts")]
//public string email_texts { get; set; }
private double high = 0, low = 99999999, maxDay, minDay;
private DateTime startT, endT;
private int oldDay;
[Output("High", LineColor = "Orange")]
public IndicatorDataSeries DayHigh { get; set; }
[Output("Low", LineColor = "Orange")]
public IndicatorDataSeries DayLow { get; set; }
protected override void Initialize()
{
// Initialize and create nested indicators
startT = StringToTime(startTime);
endT = StringToTime(endTime);
}
DateTime time;
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
HighestNLowest(startT, endT, index, ref high, ref low);
if (maxDay != 0 && minDay != 99999999)
{
DayHigh[index] = maxDay;
DayLow[index] = minDay;
if (time != Bars[index].OpenTime)
{
if (Bars[1].Close < maxDay && Bars[0].Close > maxDay)
{
if (alertOn)
{
string message = "Daily H/L Cross High. " + alert_text;
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(message, "Alert", buttons);
Print("Daily H/L Cross High. " + alert_text);
}
time = Bars[index].OpenTime;
}
if (Bars[1].Close > maxDay && Bars[0].Close < maxDay)
{
if (alertOn)
{
string message = "Daily H/L Cross Low. " + alert_text;
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(message, "Alert", buttons);
Print("Daily H/L Cross High. " + alert_text);
}
//if (emailAlertOn)
// Print("Daily H/L Cross Low. " + alert_text);
time = Bars[index].OpenTime;
}
}
}
}
private DateTime StringToTime(string sTime)
{
if (string.IsNullOrEmpty(sTime))
return DateTime.Now;
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "h:mm tt";
DateTime dTime;
dTime = DateTime.ParseExact(sTime, format, provider);
return dTime;
}
private void HighestNLowest(DateTime startT, DateTime endT, int index, ref double high, ref double low)
{
int endHour = -1;
if (endT.Hour == startT.Hour)
endHour = 24;
else
endHour = endT.Hour;
if ((Bars[index].OpenTime.Hour == 0 && Bars[index].OpenTime.Minute == 0) || Bars[index].OpenTime.Day != oldDay)
{
maxDay = high;
minDay = low;
high = 0;
low = 99999999;
oldDay = Bars[index].OpenTime.Day;
}
if ((Bars[index].OpenTime.Hour >= startT.Hour) && (Bars[index].OpenTime.Hour < endHour) && (Bars[index].OpenTime.Minute >= startT.Minute))
{
high = Math.Max(Bars[index].High, high);
low = Math.Min(Bars[index].Low, low);
}
if (Bars[index].OpenTime.Hour == endHour && Bars[index].OpenTime.Minute >= endT.Minute)
{
oldDay = Bars[index].OpenTime.Day;
}
}
}
}
Replies
davcartep
01 Jun 2023, 11:16
RE:
firemyst said:
Example to help you get started:
if (Bars[index].OpenTime.DayOfWeek != DayOfWeek.Sunday) { } else { // skip Sunday }
You just have to make sure your bot is set to work in your time zone, otherwise if your market data is being derived in another time zone, obviously that Sunday might not line up with your Sunday.
Thanks very much. Appreciated!
@davcartep
firemyst
01 Jun 2023, 04:55
Example to help you get started:
You just have to make sure your bot is set to work in your time zone, otherwise if your market data is being derived in another time zone, obviously that Sunday might not line up with your Sunday.
@firemyst