Skip to content

Commit

Permalink
Buffer: Adds BufferTick
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Nov 11, 2021
1 parent f47bd09 commit cb7ba0d
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-buffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
matrix:
test:
- BufferCandle.test
- BufferTick.test
steps:
- uses: actions/download-artifact@v2
with:
Expand Down
2 changes: 0 additions & 2 deletions Buffer/BufferCandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
template <typename TV>
class BufferCandle : public BufferStruct<CandleOHLC<TV>> {
protected:
uint cache_limit;

protected:
/* Protected methods */

Expand Down
79 changes: 79 additions & 0 deletions Buffer/BufferTick.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//+------------------------------------------------------------------+
//| 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/>.
*
*/

// Prevents processing this includes file for the second time.
#ifndef BUFFER_TICK_H
#define BUFFER_TICK_H

// Includes.
#include "../BufferStruct.mqh"
#include "../Tick.struct.h"

/**
* Class to store struct data.
*/
template <typename TV>
class BufferTick : public BufferStruct<TickAB<TV>> {
protected:
protected:
/* Protected methods */

/**
* Initialize class.
*
* Called on constructor.
*/
void Init() { SetOverflowListener(BufferTickOverflowListener, 10); }

public:
/* Constructors */

/**
* Constructor.
*/
BufferTick() { Init(); }
BufferTick(BufferTick& _right) {
THIS_REF = _right;
Init();
}

/* Callback methods */

/**
* Function should return true if resize can be made, or false to overwrite current slot.
*/
static bool BufferTickOverflowListener(ENUM_DICT_OVERFLOW_REASON _reason, int _size, int _num_conflicts) {
static int cache_limit = 86400;
switch (_reason) {
case DICT_OVERFLOW_REASON_FULL:
// We allow resize if dictionary size is less than 86400 slots.
return _size < cache_limit;
case DICT_OVERFLOW_REASON_TOO_MANY_CONFLICTS:
default:
// When there is too many conflicts, we just reject doing resize, so first conflicting slot will be reused.
break;
}
return false;
}
};

#endif // BUFFER_TICK_H
28 changes: 28 additions & 0 deletions Buffer/tests/BufferTick.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 BufferTick class.
*/

// Includes.
#include "BufferTick.test.mq5"
57 changes: 57 additions & 0 deletions Buffer/tests/BufferTick.test.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//+------------------------------------------------------------------+
//| 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 BufferTick class.
*/

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

/**
* Implements OnInit().
*/
int OnInit() {
MqlTick mql_tick; // 60
TickAB<double> _tick_ab_d; // 16
TickAB<float> _tick_ab_f; // 8
TickTAB<double> _tick_tab_d; // 24
TickTAB<float> _tick_tab_f; // 16
Print("mql_tick: ", sizeof(mql_tick));
Print("_tick_ab_d: ", sizeof(_tick_ab_d));
Print("_tick_ab_f: ", sizeof(_tick_ab_f));
Print("_tick_tab_d: ", sizeof(_tick_tab_d));
Print("_tick_tab_f: ", sizeof(_tick_tab_f));
// @todo
return (GetLastError() > 0 ? INIT_FAILED : INIT_SUCCEEDED);
}

/**
* Implements OnTick().
*/
void OnTick() {}

/**
* Implements OnDeinit().
*/
void OnDeinit(const int reason) {}
1 change: 1 addition & 0 deletions Candle.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct CandleOHLC
#endif
{
T open, high, low, close;
// Struct constructors.
CandleOHLC(T _open = 0, T _high = 0, T _low = 0, T _close = 0) : open(_open), high(_high), low(_low), close(_close) {}
CandleOHLC(ARRAY_REF(T, _prices)) {
int _size = ArraySize(_prices);
Expand Down
24 changes: 11 additions & 13 deletions Indicator/IndicatorTick.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@
#endif

// Includes.
#include "../Buffer/BufferTick.h"
#include "../IndicatorBase.h"

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

protected:
Expand Down Expand Up @@ -85,6 +86,8 @@ class IndicatorTick : public IndicatorBase {
*/
IndicatorDataEntry GetEntry(int _timestamp = 0) {
ResetLastError();
TickAB<TV> _entry = itdata.GetByKey(_timestamp);
/*
IndicatorDataEntry _entry = itdata.GetByKey(_timestamp);
if (!_entry.IsValid() && !_entry.CheckFlag(INDI_ENTRY_FLAG_INSUFFICIENT_DATA)) {
_entry.Resize(itparams.GetMaxModes());
Expand Down Expand Up @@ -133,6 +136,9 @@ class IndicatorTick : public IndicatorBase {
ResetLastError();
}
return _entry;
*/
IndicatorDataEntry _foo;
return _foo;
}

/**
Expand Down Expand Up @@ -165,17 +171,9 @@ class IndicatorTick : public IndicatorBase {
*
* @see: MqlTick.
*/
void SetTick(MqlTick& _tick, long _timestamp = 0) {
IndicatorDataEntry _entry(itparams.GetMaxModes());
_entry.timestamp = _timestamp;
_entry.values[0] = _tick.bid;
_entry.values[1] = _tick.ask;
#ifdef __MQL4__
_entry.values[2] = _tick.volume;
#else
_entry.values[2] = _tick.volume_real;
#endif
itdata.Add(_entry, _timestamp);
void SetTick(MqlTick& _mql_tick, long _timestamp = 0) {
TickAB<TV> _tick(_mql_tick);
itdata.Add(_tick, _timestamp);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Indicator/tests/IndicatorTick.test.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct IndicatorTickDummyParams : IndicatorParams {
/**
* Price Indicator.
*/
class IndicatorTickDummy : public IndicatorTick<IndicatorTickDummyParams> {
class IndicatorTickDummy : public IndicatorTick<IndicatorTickDummyParams, double> {
public:
IndicatorTickDummy(string _symbol, int _shift = 0, string _name = "")
: IndicatorTick(INDI_TICK, _symbol, _shift, _name) {
Expand Down
7 changes: 4 additions & 3 deletions Indicators/Tick/Indi_TickMt.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ struct IndiTickMtParams : IndicatorParams {
/**
* Price Indicator.
*/
class Indi_TickMt : public IndicatorTick<IndiTickMtParams> {
class Indi_TickMt : public IndicatorTick<IndiTickMtParams, double> {
protected:
MqlTick tick;

public:
/**
* Class constructor.
*/
Indi_TickMt(IndiTickMtParams &_p, IndicatorBase *_indi_src = NULL) : IndicatorTick<IndiTickMtParams>(_p, _indi_src){};
Indi_TickMt(IndiTickMtParams &_p, IndicatorBase *_indi_src = NULL)
: IndicatorTick<IndiTickMtParams, double>(_p, _indi_src){};
Indi_TickMt(string _symbol, int _shift = 0) : IndicatorTick(INDI_TICK, _symbol, _shift){};

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ class Indi_TickMt : public IndicatorTick<IndiTickMtParams> {
* This method is called on GetEntry() right after values are set.
*/
virtual void GetEntryAlter(IndicatorDataEntry &_entry, int _shift = -1) {
IndicatorTick<IndiTickMtParams>::GetEntryAlter(_entry, _shift);
IndicatorTick<IndiTickMtParams, double>::GetEntryAlter(_entry, _shift);
_entry.timestamp = _entry.timestamp > 0 ? _entry.timestamp : tick.time;
};
};
4 changes: 2 additions & 2 deletions Tick.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct TickAB {
T bid; // Current Bid price.
// Struct constructors.
TickAB(T _ask = 0, T _bid = 0) : ask(_ask), bid(_bid) {}
TickAB(MqlTick &_tick) : ask(_tick.ask), bid(_tick.bid) {}
TickAB(MqlTick &_tick) : ask((T)_tick.ask), bid((T)_tick.bid) {}
};

/**
Expand All @@ -49,7 +49,7 @@ struct TickTAB : TickAB<T> {
datetime time; // Time of the last prices update.
// Struct constructors.
TickTAB(datetime _time = 0, T _ask = 0, T _bid = 0) : time(_time), TickAB(_ask, _bid) {}
TickAB(MqlTick &_tick) : time(_tick.time), TickAB(_tick.ask, _tick.bid) {}
TickTAB(MqlTick &_tick) : time(_tick.time), TickAB(_tick) {}
};

#ifndef __MQL__
Expand Down

0 comments on commit cb7ba0d

Please sign in to comment.