Category Other  Published on 30/12/2021

Fractals Multi Timeframe

Description

Draw Fractal Lines/Icons of Higher Timeframes.

Calculates fractal pattern for Higher TF and shows on the current chart as Lines/Icon/Both.

Pattern is calculated as: (For 3 Bar Pattern)
For HIGH: <Previous Bar Has Lower High> _ Bar High _ <Next Bar Has Lower High>
For LOW: <Previous Bar Has Higher Low> _ Bar Low _ <Next Bar Has Higher Low>

Add comments for any fixes or improvements. Thanks.

Settings:

Timeframe: Select Higher Timeframe than Current Chart Timeframe
Fractal Count: Number of Bars to Check on Left/Right for Fractal (Eg. 1: Total 3 Bar Pattern)
Line Length: Number of Bars to draw the Fractal Line
Draw Type: Line / Icon / Both

Below Shows Fractal Lines on 4 Hour Chart with:
Current 4H TF: (Green/Red)
Daily TF: (Yellow/Orange)


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FractalsMultiTimeframe : Indicator
    {
        #region Common Parameters
        [Parameter("Time Frame")]
        public TimeFrame timeFrame { get; set; }

        [Parameter("Fractal Count", DefaultValue = 1)]
        public int Count { get; set; }

        [Parameter("Line Length (Bars)", DefaultValue = 10)]
        public int LineBars { get; set; }

        [Parameter("Draw Type", DefaultValue = DrawType.Both)]
        public DrawType Type { get; set; }
        #endregion

        #region High Parameters
        [Parameter("Color", Group = "High", DefaultValue = "Green")]
        public String HighColor { get; set; }

        [Parameter("Icon Type", Group = "High", DefaultValue = ChartIconType.Circle)]
        public ChartIconType HighIconType { get; set; }

        [Parameter("Line Style (for Line)", Group = "High", DefaultValue = LineStyle.Dots)]
        public LineStyle HighLineStyle { get; set; }
        #endregion

        #region Low Parameters
        [Parameter("Color", Group = "Low", DefaultValue = "Red")]
        public String LowColor { get; set; }

        [Parameter("Icon Type", Group = "Low", DefaultValue = ChartIconType.Circle)]
        public ChartIconType LowIconType { get; set; }

        [Parameter("Line Style (for Line)", Group = "Low", DefaultValue = LineStyle.Dots)]
        public LineStyle LowLineStyle { get; set; }
        #endregion

        public enum DrawType
        {
            Line,
            Icon,
            Both
        }

        int lastIndex = 0;
        Bars TFBars;
        Color _highColor, _lowColor;
        readonly Color _invalidColor = Color.FromHex("#00000000");

        protected override void Initialize()
        {
            if (timeFrame < Chart.TimeFrame)
            {
                Chart.DrawStaticText("FC_ERROR", "*** ERROR **** \n TimeFrame must be Equal OR Greater than Chart Timeframe", VerticalAlignment.Bottom, HorizontalAlignment.Center, Color.Red);
                return;
            }

            if (timeFrame > Chart.TimeFrame)
                TFBars = MarketData.GetBars(timeFrame);
            else
                TFBars = Bars;

            _highColor = HighColor.StartsWith("#") ? Color.FromHex(HighColor) : Color.FromName(HighColor);
            _lowColor = LowColor.StartsWith("#") ? Color.FromHex(LowColor) : Color.FromName(LowColor);

            if (_lowColor == _invalidColor || _highColor == _invalidColor)
            {
                string errorMsg = _lowColor == _invalidColor ? "\nLow Color (" + LowColor + ") - INVALID" : "";
                errorMsg += _highColor == _invalidColor ? "\nHigh Color (" + HighColor + ") - INVALID" : "";
                Chart.DrawStaticText("FC_ERROR", "*** ERROR ****" + errorMsg, VerticalAlignment.Bottom, HorizontalAlignment.Center, Color.Red);
            }
        }

        public override void Calculate(int index)
        {
            if (timeFrame < Chart.TimeFrame)
                return;

            // Get the index for Higher Timeframe
            index = timeFrame != Chart.TimeFrame ? TFBars.OpenTimes.GetIndexByTime(Bars[index].OpenTime) : index;

            // <Count> Bars on LEFT + Bar + <Count> Bars on RIGHT
            int minBarsRequired = 2 * Count + 1;

            // Check ONLY on New Bar
            if (index >= minBarsRequired && IsNewBar(index))
            {
                int barIndex = index - 1 - Count;
                Bar CheckBar = TFBars[barIndex];

                // Higher TF Fractal will be RECOGNIZED only when the WHOLE Pattern Completes
                // which will be (2 * Count + 1) Bars
                // Hence, the Fractal should be SHOWN in Lower TF at OpenTime of (BarIndex + Count + 1)
                int chartBarIndex = timeFrame != Chart.TimeFrame ? Bars.OpenTimes.GetIndexByTime(TFBars[barIndex + Count + 1].OpenTime) : barIndex;

                // Get Minimum Bars Required for calculation, without the Main Bar
                var CheckBars = TFBars.Skip(index - minBarsRequired).Take(minBarsRequired).ToList();
                CheckBars.RemoveAt(Count);

                // If All Others Bars have LOWER HIGH than Main Bar
                if (CheckBars.All(B => CheckBar.High > B.High))
                {
                    if (Type == DrawType.Line || Type == DrawType.Both)
                        Chart.DrawTrendLine("FTF_HIGHLINE_" + chartBarIndex, chartBarIndex, CheckBar.High, chartBarIndex + LineBars, CheckBar.High, _highColor, 1, HighLineStyle);
                    if (Type == DrawType.Icon || Type == DrawType.Both)
                        Chart.DrawIcon("FTF_HIGHICON_" + chartBarIndex, HighIconType, chartBarIndex, CheckBar.High, _highColor);
                }

                // If All Others Bars have HIGHER LOW than Main Bar
                if (CheckBars.All(B => CheckBar.Low < B.Low))
                {
                    if (Type == DrawType.Line || Type == DrawType.Both)
                        Chart.DrawTrendLine("FTF_LOWLINE_" + chartBarIndex, chartBarIndex, CheckBar.Low, chartBarIndex + LineBars, CheckBar.Low, _lowColor, 1, LowLineStyle);
                    if (Type == DrawType.Icon || Type == DrawType.Both)
                        Chart.DrawIcon("FTF_LOW_ICON_" + chartBarIndex, LowIconType, chartBarIndex, CheckBar.Low, _lowColor);
                }
            }
        }

        bool IsNewBar(int index)
        {
            bool isNewBar = index > lastIndex;
            lastIndex = isNewBar ? index : lastIndex;
            return isNewBar;
        }
    }
}


AK
aksbenz

Joined on 26.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Fractals Multi Timeframe.algo
  • Rating: 0
  • Installs: 1990
Comments
Log in to add a comment.
AK
aksbenz · 8 months ago

Will check in a few days

SH
shelly.yeaya · 8 months ago

Thanks so much for this indicator! Would it be possible to amend it so that the drawn fractals are accessible as Outputs? I've tried myself, but with my limited coding experience I couldn't quite get it right

RI

It really look like buy/sell liquidty