Category Other  Published on 22/03/2024

Easy Fibo

Description

Convenience Fibonacci drawing on chart.

Ctrl-Left Click to select an area to draw.

Fibo levels are configurable.

  • Levels: default "0.0, 23.6, 38.2, 50.0, 61.8, 76.4, 100.0, 138.2, 161.8, 200.0, -38.2, -61.8, -100.0, -161.8". Add or remove up to your need.

 

Updated as of 24 Mar 2024:

  • Added ExtremumPeriods setting. It serves as a guide to find wave. In prev version, it was hardcoded to 5.

 

 

 

 

 

Updated as of 29 Mar 2004:

  • Void the 24 Mar Update.
  • Fixed bug Close button retains on chart.
  • Auto find the start and end based on current selection.

 



using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Linq;
using System.Collections.Generic;
using LittleTrader;
using LittleTrader.Extensions;
using LittleTrader.Huy;
using LittleTrader.Models.PriceActions;
using LittleTrader.Utils;
using System.Drawing;
using Color = cAlgo.API.Color;
using System.Reflection;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = true, AccessRights = AccessRights.None)]
    public class LT_Ind_Fibo : Indicator
    {
        [Parameter(DefaultValue = "0.0, 23.6, 38.2, 50.0, 61.8, 76.4, 100.0, 138.2, 161.8, 200.0, -38.2, -61.8, -100.0, -161.8", Group = "Fibo")]
        public string Levels { get; set; }
        [Parameter(DefaultValue = "DarkGreen", Group = "Fibo")]
        public Color ColorRise { get; set; }
        [Parameter(DefaultValue = "DarkRed", Group = "Fibo")]
        public Color ColorFall { get; set; }
        [Parameter(DefaultValue = false, Group = "Fibo")]
        public bool ShowPrices { get; set; }

        Fibo _fibo;

        protected override void Initialize()
        {
            new ChartSelector() { SelectRecColor = "#8F6F6FE4" }
                .InstallChartSelector(this, OnSelect);

        }

        void OnSelect(DateTime start, DateTime end, int startIndex, int endIndex)
        {
            _fibo = new Fibo(Bars, endIndex - startIndex + 1, Levels);
            _fibo.Calculate(endIndex);
            Draw(true);
        }

        int _lastIndex = -1;
        public override void Calculate(int index)
        {
            if (IsLastBar && _lastIndex != index && _fibo != null)
            {
                Draw(false);
                _lastIndex = index;
            }

        }

        Button _btn = null;
        void Draw(bool fromScratch)
        {
            int hashCode = this.GetHashCode();
            int start = _fibo.Extremums.First().Index;
            int end = Chart.LastVisibleBarIndex + 1;

            // Draw all exs
            if (fromScratch)
                foreach (var item in _fibo.Extremums)
                {
                    Chart.DrawIcon($"{hashCode}_F_{item.Type}", ChartIconType.Circle, Bars.OpenTimes[item.Index], item.Value,
                        item.Type == ExtremumTypes.Max ? Color.Red : Color.Blue);

                    if (item == _fibo.Extremums.First())
                    {
                        if (_btn != null) Chart.RemoveControl(_btn);
                        // The remove button
                        _btn = new Button() { Text = "x", Margin = new Thickness(5, 0, 0, 0), FontSize = 8.75 };
                        _btn.Click += (e) =>
                        {
                            Chart.RemoveControl(_btn);
                            var objects = Chart.Objects.Where(x => x.Name.StartsWith($"{hashCode}")).ToList();
                            objects.ForEach(x => Chart.RemoveObject(x.Name));
                            _fibo = null;
                        };
                        Chart.AddControl(_btn, item.Index + 1, item.Value);

                    }
                }

            var color = _fibo.Rise ? ColorRise : ColorFall;
            foreach (var item in _fibo.FiboLevels)
            {
                var key = item.Key;
                var line = Chart.DrawTrendLine($"{hashCode}{key}", start, item.Value, end, item.Value, color);
                if (key.InRange(0, 100, false))
                    line.LineStyle = LineStyle.Dots;
                else if (key.OffRange(0, 100, false))
                    line.LineStyle = LineStyle.DotsRare;
                else
                    line.LineStyle = LineStyle.Lines;
                string pstr = ShowPrices ? $"({item.Value.MathRound(Symbol.Digits)})" : string.Empty;
                var text = Chart.DrawText($"{hashCode}{key}_t", $"{key} {pstr}", end, item.Value, color);
                text.VerticalAlignment = VerticalAlignment.Center;
                text.FontSize = 8.75;
            }

            //Print($"Draw Fibo [{start}, {end}");

        }

    }
   


}



dhnhuy's avatar
dhnhuy

Joined on 03.04.2023

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: LT_Ind_Fibo.algo
  • Rating: 0
  • Installs: 470
  • Modified: 29/03/2024 07:49
Comments
Log in to add a comment.
EX
Exzell · 8 months ago

Good Fibo tool - my only crtique is that it dissapears when you change time frames and doesnt synchronize with multiple screens which makes the deafult ctrader Fibo a better option in that regard