Tiger.com Windows Wiki
English
English
  • πŸ‘‹Welcome!
  • ⭐How to start?
    • Recommended system requirements
    • Register and setup
    • Licenses and login
  • βš™οΈSettings
    • Workspace
    • Trading modes
    • Connections
      • Crypto exchanges
        • TigerX
        • Binance via Tiger.com Broker
        • Bybit via Tiger.com Broker
        • Binance
        • Bitfinex
        • BitMEX
        • Bybit
        • OKX
        • Gate.io
        • MEXC
      • Trading terminals
        • DataFeed
        • MetaTrader 5
        • OEC Trader (GAIN Capital)
        • Rithmic
        • QUIK
        • SmartCOM
        • Trader WorkStation (Interactive Brokers)
        • Transaq Connector
    • Basic setup
      • Selecting a symbol
      • Selecting a timeframe
      • Linking windows
      • Setting up exit strategies
      • Setting offsets
    • Terminal
      • Order volumes preset
      • Configuration
        • How to create public configuration
      • General settings
      • Email notifications
      • Telegram alerts
      • Hotkeys Manager
      • Sound alerts
      • Symbols manager
  • πŸ–₯️Windows
    • Chart
      • Chart trading
      • Toolbar setup
      • Chart theme
      • Chart settings
      • Scaling and moving the chart
      • Cluster chart
      • Cluster presets
      • Graphical objects in Chart window
        • Text
        • Ruler
        • Volume profile
        • Trend angle
        • Elliott Correction Wave
        • Elliott Impulse Wave
        • Fibonacci Fan
        • Fibonacci Extensions
        • Fibonacci Retracement
        • Fibonacci Time Zones
        • Linear Regression
      • Main indicators in Chart window
        • Bar Search
        • Bar Timer
        • Bid Ask
        • Big Trades
        • BW MFI
        • Cluster Search
        • Cluster Statistic
        • Cumulative Delta
        • Delta
        • Depth Of Market
        • Dynamic Levels
        • Elders Force Index
        • External Chart
        • Gradient
        • Heatmap
        • High Low
        • Histogram
        • Ichimoku
        • Margin Zones
        • Maximum Levels
        • Open Interest
        • Price
        • Session Color
        • Trades Flow
        • Volume
        • Volume Profile
        • VWAP
        • Weis Wave Volume
        • ZigZag
    • DOM
      • Selecting a trading account
      • Ruler
      • Multiplier
      • DOM settings
        • Main
        • Cluster
        • Trading
      • Stop Loss
      • Take Profit
      • Trigger orders
    • Watchlist
      • How to filter watchlist
    • SmartTape
    • Statistics
    • Volume search
    • All prices
    • Options board
  • πŸ“‹Tables
    • Orders
    • Executions
    • Positions
    • XPositions
    • Accounts
    • Limits
    • Player
    • Signals
    • Log
  • πŸ”Video Tutorials
    • Terminal Basics
    • Charts and technical analysis
    • Charts, Trade Tape and Player
    • Trading in Tiger.com Windows
  • πŸ’‘Platform Updates
    • Version 7.0 Beta
    • Version 6.9
    • Version 6.8
    • Version 6.7
    • Version 6.6
    • Version 6.5
    • Version 6.4
    • Version 6.3
    • Version 6.2
    • Version 6.1
    • Version 6.0.2
    • Version 6.0.1
    • Version 6.0.0
    • Version 5.0.7
    • Version 4.5.15
  • ⌨️Development for Tiger.com Windows
    • Indicator examples
      • DepthOfMarket
      • Trades Flow
      • Volume Profiles
      • Cluster Search
      • Bar Search
      • VWAP
      • Bar Timer
      • Volume
      • Trades
      • Session color
      • Open Interest
      • Dynamic Levels
      • Delta
      • Cumulative Delta
      • Cluster Statistic
      • Bid Ask
      • External Chart
      • High Low
      • Histogram
    • Source examples
      • Moving Average
      • Stock
    • Examples of graphical objects
      • Fibonacci Extensions
      • Fibonacci Retracement
      • Vertical Line
      • Horizontal Line
      • Volume Profile
      • Rectangle
  • ❓Frequently Asked Questions
    • Questions about licenses
    • Questions about indicators
    • Questions about connections
    • Questions about trading
    • Error "Connection lost" when launching the terminal
    • Why do I need to set commission in the terminal?
    • How does automatic account selection work?
    • How do I optimize the terminal to improve performance?
    • How to use the Crypto license to trade on Binance
    • Which order types are available in Tiger.com?
    • How to set up chart auto-refresh for QUIK connection?
    • How do I enable Take Profit orders on Binance?
    • How does autoselection of data type work?
    • Troubles installing the terminal
    • What are the system requirements for the terminal?
    • How to provide Tiger.com Windows app logs for investigation?
    • Fixing Network Issues: How to Adjust DNS Settings
  • πŸ“¬Technical support
  • πŸ’­Suggest an improvement
Powered by GitBook
On this page
  1. Development for Tiger.com Windows
  2. Examples of graphical objects

Fibonacci Retracement

//------------------------------------------------------------------------------
//
// ГрафичСский ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ FibonacciRetracement. Copyright (c) 2023 Tiger Trade Capital AG. All rights reserved.
//
//------------------------------------------------------------------------------
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Media;
using TigerTrade.Chart.Base;
using TigerTrade.Chart.Objects.Common;
using TigerTrade.Chart.Objects.Enums;
using TigerTrade.Dx;
using TigerTrade.Dx.Enums;
 
namespace TigerTrade.Chart.Objects.Custom
{
    [DataContract(Name = "FibonacciRetracementObject",
        Namespace = "http://schemas.datacontract.org/2004/07/TigerTrade.Chart.Objects.Custom")]
    [ChartObject("X_FibonacciRetracement", "Fibonacci Retracement", 2, Type = typeof(FibonacciRetracementObject))]
    public sealed class FibonacciRetracementObject : ObjectBase
    {
        [Browsable(false)]
        private XBrush LineBrush { get; set; }
 
        [Browsable(false)]
        public XPen LinePen { get; private set; }
 
        private XColor _lineColor;
 
        [DataMember(Name = "LineColor")]
        [Category("Линия"), DisplayName("Π¦Π²Π΅Ρ‚ Π»ΠΈΠ½ΠΈΠΈ")]
        public XColor LineColor
        {
            get => _lineColor;
            set
            {
                if (value == _lineColor)
                {
                    return;
                }
 
                _lineColor = value;
 
                LineBrush = new XBrush(_lineColor);
                LinePen = new XPen(LineBrush, LineWidth, LineStyle);
 
                OnPropertyChanged();
            }
        }
 
        private int _lineWidth;
 
        [DataMember(Name = "LineWidth")]
        [Category("Линия"), DisplayName("Π’ΠΎΠ»Ρ‰ΠΈΠ½Π° Π»ΠΈΠ½ΠΈΠΈ")]
        public int LineWidth
        {
            get => _lineWidth;
            set
            {
                value = Math.Max(1, Math.Min(10, value));
 
                if (value == _lineWidth)
                {
                    return;
                }
 
                _lineWidth = value;
 
                LinePen = new XPen(LineBrush, _lineWidth, LineStyle);
 
                OnPropertyChanged();
            }
        }
 
        private XDashStyle _lineStyle;
 
        [DataMember(Name = "LineStyle")]
        [Category("Линия"), DisplayName("Π‘Ρ‚ΠΈΠ»ΡŒ Π»ΠΈΠ½ΠΈΠΈ")]
        public XDashStyle LineStyle
        {
            get => _lineStyle;
            set
            {
                if (value == _lineStyle)
                {
                    return;
                }
 
                _lineStyle = value;
 
                LinePen = new XPen(LineBrush, LineWidth, _lineStyle);
 
                OnPropertyChanged();
            }
        }
 
        private bool _openStart;
 
        [DataMember(Name = "OpenStart")]
        [Category("Линия"), DisplayName("ΠŸΡ€ΠΎΠ΄Π»ΠΈΡ‚ΡŒ Π²Π»Π΅Π²ΠΎ")]
        public bool OpenStart
        {
            get => _openStart;
            set
            {
                if (value == _openStart)
                {
                    return;
                }
 
                _openStart = value;
 
                OnPropertyChanged();
            }
        }
 
        private bool _openEnd;
 
        [DataMember(Name = "OpenEnd")]
        [Category("Линия"), DisplayName("ΠŸΡ€ΠΎΠ΄Π»ΠΈΡ‚ΡŒ Π²ΠΏΡ€Π°Π²ΠΎ")]
        public bool OpenEnd
        {
            get => _openEnd;
            set
            {
                if (value == _openEnd)
                {
                    return;
                }
 
                _openEnd = value;
 
                OnPropertyChanged();
            }
        }
 
        private ObjectTextAlignment _textAlignment;
 
        [DataMember(Name = "TextAlignment")]
        [Category("ВСкст"), DisplayName("РасполоТСниС")]
        public ObjectTextAlignment TextAlignment
        {
            get => _textAlignment;
            set
            {
                if (value == _textAlignment)
                {
                    return;
                }
 
                _textAlignment = value;
 
                OnPropertyChanged();
            }
        }
 
        private bool _customLevels;
 
        [DataMember(Name = "CustomLevels")]
        [Category("Π‘Π²ΠΎΠΈ ΡƒΡ€ΠΎΠ²Π½ΠΈ"), DisplayName("Π’ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ")]
        public bool CustomLevels
        {
            get => _customLevels;
            set
            {
                if (value == _customLevels)
                {
                    return;
                }
 
                _customLevels = value;
 
                OnPropertyChanged();
            }
        }
 
        private List<ObjectLine> _lines;
 
        [DataMember(Name = "Levels")]
        [Category("Π‘Π²ΠΎΠΈ ΡƒΡ€ΠΎΠ²Π½ΠΈ"), DisplayName("Π£Ρ€ΠΎΠ²Π½ΠΈ")]
        public List<ObjectLine> Levels
        {
            get => _lines ?? (_lines = new List<ObjectLine>());
            set
            {
                if (Equals(value, _lines))
                {
                    return;
                }
 
                _lines = value;
 
                OnPropertyChanged();
            }
        }
 
        private Point[] _startPoints;
        private Point[] _endPoints;
 
        private double[] _split;
 
        protected override int PenWidth => LineWidth;
 
        public FibonacciRetracementObject()
        {
            LineColor = Colors.Black;
            LineWidth = 1;
            LineStyle = XDashStyle.Solid;
 
            OpenStart = false;
            OpenEnd = false;
 
            TextAlignment = ObjectTextAlignment.LeftBottom;
 
            CustomLevels = false;
 
            Levels = new List<ObjectLine>
            {
                new ObjectLine(0.0, Colors.Black),
                new ObjectLine(23.6, Colors.Black),
                new ObjectLine(38.2, Colors.Black),
                new ObjectLine(50.0, Colors.Black),
                new ObjectLine(61.8, Colors.Black),
                new ObjectLine(78.6, Colors.Black),
                new ObjectLine(100.0, Colors.Black)
            };
        }
 
        protected override void Draw(DxVisualQueue visual, ref List<ObjectLabelInfo> labels)
        {
            CalcPoint();
 
            if (_startPoints == null || _endPoints == null)
            {
                return;
            }
 
            var p1 = ToPoint(ControlPoints[0]);
            var p2 = ToPoint(ControlPoints[1]);
 
            if (InMove && p1 != new Point() && p2 != new Point())
            {
                visual.DrawLine(LinePen, p1.X, p1.Y, p2.X, p2.Y);
            }
 
            for (var i = 0; i < _startPoints.Length; i++)
            {
                if (CustomLevels)
                {
                    var level = Levels[i];
 
                    if (!level.ShowLine)
                    {
                        continue;
                    }
 
                    visual.DrawLine(level.LinePen, _startPoints[i], _endPoints[i]);
 
                    if (TextAlignment != ObjectTextAlignment.Hide)
                    {
                        visual.DrawString(GetStr(i), Canvas.ChartFont, level.LineBrush, GetRect(i, level.LineWidth));
                    }
                }
                else
                {
                    visual.DrawLine(LinePen, _startPoints[i], _endPoints[i]);
 
                    if (TextAlignment != ObjectTextAlignment.Hide)
                    {
                        visual.DrawString(GetStr(i), Canvas.ChartFont, LineBrush, GetRect(i, LineWidth));
                    }
                }
            }
        }
 
        private string GetStr(int i)
        {
            var p = DataProvider.Symbol.FormatPrice((decimal)GetPrice(i), true);
 
            return $"{_split[i]:P2} ({p})";
        }
 
        private Rect GetRect(int i, int lineWidth)
        {
            var textSize = Canvas.ChartFont.GetSize(GetStr(i));
 
            var x = 0.0;
            var y = 0.0;
 
            var width = textSize.Width;
 
            switch (TextAlignment)
            {
                case ObjectTextAlignment.LeftTop:
                case ObjectTextAlignment.CenterTop:
                case ObjectTextAlignment.RightTop:
 
                    y = _startPoints[i].Y - 4 - Math.Ceiling(lineWidth / 2.0) - textSize.Height;
 
                    break;
 
                case ObjectTextAlignment.LeftMiddle:
                case ObjectTextAlignment.CenterMiddle:
                case ObjectTextAlignment.RightMiddle:
 
                    y = _startPoints[i].Y - 4 - Math.Ceiling(lineWidth / 2.0);
 
                    break;
 
                case ObjectTextAlignment.LeftBottom:
                case ObjectTextAlignment.CenterBottom:
                case ObjectTextAlignment.RightBottom:
 
                    y = _startPoints[i].Y + 4 + Math.Ceiling(lineWidth / 2.0);
 
                    break;
            }
 
            switch (TextAlignment)
            {
                case ObjectTextAlignment.LeftTop:
                case ObjectTextAlignment.LeftMiddle:
                case ObjectTextAlignment.LeftBottom:
 
                    x = Math.Min(_startPoints[i].X, _endPoints[i].X) + 4;
 
                    break;
 
                case ObjectTextAlignment.CenterTop:
                case ObjectTextAlignment.CenterMiddle:
                case ObjectTextAlignment.CenterBottom:
 
                    x = (_startPoints[i].X + _endPoints[i].X - width) / 2.0;
 
                    break;
 
                case ObjectTextAlignment.RightTop:
                case ObjectTextAlignment.RightMiddle:
                case ObjectTextAlignment.RightBottom:
 
                    x = Math.Max(_startPoints[i].X, _endPoints[i].X) - width - 4;
 
                    break;
            }
 
            return new Rect(x, y, width, textSize.Height);
        }
 
        private void CalcPoint()
        {
            if (CustomLevels)
            {
                _split = new double[Levels.Count];
 
                for (var i = 0; i < Levels.Count; i++)
                {
                    _split[i] = Levels[i].Value / 100.0;
                }
            }
            else
            {
                _split = new[] { 0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0 };
            }
 
            var p1 = ToPoint(ControlPoints[0]);
            var p2 = ToPoint(ControlPoints[1]);
 
            _startPoints = new Point[_split.Length];
            _endPoints = new Point[_split.Length];
 
            var op = new ObjectPoint(ControlPoints[0].X, 0.0);
 
            for (var i = 0; i < _split.Length; i++)
            {
                op.Y = GetPrice(i);
 
                var p3 = ToPoint(op);
 
                _startPoints[i] = new Point(p1.X, p3.Y);
                _endPoints[i] = new Point(p2.X, p3.Y);
            }
 
            for (var i = 0; i < _startPoints.Length; i++)
            {
                if (_startPoints[i].X <= _endPoints[i].X)
                {
                    if (OpenStart)
                    {
                        _startPoints[i].X = 0;
                    }
 
                    if (OpenEnd)
                    {
                        _endPoints[i].X = Canvas.Rect.Right;
                    }
                }
                else
                {
                    if (OpenStart)
                    {
                        _endPoints[i].X = 0;
                    }
 
                    if (OpenEnd)
                    {
                        _startPoints[i].X = Canvas.Rect.Right;
                    }
                }
            }
        }
 
        private double GetPrice(int lineIndex)
        {
            return (ControlPoints[0].Y - ControlPoints[1].Y) * _split[lineIndex] +
                   ControlPoints[1].Y;
        }
 
        protected override bool InObject(int x, int y)
        {
            if (_startPoints == null || _endPoints == null || _startPoints.Length != Levels.Count)
            {
                return false;
            }
 
            for (var i = 0; i < _startPoints.Length; i++)
            {
                if (_startPoints[i] == new Point() || (CustomLevels && !Levels[i].ShowLine))
                {
                    continue;
                }
 
                var result = InLineSegment(x, y, _startPoints[i], _endPoints[i], PenWidth + 2);
 
                if (result)
                {
                    return true;
                }
            }
 
            return false;
        }
 
        private static bool InLineSegment(int x, int y, Point p1, Point p2, int width)
        {
            var n1 = Math.Max(p1.X, p2.X);
            var n2 = Math.Min(p1.X, p2.X);
            var n3 = Math.Max(p1.Y, p2.Y);
            var n4 = Math.Min(p1.Y, p2.Y);
 
            return (Math.Abs(Dist(x, y, p1, p2)) <= width) && (x <= n1 + width) && (x >= n2 - width) &&
                   (y <= n3 + width) && (y >= n4 - width);
        }
 
        private static double Dist(int x, int y, Point p1, Point p2)
        {
            var d1 = p1.X - p2.X;
            var d2 = p1.Y - p2.Y;
 
            return ((x - p1.X) * (p2.Y - p1.Y) - (p2.X - p1.X) * (y - p1.Y)) / Math.Sqrt(d1 * d1 + d2 * d2);
        }
 
        public override void ApplyTheme(IChartTheme theme)
        {
            base.ApplyTheme(theme);
 
            LineColor = theme.ChartObjectLineColor;
 
            foreach (var level in Levels)
            {
                level.LineColor = theme.ChartObjectLineColor;
            }
        }
 
        public override void CopyTemplate(ObjectBase objectBase, bool style)
        {
            if (objectBase is FibonacciRetracementObject obj)
            {
                LineColor = obj.LineColor;
                LineWidth = obj.LineWidth;
                LineStyle = obj.LineStyle;
 
                OpenStart = obj.OpenStart;
                OpenEnd = obj.OpenEnd;
 
                TextAlignment = obj.TextAlignment;
 
                CustomLevels = obj.CustomLevels;
 
                Levels = new List<ObjectLine>();
 
                foreach (var level in obj.Levels)
                {
                    Levels.Add(new ObjectLine(level));
                }
 
                OnPropertyChanged(nameof(Levels));
            }
 
            base.CopyTemplate(objectBase, style);
        }
    }
}
PreviousFibonacci ExtensionsNextVertical Line

Last updated 2 years ago

⌨️