Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… dev-ref-refactor
  • Loading branch information
nseam committed Oct 21, 2021
2 parents 1b591c4 + 1ca63e5 commit 1104fc7
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 58 deletions.
15 changes: 0 additions & 15 deletions EA.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,3 @@ enum ENUM_EA_PARAM_FLAGS {
EA_PARAM_FLAG_REPORT_EXPORT = 1 << 1, // Export report on exit.
EA_PARAM_FLAG_REPORT_PRINT = 1 << 2, // Print report on exit.
};

/* Defines EA state flags. */
enum ENUM_EA_STATE_FLAGS {
EA_STATE_FLAG_NONE = 0 << 0, // None flags.
EA_STATE_FLAG_ACTIVE = 1 << 0, // Is active (can trade).
EA_STATE_FLAG_CONNECTED = 1 << 1, // Indicates connectedness to a trade server.
EA_STATE_FLAG_ENABLED = 1 << 2, // Is enabled.
EA_STATE_FLAG_LIBS_ALLOWED = 1 << 3, // Indicates the permission to use external libraries (such as DLL).
EA_STATE_FLAG_ON_INIT = 1 << 4, // Indicates EA is during initializing procedure (constructor).
EA_STATE_FLAG_ON_QUIT = 1 << 5, // Indicates EA is during exiting procedure (deconstructor).
EA_STATE_FLAG_OPTIMIZATION = 1 << 6, // Indicates EA runs in optimization mode.
EA_STATE_FLAG_TESTING = 1 << 7, // Indicates EA runs in testing mode.
EA_STATE_FLAG_TRADE_ALLOWED = 1 << 8, // Indicates the permission to trade on the chart.
EA_STATE_FLAG_VISUAL_MODE = 1 << 9, // Indicates EA runs in visual testing mode.
};
82 changes: 50 additions & 32 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ class EA {
EA(EAParams &_params)
: account(new Account), market(new Market(_params.Get<string>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_SYMBOL)))) {
eparams = _params;
estate.SetFlag(EA_STATE_FLAG_ON_INIT, true);
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_ON_INIT), true);
UpdateStateFlags();
// Add and process tasks.
TaskAdd(eparams.GetStruct<TaskEntry>(STRUCT_ENUM(EAParams, EA_PARAM_STRUCT_TASK_ENTRY)));
ProcessTasks();
estate.SetFlag(EA_STATE_FLAG_ON_INIT, false);
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_ON_INIT), false);
// Initialize a trade instance for the current chart and symbol.
ChartParams _cparams((ENUM_TIMEFRAMES)_Period, _Symbol);
TradeParams _tparams;
Expand All @@ -104,7 +104,7 @@ class EA {
*/
~EA() {
// Process tasks on quit.
estate.SetFlag(EA_STATE_FLAG_ON_QUIT, true);
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_ON_QUIT), true);
ProcessTasks();
// Deinitialize classes.
Object::Delete(account);
Expand All @@ -113,13 +113,29 @@ class EA {
/* Getters */

/**
* Gets a strategy parameter value.
* Gets EA parameter value.
*/
template <typename T>
T Get(STRUCT_ENUM(EAParams, ENUM_EA_PARAM_PROP) _param) {
return eparams.Get<T>(_param);
}

/**
* Gets EA state flag value.
*/
template <typename T>
T Get(STRUCT_ENUM(EAState, ENUM_EA_STATE_FLAGS) _prop) {
return estate.Get<T>(_prop);
}

/**
* Gets EA state property value.
*/
template <typename T>
T Get(STRUCT_ENUM(EAState, ENUM_EA_STATE_PROP) _prop) {
return estate.Get<T>(_prop);
}

/**
* Gets a Trade's state value.
*/
Expand Down Expand Up @@ -377,11 +393,11 @@ class EA {
Strategy *_strat = iter.Value().Ptr();
Trade *_trade = trade.GetByKey(_Symbol);
if (_strat.IsEnabled()) {
if (estate.new_periods >= DATETIME_MINUTE) {
if (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) >= DATETIME_MINUTE) {
// Process when new periods started.
_strat.OnPeriod(estate.new_periods);
_strat.OnPeriod(estate.Get<unsigned int>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)));
_strat.ProcessTasks();
_trade.OnPeriod(estate.new_periods);
_trade.OnPeriod(estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)));
eresults.stg_processed_periods++;
}
if (_strat.TickFilter(_tick)) {
Expand All @@ -402,19 +418,21 @@ class EA {
}
}
}
// Process all strategies' signals and trigger trading orders.
ProcessSignals(_tick, eparams.Get<unsigned int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_SIGNAL_FILTER)));
if (tsm.GetSignalsActive().Size() > 0 && tsm.IsReady()) {
// Process all strategies' signals and trigger trading orders.
ProcessSignals(_tick, eparams.Get<unsigned int>(STRUCT_ENUM(EAParams, EA_PARAM_PROP_SIGNAL_FILTER)));
}
if (eresults.last_error > ERR_NO_ERROR) {
// On error, print logs.
logger.Flush();
}
if (estate.new_periods >= DATETIME_MINUTE) {
if (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) >= DATETIME_MINUTE) {
// Process data, tasks and trades on new periods.
ProcessTrades();
}
}
estate.last_updated.Update();
if (estate.new_periods >= DATETIME_MINUTE) {
if (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) >= DATETIME_MINUTE) {
// Process data and tasks on new periods.
ProcessData();
ProcessTasks();
Expand Down Expand Up @@ -477,9 +495,9 @@ class EA {
* Checks for new starting periods.
*/
unsigned int ProcessPeriods() {
estate.new_periods = estate.last_updated.GetStartedPeriods();
estate.Set<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS), estate.last_updated.GetStartedPeriods());
OnPeriod();
return estate.new_periods;
return estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS));
}

/**
Expand Down Expand Up @@ -839,12 +857,12 @@ class EA {
* Update EA state flags.
*/
void UpdateStateFlags() {
estate.SetFlag(EA_STATE_FLAG_CONNECTED, GetTerminal().IsConnected());
estate.SetFlag(EA_STATE_FLAG_LIBS_ALLOWED, GetTerminal().IsLibrariesAllowed());
estate.SetFlag(EA_STATE_FLAG_OPTIMIZATION, GetTerminal().IsOptimization());
estate.SetFlag(EA_STATE_FLAG_TESTING, GetTerminal().IsTesting());
estate.SetFlag(EA_STATE_FLAG_TRADE_ALLOWED, GetTerminal().IsTradeAllowed());
estate.SetFlag(EA_STATE_FLAG_VISUAL_MODE, GetTerminal().IsVisualMode());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_CONNECTED), GetTerminal().IsConnected());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_LIBS_ALLOWED), GetTerminal().IsLibrariesAllowed());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_OPTIMIZATION), GetTerminal().IsOptimization());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_TESTING), GetTerminal().IsTesting());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_TRADE_ALLOWED), GetTerminal().IsTradeAllowed());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_VISUAL_MODE), GetTerminal().IsVisualMode());
}

/**
Expand Down Expand Up @@ -894,20 +912,20 @@ class EA {
case EA_COND_IS_ENABLED:
return estate.IsEnabled();
case EA_COND_IS_NOT_CONNECTED:
estate.SetFlag(EA_STATE_FLAG_CONNECTED, GetTerminal().IsConnected());
estate.Set(STRUCT_ENUM(EAState, EA_STATE_FLAG_CONNECTED), GetTerminal().IsConnected());
return !estate.IsConnected();
case EA_COND_ON_NEW_MINUTE: // On new minute.
return (estate.new_periods & DATETIME_MINUTE) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_MINUTE) != 0;
case EA_COND_ON_NEW_HOUR: // On new hour.
return (estate.new_periods & DATETIME_HOUR) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_HOUR) != 0;
case EA_COND_ON_NEW_DAY: // On new day.
return (estate.new_periods & DATETIME_DAY) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_DAY) != 0;
case EA_COND_ON_NEW_WEEK: // On new week.
return (estate.new_periods & DATETIME_WEEK) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_WEEK) != 0;
case EA_COND_ON_NEW_MONTH: // On new month.
return (estate.new_periods & DATETIME_MONTH) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_MONTH) != 0;
case EA_COND_ON_NEW_YEAR: // On new year.
return (estate.new_periods & DATETIME_YEAR) != 0;
return (estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_YEAR) != 0;
case EA_COND_ON_INIT:
return estate.IsOnInit();
case EA_COND_ON_QUIT:
Expand Down Expand Up @@ -1089,32 +1107,32 @@ class EA {
* Executed when new time is started (like each minute).
*/
virtual void OnPeriod() {
if ((estate.new_periods & DATETIME_MINUTE) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_MINUTE) != 0) {
// New minute started.
#ifndef __optimize__
if (Terminal::IsRealtime()) {
logger.Flush();
}
#endif
}
if ((estate.new_periods & DATETIME_HOUR) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_HOUR) != 0) {
// New hour started.
tsm.Refresh();
}
if ((estate.new_periods & DATETIME_DAY) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_DAY) != 0) {
// New day started.
UpdateLotSize();
#ifndef __optimize__
logger.Flush();
#endif
}
if ((estate.new_periods & DATETIME_WEEK) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_WEEK) != 0) {
// New week started.
}
if ((estate.new_periods & DATETIME_MONTH) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_MONTH) != 0) {
// New month started.
}
if ((estate.new_periods & DATETIME_YEAR) != 0) {
if ((estate.Get<uint>(STRUCT_ENUM(EAState, EA_STATE_PROP_NEW_PERIODS)) & DATETIME_YEAR) != 0) {
// New year started.
}
}
Expand Down
68 changes: 67 additions & 1 deletion EA.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,78 @@ struct EAProcessResult {

/* Defines EA state variables. */
struct EAState {
public: // @todo: Move to protected.
DateTime last_updated; // Last updated.
protected:
unsigned int flags; // Action flags.
unsigned int new_periods; // Started periods.
DateTime last_updated; // Last updated.
public:
/* Struct's enumerations */

/* Defines EA state flags. */
enum ENUM_EA_STATE_FLAGS {
EA_STATE_FLAG_NONE = 0 << 0, // None flags.
EA_STATE_FLAG_ACTIVE = 1 << 0, // Is active (can trade).
EA_STATE_FLAG_CONNECTED = 1 << 1, // Indicates connectedness to a trade server.
EA_STATE_FLAG_ENABLED = 1 << 2, // Is enabled.
EA_STATE_FLAG_LIBS_ALLOWED = 1 << 3, // Indicates the permission to use external libraries (such as DLL).
EA_STATE_FLAG_ON_INIT = 1 << 4, // Indicates EA is during initializing procedure (constructor).
EA_STATE_FLAG_ON_QUIT = 1 << 5, // Indicates EA is during exiting procedure (deconstructor).
EA_STATE_FLAG_OPTIMIZATION = 1 << 6, // Indicates EA runs in optimization mode.
EA_STATE_FLAG_TESTING = 1 << 7, // Indicates EA runs in testing mode.
EA_STATE_FLAG_TRADE_ALLOWED = 1 << 8, // Indicates the permission to trade on the chart.
EA_STATE_FLAG_VISUAL_MODE = 1 << 9, // Indicates EA runs in visual testing mode.
};

// Enumeration for strategy signal properties.
enum ENUM_EA_STATE_PROP {
EA_STATE_PROP_FLAGS = 1,
EA_STATE_PROP_LAST_UPDATED,
EA_STATE_PROP_NEW_PERIODS,
EA_STATE_PROP_TIMESTAMP,
};

/* Constructors */

// Constructor.
EAState() { EAState::AddFlags(EA_STATE_FLAG_ACTIVE | EA_STATE_FLAG_ENABLED); }
// Struct methods.
/* Getters */
template <typename T>
T Get(STRUCT_ENUM(EAState, ENUM_EA_STATE_PROP) _prop) {
switch (_prop) {
case EA_STATE_PROP_FLAGS:
return (T)flags;
/* @fixme
case EA_STATE_PROP_LAST_UPDATED:
return (T)last_updated;
*/
case EA_STATE_PROP_NEW_PERIODS:
return (T)new_periods;
}
SetUserError(ERR_INVALID_PARAMETER);
return (T)WRONG_VALUE;
}
bool Get(STRUCT_ENUM(EAState, ENUM_EA_STATE_FLAGS) _prop) { return CheckFlag(_prop); }
/* Setters */
template <typename T>
void Set(STRUCT_ENUM(EAState, ENUM_EA_STATE_PROP) _prop, T _value) {
switch (_prop) {
case EA_STATE_PROP_FLAGS:
flags = (unsigned int)_value;
return;
/* @fixme
case EA_STATE_PROP_LAST_UPDATED:
last_updated = (unsigned int)_value;
return;
*/
case EA_STATE_PROP_NEW_PERIODS:
new_periods = (unsigned int)_value;
return;
}
SetUserError(ERR_INVALID_PARAMETER);
}
void Set(STRUCT_ENUM(EAState, ENUM_EA_STATE_FLAGS) _prop, bool _value) { SetFlag(_prop, _value); }
// Flag methods.
bool CheckFlag(unsigned int _flag) { return bool(flags & _flag); }
void AddFlags(unsigned int _flags) { flags |= _flags; }
Expand Down
12 changes: 7 additions & 5 deletions Indicators/Indi_BWMFI.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ class Indi_BWMFI : public Indicator<BWMFIParams> {
}

/**
* Returns the indicator's entry value.
* Checks if indicator entry is valid.
*
* @return
* Returns true if entry is valid (has valid values), otherwise false.
*/
MqlParam GetEntryValue(int _shift = 0, int _mode = 0) {
MqlParam _param = {TYPE_DOUBLE};
GetEntry(_shift).values[_mode].Get(_param.double_value);
return _param;
virtual bool IsValidEntry(IndicatorDataEntry &_entry) {
return _entry[(int)BWMFI_BUFFER] > 0 && _entry[(int)BWMFI_HISTCOLOR] >= 0 && !_entry.HasValue<double>(DBL_MAX) &&
!_entry.HasValue<double>(EMPTY_VALUE);
}
};
2 changes: 1 addition & 1 deletion Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ class Strategy : public Object {
int _count = (int)fmax(fabs(_level), fabs(_method));
int _direction = Order::OrderDirection(_cmd, _mode);
Chart *_chart = trade.GetChart();
IndicatorBase *_indi = GetIndicator();
IndicatorBase *_indi = GetIndicators().Begin().Value().Ptr();
StrategyPriceStop _psm(_method);
_psm.SetChartParams(_chart.GetParams());
if (Object::IsValid(_indi)) {
Expand Down
2 changes: 1 addition & 1 deletion Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Trade {
/* Setters */

/**
* Gets a trade parameter value.
* Sets a trade parameter value.
*/
template <typename T>
void Set(ENUM_TRADE_PARAM _param, T _value) {
Expand Down
1 change: 1 addition & 0 deletions Trade/TradeSignal.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct TradeSignalEntry {
TRADE_SIGNAL_PROP_TIME,
TRADE_SIGNAL_PROP_WEIGHT,
};

// Enumeration for strategy signal types.
enum ENUM_TRADE_SIGNAL_OP {
TRADE_SIGNAL_OP_SELL = -1, // Signal to sell.
Expand Down
Loading

0 comments on commit 1104fc7

Please sign in to comment.