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

Adds strategy init #771

Open
wants to merge 7 commits into
base: v3.001-dev-new
Choose a base branch
from
Open
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
57 changes: 55 additions & 2 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
name: Compile

env:
ARTIFACT_PREFIX: ${{ inputs.artifact_prefix || 'mt' }}

# yamllint disable-line rule:truthy
on:
pull_request:
Expand All @@ -9,18 +12,68 @@ on:
push:
paths-ignore:
- '**.md'
workflow_call:
inputs:
artifact_prefix:
default: mt
description: Artifact prefix.
required: false
type: string
skip_cleanup:
default: false
description: Whether to skip a clean-up job.
required: false
type: boolean

jobs:

Compile:
mt4:
name: Installs platform (4)
uses: EA31337/EA-Tester/.github/workflows/platform-linux.yml@dev
with:
artifact_name: ${{ inputs.artifact_prefix || 'mt' }}4
artifact_overwrite: true
skip_cleanup: true
version: 4
mt5:
name: Installs platform (5)
uses: EA31337/EA-Tester/.github/workflows/platform-linux.yml@dev
with:
artifact_name: ${{ inputs.artifact_prefix || 'mt' }}5
artifact_overwrite: true
skip_cleanup: true
version: 5

compile:
name: Compile
needs: [mt4, mt5]
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: mt${{ matrix.version }}
path: .mt${{ matrix.version }}
- name: Compile
uses: fx31337/mql-compile-action@master
with:
init-platform: true
mt-path: .mt${{ matrix.version }}
verbose: true
- name: Print compiled files
run: '(Get-ChildItem -Recurse -Path . -Include *.ex[45]).fullname'
shell: powershell

strategy:
matrix:
version: [4, 5]

cleanup:
if: inputs.skip_cleanup != true
name: Clean-up
needs: [compile]
runs-on: ubuntu-latest
steps:
- uses: geekyeggo/delete-artifact@v5
with:
name: ${{ env.ARTIFACT_PREFIX }}*
5 changes: 4 additions & 1 deletion Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ class Account {
/**
* Get current account drawdown in percent.
*/
static double GetDrawdownInPct() { return (100 / AccountTotalBalance()) * (AccountTotalBalance() - AccountEquity()); }
static double GetDrawdownInPct() {
double _balance_total = AccountTotalBalance();
return _balance_total != 0 ? (100 / AccountTotalBalance()) * (AccountTotalBalance() - AccountEquity()) : 0.0;
}

/**
* Get current account risk margin level.
Expand Down
2 changes: 1 addition & 1 deletion EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ class EA {
_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.Ptr().Init();
if (!strats.KeyExists(_magic_no)) {
_result &= strats.Set(_magic_no, _strat);
} else {
Expand Down
12 changes: 9 additions & 3 deletions Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Strategy : public Object {
int filter_method[]; // Filter method to consider the trade.
int open_condition[]; // Open conditions.
int close_condition[]; // Close conditions.
bool is_initialized; // Whether OnInit() was already called.

public:
/* Special methods */
Expand All @@ -137,8 +138,9 @@ class Strategy : public Object {
// UpdateOrderStats(EA_STATS_MONTHLY);
// UpdateOrderStats(EA_STATS_TOTAL);

// Call strategy's OnInit method.
Strategy::OnInit();
// Instead of calling Stategy::OnInit(), we do the initialization here.
// It is because we can't call virtual methods in constructor.
SetStops(GetPointer(this), GetPointer(this));
}

Log *GetLogger() { return GetPointer(logger); }
Expand Down Expand Up @@ -660,6 +662,11 @@ class Strategy : public Object {
* Initialize strategy.
*/
bool Init() {
if (!is_initialized) {
is_initialized = true;
OnInit(); // Calling virtual method.
}

if (!trade.IsValid()) {
/* @fixme
logger.Warning(StringFormat("Could not initialize %s on %s timeframe!", GetName(),
Expand Down Expand Up @@ -863,7 +870,6 @@ class Strategy : public Object {
* Event on strategy's init.
*/
virtual void OnInit() {
SetStops(GetPointer(this), GetPointer(this));
// trade.SetStrategy(&this); // @fixme
}

Expand Down
3 changes: 3 additions & 0 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ class Trade {
_request.symbol = GetChart().Get<string>(CHART_PARAM_SYMBOL);
_request.price = SymbolInfoStatic::GetOpenOffer(_request.symbol, _type);
_request.type = _type;
#ifndef __MQL4__
// Filling modes not supported under MQL4.
_request.type_filling = Order::GetOrderFilling(_request.symbol);
#endif
_request.volume = _volume > 0 ? _volume : tparams.Get<float>(TRADE_PARAM_LOT_SIZE);
_request.volume = NormalizeLots(fmax(_request.volume, SymbolInfoStatic::GetVolumeMin(_request.symbol)));
return _request;
Expand Down
Loading