Self updated Asia/Europe/US Market Hours

by Xavier R in category Other at 24/03/2017
Description

UPDATE

I've edited this indicator so it can use the new charting API, now you can pick opacity for the boxes.


Also fixed a small weekend bug, no charting will be made at that time.

This Indicator uses http://forex.timezoneconverter.com/ to retrieve open and close of market times, so you don't need to update it as it will update itself.

If you spot a bug, malfunctioning or have a suggestion feel free to contact me: waxavi@outlook.com

If you want to code any specific bot or indicator based on this or any other, my services are available on Freelancer:

Download
2264 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
//The MIT License (MIT)
//Copyright(c) <2017> <Xavier R. waxavi@outlook.com>

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using HtmlAgilityPack;
using System.Linq;

namespace cAlgo
{

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet)]
    public class ForexMarketHours : Indicator
    {
        [Parameter("Days Back", DefaultValue = 5)]
        public int _DaysBack { get; set; }
        [Parameter("Refresh Time (minutes)", DefaultValue = 5)]
        public int _RefreshTime { get; set; }

        [Parameter("Alpha", DefaultValue = 50, MinValue = 0, MaxValue = 255)]
        public int _Alpha { get; set; }


        object[,] _Zones = new object[5, 5];
        object[] _EU = new object[5];
        object[] _US = new object[5];
        object[] _AS = new object[5];

        Color _AsiaColor = Color.Yellow;
        Color _EuropeColor = Color.Purple;
        Color _AmericaColor = Color.Blue;

        private int[] GetIndexesFromTime(DateTime t1, DateTime t2)
        {
            int i1, i2;

            for (int i = 0;; i++)
            {
                if (MarketSeries.OpenTime.Last(i) <= t2)
                {
                    i2 = i;
                    break;
                }
            }

            for (int i = 0;; i++)
            {
                if (MarketSeries.OpenTime.Last(i) <= t1)
                {
                    i1 = i;
                    break;
                }
            }

            return new int[] 
            {
                i1,
                i2
            };
        }

        private double[] GetRangeFromIndexes(int[] _i)
        {
            double _high = 0, _low = 0;

            for (int i = _i[1]; i <= _i[0]; i++)
            {
                if (MarketSeries.High.Last(i) > _high)
                    _high = MarketSeries.High.Last(i);

                if (MarketSeries.Low.Last(i) < _low || _low == 0)
                    _low = MarketSeries.Low.Last(i);
            }

            return new double[] 
            {
                _high,
                _low
            };
        }

        //private int GetPeriodsFromDate(DateTime _dt)
        //{
        //    for (int i = 0;; i++)
        //    {
        //        if (MarketSeries.OpenTime.Last(i) == _dt)
        //            return i;
        //        //Break Safety
        //        else if (MarketSeries.OpenTime.Last(i) < MarketSeries.OpenTime.Last(0).AddDays(-2))
        //        {
        //            Print("Return -1");
        //            return -1;
        //        }
        //    }
        //}

        private void PlotLines(string _session, Color _color, int _z, int _daysback)
        {
            for (int p = 0; p < _Zones.GetLength(1); p++)
            {
                DateTime t1, t2;
                if (_Zones[_z, p].ToString().Contains(_session))
                {
                    DateTime.TryParse(_Zones[_z, p + 2].ToString(), out t2);
                    DateTime.TryParse(_Zones[_z, p + 1].ToString(), out t1);
                    t1 = t1.AddDays(-_daysback);
                    t2 = t2.AddDays(-_daysback);

                    double[] range = GetRangeFromIndexes(GetIndexesFromTime(t1, t2));

                    double _highest = range[0];
                    double _lowest = range[1];

                    var span = GetIndexesFromTime(t1, t2);

                    if (Math.Abs(span[0] - span[1]) < 2)
                        return;

                    Print("Highest is: {0}. Lowest is: {1}. ", _highest, _lowest);
                    Print("Range is: {0}", range.Length);

                    Chart.DrawRectangle(_session + _daysback.ToString(), t1, _highest, t2, _lowest, Color.FromArgb(_Alpha, _color), 1, LineStyle.Solid).IsFilled = true;

                    //ChartObjects.DrawLine(_session + " Start" + _daysback.ToString(), t1, _highest, t1, _lowest, _color, 1, LineStyle.DotsRare);
                    //ChartObjects.DrawLine(_session + " End" + _daysback.ToString(), t2, _highest, t2, _lowest, _color, 1, LineStyle.DotsRare);

                    if (_daysback != 0)
                    {
                        Chart.DrawText(_session + "Text" + _daysback.ToString(), _session, MarketSeries.Close.Count - 1 - (int)(GetIndexesFromTime(t1,t2).Sum()/2), _highest + 5 * Symbol.PipSize, _color);
                        //ChartObjects.DrawText(_session + "Text" + _daysback.ToString(), _session, MarketSeries.Close.Count - 1 - (int)(GetIndexesFromTime(t1, t2).Sum() / 2), _highest + 5 * Symbol.PipSize, VerticalAlignment.Center, HorizontalAlignment.Center, _color);
                    }
                    else
                    {
                        Chart.DrawText(_session + "Text" + _daysback.ToString(), _session, MarketSeries.Close.Count - 1 - (int)(GetIndexesFromTime(t1, t2).Sum() / 2) + 50, _highest + 5 * Symbol.PipSize, _color);
                        //ChartObjects.DrawText(_session + "Text" + _daysback.ToString(), _session, MarketSeries.Close.Count - 1 - (int)(GetIndexesFromTime(t1, t2).Sum() / 2) + 50, _highest + 5 * Symbol.PipSize, VerticalAlignment.Center, HorizontalAlignment.Center, _color);
                    }

                    //ChartObjects.DrawLine(_session + "upline" + _daysback.ToString(), t1, _highest, t2, _highest, _color, 1, LineStyle.DotsRare);
                    //ChartObjects.DrawLine(_session + "downline" + _daysback.ToString(), t1, _lowest, t2, _lowest, _color, 1, LineStyle.DotsRare);

                    Print(_session + " Start: {0}", t1);
                    Print(_session + " Ends: {0}", t2);
                    Print(_session + " TimeSpan: {0}", t2 - t1);
                    Print("---------------------------------");
                    break;
                }
            }
        }

        protected override void Initialize()
        {
            HtmlWeb web = new HtmlWeb();
            HtmlDocument document = web.Load("http://forex.timezoneconverter.com/?timezone=UTC&refresh=5");
            var node = document.DocumentNode.Descendants("td").Where(d => d.GetAttributeValue("class", "").Contains("market"));
            var node2 = document.DocumentNode.SelectNodes("//tr");
            //--

            int i = 0;
            int j = 0;

            foreach (var item in node)
            {
                Console.WriteLine(item.InnerText.Trim());
                //--
                _Zones[i, j++] = item.InnerText.Trim();

                if (j == 5)
                {
                    i++;
                    j = 0;
                }

                if (i == 5)
                    break;
            }

            //---
            for (int k = 0; k < _DaysBack; k++)
            {
                PlotLines("Asia", _AsiaColor, 4, k);
                PlotLines("Europe", _EuropeColor, 0, k);
                PlotLines("America", _AmericaColor, 2, k);
            }

            Timer.Start(60 * _RefreshTime);
        }

        protected override void OnTimer()
        {
            HtmlWeb web = new HtmlWeb();
            HtmlDocument document = web.Load("http://forex.timezoneconverter.com/?timezone=UTC&refresh=5");
            var node = document.DocumentNode.Descendants("td").Where(d => d.GetAttributeValue("class", "").Contains("market"));
            var node2 = document.DocumentNode.SelectNodes("//tr");
            //--

            int i = 0;
            int j = 0;

            foreach (var item in node)
            {
                Console.WriteLine(item.InnerText.Trim());
                //--
                _Zones[i, j++] = item.InnerText.Trim();

                if (j == 5)
                {
                    i++;
                    j = 0;
                }

                if (i == 5)
                    break;
            }

            //--

            //---
            for (int k = 0; k < _DaysBack; k++)
            {
                PlotLines("Asia", _AsiaColor, 4, k);
                PlotLines("Europe", _EuropeColor, 0, k);
                PlotLines("America", _AmericaColor, 2, k);
            }

            base.OnTimer();
        }

        public override void Calculate(int index)
        {

        }
    }
}
Comments

GoldnOil750 - April 29, 2017 @ 10:31

Thank you.  very nice clean code.  If you can insert extension methods that can be used in cBOT like

bool Flag_Londong =  ForexMarketHours.isLondon();

then this indicator can be of great use.  

anyways, great work.

///S.Khan

axeldos87 - June 27, 2017 @ 07:56

hola waxy, veo que eres venezolano, me intersamucho lo que hiciste,ya lo baje pero me aparece "source code is not available"

 

me podirias decir que es lo que necesito  por favor? soy nuevo en esoto de Calgo

 

de antemano gracias y saludos!

Xavier R - December 11, 2017 @ 05:20

Hola Axeldos87,

Prueba volver a bajar el indicador, obviamente el codigo esta disponible.

Si aun no te funciona, simplemente copia el codigo en un nuevo indicador y luego lo compilas.

5