Skip to content

Commit

Permalink
WIP. Trying to make indicators to work with historic ticks.
Browse files Browse the repository at this point in the history
Also changed .clang-format in order to indent macros.
Changed Indi_Bands's OnCalculate param names in order to prevent warnings.
  • Loading branch information
nseam committed May 22, 2024
1 parent 42ccf6a commit d8eb065
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 408 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Language: Cpp
BasedOnStyle: Google
ColumnLimit: 120
IndentPPDirectives: BeforeHash
11 changes: 7 additions & 4 deletions Indicator/Indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,13 @@ class Indicator : public IndicatorData {
}

if (_LastError != ERR_SUCCESS) {
datetime _bar_dt = (datetime)_bar_time;
Print("Error: Code ", _LastError, " while trying to retrieve entry at shift ", _rel_shift, " (absolute ",
ToAbsShift(_rel_shift), "), mode ", _mode, ", time ", _bar_dt);
DebugBreak();
if (_LastError != 4806) {
// Error occured and it's not "4806 Requested data not found".
datetime _bar_dt = (datetime)_bar_time;
Print("Error: Code ", _LastError, " while trying to retrieve entry at shift ", _rel_shift, " (absolute ",
ToAbsShift(_rel_shift), "), mode ", _mode, ", time ", _bar_dt);
DebugBreak();
}
}
}
GetEntryAlter(_entry, _rel_shift);
Expand Down
8 changes: 5 additions & 3 deletions Indicator/IndicatorTf.provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#define INDICATOR_TF_PROVIDER_H

#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

/**
Expand Down Expand Up @@ -159,9 +159,11 @@ class ItemsHistoryTfCandleProvider : public ItemsHistoryCandleProvider<TV> {

// Adding candle to the output array.
ArrayPushObject(_out_arr, _candle);
--_num_items;
}

// Even if we don't form an item (a candle), we assume we've done one item.
--_num_items;

if (_dir == ITEMS_HISTORY_DIRECTION_FORWARD) {
_from_time_ms += _candle_length_ms;
} else {
Expand Down
413 changes: 234 additions & 179 deletions IndicatorLegacy.h

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions Indicators/Indi_Bands.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,33 @@ class Indi_Bands : public Indicator<IndiBandsParams> {
/**
* OnCalculate() method for Bands indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_SHORT, ValueStorage<double> &ExtMLBuffer,
ValueStorage<double> &ExtTLBuffer, ValueStorage<double> &ExtBLBuffer,
ValueStorage<double> &ExtStdDevBuffer, int InpBandsPeriod, int InpBandsShift,
double InpBandsDeviations) {
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_SHORT, ValueStorage<double> &_ExtMLBuffer,
ValueStorage<double> &_ExtTLBuffer, ValueStorage<double> &_ExtBLBuffer,
ValueStorage<double> &_ExtStdDevBuffer, int _InpBandsPeriod, int _InpBandsShift,
double _InpBandsDeviations) {
int ExtBandsPeriod, ExtBandsShift;
double ExtBandsDeviations;
int ExtPlotBegin = 0;

if (InpBandsPeriod < 2) {
if (_InpBandsPeriod < 2) {
ExtBandsPeriod = 20;
PrintFormat("Incorrect value for input variable InpBandsPeriod=%d. Indicator will use value=%d for calculations.",
InpBandsPeriod, ExtBandsPeriod);
_InpBandsPeriod, ExtBandsPeriod);
} else
ExtBandsPeriod = InpBandsPeriod;
if (InpBandsShift < 0) {
ExtBandsPeriod = _InpBandsPeriod;
if (_InpBandsShift < 0) {
ExtBandsShift = 0;
PrintFormat("Incorrect value for input variable InpBandsShift=%d. Indicator will use value=%d for calculations.",
InpBandsShift, ExtBandsShift);
_InpBandsShift, ExtBandsShift);
} else
ExtBandsShift = InpBandsShift;
if (InpBandsDeviations == 0.0) {
ExtBandsShift = _InpBandsShift;
if (_InpBandsDeviations == 0.0) {
ExtBandsDeviations = 2.0;
PrintFormat(
"Incorrect value for input variable InpBandsDeviations=%f. Indicator will use value=%f for calculations.",
InpBandsDeviations, ExtBandsDeviations);
_InpBandsDeviations, ExtBandsDeviations);
} else
ExtBandsDeviations = InpBandsDeviations;
ExtBandsDeviations = _InpBandsDeviations;

if (rates_total < ExtPlotBegin) return (0);
//--- indexes draw begin settings, when we've recieved previous begin
Expand All @@ -221,13 +221,13 @@ class Indi_Bands : public Indicator<IndiBandsParams> {
//--- main cycle
for (int i = pos; i < rates_total && !IsStopped(); i++) {
//--- middle line
ExtMLBuffer[i] = Indi_MA::SimpleMA(i, ExtBandsPeriod, price);
_ExtMLBuffer[i] = Indi_MA::SimpleMA(i, ExtBandsPeriod, price);
//--- calculate and write down StdDev
ExtStdDevBuffer[i] = StdDev_Func(i, price, ExtMLBuffer, ExtBandsPeriod);
_ExtStdDevBuffer[i] = StdDev_Func(i, price, _ExtMLBuffer, ExtBandsPeriod);
//--- upper line
ExtTLBuffer[i] = ExtMLBuffer[i] + ExtBandsDeviations * ExtStdDevBuffer[i].Get();
_ExtTLBuffer[i] = _ExtMLBuffer[i] + ExtBandsDeviations * _ExtStdDevBuffer[i].Get();
//--- lower line
ExtBLBuffer[i] = ExtMLBuffer[i] - ExtBandsDeviations * ExtStdDevBuffer[i].Get();
_ExtBLBuffer[i] = _ExtMLBuffer[i] - ExtBandsDeviations * _ExtStdDevBuffer[i].Get();
}
//--- OnCalculate done. Return new prev_calculated.
return (rates_total);
Expand Down
13 changes: 11 additions & 2 deletions Indicators/Indi_MA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define INDI_MA_MQH

// Includes.
#include <MovingAverages.mqh>

#include "../Dict.mqh"
#include "../DictObject.mqh"
#include "../Indicator/Indicator.h"
Expand All @@ -33,7 +35,14 @@
#include "../Storage/ValueStorage.h"
#include "../String.mqh"

#ifndef __MQL4__
#ifdef __MQL4__
// MQL4 version of the method doesn't have last parameter.
int LinearWeightedMAOnBuffer(const int rates_total, const int prev_calculated, const int begin, const int period,
const double &price[], double &buffer[]) {
int _weight_sum;
return LinearWeightedMAOnBuffer(rates_total, prev_calculated, begin, period, price, buffer, _weight_sum);
}
#else // !__MQL__4
// Defines global functions (for MQL4 backward compability).
double iMA(string _symbol, int _tf, int _ma_period, int _ma_shift, int _ma_method, int _ap, int _shift) {
ResetLastError();
Expand All @@ -45,7 +54,7 @@ double iMAOnArray(double &_arr[], int _total, int _period, int _ma_shift, int _m
ResetLastError();
return Indi_MA::iMAOnArray(_arr, _total, _period, _ma_shift, _ma_method, _abs_shift, _cache);
}
#endif
#endif // __MQL4__

// Structs.
struct IndiMAParams : IndicatorParams {
Expand Down
24 changes: 18 additions & 6 deletions Indicators/Tick/Indi_TickMt.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/

#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

// Includes.
Expand Down Expand Up @@ -196,10 +196,10 @@ class Indi_TickMt : public IndicatorTick<Indi_TickMtParams, double, ItemsHistory
} else {
for (int i = 0; i < _num_copied; ++i) {
TickTAB<double> _tick(_tmp_ticks[i]);
#ifdef __debug_verbose__
#ifdef __debug_verbose__
Print("Fetched tick at ", TimeToString(_tmp_ticks[i].time, TIME_DATE | TIME_MINUTES | TIME_SECONDS), ": ",
_tmp_ticks[i].ask, ", ", _tmp_ticks[i].bid);
#endif
#endif
ArrayPushObject(_out_ticks, _tick);
}

Expand Down Expand Up @@ -232,6 +232,9 @@ class Indi_TickMt : public IndicatorTick<Indi_TickMtParams, double, ItemsHistory
double _bid = Bid;
long _time = TimeCurrent();
#else

#ifdef __DISABLE

static MqlTick _tmp_ticks[];
// Copying only the last tick.
int _num_copied = CopyTicks(GetSymbol(), _tmp_ticks, COPY_TICKS_INFO, 0, 1);
Expand All @@ -248,15 +251,24 @@ class Indi_TickMt : public IndicatorTick<Indi_TickMtParams, double, ItemsHistory
return;
}

#ifdef __debug_verbose__
#ifdef __debug_verbose__
Print("CpyT: ", TimeToString(_tmp_ticks[0].time, TIME_DATE | TIME_MINUTES | TIME_SECONDS), " = ", _tmp_ticks[0].bid,
" (", _tmp_ticks[0].time, ")");
Print("RlCl: ", TimeToString(::iTime(GetSymbol(), PERIOD_CURRENT, 0), TIME_DATE | TIME_MINUTES | TIME_SECONDS),
" = ", ::iClose(GetSymbol(), PERIOD_CURRENT, 0));
#endif
#endif

double _ask = _tmp_ticks[0].ask;
double _bid = _tmp_ticks[0].bid;

#endif

MqlTick _tick_data;
SymbolInfoTick(GetSymbol(), _tick_data);

double _ask = _tick_data.ask;
double _bid = _tick_data.bid;

// long _time = _tmp_ticks[0].time;
long _time = TimeCurrent();
#endif
Expand Down
Loading

0 comments on commit d8eb065

Please sign in to comment.