Skip to content

Commit

Permalink
Merge branch 'v3.004-dev' into v3.005-dev
Browse files Browse the repository at this point in the history
* v3.004-dev:
  Indicators: Renames some variables to avoid global variable conflict
  Indi_ADXW: Fixes logic for SetCustomIndicatorName()
  ADXW: Renames variables to avoid global conflicts
  Indi_ADXW: Fixes logic for SetCustomIndicatorName()
  ADXW: Renames variables to avoid global conflicts
  Cherry-pick: Added SerializeStub() method to Chart class. Added more error checks. (#629)
  • Loading branch information
kenorb committed Jun 24, 2023
2 parents e89966d + a593ae8 commit 2fa7dd0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 78 deletions.
75 changes: 37 additions & 38 deletions Indicators/Indi_ADXW.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct IndiADXWParams : IndiADXParams {
IndiADXWParams(int _period = 14, ENUM_APPLIED_PRICE _ap = PRICE_TYPICAL, int _shift = 0)
: IndiADXParams(_period, _ap, _shift) {
itype = itype == INDI_NONE || itype == INDI_ADX ? INDI_ADXW : itype;
if (custom_indi_name == "") {
if (custom_indi_name == "" || custom_indi_name == "Examples\\ADX") {
SetCustomIndicatorName("Examples\\ADXW");
}
};
Expand Down Expand Up @@ -158,33 +158,32 @@ class Indi_ADXW : public Indicator<IndiADXWParams> {
/**
* OnCalculate() method for ADXW indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ExtADXWBuffer,
ValueStorage<double> &ExtPDIBuffer, ValueStorage<double> &ExtNDIBuffer,
ValueStorage<double> &ExtPDSBuffer, ValueStorage<double> &ExtNDSBuffer,
ValueStorage<double> &ExtPDBuffer, ValueStorage<double> &ExtNDBuffer,
ValueStorage<double> &ExtTRBuffer, ValueStorage<double> &ExtATRBuffer,
ValueStorage<double> &ExtDXBuffer, int ExtADXWPeriod) {
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &_adxw_buff,
ValueStorage<double> &_pdi_buff, ValueStorage<double> &_ndi_buff,
ValueStorage<double> &_pds_buff, ValueStorage<double> &_nds_buff,
ValueStorage<double> &_pdb_buff, ValueStorage<double> &_nd_buff, ValueStorage<double> &_tr_buff,
ValueStorage<double> &_atr_buff, ValueStorage<double> &_dx_buff, int _adxw_period) {
int i;

// Checking for bars count.
if (rates_total < ExtADXWPeriod) return (0);
if (rates_total < _adxw_period) return (0);
// Detect start position.
int start;
if (prev_calculated > 1)
start = prev_calculated - 1;
else {
start = 1;
for (i = 0; i < ExtADXWPeriod; i++) {
ExtADXWBuffer[i] = 0;
ExtPDIBuffer[i] = 0;
ExtNDIBuffer[i] = 0;
ExtPDSBuffer[i] = 0;
ExtNDSBuffer[i] = 0;
ExtPDBuffer[i] = 0;
ExtNDBuffer[i] = 0;
ExtTRBuffer[i] = 0;
ExtATRBuffer[i] = 0;
ExtDXBuffer[i] = 0;
for (i = 0; i < _adxw_period; i++) {
_adxw_buff[i] = 0;
_pdi_buff[i] = 0;
_ndi_buff[i] = 0;
_pds_buff[i] = 0;
_nds_buff[i] = 0;
_pdb_buff[i] = 0;
_nd_buff[i] = 0;
_tr_buff[i] = 0;
_atr_buff[i] = 0;
_dx_buff[i] = 0;
}
}
for (i = start; i < rates_total && !IsStopped(); i++) {
Expand All @@ -208,40 +207,40 @@ class Indi_ADXW : public Indicator<IndiADXWParams> {
else
tmp_neg = 0.0;
}
ExtPDBuffer[i] = tmp_pos;
ExtNDBuffer[i] = tmp_neg;
_pdb_buff[i] = tmp_pos;
_nd_buff[i] = tmp_neg;
// Define TR.
double tr = MathMax(MathMax(MathAbs(high_price - low_price), MathAbs(high_price - prev_close)),
MathAbs(low_price - prev_close));
// Write down TR to TR buffer.
ExtTRBuffer[i] = tr;
_tr_buff[i] = tr;
// Fill smoothed positive and negative buffers and TR buffer.
if (i < ExtADXWPeriod) {
ExtATRBuffer[i] = 0.0;
ExtPDIBuffer[i] = 0.0;
ExtNDIBuffer[i] = 0.0;
if (i < _adxw_period) {
_atr_buff[i] = 0.0;
_pdi_buff[i] = 0.0;
_ndi_buff[i] = 0.0;
} else {
ExtATRBuffer[i] = SmoothedMA(i, ExtADXWPeriod, ExtATRBuffer[i - 1].Get(), ExtTRBuffer);
ExtPDSBuffer[i] = SmoothedMA(i, ExtADXWPeriod, ExtPDSBuffer[i - 1].Get(), ExtPDBuffer);
ExtNDSBuffer[i] = SmoothedMA(i, ExtADXWPeriod, ExtNDSBuffer[i - 1].Get(), ExtNDBuffer);
_atr_buff[i] = SmoothedMA(i, _adxw_period, _atr_buff[i - 1].Get(), _tr_buff);
_pds_buff[i] = SmoothedMA(i, _adxw_period, _pds_buff[i - 1].Get(), _pdb_buff);
_nds_buff[i] = SmoothedMA(i, _adxw_period, _nds_buff[i - 1].Get(), _nd_buff);
}
// Calculate PDI and NDI buffers.
if (ExtATRBuffer[i] != 0.0) {
ExtPDIBuffer[i] = 100.0 * ExtPDSBuffer[i].Get() / ExtATRBuffer[i].Get();
ExtNDIBuffer[i] = 100.0 * ExtNDSBuffer[i].Get() / ExtATRBuffer[i].Get();
if (_atr_buff[i] != 0.0) {
_pdi_buff[i] = 100.0 * _pds_buff[i].Get() / _atr_buff[i].Get();
_ndi_buff[i] = 100.0 * _nds_buff[i].Get() / _atr_buff[i].Get();
} else {
ExtPDIBuffer[i] = 0.0;
ExtNDIBuffer[i] = 0.0;
_pdi_buff[i] = 0.0;
_ndi_buff[i] = 0.0;
}
// Calculate DX buffer.
double dTmp = ExtPDIBuffer[i] + ExtNDIBuffer[i];
double dTmp = _pdi_buff[i] + _ndi_buff[i];
if (dTmp != 0.0)
dTmp = 100.0 * MathAbs((ExtPDIBuffer[i] - ExtNDIBuffer[i]) / dTmp);
dTmp = 100.0 * MathAbs((_pdi_buff[i] - _ndi_buff[i]) / dTmp);
else
dTmp = 0.0;
ExtDXBuffer[i] = dTmp;
_dx_buff[i] = dTmp;
// Fill ADXW buffer as smoothed DX buffer.
ExtADXWBuffer[i] = SmoothedMA(i, ExtADXWPeriod, ExtADXWBuffer[i - 1].Get(), ExtDXBuffer);
_adxw_buff[i] = SmoothedMA(i, _adxw_period, _adxw_buff[i - 1].Get(), _dx_buff);
}
// OnCalculate done. Return new prev_calculated.
return (rates_total);
Expand Down
22 changes: 11 additions & 11 deletions Indicators/Indi_AMA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Indi_AMA : public Indicator<IndiAMAParams> {
/**
* OnInit() method for AMA indicator.
*/
static void CalculateInit(int InpPeriodAMA, int InpFastPeriodEMA, int InpSlowPeriodEMA, int InpShiftAMA,
static void CalculateInit(int InpPeriodAMA, int _period_fast_ema, int _period_slow_ema, int _ishift_ama,
double &ExtFastSC, double &ExtSlowSC, int &ExtPeriodAMA, int &ExtSlowPeriodEMA,
int &ExtFastPeriodEMA) {
// Check for input values.
Expand All @@ -156,20 +156,20 @@ class Indi_AMA : public Indicator<IndiAMAParams> {
InpPeriodAMA, ExtPeriodAMA);
} else
ExtPeriodAMA = InpPeriodAMA;
if (InpSlowPeriodEMA <= 0) {
if (_period_slow_ema <= 0) {
ExtSlowPeriodEMA = 30;
PrintFormat(
"Input parameter InpSlowPeriodEMA has incorrect value (%d). Indicator will use value %d for calculations.",
InpSlowPeriodEMA, ExtSlowPeriodEMA);
"Input parameter _period_slow_ema has incorrect value (%d). Indicator will use value %d for calculations.",
_period_slow_ema, ExtSlowPeriodEMA);
} else
ExtSlowPeriodEMA = InpSlowPeriodEMA;
if (InpFastPeriodEMA <= 0) {
ExtSlowPeriodEMA = _period_slow_ema;
if (_period_fast_ema <= 0) {
ExtFastPeriodEMA = 2;
PrintFormat(
"Input parameter InpFastPeriodEMA has incorrect value (%d). Indicator will use value %d for calculations.",
InpFastPeriodEMA, ExtFastPeriodEMA);
"Input parameter _period_fast_ema has incorrect value (%d). Indicator will use value %d for calculations.",
_period_fast_ema, ExtFastPeriodEMA);
} else
ExtFastPeriodEMA = InpFastPeriodEMA;
ExtFastPeriodEMA = _period_fast_ema;

// Calculate ExtFastSC & ExtSlowSC.
ExtFastSC = 2.0 / (ExtFastPeriodEMA + 1.0);
Expand All @@ -180,14 +180,14 @@ class Indi_AMA : public Indicator<IndiAMAParams> {
* OnCalculate() method for AMA indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_SHORT, ValueStorage<double> &ExtAMABuffer, int InpPeriodAMA,
int InpFastPeriodEMA, int InpSlowPeriodEMA, int InpShiftAMA) {
int _period_fast_ema, int _period_slow_ema, int _ishift_ama) {
double ExtFastSC;
double ExtSlowSC;
int ExtPeriodAMA;
int ExtSlowPeriodEMA;
int ExtFastPeriodEMA;

CalculateInit(InpPeriodAMA, InpFastPeriodEMA, InpSlowPeriodEMA, InpShiftAMA, ExtFastSC, ExtSlowSC, ExtPeriodAMA,
CalculateInit(InpPeriodAMA, _period_fast_ema, _period_slow_ema, _ishift_ama, ExtFastSC, ExtSlowSC, ExtPeriodAMA,
ExtSlowPeriodEMA, ExtFastPeriodEMA);
int i;
// Check for rates count.
Expand Down
6 changes: 3 additions & 3 deletions Indicators/Indi_FractalAdaptiveMA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ class Indi_FrAMA : public Indicator<IndiFrAIndiMAParams> {
}

static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &FrAmaBuffer, int InpPeriodFrAMA,
int InpShift, ENUM_APPLIED_PRICE InpAppliedPrice) {
int _ishift, ENUM_APPLIED_PRICE _applied_price) {
if (rates_total < 2 * InpPeriodFrAMA) return (0);

int start, i;
// Start calculations.
if (prev_calculated == 0) {
start = 2 * InpPeriodFrAMA - 1;
for (i = 0; i <= start; i++)
FrAmaBuffer[i] = AppliedPriceValueStorage::GetApplied(open, high, low, close, i, InpAppliedPrice);
FrAmaBuffer[i] = AppliedPriceValueStorage::GetApplied(open, high, low, close, i, _applied_price);
} else
start = prev_calculated - 1;

Expand All @@ -170,7 +170,7 @@ class Indi_FrAMA : public Indicator<IndiFrAIndiMAParams> {
double n3 = (hi3 - lo3) / (2 * InpPeriodFrAMA);
double d = (MathLog(n1 + n2) - MathLog(n3)) / math_log_2;
double alfa = MathExp(-4.6 * (d - 1.0));
double _iprice = AppliedPriceValueStorage::GetApplied(open, high, low, close, i, InpAppliedPrice);
double _iprice = AppliedPriceValueStorage::GetApplied(open, high, low, close, i, _applied_price);

FrAmaBuffer[i] = alfa * _iprice + (1 - alfa) * FrAmaBuffer[i - 1].Get();
}
Expand Down
44 changes: 22 additions & 22 deletions Indicators/Indi_MA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -268,53 +268,53 @@ class Indi_MA : public Indicator<IndiMAParams> {
* Calculates Simple Moving Average (SMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateSimpleMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
ValueStorage<double> &_line_buff, int _ma_period) {
int i, start;
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
start = _ma_period + begin;
// Set empty value for first start bars.
for (i = 0; i < start - 1; i++) ExtLineBuffer[i] = 0.0;
for (i = 0; i < start - 1; i++) _line_buff[i] = 0.0;
// Calculate first visible value.
double first_value = 0;
for (i = begin; i < start; i++) first_value += price[i].Get();
first_value /= _ma_period;
ExtLineBuffer[start - 1] = first_value;
_line_buff[start - 1] = first_value;
} else
start = prev_calculated - 1;
// Main loop.
for (i = start; i < rates_total && !IsStopped(); i++) {
ExtLineBuffer[i] = ExtLineBuffer[i - 1] + (price[i] - price[i - _ma_period]) / _ma_period;
_line_buff[i] = _line_buff[i - 1] + (price[i] - price[i - _ma_period]) / _ma_period;
}
}

/**
* Calculates Exponential Moving Average (EMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateEMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
ValueStorage<double> &_line_buff, int _ma_period) {
int i, limit;
double SmoothFactor = 2.0 / (1.0 + _ma_period);
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
limit = _ma_period + begin;
ExtLineBuffer[begin] = price[begin];
_line_buff[begin] = price[begin];
for (i = begin + 1; i < limit; i++) {
ExtLineBuffer[i] = price[i] * SmoothFactor + ExtLineBuffer[i - 1] * (1.0 - SmoothFactor);
_line_buff[i] = price[i] * SmoothFactor + _line_buff[i - 1] * (1.0 - SmoothFactor);
}
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++) {
ExtLineBuffer[i] = price[i] * SmoothFactor + ExtLineBuffer[i - 1] * (1.0 - SmoothFactor);
_line_buff[i] = price[i] * SmoothFactor + _line_buff[i - 1] * (1.0 - SmoothFactor);
}
}

/**
* Calculates Linearly Weighted Moving Average (LWMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateLWMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
ValueStorage<double> &_line_buff, int _ma_period) {
int i, limit;
static int weightsum;
double sum;
Expand All @@ -323,7 +323,7 @@ class Indi_MA : public Indicator<IndiMAParams> {
weightsum = 0;
limit = _ma_period + begin;
// Set empty value for first limit bars.
for (i = 0; i < limit; i++) ExtLineBuffer[i] = 0.0;
for (i = 0; i < limit; i++) _line_buff[i] = 0.0;
// Calculate first visible value.
double firstValue = 0;
for (i = begin; i < limit; i++) {
Expand All @@ -332,14 +332,14 @@ class Indi_MA : public Indicator<IndiMAParams> {
firstValue += k * price[i].Get();
}
firstValue /= (double)weightsum;
ExtLineBuffer[limit - 1] = firstValue;
_line_buff[limit - 1] = firstValue;
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++) {
sum = 0;
for (int j = 0; j < _ma_period; j++) sum += (_ma_period - j) * price[i - j].Get();
ExtLineBuffer[i] = sum / weightsum;
_line_buff[i] = sum / weightsum;
}
//---
}
Expand All @@ -348,23 +348,23 @@ class Indi_MA : public Indicator<IndiMAParams> {
* Calculates Smoothed Moving Average (SMMA). The same as in "Example Moving Average" indicator.
*/
static void CalculateSmoothedMA(int rates_total, int prev_calculated, int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_period) {
ValueStorage<double> &_line_buff, int _ma_period) {
int i, limit;
// First calculation or number of bars was changed.
if (prev_calculated == 0) {
limit = _ma_period + begin;
// Set empty value for first limit bars.
for (i = 0; i < limit - 1; i++) ExtLineBuffer[i] = 0.0;
for (i = 0; i < limit - 1; i++) _line_buff[i] = 0.0;
// Calculate first visible value.
double firstValue = 0;
for (i = begin; i < limit; i++) firstValue += price[i].Get();
firstValue /= _ma_period;
ExtLineBuffer[limit - 1] = firstValue;
_line_buff[limit - 1] = firstValue;
} else
limit = prev_calculated - 1;
// Main loop.
for (i = limit; i < rates_total && !IsStopped(); i++)
ExtLineBuffer[i] = (ExtLineBuffer[i - 1] * (_ma_period - 1) + price[i].Get()) / _ma_period;
_line_buff[i] = (_line_buff[i - 1] * (_ma_period - 1) + price[i].Get()) / _ma_period;
//---
}

Expand Down Expand Up @@ -583,30 +583,30 @@ class Indi_MA : public Indicator<IndiMAParams> {
* Calculates Moving Average. The same as in "Example Moving Average" indicator.
*/
static int Calculate(const int rates_total, const int prev_calculated, const int begin, ValueStorage<double> &price,
ValueStorage<double> &ExtLineBuffer, int _ma_method, int _ma_period) {
ValueStorage<double> &_line_buff, int _ma_method, int _ma_period) {
// Check for bars count.
if (rates_total < _ma_period - 1 + begin) {
// Not enough bars for calculation.
return (0);
}
if (prev_calculated == 0) {
// First calculation or number of bars was changed.
ArrayInitialize(ExtLineBuffer, (double)0);
ArrayInitialize(_line_buff, (double)0);
}

// Calculation.
switch (_ma_method) {
case MODE_EMA:
CalculateEMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
CalculateEMA(rates_total, prev_calculated, begin, price, _line_buff, _ma_period);
break;
case MODE_LWMA:
CalculateLWMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
CalculateLWMA(rates_total, prev_calculated, begin, price, _line_buff, _ma_period);
break;
case MODE_SMMA:
CalculateSmoothedMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
CalculateSmoothedMA(rates_total, prev_calculated, begin, price, _line_buff, _ma_period);
break;
case MODE_SMA:
CalculateSimpleMA(rates_total, prev_calculated, begin, price, ExtLineBuffer, _ma_period);
CalculateSimpleMA(rates_total, prev_calculated, begin, price, _line_buff, _ma_period);
break;
}
// Return value of prev_calculated for next call.
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Indi_TEMA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ class Indi_TEMA : public Indicator<IndiTEMAParams> {
/**
* OnCalculate() method for TEMA indicator.
*
* Note that InpShift is used for drawing only and thus is unused.
* Note that _ishift is used for drawing only and thus is unused.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_SHORT, ValueStorage<double> &TemaBuffer,
ValueStorage<double> &Ema, ValueStorage<double> &EmaOfEma, ValueStorage<double> &EmaOfEmaOfEma,
int InpPeriodEMA, int InpShift) {
int InpPeriodEMA, int _ishift) {
if (rates_total < 3 * InpPeriodEMA - 3) return (0);
//---
int start;
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Indi_VIDYA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ class Indi_VIDYA : public Indicator<IndiVIDYAParams> {
/**
* OnCalculate() method for VIDyA indicator.
*
* Note that InpShift is used for drawing only and thus is unused.
* Note that _ishift is used for drawing only and thus is unused.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_SHORT, ValueStorage<double> &VIDYA_Buffer, int InpPeriodCMO,
int InpPeriodEMA, int InpShift) {
int InpPeriodEMA, int _ishift) {
double ExtF = 2.0 / (1.0 + InpPeriodEMA);

if (rates_total < InpPeriodEMA + InpPeriodCMO - 1) return (0);
Expand Down

0 comments on commit 2fa7dd0

Please sign in to comment.