How to get open session time?

Created at 14 May 2020, 18:34
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!
Kbee's avatar

Kbee

Joined 28.03.2020

How to get open session time?
14 May 2020, 18:34


I want to build a Pivot Points Indicator for my cBot. But I'm stuck at the opening time. I tried with regular times 21:00 & 22:00 UTC and it works well. However, rarely the opening time opens at 12:00 UTC, and my code bugs out. So, is there a better way to do this or is there a way to get the opening time from the server itself? Thank you.


My Code:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Runtime.CompilerServices;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PivotPointsIndicator : Indicator
    {
        [Parameter("Pivot Types", Group = "User Defines", DefaultValue = PivotT.Standard)]
        public PivotT Type { get; set; }

        [Parameter("Show Texts", Group = "User Defines", DefaultValue = true)]
        public bool ShowText { get; set; }

        public enum PivotT
        {
            Standard,
            Fibonacci,
            Woodie,
            Camarilla
        }

        [Output("Pivot Line", LineColor = "White")]
        public IndicatorDataSeries Pivot { get; set; }

        [Output("Resist 1", LineColor = "LightGreen")]
        public IndicatorDataSeries Resist1 { get; set; }

        [Output("Resist 2", LineColor = "LightGreen")]
        public IndicatorDataSeries Resist2 { get; set; }

        [Output("Resist 3", LineColor = "LightGreen")]
        public IndicatorDataSeries Resist3 { get; set; }

        [Output("Resist 4", LineColor = "LightGreen")]
        public IndicatorDataSeries Resist4 { get; set; }

        [Output("Resist 5", LineColor = "LightGreen")]
        public IndicatorDataSeries Resist5 { get; set; }

        [Output("Support 1", LineColor = "OrangeRed")]
        public IndicatorDataSeries Support1 { get; set; }

        [Output("Support 2", LineColor = "OrangeRed")]
        public IndicatorDataSeries Support2 { get; set; }

        [Output("Support 3", LineColor = "OrangeRed")]
        public IndicatorDataSeries Support3 { get; set; }

        [Output("Support 4", LineColor = "OrangeRed")]
        public IndicatorDataSeries Support4 { get; set; }

        [Output("Support 5", LineColor = "OrangeRed")]
        public IndicatorDataSeries Support5 { get; set; }

        double Low = 0;
        double High = 0;
        double Close = 0;

        int AddTime = 0;
        int TotalBars = 0;
        int NextIndex = 0;

        DateTime LastCloseDate;

        readonly int NYCloseTime = 21;

        public override void Calculate(int index)
        {
            if (TimeFrame >= TimeFrame.Daily || TimeFrame.ToString().Contains("Tick"))
                return;

            var OpenTime = Bars.OpenTimes.LastValue;

            if (LastCloseDate.Day != OpenTime.Day)
            {
                if (OpenTime.Hour == NYCloseTime)
                    AddTime = 0;
                else if (OpenTime.Hour == NYCloseTime + 1)
                    AddTime = 1;
            }

            LastCloseDate = OpenTime;

            if (OpenTime.Hour == NYCloseTime + AddTime && OpenTime.Minute <= 1)
            {
                for (int i = index; i <= index + TotalBars; i++)
                {
                    switch (Type)
                    {
                        case PivotT.Standard:
                            var Range = High - Low;
                            var P = (Low + High + Close) / 3;
                            var R1 = (2 * P) - Low;
                            var R2 = P + Range;
                            var R3 = High + (2 * (P - Low));
                            var S1 = (2 * P) - High;
                            var S2 = P - Range;
                            var S3 = Low - (2 * (High - P));
                            Pivot[i] = P;
                            Resist1[i] = R1;
                            Resist2[i] = R2;
                            Resist3[i] = R3;
                            Support1[i] = S1;
                            Support2[i] = S2;
                            Support3[i] = S3;
                            break;
                        case PivotT.Fibonacci:
                            Range = High - Low;
                            P = (Low + High + Close) / 3;
                            R1 = P + (Range * 0.382);
                            R2 = P + (Range * 0.618);
                            R3 = P + (Range * 1.0);
                            S1 = P - (Range * 0.382);
                            S2 = P - (Range * 0.618);
                            S3 = P - (Range * 1.0);
                            Pivot[i] = P;
                            Resist1[i] = R1;
                            Resist2[i] = R2;
                            Resist3[i] = R3;
                            Support1[i] = S1;
                            Support2[i] = S2;
                            Support3[i] = S3;
                            break;
                        case PivotT.Woodie:
                            Range = High - Low;
                            P = (High + Low + (2 * Close)) / 4;
                            R1 = (2 * P) - Low;
                            R2 = P + (High - Low);
                            R3 = High + (2 * (P - Low));
                            var R4 = R3 + Range;
                            S1 = (2 * P) - High;
                            S2 = P - Range;
                            S3 = Low - (2 * (High - P));
                            var S4 = S3 - Range;
                            Pivot[i] = P;
                            Resist1[i] = R1;
                            Resist2[i] = R2;
                            Resist3[i] = R3;
                            Resist4[i] = R4;
                            Support1[i] = S1;
                            Support2[i] = S2;
                            Support3[i] = S3;
                            Support4[i] = S4;
                            break;
                        case PivotT.Camarilla:
                            Range = High - Low;
                            P = (High + Low + Close) / 3;
                            R1 = Close + (Range * (1.1 / 12));
                            R2 = Close + (Range * (1.1 / 6));
                            R3 = Close + (Range * (1.1 / 4));
                            R4 = Close + (Range * (1.1 / 2));
                            var R5 = (High / Low) * Close;
                            S1 = Close - (Range * (1.1 / 12));
                            S2 = Close - (Range * (1.1 / 6));
                            S3 = Close - (Range * (1.1 / 4));
                            S4 = Close - (Range * (1.1 / 2));
                            var S5 = Close - (R5 - Close);
                            Pivot[i] = P;
                            Resist1[i] = R1;
                            Resist2[i] = R2;
                            Resist3[i] = R3;
                            Resist4[i] = R4;
                            Resist5[i] = R5;
                            Support1[i] = S1;
                            Support2[i] = S2;
                            Support3[i] = S3;
                            Support4[i] = S4;
                            Support5[i] = S5;
                            break;
                    }
                }

                TotalBars = 0;
                Low = Bars.LowPrices.LastValue;
                High = Bars.HighPrices.LastValue;
            }

            Close = Bars.ClosePrices.LastValue;
            High = Math.Max(High, Bars.HighPrices.LastValue);
            Low = Low != 0 ? Math.Min(Low, Bars.LowPrices.LastValue) : Bars.LowPrices.LastValue;

            if (NextIndex == index)
            {
                TotalBars++;
                NextIndex = index + 1;
            }
        }
    }
}


@Kbee
Replies

PanagiotisCharalampous
15 May 2020, 08:27

Hi Watanaku,

You can try getting the Daily bars and read the open time from there.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

Kbee
15 May 2020, 15:26

RE:

PanagiotisCharalampous said:

Hi Watanaku,

You can try getting the Daily bars and read the open time from there.

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you for the suggestion sir

Best Regards,


@Kbee