Skip to content

Commit

Permalink
1.06
Browse files Browse the repository at this point in the history
Fixed issues with MTF.
  • Loading branch information
EarnForex authored Mar 31, 2022
1 parent d93bad4 commit 87b88d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 6 additions & 4 deletions TradersDynamicIndex.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//+------------------------------------------------------------------+
#property copyright "www.EarnForex.com, 2015-2022"
#property link "https://www.earnforex.com/metatrader-indicators/Traders-Dynamic-Index/"
#property version "1.05"
#property version "1.06"
#property strict

#property description "Shows trend direction, strength, and volatility."
Expand Down Expand Up @@ -136,17 +136,18 @@ int OnCalculate(const int rates_total,
int counted_bars = IndicatorCounted();
if (counted_bars > 0) counted_bars--;
int limit = Bars - 1 - counted_bars;

if (UpperTimeframe == Period())
if (PeriodSeconds(UpperTimeframe) == PeriodSeconds(Period()))
{
FillIndicatorBuffers((ENUM_TIMEFRAMES)Period(), limit, RSIBuf, UpZone, DnZone, MdZone, MaBuf, MbBuf);
if (FillIndicatorBuffers((ENUM_TIMEFRAMES)Period(), limit, RSIBuf, UpZone, DnZone, MdZone, MaBuf, MbBuf) < 0) return 0; // Bad value returned. Data not yet ready. Recalculate everything.
}
else
{
static int upper_prev_counted = 0;
if (upper_prev_counted > 0) upper_prev_counted--;
int upper_limit = iBars(Symbol(), UpperTimeframe) - 1 - upper_prev_counted;
if (upper_limit > Bars - Volatility_Band) upper_limit = Bars - Volatility_Band; // Buffers cannot hold more than the current period's bars worth of data!
upper_prev_counted = FillIndicatorBuffers(UpperTimeframe, upper_limit, _RSIBuf, _UpZone, _DnZone, _MdZone, _MaBuf, _MbBuf);
if (upper_prev_counted < -1) return 0; // Bad value returned. Data not yet ready. Recalculate everything.
for (int i = 0, j = 0; Time[i] >= iTime(Symbol(), UpperTimeframe, upper_limit); i++)
{
while ((iTime(Symbol(), UpperTimeframe, j) > Time[i]) && (j < iBars(Symbol(), UpperTimeframe))) j++;
Expand Down Expand Up @@ -343,6 +344,7 @@ int FillIndicatorBuffers(ENUM_TIMEFRAMES period, int limit, double& rsibuf[], do
for (int x = i; x < i + Volatility_Band; x++)
{
if (x > bars - 1) break;
if ((rsibuf[x] > 100) || (rsibuf[x] < 0)) return -1; // Bad RSI value. Try later.
RSI[x - i] = rsibuf[x];
MA += rsibuf[x] / Volatility_Band;
}
Expand Down
12 changes: 7 additions & 5 deletions TradersDynamicIndex.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//+------------------------------------------------------------------+
#property copyright "www.EarnForex.com, 2015-2022"
#property link "https://www.earnforex.com/metatrader-indicators/Traders-Dynamic-Index/"
#property version "1.05"
#property version "1.06"

#property description "Shows trend direction, strength, and volatility."
#property description "Green line - RSI Price line."
Expand Down Expand Up @@ -115,7 +115,7 @@ int OnInit()
PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, 0);

if (UpperTimeframe != Period())
if (PeriodSeconds(UpperTimeframe) != PeriodSeconds(Period()))
{
SetIndexBuffer(6, _RSIBuf, INDICATOR_CALCULATIONS);
SetIndexBuffer(7, _UpZone, INDICATOR_CALCULATIONS);
Expand All @@ -133,7 +133,7 @@ int OnInit()

IndicatorSetInteger(INDICATOR_DIGITS, 1);

RSI_handle = iRSI(Symbol(), Period(), RSI_Period, RSI_Price);
RSI_handle = iRSI(Symbol(), UpperTimeframe, RSI_Period, RSI_Price);

MaxPeriod = Volatility_Band + RSI_Period;

Expand Down Expand Up @@ -176,7 +176,7 @@ int OnCalculate(const int rates_total,
int limit = rates_total - 1 - counted_bars;
if (limit > rates_total - MaxPeriod - 1) limit = rates_total - MaxPeriod - 1;

if (UpperTimeframe == Period())
if (PeriodSeconds(UpperTimeframe) == PeriodSeconds(Period()))
{
if (FillIndicatorBuffers((ENUM_TIMEFRAMES)Period(), limit, RSIBuf, UpZone, DnZone, MdZone, MaBuf, MbBuf) == -1) return 0; // No RSI data yet.
}
Expand All @@ -186,6 +186,7 @@ int OnCalculate(const int rates_total,
if (upper_prev_counted > 0) upper_prev_counted--;
int upper_limit = iBars(Symbol(), UpperTimeframe) - 1 - upper_prev_counted;
if (upper_limit > iBars(Symbol(), UpperTimeframe) - MaxPeriod - 1) upper_limit = iBars(Symbol(), UpperTimeframe) - MaxPeriod - 1;
if (upper_limit > rates_total - Volatility_Band) upper_limit = rates_total - Volatility_Band; // Buffers cannot hold more than the current period's bars worth of data!
upper_prev_counted = FillIndicatorBuffers(UpperTimeframe, upper_limit, _RSIBuf, _UpZone, _DnZone, _MdZone, _MaBuf, _MbBuf);
if (upper_prev_counted == -1) return 0; // No RSI data yet.
for (int i = 0, j = 0; Time[i] >= iTime(Symbol(), UpperTimeframe, upper_limit); i++)
Expand Down Expand Up @@ -458,14 +459,15 @@ int FillIndicatorBuffers(ENUM_TIMEFRAMES period, int limit, double& rsibuf[], do

int RSI_bars = CopyBuffer(RSI_handle, 0, 0, i + Volatility_Band, rsibuf); // No need to copy all RSI for too old bars.

if (RSI_bars == -1) return -1;
if (RSI_bars < i + Volatility_Band) return -1;

// Calculate BB on RSI.
while (i >= 0)
{
MA = 0;
for (int x = i; x < i + Volatility_Band; x++)
{
if ((rsibuf[x] > 100) || (rsibuf[x] < 0)) return -1; // Bad RSI value. Try later.
RSI[x - i] = rsibuf[x];
MA += rsibuf[x] / Volatility_Band;
}
Expand Down

0 comments on commit 87b88d8

Please sign in to comment.