Untitled
unknown
plain_text
4 months ago
7.1 kB
3
Indexable
using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using System; using static System.Net.Mime.MediaTypeNames; using System.Drawing; using System.Xml.Linq; namespace cAlgo { [Indicator(IsOverlay = true, TimeZone = TimeZones.CentralEuropeStandardTime, AccessRights = AccessRights.None)] public class NewDayOpening : Indicator { [Parameter("Daily Line Color", DefaultValue = "DodgerBlue")] public string DailyLineColor { get; set; } [Parameter("Previous Day Close Line Color", DefaultValue = "DodgerBlue")] public string PreviousDayCloseLineColor { get; set; } [Parameter("Weekly Line Color", DefaultValue = "Green")] public string WeeklyLineColor { get; set; } [Parameter("Show NDOG", DefaultValue = true)] public bool IsShowNDOG { get; set; } [Parameter("Line Thickness", DefaultValue = 2)] public int LineThickness { get; set; } [Parameter("Line Style", DefaultValue = LineStyle.Solid)] public LineStyle LineStyle { get; set; } private MarketSeries _dailySeries; private MarketSeries _weeklySeries; protected override void Initialize() { // Load daily and weekly timeframe series _dailySeries = MarketData.GetSeries(TimeFrame.Daily); _weeklySeries = MarketData.GetSeries(TimeFrame.Weekly); } public override void Calculate(int index) { var currentBarTime = MarketSeries.OpenTime[index]; // Draw Daily Opening Line DrawDailyOpeningLine(index, currentBarTime); if (IsShowNDOG) { // Draw Previous Day Close Line DrawNdogLine(index, currentBarTime); } // Draw Weekly Opening Line DrawWeeklyOpeningLine(index, currentBarTime); } private void DrawDailyOpeningLine(int index, DateTime currentBarTime) { // Get the current day var currentDay = currentBarTime.Date; // Find the daily open price int dailyIndex = _dailySeries.OpenTime.GetIndexByTime(currentDay); if (dailyIndex < 0) return; double dailyOpenPrice = _dailySeries.Open[dailyIndex]; // Find the first and last bar indices for the current day int firstBarIndex = GetFirstBarIndexOfDay(currentDay); int lastBarIndex = index; // Draw a horizontal trendline for the daily open price Chart.DrawTrendLine( $"DailyOpen_{currentDay}", MarketSeries.OpenTime[firstBarIndex], dailyOpenPrice, MarketSeries.OpenTime[lastBarIndex], dailyOpenPrice, DailyLineColor, LineThickness, LineStyle ); } private void DrawNdogLine(int index, DateTime currentBarTime) { // Get the previous day var previousDay = currentBarTime.Date.AddDays(-1); var currentDay = currentBarTime.Date; int firstBarIndex = GetFirstBarIndexOfDay(currentDay); int lastBarIndex = index; // Find the close price of the previous day int previousDayIndex = _dailySeries.OpenTime.GetIndexByTime(previousDay); int dailyIndex = _dailySeries.OpenTime.GetIndexByTime(currentDay); if (dailyIndex < 0) return; if (previousDayIndex < 0) return; double previousDayClosePrice = _dailySeries.Close[previousDayIndex]; double dailyOpenPrice = _dailySeries.Open[dailyIndex]; double midPrice = (dailyOpenPrice + previousDayClosePrice) / 2; // Draw a horizontal trendline for the previous day's close price Chart.DrawTrendLine( $"PreviousDayClose_{previousDay}", MarketSeries.OpenTime[firstBarIndex], // From the start of the chart previousDayClosePrice, MarketSeries.OpenTime[lastBarIndex], // To the current index previousDayClosePrice, PreviousDayCloseLineColor, LineThickness, LineStyle ); Chart.DrawTrendLine( $"Mid_NDOG_{previousDay}", MarketSeries.OpenTime[firstBarIndex], // From the start of the chart midPrice, MarketSeries.OpenTime[lastBarIndex], // To the current index midPrice, PreviousDayCloseLineColor, LineThickness, LineStyle.DotsVeryRare ); // Chart.DrawText($"PreviousDayCloseLabel_{previousDay}", "prev day close", MarketSeries.OpenTime[(int)(firstBarIndex + lastBarIndex) / 2], previousDayClosePrice, PreviousDayCloseLineColor); } private void DrawWeeklyOpeningLine(int index, DateTime currentBarTime) { // Get the start of the current week (Monday) var currentWeekStart = StartOfWeek(currentBarTime); // Find the weekly open price int weeklyIndex = _weeklySeries.OpenTime.GetIndexByTime(currentWeekStart); if (weeklyIndex < 0) return; double weeklyOpenPrice = _weeklySeries.Open[weeklyIndex]; // Find the first and last bar indices for the current week int firstBarIndex = GetFirstBarIndexOfWeek(currentWeekStart); int lastBarIndex = index; // Draw a horizontal trendline for the weekly open price Chart.DrawTrendLine( $"WeeklyOpen_{currentWeekStart}", MarketSeries.OpenTime[firstBarIndex], weeklyOpenPrice, MarketSeries.OpenTime[lastBarIndex], weeklyOpenPrice, WeeklyLineColor, LineThickness, LineStyle ); } private int GetFirstBarIndexOfDay(DateTime day) { for (int i = 0; i < MarketSeries.OpenTime.Count; i++) { if (MarketSeries.OpenTime[i].Date == day) return i; } return 0; // Default to the first index if not found } private int GetFirstBarIndexOfWeek(DateTime weekStart) { for (int i = 0; i < MarketSeries.OpenTime.Count; i++) { if (MarketSeries.OpenTime[i].Date >= weekStart) return i; } return 0; // Default to the first index if not found } private DateTime StartOfWeek(DateTime date) { int diff = date.DayOfWeek - DayOfWeek.Monday; if (diff < 0) diff += 7; // Adjust for Sunday as the last day of the week return date.AddDays(-diff).Date; } } }
Editor is loading...
Leave a Comment