Untitled

 avatar
unknown
plain_text
4 months ago
2.3 kB
1
Indexable
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.CentralEuropeStandardTime, AccessRights = AccessRights.None)]
    public class NewDayOpening : Indicator
    {
        [Parameter("Line Color", DefaultValue = "DodgerBlue")]
        public string LineColor { 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;

        protected override void Initialize()
        {
            // Load the daily timeframe series
            _dailySeries = MarketData.GetSeries(TimeFrame.Daily);
        }

        public override void Calculate(int index)
        {
            // Get the current bar's date
            var currentDay = MarketSeries.OpenTime[index].Date;

            // Find the daily open price
            int dailyIndex = _dailySeries.OpenTime.GetIndexByTime(currentDay);
            if (dailyIndex < 0) return; // Ensure the index exists in the daily series

            double dailyOpenPrice = _dailySeries.Open[dailyIndex];

            // Find the first and last bar indices for the current day in the current timeframe
            int firstBarIndex = GetFirstBarIndexOfDay(currentDay);
            int lastBarIndex = index;

            // Draw a trendline for the daily open price, limited to the range of the day
            Chart.DrawTrendLine(
                $"DailyOpen_{currentDay}",
                MarketSeries.OpenTime[firstBarIndex],
                dailyOpenPrice,
                MarketSeries.OpenTime[lastBarIndex],
                dailyOpenPrice,
                LineColor,
                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
        }
    }
}
Editor is loading...
Leave a Comment