Calculate High and Low between 08:00:00 and 09:00:00

Created at 06 Jul 2023, 01:29
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
RD

rdebacker

Joined 06.07.2023

Calculate High and Low between 08:00:00 and 09:00:00
06 Jul 2023, 01:29


Hello,

 

I am new to creating indicators in C# and cTrader API.

 

I tried several thing i found here but no success yet.

 

What i am trying to accomplish is calculate the high and low between 08:00:00 and 09:00:00 in my time zone, and plot the resulting values as two horizontal lines after 09:00:00 for the complete rest of the day on the chart.

 

I appreciate any help.

Thanks in advance

 


@rdebacker
Replies

firemyst
07 Jul 2023, 03:24

Why not post the code you have so people can correct it?

 

In a nutshell:

1) set your time zone in your indicator if you choose:

    [Indicator(AccessRights = AccessRights.None, TimeZone = TimeZones.AtlanticStandardTime)]

2) get your time Server.Time or if you want your local computer's DateTime.Now

3) compare that the hour of the time you got is >= 8 && <= 9

4) if 3 is true, Chart.DrawHorizontalLine on whatever value you want at 8 and 9am prices. Depending on the chart you're using, you may need to get tick data as opposed to the chart's time frame (eg, if you use something like Range or Renko, you can't depend on a bar's time as a new bar may not open/close on the exact hours)


@firemyst

rdebacker
22 Jul 2023, 21:59 ( Updated at: 21 Dec 2023, 09:23 )

I took an example from the forum to show the developing high/low for the day for number of days.

I made some adjustments to have only the high/low between8 and 9 AM and after that the 2 lines have to remain flat until the end off the day.

The adjustments do not produce an error but the result stays the same.

  • changes from Original =
  • drop seperatorline stuff
  • add    var dayEndBarIndex = Bars.OpenTimes.GetIndexByTime(dayEndTime);
     
  • for :   for (var iBarIndex = dayStartBarIndex; iBarIndex <= dayEndBarIndex; iBarIndex++)

 

Does anybody have an idea what i am doing wrong?

using cAlgo.API;
using System;
using System.Globalization;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.RomanceStandardTime, AccessRights = AccessRights.FullAccess)]
    public class DailyHighLowSample : Indicator
    {
        private readonly string _name = "Daily High Low";

        private string _chartObjectNamesSuffix;

        private int _lastBarIndex;

        private TimeSpan _dayStartTime;
        private TimeSpan _dayEndTime;

        [Parameter("Use Bar Bodies", DefaultValue = false, Group = "General")]
        public bool UseBarBodies { get; set; }

        [Parameter("Day Start Time", DefaultValue = "08:00:00", Group = "General")]
        public string DayStartTime { get; set; }
        
        [Parameter("Day End Time", DefaultValue = "09:00:00", Group = "General")]
        public string DayEndTime { get; set; }
          
        [Parameter("Days #", DefaultValue = 5, MinValue = 0, Group = "General")]
        public int DaysNumber { get; set; }  

        [Output("High", LineColor = "Lime", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries High { get; set; }

        [Output("Low", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Low { get; set; }

        protected override void Initialize()

        
       

            _chartObjectNamesSuffix = string.Format("{0}_{1}", _name, DateTime.Now.Ticks);

            if (!TimeSpan.TryParse(DayStartTime, CultureInfo.InvariantCulture, out _dayStartTime))
            {
                Print("Your provided value for 'Day Start Time' parameter is not valid");
            }
            
            if (!TimeSpan.TryParse(DayEndTime, CultureInfo.InvariantCulture, out _dayEndTime))
            {
                Print("Your provided value for 'Day End Time' parameter is not valid");
            }

            Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;
        }

        public override void Calculate(int index)
        {
            if (index == _lastBarIndex)
            {
                return;
            }

            _lastBarIndex = index;

            //var currentBarTime = Bars.OpenTimes[index].Add(Application.UserTimeOffset);
            var currentBarTime = Bars.OpenTimes[index];
            var dayStartTime = currentBarTime.TimeOfDay >= _dayStartTime ? currentBarTime.Date.Add(_dayStartTime) : currentBarTime.Date.AddDays(-1).Add(_dayStartTime);
            var dayEndTime = currentBarTime.TimeOfDay <= _dayEndTime ? currentBarTime.Date.Add(_dayEndTime) : currentBarTime.Date.AddDays(-1).Add(_dayEndTime);
            
            //dayStartTime = dayStartTime.Add(-Application.UserTimeOffset);
            //dayEndTime = dayEndTime.Add(-Application.UserTimeOffset);

            if (DaysNumber > 0)
            {
                //var currentBarTimeDiffWithToday = Server.Time.Add(Application.UserTimeOffset) - dayStartTime;
                //var currentBarTimeDiffWithTodayEnd = Server.Time.Add(Application.UserTimeOffset) - dayEndTime;

                var currentBarTimeDiffWithToday = Server.Time - dayStartTime;
                var currentBarTimeDiffWithTodayEnd = Server.Time - dayEndTime;
                if (currentBarTimeDiffWithToday.TotalDays > DaysNumber)
                {
                    return;
                }
            }

        
            var dayStartBarIndex = Bars.OpenTimes.GetIndexByTime(dayStartTime);
            var dayEndBarIndex = Bars.OpenTimes.GetIndexByTime(dayEndTime);

            var dayHigh = double.MinValue;
            var dayLow = double.MaxValue;
            
           //  for (var iBarIndex = dayStartBarIndex; iBarIndex <= index; iBarIndex++)

            for (var iBarIndex = dayStartBarIndex; iBarIndex <= dayEndBarIndex; iBarIndex++)
            {
                if (UseBarBodies)
                {
                    if (Bars[iBarIndex].Close > Bars[iBarIndex].Open)
                    {
                        dayHigh = Math.Max(Bars.ClosePrices[iBarIndex], dayHigh);
                        dayLow = Math.Min(Bars.OpenPrices[iBarIndex], dayLow);
                    }
                    else
                    {
                        dayHigh = Math.Max(Bars.OpenPrices[iBarIndex], dayHigh);
                        dayLow = Math.Min(Bars.ClosePrices[iBarIndex], dayLow);
                    }
                }
                else
                {
                    dayHigh = Math.Max(Bars.HighPrices[iBarIndex], dayHigh);
                    dayLow = Math.Min(Bars.LowPrices[iBarIndex], dayLow);
                }
            }

            High[index] = dayHigh;
            Low[index] = dayLow;
        }

        public void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
        {

            _lastBarIndex = -1;

            //for (var iBarIndex = 0; iBarIndex < Bars.Count; iBarIndex++)
            for (var iBarIndex = dayStartBarIndex; iBarIndex < Bars.Count; iBarIndex++)
{
                High[iBarIndex] = double.NaN;
                Low[iBarIndex] = double.NaN;

                Calculate(iBarIndex);
            }
        }
    }
}

 

 


@rdebacker