Description
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System;
using System.Linq;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class MomentumIndicator : Indicator
{
[Parameter("N Value", DefaultValue = 60, MinValue = 1)]
public int N { get; set; }
[Parameter("N1 Value", DefaultValue = 40, MinValue = 1)]
public int N1 { get; set; }
[Output("MTM", LineColor = "Blue")]
public IndicatorDataSeries MTM { get; set; }
[Output("MTMMA", LineColor = "Green")]
public IndicatorDataSeries MTMMA { get; set; }
private MovingAverage _mtmma;
protected override void Initialize()
{
// Initialize and create a moving average
_mtmma = Indicators.MovingAverage(MTM, N1, MovingAverageType.Simple);
}
public override void Calculate(int index)
{
// Calculate MTM
MTM[index] = Bars.ClosePrices[index] - Bars.ClosePrices[Math.Max(0, index - N)];
// Calculate MTMMA
_mtmma.Calculate(index);
MTMMA[index] = _mtmma.Result[index];
// Change the line color based on condition
if (MTM[index] < MTMMA[index])
{
ChartObjects.DrawText("colorTextRed", "MTM < MTMMA", StaticPosition.TopLeft, Colors.Red);
ChartObjects.RemoveObject("colorTextGreen");
}
else if (MTM[index] > MTMMA[index])
{
ChartObjects.DrawText("colorTextGreen", "MTM > MTMMA", StaticPosition.TopLeft, Colors.Green);
ChartObjects.RemoveObject("colorTextRed");
}
}
}
}
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Levels(0)]
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
public class mMomentumTendency : Indicator
{
[Parameter("Periods (14)", DefaultValue = 14)]
public int inpPeriods { get; set; }
[Parameter("Periods (3)", DefaultValue = 3)]
public int inpLookBack { get; set; }
[Parameter("Smooth Type (vid)", DefaultValue = MovingAverageType.VIDYA)]
public MovingAverageType inpSmoothType { get; set; }
[Parameter("Show Arrows ", DefaultValue = true)]
public bool inpShowArrows { get; set; }
[Output("MomentumTendency", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
public IndicatorDataSeries outMomentumTendency { get; set; }
private MovingAverage _ma;
private IndicatorDataSeries _momentum, _log, _tendency;
private DetrendedPriceOscillator _dpomom, _dpo;
protected override void Initialize()
{
_ma = Indicators.MovingAverage(Bars.ClosePrices, inpPeriods, inpSmoothType);
_log = CreateDataSeries();
_momentum = CreateDataSeries();
_dpomom = Indicators.DetrendedPriceOscillator(_momentum, inpPeriods, MovingAverageType.Simple);
_dpo = Indicators.DetrendedPriceOscillator(Bars.ClosePrices, inpPeriods, MovingAverageType.Simple);
_tendency = CreateDataSeries();
}
public override void Calculate(int i)
{
_log[i] = Math.Log(_ma.Result[i]);
_momentum[i] = _log[i-1] - _log[i-inpLookBack];
_tendency[i] = (_momentum[i] > 0 ? +1 : -1) + (_dpomom.Result[i] > 0 ? +1 : -1) + (_dpo.Result[i] > 0 ? +1 : -1);
if(inpShowArrows)
{
if(_tendency[i] == +3 && Bars.OpenPrices[i] > Bars.ClosePrices[i])
Chart.DrawIcon("MomentumTendencyUp" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.Star, Bars.OpenTimes.Last(0), Bars.LowPrices.Last(0), "Teal");
else
Chart.RemoveObject("MomentumTendencyUp" + Bars.OpenTimes.Last(0).ToString());
if(_tendency[i] == -3 && Bars.OpenPrices[i] < Bars.ClosePrices[i])
Chart.DrawIcon("MomentumTendencyDown" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.Star, Bars.OpenTimes.Last(0), Bars.HighPrices.Last(0), "Brown");
else
Chart.RemoveObject("MomentumTendencyDown" + Bars.OpenTimes.Last(0).ToString());
}
outMomentumTendency[i] = _tendency[i];
}
}
}
lolopopo
Joined on 07.06.2024
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: mMomentumTendency.algo
- Rating: 0
- Installs: 342
- Modified: 07/06/2024 23:37