Skip to content

Commit

Permalink
Indicator: Adds IndicatorTick
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Oct 28, 2021
1 parent 1ca63e5 commit dbf7af0
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 250 deletions.
18 changes: 10 additions & 8 deletions Action.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ class Action {
break;
#endif
#ifdef INDICATOR_MQH
case ACTION_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).ExecuteAction((ENUM_INDICATOR_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
/*
case ACTION_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).ExecuteAction((ENUM_INDICATOR_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
*/
#endif
#ifdef STRATEGY_MQH
case ACTION_TYPE_STRATEGY:
Expand Down
20 changes: 11 additions & 9 deletions Condition.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,17 @@ class Condition {
}
break;
#ifdef INDICATOR_MQH
case COND_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).CheckCondition((ENUM_INDICATOR_CONDITION)_entry.cond_id, _entry.args);
} else {
// Static method not supported.
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
/*
case COND_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).CheckCondition((ENUM_INDICATOR_CONDITION)_entry.cond_id,
_entry.args); } else {
// Static method not supported.
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
*/
#endif
case COND_TYPE_MARKET:
if (Object::IsValid(_entry.obj)) {
Expand Down
98 changes: 44 additions & 54 deletions Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,49 @@ class Chart;
template <typename TS>
class Indicator : public IndicatorBase {
protected:
// Structs.
BufferStruct<IndicatorDataEntry> idata;
TS iparams;

protected:
/* Protected methods */

bool Init() {
ArrayResize(value_storages, iparams.GetMaxModes());
switch (iparams.GetDataSourceType()) {
case IDATA_BUILTIN:
break;
case IDATA_ICUSTOM:
break;
case IDATA_INDICATOR:
if (indi_src == NULL) {
// Indi_Price* _indi_price = Indi_Price::GetCached(GetSymbol(), GetTf(), iparams.GetShift());
// SetDataSource(_indi_price, true, PRICE_OPEN);
}
break;
}
return InitDraw();
}

/**
* Initialize indicator data drawing on custom data.
*/
bool InitDraw() {
if (iparams.is_draw && !Object::IsValid(draw)) {
draw = new DrawIndicator(THIS_PTR);
draw.SetColorLine(iparams.indi_color);
}
return iparams.is_draw;
}

/**
* Deinitialize drawing.
*/
void DeinitDraw() {
if (draw) {
delete draw;
}
}

public:
/* Indicator enumerations */

Expand Down Expand Up @@ -115,47 +155,6 @@ class Indicator : public IndicatorBase {
}
}

/* Init methods */

bool Init() {
ArrayResize(value_storages, iparams.GetMaxModes());
switch (iparams.GetDataSourceType()) {
case IDATA_BUILTIN:
break;
case IDATA_ICUSTOM:
break;
case IDATA_INDICATOR:
if (indi_src == NULL) {
// Indi_Price* _indi_price = Indi_Price::GetCached(GetSymbol(), GetTf(), iparams.GetShift());
// SetDataSource(_indi_price, true, PRICE_OPEN);
}
break;
}
return InitDraw();
}

/**
* Initialize indicator data drawing on custom data.
*/
bool InitDraw() {
if (iparams.is_draw && !Object::IsValid(draw)) {
draw = new DrawIndicator(THIS_PTR);
draw.SetColorLine(iparams.indi_color);
}
return iparams.is_draw;
}

/* Deinit methods */

/**
* Deinitialize drawing.
*/
void DeinitDraw() {
if (draw) {
delete draw;
}
}

/* Getters */

/**
Expand Down Expand Up @@ -402,16 +401,12 @@ class Indicator : public IndicatorBase {
*/
virtual IndicatorBase* FetchDataSource(ENUM_INDICATOR_TYPE _id) { return NULL; }

/* Operator overloading methods */
/* Getters */

/**
* Access indicator entry data using [] operator.
* Get pointer to data of indicator.
*/
IndicatorDataEntry operator[](int _shift) { return GetEntry(_shift); }
IndicatorDataEntry operator[](ENUM_INDICATOR_INDEX _shift) { return GetEntry(_shift); }
IndicatorDataEntry operator[](datetime _dt) { return idata[_dt]; }

/* Getters */
BufferStruct<IndicatorDataEntry>* GetData() { return GetPointer(idata); }

/**
* Returns the highest bar's index (shift).
Expand Down Expand Up @@ -613,11 +608,6 @@ class Indicator : public IndicatorBase {
return _signals;
}

/**
* Get pointer to data of indicator.
*/
BufferStruct<IndicatorDataEntry>* GetData() { return GetPointer(idata); }

/**
* Get name of the indicator.
*/
Expand Down
96 changes: 96 additions & 0 deletions Indicator/IndicatorTick.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

// Ignore processing of this file if already included.
#ifndef INDICATOR_TICK_MQH
#define INDICATOR_TICK_MQH

// Includes.
#include "../IndicatorBase.h"
//#include "Array.mqh"
//#include "BufferStruct.mqh"
//#include "Chart.mqh"
//#include "DateTime.mqh"
//#include "DrawIndicator.mqh"
//#include "Indicator.define.h"
//#include "Indicator.enum.h"
//#include "Indicator.struct.cache.h"
//#include "Indicator.struct.h"
//#include "Indicator.struct.serialize.h"
//#include "Indicator.struct.signal.h"
//#include "IndicatorBase.h"
//#include "Math.h"
//#include "Object.mqh"
//#include "Refs.mqh"
//#include "Serializer.mqh"
//#include "SerializerCsv.mqh"
//#include "SerializerJson.mqh"
//#include "Storage/ValueStorage.h"
//#include "Storage/ValueStorage.indicator.h"
//#include "Storage/ValueStorage.native.h"

/**
* Class to deal with tick indicators.
*/
// template <typename TS>
class IndicatorTick : public IndicatorBase {
protected:
BufferStruct<IndicatorDataEntry> tickdata;

public:
/* Special methods */

/**
* Class constructor.
*/
IndicatorTick() {}

/* Virtual method implementations */

/**
* Returns the indicator's data entry.
*
* @see: IndicatorDataEntry.
*
* @return
* Returns IndicatorDataEntry struct filled with indicator values.
*/
IndicatorDataEntry GetEntry(int _timestamp = 0) {
IndicatorDataEntry _entry = tickdata.GetByKey(_timestamp);
if (!_entry.IsValid() && !_entry.CheckFlag(INDI_ENTRY_FLAG_INSUFFICIENT_DATA)) {
_entry.timestamp = _timestamp;
_entry.Resize(4);
_entry.SetFlag(INDI_ENTRY_FLAG_IS_VALID, IsValidEntry(_entry));
for (int _mode = 0; _mode < (int)iparams.GetMaxModes(); _mode++) {
//_entry.values[_mode] = GetValue(_mode, _shift); / @todo
}
if (_entry.IsValid()) {
idata.Add(_entry, _bar_time);
} else {
_entry.AddFlags(INDI_ENTRY_FLAG_INSUFFICIENT_DATA);
}
}
return _entry;
}
};

#endif
28 changes: 28 additions & 0 deletions Indicator/tests/IndicatorTick.test.mq4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @file
* Test functionality of IndicatorTick class.
*/

// Includes.
#include "IndicatorTick.test.mq5"
64 changes: 64 additions & 0 deletions Indicator/tests/IndicatorTick.test.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @file
* Test functionality of IndicatorTick class.
*/

// Includes.
#include "../../Test.mqh"
#include "../IndicatorTick.h"

/**
* Implements OnInit().
*/
int OnInit() {
/* @fixme
// Initialize.
IndicatorParams iparams(INDI_NONE, TYPE_INT, 10);
Indicator *in = new Indicator(iparams, NULL);
// Check empty values.
assertTrueOrFail(in.GetBufferSize() == 10, "Wrong buffer size!");
assertTrueOrFail(in.GetEmpty().double_value == 0.0, "Wrong empty double value!");
assertTrueOrFail(in.GetEmpty().integer_value == 0, "Wrong empty integer value!");
// Check dynamic allocation.
MqlParam entry;
entry.integer_value = 1;
for (uint i = 0; i < in.GetBufferSize() * 2; i++) {
in.AddValue(entry);
Print("Index ", i, ": Curr: ", in.GetValue(0).integer_value, "; Prev: ", in.GetValue(1).integer_value);
assertTrueOrFail(in.GetValue(0).integer_value == entry.integer_value,
StringFormat("Wrong latest value (%d <> %d)!",
in.GetValue(0).integer_value,
entry.integer_value));
assertTrueOrFail(in.GetValue(1).integer_value == entry.integer_value - 1,
StringFormat("Wrong previous value (%d <> %d)!",
in.GetValue(1).integer_value,
entry.integer_value - 1));
entry.integer_value++;
}
Print(in.ToString());
// Clean up.
delete in;
*/
return (INIT_SUCCEEDED);
}
Loading

0 comments on commit dbf7af0

Please sign in to comment.