Category Other  Published on 29/12/2018

Real Dividers 2.0

Description

Knowing when the trading day begins and the trading week ends is very important to technical analysis. You can't just use your own time zone to determine it. It is the same for every trader all over the world.  In general it occurs at 12am EEST which is +2 for part of the yesr and +3 for part. Unfortunately the period separators that are included in cTrader will not mark the correct day end and week end positions nor will it give you signals to take action within your robots.  This indicator takes the guess work out of it, marks the real day end and week end positions and provides signals to take actions within robots.


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class Dividers : Indicator
    {
        [Parameter(DefaultValue = false)]
        public bool HideWeekDividers { get; set; }
        [Parameter(DefaultValue = false)]
        public bool HideDayDividers { get; set; }
        [Parameter(DefaultValue = false)]
        public bool HideDayLabels { get; set; }

        [Output("Dayend", PlotType = PlotType.Points, Thickness = 1, Color = Colors.Orange)]
        public IndicatorDataSeries Dayend { get; set; }
        [Output("Weekend", PlotType = PlotType.Points, Thickness = 1, Color = Colors.Red)]
        public IndicatorDataSeries Weekend { get; set; }

        private int PeriodDivisor;
        private int PeriodsPerDay;

        protected override void Initialize()
        {
            switch (Convert.ToString(TimeFrame))
            {
                case "Minute":
                    PeriodDivisor = 1;
                    break;
                case "Minute5":
                    PeriodDivisor = 5;
                    break;
                case "Minute10":
                    PeriodDivisor = 10;
                    break;
                case "Minute15":
                    PeriodDivisor = 15;
                    break;
                case "Minute30":
                    PeriodDivisor = 30;
                    break;
                case "Hour":
                    PeriodDivisor = 60;
                    break;
                default:
                    PeriodDivisor = 120;
                    break;
            }
            PeriodsPerDay = 720 / PeriodDivisor;
            //Used to place label
        }

        public override void Calculate(int index)
        {
            if (index - 1 < 0) return;

            DateTimeOffset CurrentDate = TradeDate(MarketSeries.OpenTime[index]);
            DateTimeOffset PreviousDate = TradeDate(MarketSeries.OpenTime[index - 1]);

            int DateDifference = (int)(CurrentDate.Date - PreviousDate.Date).TotalDays;

            int myOffset=(int)(CurrentDate.Offset.Hours+7);
            if (TimeFrame < TimeFrame.Hour12)
            {
//*** Dayend ***
                if ((CurrentDate.DayOfWeek != PreviousDate.DayOfWeek || DateDifference > 6) && PreviousDate.DayOfWeek != DayOfWeek.Sunday && CurrentDate.DayOfWeek != DayOfWeek.Saturday)
                {
                    if (!HideDayLabels)
                        ChartObjects.DrawText("DayLabel" + index, " " + CurrentDate.DayOfWeek + (myOffset>0?" +":" ") + myOffset, index, MarketSeries.Low.Minimum(PeriodsPerDay), VerticalAlignment.Bottom, HorizontalAlignment.Right);

                    if (!HideDayDividers)
                        ChartObjects.DrawVerticalLine("Dayend" + index, index, Colors.Orange, 1, LineStyle.DotsRare);

                    Dayend[index] = MarketSeries.Median[index];
                    //Print("CurDayOfWeek:"+CurrentDate.DayOfWeek+" CurrDate:"+CurrentDate.Date+" Diff:"+DateDifference);
                }
            }

//*** Weekend ***
            if (TimeFrame < TimeFrame.Weekly)
            {
                if (CurrentDate.DayOfWeek < PreviousDate.DayOfWeek || DateDifference > 6)
                {
                    if (!HideWeekDividers)
                        ChartObjects.DrawVerticalLine("Weekend" + index, index, Colors.Red, 1, LineStyle.DotsRare);

                    Weekend[index] = MarketSeries.Median[index];
                }
            }
        }



        DateTimeOffset TradeDate(DateTime d)
        {
            DateTimeOffset CurrentDateTime = DateTime.SpecifyKind(d, DateTimeKind.Utc);
            CurrentDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(CurrentDateTime, "Eastern Standard Time");
            CurrentDateTime = CurrentDateTime.AddHours(7);
            //return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(CurrentDateTime, "Arab Standard Time");
            return CurrentDateTime;
            //return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(CurrentDateTime, "E. Europe Standard Time");
        }
    }
}


lec0456's avatar
lec0456

Joined on 14.11.2012

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Dividers.algo
  • Rating: 0
  • Installs: 3760
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
FC
fcarabat · 2 years ago

oh and are you able to fix the color for daily and weekend lines? no matter which color i choose, it still shows orange and red.

FC
fcarabat · 2 years ago

Hi, any chance you can fix the line thickness issue? I set it to 5 and still can barely see the lines. 

CR
cranmer767 · 3 years ago

Many thanks for the indicator. Any chance of altering the code so that the lines appear in the background instead of overlying the price bars? Thanks.

lec0456's avatar
lec0456 · 5 years ago

Recently udated to indicate when the trading day and week begins according to how cTrader agregates daily and weekly data.  Finally got it right.

lec0456's avatar
lec0456 · 6 years ago

Recently updated to include the Time zone offset which changes depending on the part of the year you are trading in.