How to get high and low prices in specified time(bar) range

Created at 28 Sep 2021, 20:07
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!
V.

v.fiodorov83

Joined 24.07.2019

How to get high and low prices in specified time(bar) range
28 Sep 2021, 20:07


Hello dear developers!

How can I write a code that will write the maximum and minimum values of the range 01:00 - 07:59 (or 8 hour bars) to the RangeHigh and RangeLow variables, so that I can then use these variables to open positions during the day from 08:00 to 23:59 or before the market closes. At the beginning of a new day, the variables are reset to zero and receive new values at 08:00, and so on every day


@v.fiodorov83
Replies

amusleh
29 Sep 2021, 10:18

Hi,

Try this:

using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar) return;

            // Returns the min/max of last 10 bars
            var minMax = GetMinMax(Bars.OpenTimes[index - 10], Bars.OpenTimes[index]);

            Print("Min: {0} | Max: {1}", minMax.Item1, minMax.Item2);
        }

        /// <summary>
        /// Returns the minimum/maximum prices levels during an specific time period
        /// </summary>
        /// <param name="startTime">Start Time (Inclusive)</param>
        /// <param name="endTime">End Time (Inclusive)</param>
        /// <returns>Tuple<double, double> (Item1 will be minimum price and Item2 will be maximum price)</returns>
        private Tuple<double, double> GetMinMax(DateTime startTime, DateTime endTime)
        {
            var min = double.MaxValue;
            var max = double.MinValue;

            for (int barIndex = 0; barIndex < Bars.Count; barIndex++)
            {
                var bar = Bars[barIndex];

                if (bar.OpenTime < startTime || bar.OpenTime > endTime)
                {
                    if (bar.OpenTime > endTime) break;

                    continue;
                }

                min = Math.Min(min, bar.Low);
                max = Math.Max(max, bar.High);
            }

            return new Tuple<double, double>(min, max);
        }
    }
}

You have the full historical bars data of your chart time frame, and you can iterate over it to find highs/lows.


@amusleh

v.fiodorov83
29 Sep 2021, 14:19 ( Updated at: 29 Sep 2021, 17:13 )

RE:

amusleh said:

Hi,

Try this:

using System;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar) return;

            // Returns the min/max of last 10 bars
            var minMax = GetMinMax(Bars.OpenTimes[index - 10], Bars.OpenTimes[index]);

            Print("Min: {0} | Max: {1}", minMax.Item1, minMax.Item2);
        }

        /// <summary>
        /// Returns the minimum/maximum prices levels during an specific time period
        /// </summary>
        /// <param name="startTime">Start Time (Inclusive)</param>
        /// <param name="endTime">End Time (Inclusive)</param>
        /// <returns>Tuple<double, double> (Item1 will be minimum price and Item2 will be maximum price)</returns>
        private Tuple<double, double> GetMinMax(DateTime startTime, DateTime endTime)
        {
            var min = double.MaxValue;
            var max = double.MinValue;

            for (int barIndex = 0; barIndex < Bars.Count; barIndex++)
            {
                var bar = Bars[barIndex];

                if (bar.OpenTime < startTime || bar.OpenTime > endTime)
                {
                    if (bar.OpenTime > endTime) break;

                    continue;
                }

                min = Math.Min(min, bar.Low);
                max = Math.Max(max, bar.High);
            }

            return new Tuple<double, double>(min, max);
        }
    }
}

You have the full historical bars data of your chart time frame, and you can iterate over it to find highs/lows.

Thanks for your example. But it looks like an indicator. How can I use this class or data type MyIndicator in the body of my cBot? For example - use the obtained min, max values for opening positions from these levels?


@v.fiodorov83

amusleh
30 Sep 2021, 11:21

Hi,

You have to copy the method GetMinMax to your cBot and use it the same way I did in above indicator.


@amusleh