Session (JPY / FRANKFURT / LONDON / NY) indicator

Created at 30 Mar 2021, 20:17
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!
CA

caputojr

Joined 30.03.2021

Session (JPY / FRANKFURT / LONDON / NY) indicator
30 Mar 2021, 20:17


Hello everyone,

 

I am trying to create an indicator to plot vertical lines for (at least) beginning of each market session (JPY / FRANKFURT / LONDON / NY). I have the code below which is not building but I would love to add a feature to add the user UTC (in case it is needed as it is using the MarketSeries.OpenTime function).

I also would like to plot the Session description as a text along the vertical line like the example below. Is it possible to do? 

 

|
|
|
|L
|O
|N
|D
|O
|N
|
|
|

 

I appreciate any feedback and help on this. Below the code

 

using System;
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 SessionTime : Indicator
    {

        [Parameter("Europe Summer Time", DefaultValue = false)]
        public bool EuropeSummerTime { get; set; }

        [Parameter("NY Summer Time", DefaultValue = true)]
        public bool NYSummerTime { get; set; }


        [Parameter("Japan Color", DefaultValue = "Gray")]
        public string JapanColor { get; set; }

        [Parameter("Frankfurt Color", DefaultValue = "DarkBlue")]
        public string FrankfurtColor { get; set; }

        [Parameter("London Color", DefaultValue = "Blue")]
        public string LondonColor { get; set; }

        [Parameter("NY Color", DefaultValue = "Red")]
        public string NYColor { get; set; }

        // fix for tick charts
        int seen_frankfurt = 0;
        int seen_london = 0;
        int seen_jpy = 0;
        float seen_NewYork = 0;
        int JapanTime;
        int FrankfurtTime;
        int LondonTime;
        int NYTime;

        protected override void Initialize()
        {

        }

        public override void Calculate(int index)
        {

            if (EuropeSummerTime)
            {
                FrankfurtTime = 7;
                LondonTime = 8;
            }
            else
            {
                FrankfurtTime = 6;
                LondonTime = 7;
            }


            if (NYSummerTime)
            {
                NYTime = 13;
            }
            else
            {
                NYTime = 14;
            }

            if (MarketSeries.OpenTime[index].Minute == 0)
            {
                if (MarketSeries.OpenTime[index].Hour == FrankfurtTime && seen_frankfurt != MarketSeries.OpenTime[index].Day)
                {
                    seen_frankfurt = MarketSeries.OpenTime[index].Day;
                    ChartObjects.DrawVerticalLine("Frankfurt Open" + index, index, FrankfurtColor, 1, LineStyle.Lines);

                }
                if (MarketSeries.OpenTime[index].Hour == LondonTime && seen_london != MarketSeries.OpenTime[index].Day)
                {
                    seen_london = MarketSeries.OpenTime[index].Day;
                    ChartObjects.DrawVerticalLine("London Open" + index, index, LondonColor, 1, LineStyle.Lines);

                }
                if (MarketSeries.OpenTime[index].Hour == 0 && seen_jpy != MarketSeries.OpenTime[index].Day)
                {
                    seen_jpy = MarketSeries.OpenTime[index].Day;
                    ChartObjects.DrawVerticalLine("Jpy Open" + index, index, JapanColor, 1, LineStyle.Lines);
                }

            }


            if (MarketSeries.OpenTime[index].Minute == 30)
            {
                if (MarketSeries.OpenTime[index].Hour == NYTime && seen_NewYork != MarketSeries.OpenTime[index].Day)
                {
                    seen_frankfurt = MarketSeries.OpenTime[index].Day;
                    ChartObjects.DrawVerticalLine("NewYork Open" + index, index, NYColor, 1, LineStyle.Lines);
                }

            }
        }
    }
}

 

 


@caputojr
Replies

amusleh
31 Mar 2021, 09:22

Hi,

There is already several very good free session indicators, you can use those instead of building a new one.

Regarding your code, for adding multiple time zone function you have to use the "Application.UserTimeOffset" and "Application.UserTimeOffsetChanged" for getting user current time zone offset, and then you can convert your indicator Server.Time or bar open times to the user time zone offset by adding the time offset value.

And yes its possible to show a vertical text, you have to use StringBuilder and add each latter as a new line on it.

Getting user time offset example:

using cAlgo.API;

namespace cAlgo
{
    /// <summary>
    /// This sample indicator shows how to use the API Application object nad display its properties data inside a chart control
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ApplicationSample : Indicator
    {
        private TextBlock _userTimeOffsetTextBlock, _themeTextBlock;

        [Parameter("Horizontal Alignment", DefaultValue = HorizontalAlignment.Center)]
        public HorizontalAlignment HorizontalAlignment { get; set; }

        [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Center)]
        public VerticalAlignment VerticalAlignment { get; set; }

        protected override void Initialize()
        {
            Application.ColorThemeChanged += Application_ColorThemeChanged;
            Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;

            DrawApplicationInfo();
        }

        private void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
        {
            _userTimeOffsetTextBlock.Text = obj.UserTimeOffset.ToString();
        }

        private void Application_ColorThemeChanged(ColorThemeChangeEventArgs obj)
        {
            _themeTextBlock.Text = obj.ColorTheme.ToString();
        }

        private void DrawApplicationInfo()
        {
            var grid = new Grid(3, 2) 
            {
                BackgroundColor = Color.Goldenrod,
                HorizontalAlignment = HorizontalAlignment,
                VerticalAlignment = VerticalAlignment
            };

            grid.AddChild(new TextBlock 
            {
                Text = "Version",
                Margin = 5
            }, 0, 0);
            grid.AddChild(new TextBlock 
            {
                Text = Application.Version.ToString(),
                Margin = 5
            }, 0, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "Theme",
                Margin = 5
            }, 1, 0);

            _themeTextBlock = new TextBlock 
            {
                Text = Application.ColorTheme.ToString(),
                Margin = 5
            };

            grid.AddChild(_themeTextBlock, 1, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "User Time Offset",
                Margin = 5
            }, 2, 0);

            _userTimeOffsetTextBlock = new TextBlock 
            {
                Text = Application.UserTimeOffset.ToString(),
                Margin = 5
            };

            grid.AddChild(_userTimeOffsetTextBlock, 2, 1);

            Chart.AddControl(grid);
        }

        public override void Calculate(int index)
        {
        }
    }
}

 

Using StringBuilder:

using cAlgo.API;
using System.Text;

namespace cAlgo
{
    /// <summary>
    /// This sample shows how to use Chart.DrawStaticText method to draw static locked text on chart
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ChartStaticTextSample : Indicator
    {
        protected override void Initialize()
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Symbol: " + SymbolName);
            stringBuilder.AppendLine("TimeFrame: " + TimeFrame);
            stringBuilder.AppendLine("Chart Type: " + Chart.ChartType);

            Chart.DrawStaticText("Static_Sample", stringBuilder.ToString(), VerticalAlignment.Bottom, HorizontalAlignment.Left, Color.Red);
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@amusleh

caputojr
05 Apr 2021, 00:47

RE:

amusleh said:

Hi,

There is already several very good free session indicators, you can use those instead of building a new one.

Regarding your code, for adding multiple time zone function you have to use the "Application.UserTimeOffset" and "Application.UserTimeOffsetChanged" for getting user current time zone offset, and then you can convert your indicator Server.Time or bar open times to the user time zone offset by adding the time offset value.

And yes its possible to show a vertical text, you have to use StringBuilder and add each latter as a new line on it.

Getting user time offset example:

using cAlgo.API;

namespace cAlgo
{
    /// <summary>
    /// This sample indicator shows how to use the API Application object nad display its properties data inside a chart control
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ApplicationSample : Indicator
    {
        private TextBlock _userTimeOffsetTextBlock, _themeTextBlock;

        [Parameter("Horizontal Alignment", DefaultValue = HorizontalAlignment.Center)]
        public HorizontalAlignment HorizontalAlignment { get; set; }

        [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Center)]
        public VerticalAlignment VerticalAlignment { get; set; }

        protected override void Initialize()
        {
            Application.ColorThemeChanged += Application_ColorThemeChanged;
            Application.UserTimeOffsetChanged += Application_UserTimeOffsetChanged;

            DrawApplicationInfo();
        }

        private void Application_UserTimeOffsetChanged(UserTimeOffsetChangedEventArgs obj)
        {
            _userTimeOffsetTextBlock.Text = obj.UserTimeOffset.ToString();
        }

        private void Application_ColorThemeChanged(ColorThemeChangeEventArgs obj)
        {
            _themeTextBlock.Text = obj.ColorTheme.ToString();
        }

        private void DrawApplicationInfo()
        {
            var grid = new Grid(3, 2) 
            {
                BackgroundColor = Color.Goldenrod,
                HorizontalAlignment = HorizontalAlignment,
                VerticalAlignment = VerticalAlignment
            };

            grid.AddChild(new TextBlock 
            {
                Text = "Version",
                Margin = 5
            }, 0, 0);
            grid.AddChild(new TextBlock 
            {
                Text = Application.Version.ToString(),
                Margin = 5
            }, 0, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "Theme",
                Margin = 5
            }, 1, 0);

            _themeTextBlock = new TextBlock 
            {
                Text = Application.ColorTheme.ToString(),
                Margin = 5
            };

            grid.AddChild(_themeTextBlock, 1, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "User Time Offset",
                Margin = 5
            }, 2, 0);

            _userTimeOffsetTextBlock = new TextBlock 
            {
                Text = Application.UserTimeOffset.ToString(),
                Margin = 5
            };

            grid.AddChild(_userTimeOffsetTextBlock, 2, 1);

            Chart.AddControl(grid);
        }

        public override void Calculate(int index)
        {
        }
    }
}

 

Using StringBuilder:

using cAlgo.API;
using System.Text;

namespace cAlgo
{
    /// <summary>
    /// This sample shows how to use Chart.DrawStaticText method to draw static locked text on chart
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ChartStaticTextSample : Indicator
    {
        protected override void Initialize()
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Symbol: " + SymbolName);
            stringBuilder.AppendLine("TimeFrame: " + TimeFrame);
            stringBuilder.AppendLine("Chart Type: " + Chart.ChartType);

            Chart.DrawStaticText("Static_Sample", stringBuilder.ToString(), VerticalAlignment.Bottom, HorizontalAlignment.Left, Color.Red);
        }

        public override void Calculate(int index)
        {
        }
    }
}

 

Hi,

 

Thank you very much for taking the time to help me. I see what you say and I will work around the codes you've shared in order to get what I am expecting.

 

Appreciate it.


@caputojr