Category Other  Published on 22/12/2022

DR/IDR for cTrader

Description

This script is showing the IDR and DR for the regular trading session and for the overnight session based on the rules from the creator of the DR IDR concept.
It works for all major Forex Pairs, BTC , ETH and the US Equity indices. This concept is based on rules and has a 80 % probability to be correct.

It should be applied in the 5 Min. Timeframe.

The timings for the RDR are from 09.30 - 10.30 am New York local time.
The timings for the ODR are from 03.00 - 04.00 am New York local time.

Rules:
1. If price in the 5 Min timeframe closes above the DR high after 10.30 am or 04.00 am then the DR low will be with 80 percent probability the low of the trading session. This is called confirmation.
2. If price in the 5 Min timeframe closes below the DR low after 10.30 am or 04.00 am then the DR high will be with 80 percent probability the high of the trading session. This is called confirmation.
3. If price closes above the IDR high after 10.30 am or 04.00 am it is an early indication that the low of the DR will be the low of the day and vice versa.
 

For more information: 

 


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

namespace cAlgo
{
    [Indicator(IsOverlay=true, TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class DRIDR : Indicator
    {
        [Parameter("Regular Time Starts", Group = "Time Settings", DefaultValue = "09:30")]
        public String RegularTimeStart {get; set;}
        
        [Parameter("Regular Time Ends", Group = "Time Settings", DefaultValue = "10:30")]
        public String RegularTimeEnd {get; set;}

        [Parameter("Regular Time Extends Until", Group = "Time Settings", DefaultValue = "16:00")]
        public String RegularTimeExtend {get; set;}

        [Parameter("After Session Time Starts", Group = "Time Settings", DefaultValue = "19:30")]
        public String AfterTimeStart {get; set;}
    
        [Parameter("After Session Time Ends", Group = "Time Settings", DefaultValue = "20:30")]
        public String AfterTimeEnd {get; set;}

        [Parameter("After Session Extends Ends", Group = "Time Settings", DefaultValue = "02:00")]
        public String AfterTimeExtend {get; set;}

        [Parameter("Overnight Session Time Starts", Group = "Time Settings", DefaultValue = "03:00")]
        public String OvernightTimeStart {get; set;}
    
        [Parameter("Overnight Session Time Ends", Group = "Time Settings", DefaultValue = "04:00")]
        public String OvernightTimeEnd {get; set;}
    
        [Parameter("Overnight Session Ends", Group = "Time Settings", DefaultValue = "08:30")]
        public String OvernighExtend {get; set;}

        [Parameter("Regular Session", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowRegular {get; set;}

        [Parameter("Overnight Session", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowOvernight {get; set;}

        [Parameter("After Session", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowAfterSession {get; set;}

        [Parameter("Show Middle DR Line", Group = "General Settings", DefaultValue = false)]
        public Boolean ShowMiddleDRLine {get; set;}

        [Parameter("Show Middle IDR Line", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowMiddleIDRLine {get; set;}
        
        [Parameter("Show Opening Line", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowOpeningLine {get; set;}

        [Parameter("Extend Opening Line", Group = "General Settings", DefaultValue = true)]
        public Boolean ExtendOpeningLine {get; set;}

        [Parameter("Show DR/IDR Box", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowBox {get; set;}

        public enum enmBoxType{ DR, IDR};
        [Parameter("Box Type", Group = "General Settings", DefaultValue = enmBoxType.IDR)]
        public enmBoxType BoxType {get; set;}

        [Parameter("Extend DR Lines", Group = "General Settings", DefaultValue = true)]
        public Boolean ExtendDRLines {get; set;}

        [Parameter("Box Color Based on open and close", Group = "General Settings", DefaultValue = false)]
        public Boolean ChangeBoxColor {get; set;}
        
        [Parameter("Extend IDR Lines", Group = "General Settings", DefaultValue = true)]
        public Boolean ExtendIDRLines {get; set;}
        
        [Parameter("Show SD Levels", Group = "General Settings", DefaultValue = true)]
        public Boolean ShowSD {get; set;}
        
        [Parameter("Load More History", Group = "General Settings", DefaultValue = false)]
        public Boolean LoadMoreHistory {get; set;}
        
        [Parameter("DR Line Color", Group = "Styles", DefaultValue = "Gray")]
        public String DRLineColor {get; set;}

        [Parameter("IDR Line Color", Group = "Styles", DefaultValue = "OrangeRed")]
        public String IDRLineColor {get; set;}

        [Parameter("Middle Line Color", Group = "Styles", DefaultValue = "Gray")]
        public String MiddleLineColor {get; set;}

        [Parameter("Opening Line Color", Group = "Styles", DefaultValue = "SkyBlue")]
        public String OpenLineColor {get; set;}

        [Parameter("Box Background Color", Group = "Styles", DefaultValue = "DarkGray")]
        public String BoxBgColor {get; set;}

        [Parameter("Up Box Color", Group = "Styles", DefaultValue = "Green")]
        public String BoxUpColor {get; set;}

        [Parameter("Box Background Color", Group = "Styles", DefaultValue = "Red")]
        public String BoxDownColor {get; set;}
        
        private Bars Min5Bars;
        private Color BgColor, DRColor, IDRColor, MiddleColor, OpenColor, UpColor, DownColor;
        
        protected override void Initialize()
        {
            Min5Bars = MarketData.GetBars(TimeFrame.Minute5);
            BgColor = Color.FromArgb(30, Color.FromName(BoxBgColor));
            UpColor = Color.FromArgb(30, Color.FromName(BoxUpColor));
            DownColor = Color.FromArgb(30, Color.FromName(BoxDownColor));
            DRColor = Color.FromName(DRLineColor);
            IDRColor = Color.FromName(IDRLineColor);
            MiddleColor = Color.FromName(MiddleLineColor);
            OpenColor = Color.FromName(OpenLineColor);
            
            if (LoadMoreHistory) 
            {
                int MaxBarsNeeded; 
                int RangePeriod = 21;
                
                if (Bars.TimeFrame == TimeFrame.Hour2)
                    MaxBarsNeeded = 1440 / 60 * RangePeriod;
                else if (Bars.TimeFrame == TimeFrame.Minute45)
                    MaxBarsNeeded = 1440 / 45 * RangePeriod;
                else if (Bars.TimeFrame == TimeFrame.Minute30)
                    MaxBarsNeeded = 1440 / 30 * RangePeriod;
                else if (Bars.TimeFrame == TimeFrame.Minute20)
                    MaxBarsNeeded = 1440 / 20 * RangePeriod;
                else if (Bars.TimeFrame == TimeFrame.Minute15)
                    MaxBarsNeeded = 1440 / 15 * RangePeriod;
                else if (Bars.TimeFrame == TimeFrame.Minute10)
                    MaxBarsNeeded = 144 * RangePeriod;
                else
                    MaxBarsNeeded = 1440 * RangePeriod;

                while (Bars.Count < MaxBarsNeeded)
                {
                    int loaded = Bars.LoadMoreHistory();
                    if (loaded == 0)
                        break;
                }
            }
        }

        public override void Calculate(int index)
        {
            if (TimeFrame == TimeFrame.Minute15 | TimeFrame < TimeFrame.Minute6)
            {
                int idx = Min5Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            
                if (ShowOvernight) DrawSession(idx, OvernightTimeStart, OvernighExtend, "ODR", "OIDR", index);
                if (ShowRegular) DrawSession(idx, RegularTimeStart, RegularTimeExtend, "RDR", "RIDR", index);
                if (ShowAfterSession) DrawSession(idx, AfterTimeStart, AfterTimeExtend, "ADR", "AIDR", index);
            }
        }
        
        /*
         *  Draw Defining Range
         */
        private void DrawSession(int index, String TimeStart, String TimeExtend, String DrPrefix, String IdrPrefix, int tfIndex)
        {   
            DateTime currentTime = Bars.OpenTimes[tfIndex];            
            
            String[] tsArray = TimeStart.Split(':'), teArray = TimeExtend.Split(':');
            DateTime StartTime, EndTime, UntilTime;
            
            StartTime = new DateTime(Min5Bars.OpenTimes[index].Year,Min5Bars.OpenTimes[index].Month, Min5Bars.OpenTimes[index].Day, Int32.Parse(tsArray[0]), Int32.Parse(tsArray[1]), 0);
            
            if (currentTime < StartTime) return;
            
            EndTime = StartTime.AddHours(1); 
            
            int OpenIdx = Min5Bars.OpenTimes.GetIndexByTime(StartTime);
                        
            if (ChangeBoxColor & currentTime <= EndTime)
            {
                BgColor = Bars.ClosePrices[tfIndex] >= Min5Bars.OpenPrices[OpenIdx] ? UpColor : DownColor;
            }
            
            int offset = 0;
            if (Int32.Parse(teArray[0]) < Int32.Parse(tsArray[0])) offset = 1;
            UntilTime = new DateTime(StartTime.Year, StartTime.Month, StartTime.Day + offset, Int32.Parse(teArray[0]), Int32.Parse(teArray[1]), 0);
            
            if (Min5Bars.OpenTimes[index] >= EndTime) return;
            
            if (TimeFrame == TimeFrame.Minute15) index += 2;
            
            Tuple<double, double> DRMaxMin = GetDRMaxMin(index, StartTime);
            Tuple<double, double> IDRMaxMin = GetIDRMaxMin(index, StartTime);

            String RectangleLabel = DrPrefix + "_" + StartTime.Day + "_" + StartTime.Month + "_" + StartTime.Year;
            
            if (ShowBox & BoxType == enmBoxType.DR)
            {
                Chart.DrawRectangle(RectangleLabel, StartTime, DRMaxMin.Item1, EndTime, DRMaxMin.Item2, BgColor).IsFilled = true;
                
                // if (StartTime.DayOfWeek != DayOfWeek.Friday | (StartTime.DayOfWeek == DayOfWeek.Friday & StartTime.Hour < 17))
                // {
                //     Chart.DrawText(RectangleLabel + " Pips", "R = " + Math.Round((DRMaxMin.Item1 - DRMaxMin.Item2) / Symbol.PipSize, 2), StartTime, DRMaxMin.Item2, DRColor);
                // }            
            } 
            else if (ShowBox)
            {
                Chart.DrawRectangle(RectangleLabel, StartTime, IDRMaxMin.Item1, EndTime, IDRMaxMin.Item2, BgColor).IsFilled = true;
                
                // if (StartTime.DayOfWeek != DayOfWeek.Friday | (StartTime.DayOfWeek == DayOfWeek.Friday & StartTime.Hour < 17))
                // {
                //     Chart.DrawText(RectangleLabel + " Pips", "R = " + Math.Round((IDRMaxMin.Item1 - IDRMaxMin.Item2) / Symbol.PipSize, 2), StartTime, IDRMaxMin.Item2, IDRColor);
                // }
            }
            
            Chart.DrawTrendLine(DrPrefix+"Max"+StartTime.ToString(), StartTime, DRMaxMin.Item1, ExtendDRLines ? UntilTime : EndTime, DRMaxMin.Item1, DRColor);
            Chart.DrawTrendLine(DrPrefix+"Min"+StartTime.ToString(), StartTime, DRMaxMin.Item2, ExtendDRLines ? UntilTime : EndTime, DRMaxMin.Item2, DRColor);

            Chart.DrawTrendLine(IdrPrefix+"Max"+StartTime.ToString(), StartTime, IDRMaxMin.Item1, ExtendIDRLines ? UntilTime : EndTime, IDRMaxMin.Item1, IDRColor, 1, LineStyle.LinesDots);
            Chart.DrawTrendLine(IdrPrefix+"Min"+StartTime.ToString(), StartTime, IDRMaxMin.Item2, ExtendIDRLines ? UntilTime : EndTime, IDRMaxMin.Item2, IDRColor, 1, LineStyle.LinesDots);
            
            if (ShowMiddleDRLine)
                Chart.DrawTrendLine(DrPrefix+"Middle"+StartTime.ToString(), StartTime, DRMaxMin.Item2+(DRMaxMin.Item1-DRMaxMin.Item2)/2, UntilTime, DRMaxMin.Item2+(DRMaxMin.Item1-DRMaxMin.Item2)/2, MiddleColor, 1, LineStyle.DotsRare);
       
            if (ShowMiddleIDRLine)
                Chart.DrawTrendLine(IdrPrefix+"Middle"+StartTime.ToString(), StartTime, IDRMaxMin.Item2+(IDRMaxMin.Item1-IDRMaxMin.Item2)/2, UntilTime, IDRMaxMin.Item2+(IDRMaxMin.Item1-IDRMaxMin.Item2)/2, MiddleColor, 1, LineStyle.DotsRare);
                
            if (ShowOpeningLine)
                Chart.DrawTrendLine("OpenLine"+StartTime.ToString(), StartTime, Min5Bars.OpenPrices[OpenIdx], ExtendOpeningLine ? UntilTime : EndTime, Min5Bars.OpenPrices[OpenIdx], OpenColor, 1, LineStyle.DotsRare);
                
            if (ShowSD & IdrPrefix == "RIDR")
            {
                DateTime SDEndTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, TimeZones.EasternStandardTime);
                
                if (SDEndTime > EndTime.AddHours(22)) SDEndTime = EndTime.AddHours(22);
                
                double halfRangeValue = (IDRMaxMin.Item1 - IDRMaxMin.Item2) / 2;
                
                for (int i = 1; i <= 10; i++)
                {
                    Chart.DrawTrendLine("SDLineP_" + i + EndTime.ToString(), EndTime, IDRMaxMin.Item1 + halfRangeValue * i, SDEndTime, IDRMaxMin.Item1 + halfRangeValue * i, Color.DarkGray, 1, LineStyle.DotsRare);
                    var text = Chart.DrawText("SDLevelTextP_" + i + EndTime.ToString(), "" + 0.5 * i, SDEndTime, IDRMaxMin.Item1 + halfRangeValue * i, Color.SlateGray);
                    text.VerticalAlignment = VerticalAlignment.Center;
                    
                    Chart.DrawTrendLine("SDLineN_" + i + EndTime.ToString(), EndTime, IDRMaxMin.Item2 - halfRangeValue * i, SDEndTime, IDRMaxMin.Item2 - halfRangeValue * i, Color.DarkGray, 1, LineStyle.DotsRare);
                    text = Chart.DrawText("SDLevelTextN_" + i + EndTime.ToString(), "" + i * -0.5, SDEndTime, IDRMaxMin.Item2 - halfRangeValue * i, Color.SlateGray);
                    text.VerticalAlignment = VerticalAlignment.Center;
                }
            }
       
        }

        private Tuple<double, double> GetDRMaxMin(int index, DateTime Time)
        {
            int StartIndex = Min5Bars.OpenTimes.GetIndexByTime(Time);
            double Max = 0, Min = double.PositiveInfinity;
            while (index >= StartIndex)
            {
                Max = Math.Max(Max, Min5Bars.HighPrices[index]);
                Min = Math.Min(Min, Min5Bars.LowPrices[index]);
                index--;
            }
            return Tuple.Create(Max, Min);
        }

        private Tuple<double,double> GetIDRMaxMin(int index, DateTime Time)
        {
            int StartIndex = Min5Bars.OpenTimes.GetIndexByTime(Time);
            double Max = 0, Min = double.PositiveInfinity;
            while (index >= StartIndex)
            {
                double Open = Min5Bars.OpenPrices[index];
                double Close = Min5Bars.ClosePrices[index];
                
                Max = Math.Max(Max, Open > Close ? Open : Close);
                Min = Math.Min(Min, Open < Close ? Open : Close);
                
                index--;
            }
            return Tuple.Create(Max, Min);
        }
    }
}

LA
lazarevic.miroslav

Joined on 26.01.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: DR-IDR.algo
  • Rating: 5
  • Installs: 1187
Comments
Log in to add a comment.
SE
sebtrader79 · 3 months ago

hello

 

i think the SD level are calculated wrong

RE
repapips2017 · 8 months ago

hi bro,

would you be able to make this indicator be run for m1-m10 timeframe? thanks in advance.. 

ER
ericknumberfive · 1 year ago

hi, is there any chance to add the possibility to extend the lines of the dr or overnight session a specific amount of hours and minutes?? (like if i want to extend it 2 days and 5 hours)

LA
lazarevic.miroslav · 1 year ago

@rickywong19922005@gmail.com well, that could be a task of another indicator. But maybe this one could help you with that. https://ctrader.com/algos/indicators/show/2786

RI

Is it possible to add auto mark buyside/sellside liquidity?

LA
lazarevic.miroslav · 1 year ago

New version published couple minutes ago. Added SD lines like in the new Trading View indicator version. Enjoy!

LA
lazarevic.miroslav · 1 year ago
RI

Nice work!!

I am still learning from ICT youtube channel and yours

LA
lazarevic.miroslav · 1 year ago

New update. In this moment supported time frames are M1-M5 + M15

LA
lazarevic.miroslav · 1 year ago

Code updated. Fixed DR/IDR calculation by excluding the 10:30am candle. Just noticed the problem.