Untitled

 avatar
unknown
plain_text
3 months ago
9.3 kB
2
Indexable
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class TimeRangeBox : Indicator
    {
        [Parameter("From Time (HH:mm)", DefaultValue = "07:00")]
        public string FromTime { get; set; }

        [Parameter("To Time (HH:mm)", DefaultValue = "08:30")]
        public string ToTime { get; set; }

        [Parameter("Trendline Start Time (HH:mm)", DefaultValue = "09:30")]
        public string TrendlineStartTime { get; set; }

        [Parameter("Trendline End Time (HH:mm)", DefaultValue = "10:00")]
        public string TrendlineEndTime { get; set; }

        [Parameter("Box Name", DefaultValue = "Time Box")]
        public string BoxName { get; set; }

        [Parameter("Box Color", DefaultValue = "DodgerBlue")]
        public string BoxColor { get; set; }

        [Parameter("Box Opacity (0-100)", DefaultValue = 50)]
        public int BoxOpacity { get; set; }

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

        [Parameter("Show ?", Group = "1st FVG", DefaultValue = false)]
        public bool FVG_Show { get; set; }

        [Parameter("Min Pips", Group ="1st FVG", DefaultValue = 100)]
        public double FVG_Min_Pips { get; set; }
        
        [Parameter("Add Hours", Group ="1st FVG", DefaultValue = 10)]
        public double FVG_AddHours { get; set; }

        [Parameter("FVG Color", Group = "1st FVG", DefaultValue = "#7E001B30")]
        public string FVG_Color { get; set; }

        private TimeSpan fromTimeSpan;
        private TimeSpan toTimeSpan;
        private TimeSpan trendlineStartTimeSpan;
        private TimeSpan trendlineEndTimeSpan;
        private DateTime lastDrawnDate = DateTime.MinValue;
        private double _FvgHigh = Double.MinValue, _FvgLow = Double.MaxValue;
        private DateTime _FvgOpenTime;
        protected override void Initialize()
        {
            fromTimeSpan = TimeSpan.Parse(FromTime);
            toTimeSpan = TimeSpan.Parse(ToTime);
            trendlineStartTimeSpan = TimeSpan.Parse(TrendlineStartTime);
            trendlineEndTimeSpan = TimeSpan.Parse(TrendlineEndTime);
        }

        public override void Calculate(int index)
        {
            var currentTime = MarketSeries.OpenTime[index];
            DateTime today = currentTime.Date;

            DateTime fromDateTime = today + fromTimeSpan;
            DateTime toDateTime = today + toTimeSpan;
            DateTime trendlineStartDateTime = today + trendlineStartTimeSpan;
            DateTime trendlineEndDateTime = today + trendlineEndTimeSpan;

            if (currentTime < toDateTime || lastDrawnDate == today)
                return;

            lastDrawnDate = today;

            double highestHigh = double.MinValue;
            double lowestLow = double.MaxValue;
            int _count = 0;

            for (int i = index; i >= 0; i--)
            {
                DateTime barTime = MarketSeries.OpenTime[i];

                if (barTime >= fromDateTime && barTime <= toDateTime)
                {
                    if (MarketSeries.High[i] > highestHigh)
                        highestHigh = MarketSeries.High[i];

                    if (MarketSeries.Low[i] < lowestLow)
                        lowestLow = MarketSeries.Low[i];
                    if (barTime > fromDateTime)
                    {
                        Is1stFVG(i + 1);
                    }
                }

                
                if (barTime < fromDateTime) {
                    
                    break;

                }   
            }


            if (highestHigh > double.MinValue && lowestLow < double.MaxValue)
            {
                if(FVG_Show)
                {
                    var fvgBox = Chart.DrawRectangle(
                        $"1stFVG_box_{today}",
                        _FvgOpenTime,
                        _FvgHigh,
                        trendlineEndDateTime.AddHours(FVG_AddHours),
                        _FvgLow,
                        FVG_Color,
                        BoxOpacity
                    );
                    fvgBox.IsFilled = true;
                    _FvgHigh = Double.MinValue;
                    _FvgLow = Double.MaxValue;
                }
                

                double pipSize = Symbol.PipSize;
                double pipsRange = (highestHigh - lowestLow) / pipSize;
                
                Chart.DrawRectangle(
                    $"TimeBox_{today}",
                    fromDateTime,
                    highestHigh,
                    toDateTime,
                    lowestLow,
                    Color.FromName(BoxColor),
                    BoxOpacity
                );

                Chart.DrawText(
                    $"Label_{today}",
                    $"{BoxName} {pipsRange:F1} pips",
                    fromDateTime.AddSeconds((toDateTime - fromDateTime).TotalSeconds / 2),
                    lowestLow,
                    Color.FromName(BoxColor)
                );

                Chart.DrawTrendLine(
                    $"Trendline_High_{today}",
                    trendlineStartDateTime, highestHigh,
                    trendlineEndDateTime, highestHigh,
                    Color.FromName(TrendlineColor),
                    1,
                    LineStyle.LinesDots
                );
                
                Chart.DrawTrendLine(
                    $"Trendline_50p_{today}",
                    trendlineStartDateTime,lowestLow + (highestHigh - lowestLow) / 2,
                    trendlineEndDateTime, lowestLow + (highestHigh - lowestLow) / 2,
                    Color.FromName(TrendlineColor),
                    1,
                    LineStyle.DotsRare
                );

                Chart.DrawTrendLine(
                    $"Trendline_Low_{today}",
                    trendlineStartDateTime, lowestLow,
                    trendlineEndDateTime, lowestLow,
                    Color.FromName(TrendlineColor),
                    1,
                    LineStyle.LinesDots
                );
            }
        }

        private bool isBullish(int index)
        {
            return Bars[index].Open < Bars[index].Close;
        }

        private bool isBearish(int index)
        {
            return Bars[index].Open > Bars[index].Close;
        }

        private bool Is1stFVG(int index)
        {
            // FVG Buy
            if (Bars[index].Low > Bars[index - 2].High)
            {
                double oldHigh = _FvgHigh != Double.MinValue ? _FvgHigh : 0;
                double oldLow = _FvgLow != Double.MaxValue ? _FvgLow : 0;
                DateTime oldTime = _FvgOpenTime;

                _FvgLow = Bars[index - 2].High;
                _FvgHigh = Bars[index].Low;
                _FvgOpenTime = Bars[index].OpenTime;

                if (isBullish(index) && isBullish(index - 1) && (Bars[index].Open > Bars[index - 1].Close))
                {
                    _FvgHigh = Bars[index].Open;
                }

                if (isBullish(index - 1) && isBullish(index - 2) && (Bars[index -1].Open > Bars[index - 2].Close))
                {
                    _FvgLow = Bars[index - 2].Close;
                }
                double pips = (_FvgHigh - _FvgLow) / Symbol.PipSize;
                if (pips < FVG_Min_Pips)
                {
                    Print("Check bullish");
                    _FvgHigh = oldHigh;
                    _FvgLow = oldLow;
                    _FvgOpenTime = oldTime;
                }

                return true;
            }

            // FVG Sell

            if (Bars[index].High < Bars[index - 2].Low)
            {
                double oldHigh = _FvgHigh != Double.MinValue ? _FvgHigh : 0;
                double oldLow = _FvgLow != Double.MaxValue ? _FvgLow : 0;
                DateTime oldTime = _FvgOpenTime;

                _FvgLow = Bars[index].High;
                _FvgHigh = Bars[index - 2].Low;
                _FvgOpenTime = Bars[index].OpenTime;

                if (isBearish(index) && isBearish(index - 1) && (Bars[index].Open < Bars[index - 1].Close))
                {
                    _FvgLow = Bars[index].Open;
                }

                if (isBearish(index - 1) && isBearish(index - 2) && (Bars[index - 1].Open < Bars[index - 2].Close))
                {
                    _FvgHigh = Bars[index - 2].Close;
                }

                double pips = (_FvgHigh - _FvgLow) / Symbol.PipSize;
                if (pips < FVG_Min_Pips)
                {
                    Print("Check bearish");
                    _FvgHigh = oldHigh;
                    _FvgLow = oldLow;
                    // _FvgOpenTime = oldTime;
                }

                return true;
            }
            return false;


        }
    }
}
Editor is loading...
Leave a Comment