Untitled

 avatar
unknown
plain_text
5 months ago
4.7 kB
3
Indexable
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class Us30BreakerBox : Indicator
    {
        [Parameter("Start Time (Hour)", DefaultValue = 19)]
        public int StartHour { get; set; }

        [Parameter("Start Time (Minute)", DefaultValue = 0)]
        public int StartMinute { get; set; }

        [Parameter("End Time (Hour)", DefaultValue = 23)]
        public int EndHour { get; set; }

        [Parameter("End Time (Minute)", DefaultValue = 59)]
        public int EndMinute { get; set; }

        [Parameter("UTC Shift", DefaultValue = -5)]
        public int UtcShift { get; set; }

        private DateTime _sessionStart, _sessionEnd;
        private double _sessionHigh, _sessionLow;
        private string _currentBoxName;
        private string _currentPipLabelName;

        protected override void Initialize()
        {
            _sessionHigh = double.MinValue;
            _sessionLow = double.MaxValue;
            UpdateSessionTimes(MarketSeries.OpenTime.LastValue);
        }

        public override void Calculate(int index)
        {
            var barTime = MarketSeries.OpenTime[index];

            // Check if we need to reset the box for a new day
            if (barTime.AddHours(UtcShift).Date != _sessionStart.Date)
            {
                // Finalize and draw the box for the previous day
                if (_sessionHigh > double.MinValue && _sessionLow < double.MaxValue)
                {
                    DrawBox(_sessionStart, _sessionEnd, _sessionHigh, _sessionLow);
                }

                // Reset for the new day
                UpdateSessionTimes(barTime);
                _sessionHigh = double.MinValue;
                _sessionLow = double.MaxValue;
            }

            // Update session high and low during the session
            if (barTime >= _sessionStart && barTime <= _sessionEnd)
            {
                _sessionHigh = Math.Max(_sessionHigh, MarketSeries.High[index]);
                _sessionLow = Math.Min(_sessionLow, MarketSeries.Low[index]);

                // Dynamically draw or update the box for the current session
                DrawCurrentSessionBox(_sessionStart, barTime, _sessionHigh, _sessionLow);
            }
        }

        private void UpdateSessionTimes(DateTime currentTime)
        {
            _sessionStart = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, StartHour - UtcShift, StartMinute, 0, DateTimeKind.Utc);

            if (StartHour > EndHour || (StartHour == EndHour && StartMinute > EndMinute))
            {
                _sessionEnd = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, EndHour - UtcShift, EndMinute, 0, DateTimeKind.Utc).AddDays(1);
            }
            else
            {
                _sessionEnd = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, EndHour - UtcShift, EndMinute, 0, DateTimeKind.Utc);
            }
        }

        private void DrawBox(DateTime startTime, DateTime endTime, double high, double low)
        {
            string boxName = $"SessionBox_{startTime:yyyyMMdd}";
            string pipLabelName = $"PipLabel_{startTime:yyyyMMdd}";

            Chart.RemoveObject(boxName);
            Chart.RemoveObject(pipLabelName);

            var rectangle = Chart.DrawRectangle(boxName, startTime, high, endTime, low, Color.Yellow);
            rectangle.IsFilled = false;

            // Calculate pips and display the value on the chart
            double pipDifference = (high - low) / Symbol.PipSize;
            Chart.DrawText(pipLabelName, $"{pipDifference:F1} pips", startTime, high, Color.Yellow);
        }

        private void DrawCurrentSessionBox(DateTime startTime, DateTime currentTime, double high, double low)
        {
            _currentBoxName = $"CurrentSessionBox";
            _currentPipLabelName = $"CurrentPipLabel";

            Chart.RemoveObject(_currentBoxName);
            Chart.RemoveObject(_currentPipLabelName);

            var rectangle = Chart.DrawRectangle(_currentBoxName, startTime, high, currentTime, low, Color.Green);
            rectangle.IsFilled = false;

            // Calculate pips and display the value on the chart
            double pipDifference = (high - low) / Symbol.PipSize;
            Chart.DrawText(_currentPipLabelName, $"{pipDifference:F1} pips", startTime, high, Color.Green);
        }
    }
}
Editor is loading...
Leave a Comment