Category Trend  Published on 21/05/2024

Trend Force Angle

Description

After doing https://ctrader.com/algos/indicators/show/4258 , I lost 5 more minutes and added this, the power of the angle of an MA.

Enjoy for Free =) 

Previous account here : https://ctrader.com/users/profile/70920
Contact telegram :  https://t.me/nimi012 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class STOCHTF : Indicator
    {
        [Parameter("FastLimit", DefaultValue = "Daily", Group = "Calculation Type")]
        public TimeFrame TF1 { get; set; }
        [Parameter("DrawMode (slope)", DefaultValue = EnumDrawMode.DrawModeSlope, Group = "Calculation Type")]
        public EnumDrawMode DrawMode { get; set; }
        public enum EnumDrawMode
        {
            DrawModeSteps,
            DrawModeSlope
        }

        [Parameter("Lookback Angle Calculation", DefaultValue = 1, Group = "Calculation Angle")]
        public int LoockbackPeriods { get; set; }
        [Parameter("Period", DefaultValue = 9, Group = "Calculation Angle")]
        public int Period { get; set; }
        [Parameter("Ma Type", DefaultValue = MovingAverageType.Weighted, Group = "Calculation Angle")]
        public MovingAverageType MaTypePeriod { get; set; }
        [Parameter("Period Signal", DefaultValue = 9, Group = "Calculation Angle")]
        public int PeriodSignal { get; set; }
        [Parameter("Ma Type", DefaultValue = MovingAverageType.Weighted, Group = "Calculation Angle")]
        public MovingAverageType MaTypePeriodSignal { get; set; }

        [Output("Result", PlotType = PlotType.Line, LineColor = "Lime", Thickness = 1)]
        public IndicatorDataSeries Result { get; set; }
        [Output("Signal", PlotType = PlotType.Line, LineColor = "Red", Thickness = 1)]
        public IndicatorDataSeries Signal { get; set; }

        private MovingAverage ma, signal;
        private AverageTrueRange atr;
        private Bars bars1;
        private IndicatorDataSeries res;

        protected override void Initialize()
        {
            bars1 = MarketData.GetBars(TF1);
            res = CreateDataSeries();

            if (!IsBacktesting)
            {
                while (bars1.OpenTimes[0] > Bars.OpenTimes[0])
                    bars1.LoadMoreHistory();
            }

            ma = Indicators.MovingAverage(bars1.ClosePrices, Period, MaTypePeriod);
            atr = Indicators.AverageTrueRange(bars1, 500, MaTypePeriod);
            signal = Indicators.MovingAverage(res, PeriodSignal, MaTypePeriodSignal);
        }

        public override void Calculate(int index)
        {
            int idx1 = TF1 == Chart.TimeFrame ? index : bars1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            int idx11 = TF1 == Chart.TimeFrame ? index - 1 : bars1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

            Result[index] = idx1 >= 0 ? GetCalculationAngle(ma.Result[idx1], ma.Result[idx1 - LoockbackPeriods], atr.Result[idx1]) : 50.0;
            res[idx1] = GetCalculationAngle(ma.Result[idx1], ma.Result[idx1 - LoockbackPeriods], atr.Result[idx1]);
            Signal[index] = idx1 >= 0 ? signal.Result[idx1] : 50.0;

            if (idx1 >= 0 && DrawMode == EnumDrawMode.DrawModeSlope && TF1 != Chart.TimeFrame && Bars.OpenTimes.GetIndexByTime(bars1.OpenTimes[idx1]) == Bars.OpenTimes.GetIndexByTime(bars1.OpenTimes[idx11]))
            {
                Result[index - 1] = double.NaN;
                Signal[index - 1] = double.NaN;
            }

        }

        public double GetCalculationAngle(double priceSmooth, double priceSmoothLoockBack, double atr)
        {
            var _momentumpositive = priceSmooth - priceSmoothLoockBack;
            var _momentumnegative = priceSmoothLoockBack - priceSmooth;
            var _momentum = priceSmooth > priceSmoothLoockBack
                         ? _momentumpositive / atr
                         : _momentumnegative / atr;
            var _hypothenuse = Math.Sqrt((_momentum * _momentum) + (LoockbackPeriods * LoockbackPeriods));
            var _cos = (LoockbackPeriods / _hypothenuse);
            var _angle = priceSmooth > priceSmoothLoockBack
                         ? (0 + (Math.Acos(_cos) * 100))
                         : (0 - (Math.Acos(_cos) * 100));
            return _angle;
        }
    }
}

YE
YesOrNot2

Joined on 17.05.2024

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Ma Tf Angle_withSourceCode.algo
  • Rating: 5
  • Installs: 441
  • Modified: 21/05/2024 22:03
Comments
Log in to add a comment.
No comments found.