Category Other  Published on 10/03/2021

TrueSessionBox

Description

You can easy understand your upcoming sessions.


// -------------------------------------------------------------------------------------------------
// Develop By Md shariful Hasan Sohag
//    
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrueSessionBox : Indicator
    {

        [Parameter(DefaultValue = "22:00")]
        public string asia { get; set; }
        [Parameter(DefaultValue = 24)]
        public int asiaHour { get; set; }
        [Parameter(DefaultValue = "01:00")]
        public string euro { get; set; }
        [Parameter(DefaultValue = 4)]
        public int euroHour { get; set; }
        [Parameter(DefaultValue = "05:00")]
        public string us { get; set; }
        [Parameter(DefaultValue = 4)]
        public int usHour { get; set; }

        [Parameter(DefaultValue = "09:00")]
        public string asia1 { get; set; }
        [Parameter(DefaultValue = 4)]
        public int asiaHour1 { get; set; }
        [Parameter(DefaultValue = "13:00")]
        public string euro1 { get; set; }
        [Parameter(DefaultValue = 4)]
        public int euroHour1 { get; set; }
        [Parameter(DefaultValue = "17:00")]
        public string us1 { get; set; }
        [Parameter(DefaultValue = 3)]
        public int usHour1 { get; set; }

        // store last Session Start and Session End
        public DateTime asStart;
        public DateTime asEnd;
        public DateTime euStart;
        public DateTime euEnd;
        public DateTime usStart;
        public DateTime usEnd;
        public DateTime asStart1;
        public DateTime asEnd1;
        public DateTime euStart1;
        public DateTime euEnd1;
        public DateTime usStart1;
        public DateTime usEnd1;

        protected override void Initialize()
        {
            // Initialize and create nested indicators
        }

        public override void Calculate(int index)
        {
            var dateTime = MarketSeries.OpenTime[index];
            List<Box> boxs = sbox(index);
            for (int i = 0; i < boxs.Count; i++)
            {
                var box = boxs[i];
                double[] high_low = boxHighLow(index, box);
                drawBox(box.label, box.left, box.right, high_low[0], high_low[1], box.clr);
            }

        }

        // box calcuate logic
        public List<Box> sbox(int index)
        {
            List<Box> boxs = new List<Box>();
            DateTime current = MarketSeries.OpenTime[index];
            string asStartHour = asia.Split('-')[0].Split(':')[0];
            string asStartMinute = asia.Split('-')[0].Split(':')[1];

            string euroStartHour = euro.Split('-')[0].Split(':')[0];
            string euroStartMinute = euro.Split('-')[0].Split(':')[1];

            string usStartHour = us.Split('-')[0].Split(':')[0];
            string usStartMinute = us.Split('-')[0].Split(':')[1];

            string asStartHour1 = asia1.Split('-')[0].Split(':')[0];
            string asStartMinute1 = asia1.Split('-')[0].Split(':')[1];

            string euroStartHour1 = euro1.Split('-')[0].Split(':')[0];
            string euroStartMinute1 = euro1.Split('-')[0].Split(':')[1];

            string usStartHour1 = us1.Split('-')[0].Split(':')[0];
            string usStartMinute1 = us1.Split('-')[0].Split(':')[1];

            if (current.Hour == Int32.Parse(asStartHour) && current.Minute == Int32.Parse(asStartMinute))
            {
                asStart = current;
                asEnd = current.AddHours(asiaHour);
            }
            if (current.Hour == Int32.Parse(euroStartHour) && current.Minute == Int32.Parse(euroStartMinute))
            {
                euStart = current;
                euEnd = current.AddHours(euroHour);
            }
            if (current.Hour == Int32.Parse(usStartHour) && current.Minute == Int32.Parse(usStartMinute))
            {
                usStart = current;
                usEnd = current.AddHours(usHour);
            }

            if (current >= asStart && current <= asEnd)
            {
                boxs.Add(new Box(asStart.ToString(), asStart, asEnd, Colors.Red));
            }

            if (current >= euStart && current <= euEnd)
            {

                boxs.Add(new Box(euStart.ToString(), euStart, euEnd, Colors.Green));
            }
            if (current >= usStart && current <= usEnd)
            {
                boxs.Add(new Box(usStart.ToString(), usStart, usEnd, Colors.Blue));
            }

            //customization
            if (current.Hour == Int32.Parse(asStartHour1) && current.Minute == Int32.Parse(asStartMinute1))
            {
                asStart1 = current;
                asEnd1 = current.AddHours(asiaHour1);
            }
            if (current.Hour == Int32.Parse(euroStartHour1) && current.Minute == Int32.Parse(euroStartMinute1))
            {
                euStart1 = current;
                euEnd1 = current.AddHours(euroHour1);
            }
            if (current.Hour == Int32.Parse(usStartHour1) && current.Minute == Int32.Parse(usStartMinute1))
            {
                usStart1 = current;
                usEnd1 = current.AddHours(usHour1);
            }

            if (current >= asStart1 && current <= asEnd1)
            {
                boxs.Add(new Box(asStart1.ToString(), asStart1, asEnd1, Colors.Black));
            }

            if (current >= euStart1 && current <= euEnd1)
            {

                boxs.Add(new Box(euStart1.ToString(), euStart1, euEnd1, Colors.Green));
            }
            if (current >= usStart1 && current <= usEnd1)
            {
                boxs.Add(new Box(usStart1.ToString(), usStart1, usEnd1, Colors.Yellow));
            }
            //endcustomization
            return boxs;



        }

        // calculate session High Low
        private double[] boxHighLow(int index, Box box)
        {
            DateTime left = box.left;
            double[] high_low = new double[2] 
            {
                MarketSeries.High[index],
                MarketSeries.Low[index]
            };
            while (MarketSeries.OpenTime[index] >= left)
            {
                high_low[0] = Math.Max(high_low[0], MarketSeries.High[index]);
                high_low[1] = Math.Min(high_low[1], MarketSeries.Low[index]);
                index--;
            }
            return high_low;
        }

        // draw session box
        private void drawBox(String label, DateTime left, DateTime right, Double high, Double low, Colors clr)
        {
            ChartObjects.DrawLine(label + "_low", left, low, right, low, clr);
            ChartObjects.DrawLine(label + "_high", left, high, right, high, clr);
            ChartObjects.DrawLine(label + "_left", left, high, left, low, clr);
            ChartObjects.DrawLine(label + "_right", right, high, right, low, clr);

        }

        // box data struct
        public struct Box
        {
            public string label;
            public DateTime left;
            public DateTime right;
            public Colors clr;

            public Box(string label, DateTime left, DateTime right, Colors clr)
            {
                this.label = label;
                this.left = left;
                this.right = right;
                this.clr = clr;
            }
        }
    }
}


shsohag041's avatar
shsohag041

Joined on 10.03.2021

  • Distribution: Paid
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: TrueSessionBox.algo
  • Rating: 0
  • Installs: 1692
Comments
Log in to add a comment.
No comments found.