-
Notifications
You must be signed in to change notification settings - Fork 0
/
AAA_SessionOpen.cs
192 lines (166 loc) · 8.09 KB
/
AAA_SessionOpen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
// This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class AAA_SessionOpen : Indicator
{
[NinjaScriptProperty]
[Display(Name="US Session Open", Description="Time in NYSE timezone", Order=1, GroupName="Time Parameters")]
public TimeSpan USSessionOpenTime { get; set; } = new TimeSpan(9, 30, 0); // NYSE opens at 9:30 AM
[Browsable(false)]
public string USSessionOpenTimeSerializable
{
get { return USSessionOpenTime.ToString(); }
set { USSessionOpenTime = TimeSpan.Parse(value); }
}
[NinjaScriptProperty]
[Display(Name="NYSE Timezone", Description="Timezone of NYSE", Order=2, GroupName="Time Parameters")]
public string NyseTimeZoneId { get; set; } = "Eastern Standard Time";
[NinjaScriptProperty]
[Display(Name="EU Session Open", Description="Time in London timezone", Order=3, GroupName="Time Parameters")]
public TimeSpan EUSessionOpenTime { get; set; } = new TimeSpan(8, 00, 0); // London opens at 8:00 AM
[Browsable(false)]
public string EUSessionOpenTimeSerializable
{
get { return EUSessionOpenTime.ToString(); }
set { EUSessionOpenTime = TimeSpan.Parse(value); }
}
[NinjaScriptProperty]
[Display(Name="London Timezone", Description="Timezone of London", Order=4, GroupName="Time Parameters")]
public string LondonTimeZoneId { get; set; } = "GMT Standard Time";
[NinjaScriptProperty]
[Display(Name="Chart Timezone", Description="Timezone of the chart data", Order=5, GroupName="Time Parameters")]
public string ChartTimeZoneId { get; set; } = "Central European Standard Time";
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name="Line Color", Order=6, GroupName="Drawing Parameters")]
public Brush LineColor { get; set; } = Brushes.White;
[Browsable(false)]
public string LineColorSerializable
{
get { return Serialize.BrushToString(LineColor); }
set { LineColor = Serialize.StringToBrush(value); }
}
[NinjaScriptProperty]
[Display(Name="Line Thickness", Order=7, GroupName="Drawing Parameters")]
public int LineThickness { get; set; } = 5;
[NinjaScriptProperty]
[Display(Name="Line Transparency", Order=8, GroupName="Drawing Parameters")]
public int LineTransparency { get; set; } = 50;
[NinjaScriptProperty]
[Display(Name="Line Style", Order=9, GroupName="Drawing Parameters")]
public DashStyleHelper LineStyle { get; set; } = DashStyleHelper.Dot;
private TimeZoneInfo chartTimeZone;
private TimeZoneInfo nyseTimeZone;
private TimeZoneInfo londonTimeZone;
private DateTime lastProcessedDate = DateTime.MinValue;
private DateTime lastUpdateTime = DateTime.MinValue;
protected override void OnStateChange()
{
Print($"Debug SO: Entered state: {State}");
if (State == State.SetDefaults)
{
Description = @"Indicator to draw NYSE cash session open.";
Name = "AAA_SessionOpen";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = false;
DrawOnPricePanel = true;
PaintPriceMarkers = false;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
IsSuspendedWhileInactive = false;
}
else if (State == State.Configure)
{
// Initialize timezone info
try
{
chartTimeZone = TimeZoneInfo.FindSystemTimeZoneById(ChartTimeZoneId);
nyseTimeZone = TimeZoneInfo.FindSystemTimeZoneById(NyseTimeZoneId);
londonTimeZone = TimeZoneInfo.FindSystemTimeZoneById(LondonTimeZoneId);
}
catch (Exception ex)
{
//Print($"Debug SO: Error initializing timezones: {ex.Message}");
// Set to default timezones if there's an error
chartTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
nyseTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
londonTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
}
}
}
protected override void OnBarUpdate()
{
bool lastBarOnChart = IsFirstTickOfBar && (CurrentBar + (Calculate == Calculate.OnEachTick ? 0 : 1) >= ChartBars.ToIndex);
if (!lastBarOnChart)
return;
// Only process on the last bar to minimize resource usage
if (CurrentBar < BarsRequiredToPlot)
return;
DateTime currentBarDate = Bars.GetTime(CurrentBar).Date;
// Update at most once per hour or when the date changes
if ((DateTime.Now - lastUpdateTime).TotalHours >= 1 || currentBarDate != lastProcessedDate)
{
DrawSessionOpenLineIfNeeded("US", USSessionOpenTime, nyseTimeZone, currentBarDate);
DrawSessionOpenLineIfNeeded("EU", EUSessionOpenTime, londonTimeZone, currentBarDate);
lastProcessedDate = currentBarDate;
lastUpdateTime = DateTime.Now;
}
}
protected void DrawSessionOpenLineIfNeeded(string tag, TimeSpan sessionOpenTime, TimeZoneInfo timezone, DateTime date)
{
// Define a unique key for today's session open line to avoid duplication
string sessionOpenLineTag = $"SessionOpenTime_{tag}_{date.ToString("yyyyMMdd")}";
// Check if the line has already been drawn by looking for an existing object with the same tag
if (!DrawObjects.Any(d => d.Tag.Equals(sessionOpenLineTag)))
{
// Get the session open date time for the specified date
DateTime sessionOpenInChartTimezone = GetSessionInChartTimezone(sessionOpenTime, timezone, date);
Print($"Debug SO: drawing session open at {sessionOpenInChartTimezone}");
DrawVerticalLineWithTransparency(sessionOpenLineTag, sessionOpenInChartTimezone, LineColor, LineTransparency, LineStyle, LineThickness);
}
}
private DateTime GetSessionInChartTimezone(TimeSpan sessionOpenTime, TimeZoneInfo timezone, DateTime date)
{
DateTime openTimeInSessionTimezone = date.Add(sessionOpenTime);
DateTime openTimeUtc = TimeZoneInfo.ConvertTimeToUtc(openTimeInSessionTimezone, timezone);
return TimeZoneInfo.ConvertTimeFromUtc(openTimeUtc, chartTimeZone);
}
private SolidColorBrush CreateTransparentBrush(Brush lineColor, int lineTransparency)
{
byte alpha = (byte)(lineTransparency * 255 / 100);
var color = ((SolidColorBrush)lineColor).Color;
return new SolidColorBrush(Color.FromArgb(alpha, color.R, color.G, color.B));
}
private void DrawVerticalLineWithTransparency(string tag, DateTime dateTime, Brush lineColor, int lineTransparency, DashStyleHelper lineStyle, int lineThickness)
{
SolidColorBrush transparentBrush = CreateTransparentBrush(lineColor, lineTransparency);
VerticalLine verticalLine = Draw.VerticalLine(this, tag, dateTime, transparentBrush, lineStyle, lineThickness) as VerticalLine;
if (verticalLine != null)
{
verticalLine.Stroke = new Stroke(transparentBrush, lineStyle, lineThickness);
}
}
}
}