Relative Trend Index Indicator
Created at 09 Jan 2024, 23:42
Relative Trend Index Indicator
09 Jan 2024, 23:42
Hello, I am trying to convert RTI mq4 file to cAlgo indicator with Chat-GTP as I am not familier to programing. I get no succsess so far but I think almost there.
Here is RTI mq4.
// More information about this indicator can be found at:
// https://fxcodebase.com/code/viewtopic.php?f=38&t=74207
//+------------------------------------------------------------------------------------------------+
//| Copyright © 2023, Gehtsoft USA LLC |
//| http://fxcodebase.com |
//+------------------------------------------------------------------------------------------------+
//| Developed by : Mario Jemic |
//| mario.jemic@gmail.com |
//+------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| Our work would not be possible without your support. |
//+------------------------------------------------------------------------------------------------+
//| Paypal: https://goo.gl/9Rj74e |
//| Patreon : https://goo.gl/GdXWeN |
//| Buy Me a Coffee: http://tiny.cc/pjh9vz |
//+------------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2023, Gehtsoft USA LLC"
#property link "http://fxcodebase.com"
#property version "1.0"
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Yellow
#property indicator_color2 Lime
//
input int stdev_period = 20;
input int trend_length = 200;
input int signal_period = 20;
input ENUM_MA_METHOD signal_method = MODE_EMA;
//
double ut[], lt[], uth[], lth[];
double rti[], sig[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexBuffer(0, rti);
SetIndexBuffer(1, sig);
//
SetIndexStyle(2, DRAW_NONE);
SetIndexBuffer(2, ut);
SetIndexStyle(3, DRAW_NONE);
SetIndexBuffer(3, lt);
SetIndexStyle(4, DRAW_NONE);
SetIndexBuffer(4, uth);
SetIndexStyle(5, DRAW_NONE);
SetIndexBuffer(5, lth);
//
IndicatorShortName("Relative Trend Index (" + string(stdev_period) + "," + string(trend_length) + "," + string(signal_period) + ")");
SetIndexDrawBegin(0, trend_length + 2);
IndicatorDigits(5);
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int counted_bars = IndicatorCounted();
int shift;
if(counted_bars < 0)
return(-1);
if(counted_bars > 0)
counted_bars--;
int limit = Bars - counted_bars;
if(counted_bars == 0)
limit -= 1 + stdev_period;
for(shift = limit - 1; shift >= 0; shift--)
{
double stdev = iStdDev(Symbol(), Period(), stdev_period, 0, 0, 0, shift);
ut[shift] = Close[shift] + stdev;
lt[shift] = Close[shift] - stdev;
}
for(shift = limit - 1; shift >= 0; shift--)
{
uth[shift] = ut[ArrayMaximum(ut, trend_length, shift)];
lth[shift] = lt[ArrayMinimum(lt, trend_length, shift)];
}
for(shift = limit - 1; shift >= 0; shift--)
{
rti[shift] = ((Close[shift] - lth[shift]) / (uth[shift] - lth[shift])) * 100;
}
for(shift = limit - 1; shift >= 0; shift--)
{
sig[shift] = iMAOnArray(rti, 0, signal_period, 0, signal_method, shift);
}
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------------------------------------+
//| We appreciate your support. |
//+------------------------------------------------------------------------------------------------+
//| Paypal: https://goo.gl/9Rj74e |
//| Patreon : https://goo.gl/GdXWeN |
//| Buy Me a Coffee: http://tiny.cc/pjh9vz |
//+------------------------------------------------------------------------------------------------+
//| USDT Donations |
//+------------------------------------------------+-----------------------------------------------+
//| Network | Address |
//+------------------------------------------------+-----------------------------------------------+
//| ERC20 (ETH Ethereum) | 0xe53aab6bc468a963a02d1319660ee60cf80fc8e7 |
//| TRC20 (Tron) | TTBXsfuPm2rk36AkdemY7muNXGjyziC86g |
//| BEP20 (BSC BNB Smart Chain) | 0xe53aab6bc468a963a02d1319660ee60cf80fc8e7 |
//| Matic Polygon | 0xe53aab6bc468a963a02d1319660ee60cf80fc8e7 |
//| SOL Solana | 3nh5rpUKopcYLNU4zGCdUFAkM3iRQq8VVUmuzVG6VDf2 |
//| ARBITRUM Arbitrum One | 0xe53aab6bc468a963a02d1319660ee60cf80fc8e7 |
//+------------------------------------------------+-----------------------------------------------+
and what I get so far is below. It has no error that I can buit and use it but it is not working properly.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RelativeTrendIndex : Indicator
{
private IndicatorDataSeries _rti;
private IndicatorDataSeries _maRelativeTrendIndex;
private IndicatorDataSeries _upperTrend;
private IndicatorDataSeries _lowerTrend;
private IndicatorDataSeries _upperTrendHigh;
private IndicatorDataSeries _lowerTrendLow;
[Parameter("Standard Deviation Period", DefaultValue = 20, MinValue = 1)]
public int StDevPeriod { get; set; }
[Parameter("Trend Length", DefaultValue = 200, MinValue = 1)]
public int TrendLength { get; set; }
[Parameter("Signal Period", DefaultValue = 20, MinValue = 1)]
public int SignalPeriod { get; set; }
[Parameter("Signal Method", DefaultValue = MovingAverageType.Exponential)]
public MovingAverageType SignalMethod { get; set; }
protected override void Initialize()
{
_rti = CreateDataSeries();
_maRelativeTrendIndex = CreateDataSeries();
_upperTrend = CreateDataSeries();
_lowerTrend = CreateDataSeries();
_upperTrendHigh = CreateDataSeries();
_lowerTrendLow = CreateDataSeries();
}
public override void Calculate(int index)
{
double stDev = CalculateStDev(index);
_upperTrend[index] = Bars.ClosePrices[index] + stDev;
_lowerTrend[index] = Bars.ClosePrices[index] - stDev;
CalculateUpperTrendHigh(index);
CalculateLowerTrendLow(index);
_rti[index] = ((_lowerTrendLow[index] == _upperTrendHigh[index]) ? 0 : (Bars.ClosePrices[index] - _lowerTrendLow[index]) / (_upperTrendHigh[index] - _lowerTrendLow[index])) * 100;
_maRelativeTrendIndex[index] = CalculateMA(_rti, SignalPeriod, SignalMethod, index);
}
private double CalculateStDev(int index)
{
int startIndex = Math.Max(0, index - StDevPeriod + 1);
int length = Math.Min(index + 1, StDevPeriod);
double sum = 0;
double sumSquared = 0;
for (int i = startIndex; i < startIndex + length; i++)
{
double diff = Bars.ClosePrices[i] - Bars.ClosePrices[index];
sum += diff;
sumSquared += diff * diff;
}
double variance = sumSquared / length - (sum / length) * (sum / length);
return Math.Sqrt(variance);
}
private void CalculateUpperTrendHigh(int index)
{
double max = double.MinValue;
for (int i = index - TrendLength + 1; i <= index; i++)
{
max = Math.Max(max, _upperTrend[i]);
}
_upperTrendHigh[index] = max;
}
private void CalculateLowerTrendLow(int index)
{
double min = double.MaxValue;
for (int i = index - TrendLength + 1; i <= index; i++)
{
min = Math.Min(min, _lowerTrend[i]);
}
_lowerTrendLow[index] = min;
}
private double CalculateMA(IndicatorDataSeries source, int period, MovingAverageType type, int index)
{
double sum = 0;
for (int i = index - period + 1; i <= index; i++)
{
sum += source[i];
}
return sum / period;
}
}
}
I would be grateful if someone fix it.
Thanks
Ken