Skip to content

Commit

Permalink
Refactors code to load strategies using StrategiesManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Oct 28, 2023
1 parent f318b69 commit 7506941
Showing 1 changed file with 23 additions and 205 deletions.
228 changes: 23 additions & 205 deletions Stg_Meta_Trend.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Stg_Meta_Trend_Params_Defaults : StgParams {

class Stg_Meta_Trend : public Strategy {
protected:
Ref<Strategy> strat;
DictStruct<long, Ref<Strategy>> strats;

public:
Stg_Meta_Trend(StgParams &_sparams, TradeParams &_tparams, ChartParams &_cparams, string _name = "")
Expand All @@ -73,7 +73,7 @@ class Stg_Meta_Trend : public Strategy {
* Event on strategy's init.
*/
void OnInit() {
SetStrategy(Meta_Trend_Strategy);
StrategyAdd(Meta_Trend_Strategy, 0);
// Initialize indicators.
{
IndiRSIParams _indi_params(::Meta_Trend_RSI_Period, ::Meta_Trend_RSI_Applied_Price, ::Meta_Trend_RSI_Shift);
Expand All @@ -86,213 +86,30 @@ class Stg_Meta_Trend : public Strategy {
/**
* Sets strategy.
*/
bool SetStrategy(ENUM_STRATEGY _sid) {
bool _result = true;
long _magic_no = Get<long>(STRAT_PARAM_ID);
bool StrategyAdd(ENUM_STRATEGY _sid, long _index = -1) {
ENUM_TIMEFRAMES _tf = Get<ENUM_TIMEFRAMES>(STRAT_PARAM_TF);

switch (_sid) {
case STRAT_NONE:
break;
case STRAT_AC:
_result &= StrategyAdd<Stg_AC>(_tf, _magic_no, _sid);
break;
case STRAT_AD:
_result &= StrategyAdd<Stg_AD>(_tf, _magic_no, _sid);
break;
case STRAT_ADX:
_result &= StrategyAdd<Stg_ADX>(_tf, _magic_no, _sid);
break;
case STRAT_AMA:
_result &= StrategyAdd<Stg_AMA>(_tf, _magic_no, _sid);
break;
case STRAT_ARROWS:
_result &= StrategyAdd<Stg_Arrows>(_tf, _magic_no, _sid);
break;
case STRAT_ASI:
_result &= StrategyAdd<Stg_ASI>(_tf, _magic_no, _sid);
break;
case STRAT_ATR:
_result &= StrategyAdd<Stg_ATR>(_tf, _magic_no, _sid);
break;
case STRAT_ALLIGATOR:
_result &= StrategyAdd<Stg_Alligator>(_tf, _magic_no, _sid);
break;
case STRAT_AWESOME:
_result &= StrategyAdd<Stg_Awesome>(_tf, _magic_no, _sid);
break;
case STRAT_BWMFI:
_result &= StrategyAdd<Stg_BWMFI>(_tf, _magic_no, _sid);
break;
case STRAT_BANDS:
_result &= StrategyAdd<Stg_Bands>(_tf, _magic_no, _sid);
break;
case STRAT_BEARS_POWER:
_result &= StrategyAdd<Stg_BearsPower>(_tf, _magic_no, _sid);
break;
case STRAT_BULLS_POWER:
_result &= StrategyAdd<Stg_BullsPower>(_tf, _magic_no, _sid);
break;
case STRAT_CCI:
_result &= StrategyAdd<Stg_CCI>(_tf, _magic_no, _sid);
break;
case STRAT_CHAIKIN:
_result &= StrategyAdd<Stg_Chaikin>(_tf, _magic_no, _sid);
break;
case STRAT_DEMA:
_result &= StrategyAdd<Stg_DEMA>(_tf, _magic_no, _sid);
break;
case STRAT_DEMARKER:
_result &= StrategyAdd<Stg_DeMarker>(_tf, _magic_no, _sid);
break;
case STRAT_DPO:
_result &= StrategyAdd<Stg_DPO>(_tf, _magic_no, _sid);
break;
case STRAT_ENVELOPES:
_result &= StrategyAdd<Stg_Envelopes>(_tf, _magic_no, _sid);
break;
case STRAT_FORCE:
_result &= StrategyAdd<Stg_Force>(_tf, _magic_no, _sid);
break;
case STRAT_FRACTALS:
_result &= StrategyAdd<Stg_Fractals>(_tf, _magic_no, _sid);
break;
case STRAT_GATOR:
_result &= StrategyAdd<Stg_Gator>(_tf, _magic_no, _sid);
break;
case STRAT_HEIKEN_ASHI:
_result &= StrategyAdd<Stg_HeikenAshi>(_tf, _magic_no, _sid);
break;
case STRAT_ICHIMOKU:
_result &= StrategyAdd<Stg_Ichimoku>(_tf, _magic_no, _sid);
break;
case STRAT_INDICATOR:
_result &= StrategyAdd<Stg_Indicator>(_tf, _magic_no, _sid);
break;
case STRAT_MA:
_result &= StrategyAdd<Stg_MA>(_tf, _magic_no, _sid);
break;
case STRAT_MA_BREAKOUT:
_result &= StrategyAdd<Stg_MA_Breakout>(_tf, _magic_no, _sid);
break;
case STRAT_MA_CROSS_PIVOT:
_result &= StrategyAdd<Stg_MA_Cross_Pivot>(_tf, _magic_no, _sid);
break;
case STRAT_MA_CROSS_SHIFT:
_result &= StrategyAdd<Stg_MA_Cross_Shift>(_tf, _magic_no, _sid);
break;
case STRAT_MA_CROSS_SUP_RES:
_result &= StrategyAdd<Stg_MA_Cross_Sup_Res>(_tf, _magic_no, _sid);
break;
case STRAT_MA_TREND:
_result &= StrategyAdd<Stg_MA_Trend>(_tf, _magic_no, _sid);
break;
case STRAT_MACD:
_result &= StrategyAdd<Stg_MACD>(_tf, _magic_no, _sid);
break;
case STRAT_MFI:
_result &= StrategyAdd<Stg_MFI>(_tf, _magic_no, _sid);
break;
case STRAT_MOMENTUM:
_result &= StrategyAdd<Stg_Momentum>(_tf, _magic_no, _sid);
break;
case STRAT_OBV:
_result &= StrategyAdd<Stg_OBV>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR:
_result &= StrategyAdd<Stg_Oscillator>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_DIVERGENCE:
_result &= StrategyAdd<Stg_Oscillator_Divergence>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_MULTI:
_result &= StrategyAdd<Stg_Oscillator_Multi>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_CROSS:
_result &= StrategyAdd<Stg_Oscillator_Cross>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_CROSS_SHIFT:
_result &= StrategyAdd<Stg_Oscillator_Cross_Shift>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_CROSS_ZERO:
_result &= StrategyAdd<Stg_Oscillator_Cross_Zero>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_RANGE:
_result &= StrategyAdd<Stg_Oscillator_Range>(_tf, _magic_no, _sid);
break;
case STRAT_OSCILLATOR_TREND:
_result &= StrategyAdd<Stg_Oscillator_Trend>(_tf, _magic_no, _sid);
break;
case STRAT_OSMA:
_result &= StrategyAdd<Stg_OsMA>(_tf, _magic_no, _sid);
break;
case STRAT_PATTERN:
_result &= StrategyAdd<Stg_Pattern>(_tf, _magic_no, _sid);
break;
case STRAT_PINBAR:
_result &= StrategyAdd<Stg_Pinbar>(_tf, _magic_no, _sid);
break;
case STRAT_PIVOT:
_result &= StrategyAdd<Stg_Pivot>(_tf, _magic_no, _sid);
break;
case STRAT_RSI:
_result &= StrategyAdd<Stg_RSI>(_tf, _magic_no, _sid);
break;
case STRAT_RVI:
_result &= StrategyAdd<Stg_RVI>(_tf, _magic_no, _sid);
break;
case STRAT_SAR:
_result &= StrategyAdd<Stg_SAR>(_tf, _magic_no, _sid);
break;
case STRAT_STDDEV:
_result &= StrategyAdd<Stg_StdDev>(_tf, _magic_no, _sid);
break;
case STRAT_STOCHASTIC:
_result &= StrategyAdd<Stg_Stochastic>(_tf, _magic_no, _sid);
break;
case STRAT_WPR:
_result &= StrategyAdd<Stg_WPR>(_tf, _magic_no, _sid);
break;
case STRAT_ZIGZAG:
_result &= StrategyAdd<Stg_ZigZag>(_tf, _magic_no, _sid);
break;
default:
logger.Warning(StringFormat("Unknown strategy: %d", _sid), __FUNCTION_LINE__, GetName());
break;
Ref<Strategy> _strat = StrategiesManager::StrategyInitByEnum(_sid, _tf);
if (_strat.IsSet()) {
_strat.Ptr().Set<long>(STRAT_PARAM_ID, Get<long>(STRAT_PARAM_ID));
_strat.Ptr().Set<ENUM_TIMEFRAMES>(STRAT_PARAM_TF, _tf);
_strat.Ptr().Set<int>(STRAT_PARAM_TYPE, _sid);
_strat.Ptr().OnInit();
if (_index >= 0) {
strats.Set(_index, _strat);
} else {
strats.Push(_strat);
}
}

return _result;
}

/**
* Adds strategy to specific timeframe.
*
* @param
* _tf - timeframe to add the strategy.
* _magic_no - unique order identified
*
* @return
* Returns true if the strategy has been initialized correctly, otherwise false.
*/
template <typename SClass>
bool StrategyAdd(ENUM_TIMEFRAMES _tf, long _magic_no = 0, int _type = 0) {
bool _result = true;
_magic_no = _magic_no > 0 ? _magic_no : rand();
Ref<Strategy> _strat = ((SClass *)NULL).Init(_tf);
_strat.Ptr().Set<long>(STRAT_PARAM_ID, _magic_no);
_strat.Ptr().Set<ENUM_TIMEFRAMES>(STRAT_PARAM_TF, _tf);
_strat.Ptr().Set<int>(STRAT_PARAM_TYPE, _type);
_strat.Ptr().OnInit();
strat = _strat;
return _result;
return _strat.IsSet();
}

/**
* Check strategy's opening signal.
*/
bool SignalOpen(ENUM_ORDER_TYPE _cmd, int _method, float _level = 0.0f, int _shift = 0) {
bool _result = true;
if (!strat.IsSet()) {
Ref<Strategy> _strat = strats.GetByKey(0);
if (!_strat.IsSet()) {
// Returns false when strategy is not set.
return false;
}
Expand All @@ -307,10 +124,10 @@ class Stg_Meta_Trend : public Strategy {
_result &= _indi[_shift][0] <= 50 + _level && _indi[_shift + 1][0] <= 50 + _level;
break;
}
_level = _level == 0.0f ? strat.Ptr().Get<float>(STRAT_PARAM_SOL) : _level;
_method = _method == 0 ? strat.Ptr().Get<int>(STRAT_PARAM_SOM) : _method;
_shift = _shift == 0 ? strat.Ptr().Get<int>(STRAT_PARAM_SHIFT) : _shift;
_result &= strat.Ptr().SignalOpen(_cmd, _method, _level, _shift);
_level = _level == 0.0f ? _strat.Ptr().Get<float>(STRAT_PARAM_SOL) : _level;
_method = _method == 0 ? _strat.Ptr().Get<int>(STRAT_PARAM_SOM) : _method;
_shift = _shift == 0 ? _strat.Ptr().Get<int>(STRAT_PARAM_SHIFT) : _shift;
_result &= _strat.Ptr().SignalOpen(_cmd, _method, _level, _shift);
return _result;
}

Expand All @@ -319,7 +136,8 @@ class Stg_Meta_Trend : public Strategy {
*/
bool SignalClose(ENUM_ORDER_TYPE _cmd, int _method, float _level = 0.0f, int _shift = 0) {
bool _result = true;
if (!strat.IsSet()) {
Ref<Strategy> _strat = strats.GetByKey(0);
if (!_strat.IsSet()) {
// Returns false when strategy is not set.
return false;
}
Expand Down

0 comments on commit 7506941

Please sign in to comment.