Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements Meta Trend strategy logic #1

Merged
merged 6 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/backtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ jobs:
include: .
init-platform: true
mt-version: 5.0.0.2361
path: Stg_Meta_Reversal.mq4
path: Stg_Meta_Trend.mq4
verbose: true
- name: Compile for MQL5
uses: fx31337/mql-compile-action@master
with:
include: .
mt-version: 5.0.0.2515
path: Stg_Meta_Reversal.mq5
path: Stg_Meta_Trend.mq5
verbose: true
- name: List compiled files
run: '(Get-ChildItem -Recurse -Path . -Include *.ex[45]).fullname'
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
OptFormatBrief: true
OptFormatJson: true
OptVerbose: true
TestExpert: "Stg_Meta_Reversal"
TestExpert: "Stg_Meta_Trend"
TestPeriod: M1
TestReportName: Report-${{ matrix.year }}-${{ matrix.month }}
- name: Upload results
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ jobs:
include: .
init-platform: true
mt-version: 5.0.0.2361
path: Stg_Meta_Reversal.mq4
path: Stg_Meta_Trend.mq4
verbose: true
- name: Compile for MQL5
uses: fx31337/mql-compile-action@master
with:
include: .
mt-version: 5.0.0.2515
path: Stg_Meta_Reversal.mq5
path: Stg_Meta_Trend.mq5
verbose: true
- name: List compiled files
run: '(Get-ChildItem -Recurse -Path . -Include *.ex[45]).fullname'
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Strategy Meta Reversal
# Strategy Meta Trend

[![Status][gha-image-check-master]][gha-link-check-master]
[![Status][gha-image-compile-master]][gha-link-compile-master]
Expand All @@ -7,7 +7,8 @@
[![License][license-image]][license-link]
[![Edit][gh-edit-badge]][gh-edit-link]

Reversal meta strategy reverses signals of another strategy.
Trend meta strategy to filter out
signals of other strategies based on the current trend.

## Dependencies

Expand All @@ -21,12 +22,12 @@ Reversal meta strategy reverses signals of another strategy.
[gh-discuss-link]: https://github.com/EA31337/EA31337-Strategies/discussions

[gh-edit-badge]: https://img.shields.io/badge/GitHub-edit-purple.svg?logo=github
[gh-edit-link]: https://github.dev/EA31337/Strategy-Meta_Reversal
[gh-edit-link]: https://github.dev/EA31337/Strategy-Meta_Trend

[gha-link-check-master]: https://github.com/EA31337/Strategy-Meta_Reversal/actions?query=workflow:Check+branch%3Amaster
[gha-image-check-master]: https://github.com/EA31337/Strategy-Meta_Reversal/workflows/Check/badge.svg?branch=master
[gha-link-compile-master]: https://github.com/EA31337/Strategy-Meta_Reversal/actions?query=workflow:Compile+branch%3Amaster
[gha-image-compile-master]: https://github.com/EA31337/Strategy-Meta_Reversal/workflows/Compile/badge.svg?branch=master
[gha-link-check-master]: https://github.com/EA31337/Strategy-Meta_Trend/actions?query=workflow:Check+branch%3Amaster
[gha-image-check-master]: https://github.com/EA31337/Strategy-Meta_Trend/workflows/Check/badge.svg?branch=master
[gha-link-compile-master]: https://github.com/EA31337/Strategy-Meta_Trend/actions?query=workflow:Compile+branch%3Amaster
[gha-image-compile-master]: https://github.com/EA31337/Strategy-Meta_Trend/workflows/Compile/badge.svg?branch=master

[tg-channel-image]: https://img.shields.io/badge/Telegram-join-0088CC.svg?logo=telegram
[tg-channel-link]: https://t.me/EA31337
Expand Down
4 changes: 2 additions & 2 deletions Stg_Meta_Reversal.mq4 → Stg_Meta_Trend.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/**
* @file
* Implements Reversal meta strategy.
* Implements Trend meta strategy.
*/

// Includes the main code.
#include "Stg_Meta_Reversal.mq5"
#include "Stg_Meta_Trend.mq5"
12 changes: 6 additions & 6 deletions Stg_Meta_Reversal.mq5 → Stg_Meta_Trend.mq5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file
* Implements Reversal meta strategy.
* Implements Trend meta strategy.
*/

// Includes conditional compilation directives.
Expand Down Expand Up @@ -32,13 +32,13 @@ input ENUM_LOG_LEVEL Log_Level = V_INFO; // Log level.
input bool Info_On_Chart = true; // Display info on chart.

// Includes strategy class.
#include "Stg_Meta_Reversal.mqh"
#include "Stg_Meta_Trend.mqh"

// Defines.
#define ea_name "Strategy Meta Reversal"
#define ea_name "Strategy Meta Trend"
#define ea_version "2.000"
#define ea_desc "Reversal meta strategy reverses signals of another strategy."
#define ea_link "https://github.com/EA31337/Strategy-Meta_Reversal"
#define ea_desc "Trend meta strategy to filter out signals of other strategies based on the current trend."
#define ea_link "https://github.com/EA31337/Strategy-Meta_Trend"
#define ea_author "EA31337 Ltd"

// Properties.
Expand All @@ -64,7 +64,7 @@ int OnInit() {
bool _result = true;
EAParams ea_params(__FILE__, Log_Level);
ea = new EA(ea_params);
_result &= ea.StrategyAdd<Stg_Meta_Reversal>(Active_Tfs);
_result &= ea.StrategyAdd<Stg_Meta_Trend>(Active_Tfs);
return (_result ? INIT_SUCCEEDED : INIT_FAILED);
}

Expand Down
123 changes: 76 additions & 47 deletions Stg_Meta_Reversal.mqh → Stg_Meta_Trend.mqh
Original file line number Diff line number Diff line change
@@ -1,75 +1,87 @@
/**
* @file
* Implements Reversal meta strategy.
* Implements Trend meta strategy.
*/

// Prevents processing this includes file multiple times.
#ifndef STG_META_REVERSAL_MQH
#define STG_META_REVERSAL_MQH
#ifndef STG_META_TREND_MQH
#define STG_META_TREND_MQH

// User input params.
INPUT2_GROUP("Meta Reversal strategy: main params");
INPUT2 ENUM_STRATEGY Meta_Reversal_Strategy = STRAT_ATR; // Strategy to reverse signals
INPUT2_GROUP("Meta Reversal strategy: common params");
INPUT2 float Meta_Reversal_LotSize = 0; // Lot size
INPUT2 int Meta_Reversal_SignalOpenMethod = 0; // Signal open method
INPUT2 float Meta_Reversal_SignalOpenLevel = 0; // Signal open level
INPUT2 int Meta_Reversal_SignalOpenFilterMethod = 32; // Signal open filter method
INPUT2 int Meta_Reversal_SignalOpenFilterTime = 3; // Signal open filter time (0-31)
INPUT2 int Meta_Reversal_SignalOpenBoostMethod = 0; // Signal open boost method
INPUT2 int Meta_Reversal_SignalCloseMethod = 0; // Signal close method
INPUT2 int Meta_Reversal_SignalCloseFilter = 32; // Signal close filter (-127-127)
INPUT2 float Meta_Reversal_SignalCloseLevel = 0; // Signal close level
INPUT2 int Meta_Reversal_PriceStopMethod = 0; // Price limit method
INPUT2 float Meta_Reversal_PriceStopLevel = 2; // Price limit level
INPUT2 int Meta_Reversal_TickFilterMethod = 32; // Tick filter method (0-255)
INPUT2 float Meta_Reversal_MaxSpread = 4.0; // Max spread to trade (in pips)
INPUT2 short Meta_Reversal_Shift = 0; // Shift
INPUT2 float Meta_Reversal_OrderCloseLoss = 30; // Order close loss
INPUT2 float Meta_Reversal_OrderCloseProfit = 30; // Order close profit
INPUT2 int Meta_Reversal_OrderCloseTime = -10; // Order close time in mins (>0) or bars (<0)
INPUT2_GROUP("Meta Trend strategy: main params");
INPUT2 ENUM_STRATEGY Meta_Trend_Strategy = STRAT_STOCHASTIC; // Strategy to filter by trend
INPUT2_GROUP("Meta Trend strategy: common params");
INPUT2 float Meta_Trend_LotSize = 0; // Lot size
INPUT2 int Meta_Trend_SignalOpenMethod = 0; // Signal open method
INPUT2 float Meta_Trend_SignalOpenLevel = 0; // Signal open level
INPUT2 int Meta_Trend_SignalOpenFilterMethod = 32; // Signal open filter method
INPUT2 int Meta_Trend_SignalOpenFilterTime = 3; // Signal open filter time (0-31)
INPUT2 int Meta_Trend_SignalOpenBoostMethod = 0; // Signal open boost method
INPUT2 int Meta_Trend_SignalCloseMethod = 0; // Signal close method
INPUT2 int Meta_Trend_SignalCloseFilter = 32; // Signal close filter (-127-127)
INPUT2 float Meta_Trend_SignalCloseLevel = 0; // Signal close level
INPUT2 int Meta_Trend_PriceStopMethod = 0; // Price limit method
INPUT2 float Meta_Trend_PriceStopLevel = 2; // Price limit level
INPUT2 int Meta_Trend_TickFilterMethod = 32; // Tick filter method (0-255)
INPUT2 float Meta_Trend_MaxSpread = 4.0; // Max spread to trade (in pips)
INPUT2 short Meta_Trend_Shift = 0; // Shift
INPUT2 float Meta_Trend_OrderCloseLoss = 200; // Order close loss
INPUT2 float Meta_Trend_OrderCloseProfit = 200; // Order close profit
INPUT2 int Meta_Trend_OrderCloseTime = 2880; // Order close time in mins (>0) or bars (<0)
INPUT_GROUP("Meta Trend strategy: RSI oscillator params");
INPUT int Meta_Trend_RSI_Period = 16; // Period
INPUT ENUM_APPLIED_PRICE Meta_Trend_RSI_Applied_Price = PRICE_WEIGHTED; // Applied Price
INPUT int Meta_Trend_RSI_Shift = 0; // Shift
INPUT ENUM_IDATA_SOURCE_TYPE Meta_Trend_RSI_SourceType = IDATA_BUILTIN; // Source type

// Structs.

// Defines struct with default user strategy values.
struct Stg_Meta_Reversal_Params_Defaults : StgParams {
Stg_Meta_Reversal_Params_Defaults()
: StgParams(::Meta_Reversal_SignalOpenMethod, ::Meta_Reversal_SignalOpenFilterMethod,
::Meta_Reversal_SignalOpenLevel, ::Meta_Reversal_SignalOpenBoostMethod,
::Meta_Reversal_SignalCloseMethod, ::Meta_Reversal_SignalCloseFilter,
::Meta_Reversal_SignalCloseLevel, ::Meta_Reversal_PriceStopMethod, ::Meta_Reversal_PriceStopLevel,
::Meta_Reversal_TickFilterMethod, ::Meta_Reversal_MaxSpread, ::Meta_Reversal_Shift) {
Set(STRAT_PARAM_LS, Meta_Reversal_LotSize);
Set(STRAT_PARAM_OCL, Meta_Reversal_OrderCloseLoss);
Set(STRAT_PARAM_OCP, Meta_Reversal_OrderCloseProfit);
Set(STRAT_PARAM_OCT, Meta_Reversal_OrderCloseTime);
Set(STRAT_PARAM_SOFT, Meta_Reversal_SignalOpenFilterTime);
struct Stg_Meta_Trend_Params_Defaults : StgParams {
Stg_Meta_Trend_Params_Defaults()
: StgParams(::Meta_Trend_SignalOpenMethod, ::Meta_Trend_SignalOpenFilterMethod, ::Meta_Trend_SignalOpenLevel,
::Meta_Trend_SignalOpenBoostMethod, ::Meta_Trend_SignalCloseMethod, ::Meta_Trend_SignalCloseFilter,
::Meta_Trend_SignalCloseLevel, ::Meta_Trend_PriceStopMethod, ::Meta_Trend_PriceStopLevel,
::Meta_Trend_TickFilterMethod, ::Meta_Trend_MaxSpread, ::Meta_Trend_Shift) {
Set(STRAT_PARAM_LS, ::Meta_Trend_LotSize);
Set(STRAT_PARAM_OCL, ::Meta_Trend_OrderCloseLoss);
Set(STRAT_PARAM_OCP, ::Meta_Trend_OrderCloseProfit);
Set(STRAT_PARAM_OCT, ::Meta_Trend_OrderCloseTime);
Set(STRAT_PARAM_SOFT, ::Meta_Trend_SignalOpenFilterTime);
}
};

class Stg_Meta_Reversal : public Strategy {
class Stg_Meta_Trend : public Strategy {
protected:
Ref<Strategy> strat;

public:
Stg_Meta_Reversal(StgParams &_sparams, TradeParams &_tparams, ChartParams &_cparams, string _name = "")
Stg_Meta_Trend(StgParams &_sparams, TradeParams &_tparams, ChartParams &_cparams, string _name = "")
: Strategy(_sparams, _tparams, _cparams, _name) {}

static Stg_Meta_Reversal *Init(ENUM_TIMEFRAMES _tf = NULL, EA *_ea = NULL) {
static Stg_Meta_Trend *Init(ENUM_TIMEFRAMES _tf = NULL, EA *_ea = NULL) {
// Initialize strategy initial values.
Stg_Meta_Reversal_Params_Defaults stg_reversal_defaults;
StgParams _stg_params(stg_reversal_defaults);
Stg_Meta_Trend_Params_Defaults stg_trend_defaults;
StgParams _stg_params(stg_trend_defaults);
// Initialize Strategy instance.
ChartParams _cparams(_tf, _Symbol);
TradeParams _tparams;
Strategy *_strat = new Stg_Meta_Reversal(_stg_params, _tparams, _cparams, "(Meta) Reversal");
Strategy *_strat = new Stg_Meta_Trend(_stg_params, _tparams, _cparams, "(Meta) Trend");
return _strat;
}

/**
* Event on strategy's init.
*/
void OnInit() { SetStrategy(Meta_Reversal_Strategy); }
void OnInit() {
SetStrategy(Meta_Trend_Strategy);
// Initialize indicators.
{
IndiRSIParams _indi_params(::Meta_Trend_RSI_Period, ::Meta_Trend_RSI_Applied_Price, ::Meta_Trend_RSI_Shift);
_indi_params.SetDataSourceType(::Meta_Trend_RSI_SourceType);
_indi_params.SetTf(PERIOD_D1);
SetIndicator(new Indi_RSI(_indi_params));
}
}

/**
* Sets strategy.
Expand Down Expand Up @@ -133,6 +145,9 @@ class Stg_Meta_Reversal : public Strategy {
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;
Expand All @@ -157,6 +172,9 @@ class Stg_Meta_Reversal : public Strategy {
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;
Expand Down Expand Up @@ -273,15 +291,26 @@ class Stg_Meta_Reversal : public Strategy {
* Check strategy's opening signal.
*/
bool SignalOpen(ENUM_ORDER_TYPE _cmd, int _method, float _level = 0.0f, int _shift = 0) {
bool _result = false;
bool _result = true;
if (!strat.IsSet()) {
// Returns false when strategy is not set.
return _result;
return false;
}
IndicatorBase *_indi = GetIndicator();
// uint _ishift = _indi.GetShift();
uint _ishift = _shift;
switch (_cmd) {
case ORDER_TYPE_BUY:
_result &= _indi[_shift][0] >= 50 && _indi[_shift + 1][0] >= 50;
break;
case ORDER_TYPE_SELL:
_result &= _indi[_shift][0] <= 50 && _indi[_shift + 1][0] <= 50;
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(Order::NegateOrderType(_cmd), _method, _level, _shift);
_result &= strat.Ptr().SignalOpen(_cmd, _method, _level, _shift);
return _result;
}

Expand All @@ -302,4 +331,4 @@ class Stg_Meta_Reversal : public Strategy {
}
};

#endif // STG_META_REVERSAL_MQH
#endif // STG_META_TREND_MQH
Loading