Category Trend  at 05/04/2023

Auto TrendlinesPair with Retest

Description

Similar to my Auto Trendlines with Retest, however you add a pair in 1 shot, one on Price Chart an another one on an Oscillator. It could be useful to spot Divergence.

Period: Period for finding local Extrema.

Ctrl click on a place on Chart to do retest. When retest active, a vertical dot yellow line appears, drag the line to move retest. Press delete to delete the retest and switch to live mode.

The display beside each trendline are the angle and the period. Angle (Period).


using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using LittleTrader;
using LittleTrader.Models.PriceActions;
using LittleTrader.Extensions;
using LittleTrader.Huy;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class LT_Ind_TrendLinesPair : Indicator
    {
        [Parameter(Group = "Sources")]
        public DataSeries HighSource { get; set; }
        [Parameter(Group = "Sources")]
        public DataSeries LowSource { get; set; }

        [Parameter(Group = "Sources")]
        public DataSeries OscHigh { get; set; }
        [Parameter(Group = "Sources")]
        public DataSeries OscLow { get; set; }

        [Parameter(DefaultValue = 20, MinValue = 1, Group = "SwingsFinder")]
        public int Period { get; set; }

        ChartVerticalLine _shiftLine;
        DateTime _lastShiftLineTime;

        protected override void Initialize()
        {
            Chart.ObjectsUpdated += e =>
            {
                if (_shiftLine != null && _shiftLine.Time != _lastShiftLineTime)
                {
                    _lastShiftLineTime = _shiftLine.Time;
                    var index = Bars.OpenTimes.GetIndexByTime(_shiftLine.Time);
                    RetestAt(index);
                }
            };

            Chart.MouseDown += e =>
            {
                if (e.CtrlKey == true)
                {
                    var index = (int)e.BarIndex;
                    DrawShiftLine(index);
                    RetestAt(index);
                }
            };

            Chart.ObjectsRemoved += e =>
            {
                if (e.ChartObjects[0] == _shiftLine)
                {
                    _shiftLine = null;
                    Print("Shift line removed!");
                    Calculate(Bars.GetLastIndex());
                }
            };
        }

        void RetestAt(int index)
        {
            CalTrendlines(index);
            Print($"Retest at {index}");
        }

        void DrawShiftLine(int index)
        {
            _shiftLine = Chart.DrawVerticalLine("shift", index, Color.Yellow, 1, LineStyle.DotsRare);
            _shiftLine.IsInteractive = true;
        }

        public override void Calculate(int index)
        {
            if (_shiftLine != null) return;
            try
            {
                CalTrendlines(index);
            }
            catch (LTNoDataException)
            {
            }
            catch (Exception ex)
            {
                Print(ex.ToString());
            }
        }

        class TrendData
        {
            public DataSeries High { get; set; }
            public DataSeries Low { get; set; }
            public ChartArea Chart { get; set; }
        }

        void CalTrendlines(int index)
        {
            var list = new List<TrendData>
            {
                new TrendData { High = HighSource, Low = LowSource, Chart = Chart },
                new TrendData { High = OscHigh, Low = OscLow, Chart = IndicatorArea },
            };

            try
            {
                foreach (var item in list)
                {
                    var high = new Trendline(item.High, Swings.High, Period, index);
                    var low = new Trendline(item.Low, Swings.Low, Period, index);
                    high.Draw(item.Chart, "high", 200);
                    low.Draw(item.Chart, "low", 200);
                }
            }

            catch (LTNoDataException)
            { }
        }

    }
}


dhnhuy's avatar
dhnhuy

Joined 03.04.2023

  • Type: Free
  • Language: C#
  • Trading Platform: cTrader Automate
  • Filename: LT_Ind_TrendLinesPair.algo
  • Rating: 5
  • Installs: 842
Comments
Only logged in users can post a comment
Comments not found