Simple Fibonacci Timezones free

by devman in category Other at 18/11/2019
Description

Simple Fibonacci Timezones Indicator.

How to use:

1. Draw any trendline.
2. Open trendline settings (right click).
3. Type `ftz` in comment.
4. Type `ftz|7` to create 7 levels.
5. Give your feedback and enjoy!

Of course, you can move trendline and edit its settings! Just try it!

Feel free to make your suggestions to improve this indicator!

Settings:

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
´╗┐using System;
using cAlgo.API;
using cAlgo.API.Internals;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FibonacchiTimezones : Indicator
    {
        private const string LevelSuffix = "ftzl";

        [Parameter("Draw method", DefaultValue = Method.ByBars, Group = "Levels")]
        public Method Method { get; set; }

        [Parameter("Default count", DefaultValue = 10, MinValue = 0, MaxValue = 100, Group = "Levels")]
        public int DefaultLevelCount { get; set; }

        [Parameter("Base line opacity", DefaultValue = 0.8, MinValue = 0, MaxValue = 1, Group = "Base line")]
        public double BaseLineOpacity { get; set; }

        [Parameter("Base line trigger", DefaultValue = "ftz", Group = "Base line")]
        public string BaseLineTrigger { get; set; }

        [Parameter("Show instructions", DefaultValue = true, Group = "Other")]
        public bool ShowHelp { get; set; }

        protected override void Initialize()
        {
            var lines = Chart
                .FindAllObjects<ChartTrendLine>()
                .Where(CanDrawLevels)
                .ToArray();

            foreach (var line in lines)
                UpdateLevels(line);

            Chart.ObjectAdded += Chart_ObjectAdded;
            Chart.ObjectRemoved += Chart_ObjectRemoved;
            Chart.ObjectUpdated += Chart_ObjectUpdated;

            if (ShowHelp)
            {
                Chart.DrawStaticText("ftz-help", string.Format(@"
1. Draw any trendline.
2. Open trendline settings (right click).
3. Type `{0}` in comment.
4. Type `{0}|7` to create 7 levels.
5. Give your feedback and enjoy!", BaseLineTrigger),
                    VerticalAlignment.Top,
                    HorizontalAlignment.Left,
                    Chart.ColorSettings.ForegroundColor);
            }
        }

        private void Chart_ObjectUpdated(ChartObjectUpdatedEventArgs args)
        {
            if (args.ChartObject is ChartTrendLine)
            {
                var line = (ChartTrendLine)args.ChartObject;

                if (CanDrawLevels(line))
                    UpdateLevels(line);
                else
                    RemoveLevels(line);
            }
        }

        private void Chart_ObjectAdded(ChartObjectAddedEventArgs args)
        {
            if (args.ChartObject is ChartTrendLine && CanDrawLevels((ChartTrendLine)args.ChartObject))
            {
                UpdateLevels((ChartTrendLine)args.ChartObject);
            }
        }

        private void Chart_ObjectRemoved(ChartObjectRemovedEventArgs args)
        {
            if (args.ChartObject is ChartTrendLine && CanDrawLevels((ChartTrendLine)args.ChartObject))
            {
                RemoveLevels((ChartTrendLine)args.ChartObject);
            }
        }

        private void RemoveLevels(ChartTrendLine line)
        {
            var levels = Chart
                .FindAllObjects<ChartVerticalLine>()
                .Where(vl => !vl.IsInteractive && vl.Name.StartsWith(line.Name + LevelSuffix))
                .ToArray();

            foreach (var level in levels)
            {
                Chart.RemoveObject(level.Name);
            }
        }

        private void UpdateLevels(ChartTrendLine line)
        {
            RemoveLevels(line);

            line.Color = Color.FromArgb((int) (BaseLineOpacity * byte.MaxValue), line.Color);

            var levelCount = GetLevelCount(line);
            if (levelCount <= 0)
                return;

            var size = GetSize(line);

            int lastLevel = 0;
            int currentLevel = 1;

            for (var i = 0; i < levelCount; i++)
            {
                var tempLevel = currentLevel;

                currentLevel = lastLevel + currentLevel;
                lastLevel = tempLevel;

                DrawLevel(line, currentLevel, size);
            }
        }

        private void DrawLevel(ChartTrendLine line, double level, double size)
        {
            var lineName = line.Name + LevelSuffix + level;
            var lineColor = Color.FromArgb(byte.MaxValue, line.Color);
            var levelSize = (int) Math.Round((level - 1) * size);

            ChartVerticalLine levelLine;

            switch (Method)
            {
                case Method.ByBars:
                    var barIndex = MarketSeries.OpenTime.GetIndexByTime(line.Time1) + levelSize;

                    levelLine = Chart.DrawVerticalLine(
                        lineName,
                        barIndex,
                        lineColor);

                    break;
                case Method.ByTime:
                    var time = line.Time1.AddSeconds(levelSize);

                    levelLine = Chart.DrawVerticalLine(
                        lineName,
                        time,
                        lineColor);

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            levelLine.LineStyle = line.LineStyle;
            levelLine.Thickness = line.Thickness;
        }

        private double GetSize(ChartTrendLine line)
        {
            switch (Method)
            {
                case Method.ByBars:
                    return MarketSeries.OpenTime.GetIndexByTime(line.Time2) - MarketSeries.OpenTime.GetIndexByTime(line.Time1);
                case Method.ByTime:
                    return (line.Time2 - line.Time1).TotalSeconds;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private int GetLevelCount(ChartTrendLine line)
        {
            int levelCount = 0;

            if (!line.Comment.Contains("|") || !int.TryParse(line.Comment.Split('|')[1], out levelCount))
            {
                levelCount = DefaultLevelCount;
            }

            return levelCount;
        }

        private bool CanDrawLevels(ChartTrendLine line)
        {
            return line.Comment != null && line.Comment.StartsWith(BaseLineTrigger);
        }

        public override void Calculate(int index)
        {
            // do nothing
        }
    }

    public enum Method
    {
        ByBars,
        ByTime
    }
}
Comments
0