Skip to content

Commit

Permalink
Adds initial Backtest class
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Sep 9, 2024
1 parent f2bb79a commit cc93786
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
strategy:
matrix:
test:
- Backtest.test
- Order.test
- OrderQuery.test
- Orders.test
Expand Down
62 changes: 62 additions & 0 deletions Platform/Backtest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2024, EA31337 Ltd |
//| https://ea31337.github.io |
//+------------------------------------------------------------------+

/*
* 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
* Implements Backtest class for internally backtesting strategies.
*/

#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

// Includes.
#include "../EA.mqh"

class Backtest {
protected:
// Class variables.
EA *ea;

/**
* Init code (called on constructor).
*/
void Init() {
// ...
}

public:
/**
* Class constructor.
*/
Backtest() {
Init();
}

/**
* Class constructor.
*/
Backtest(EAParams &_eparam) : ea(_eparam) {
Init();
}
};
34 changes: 34 additions & 0 deletions Platform/tests/Backtest.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2024, EA31337 Ltd |
//| https://ea31337.github.io |
//+------------------------------------------------------------------+

/*
* 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 C++ compilation of Backtest class.
*/

// Includes.
#include "../Backtest.h"

int main(int argc, char **argv) {
// @todo

return 0;
}
28 changes: 28 additions & 0 deletions Platform/tests/Backtest.test.mq4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2024, EA31337 Ltd |
//| https://ea31337.github.io |
//+------------------------------------------------------------------+

/*
* 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 Backtest class.
*/

// Includes.
#include "Backtest.test.mq5"
50 changes: 50 additions & 0 deletions Platform/tests/Backtest.test.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2024, EA31337 Ltd |
//| https://ea31337.github.io |
//+------------------------------------------------------------------+

/*
* 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 Backtest class.
*/

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

// Test Backtest.
bool TestBacktest01() {
bool _result = true;
// @todo
return _result;
}

/**
* Implements OnInit().
*/
int OnInit() {
bool _result = true;
assertTrueOrFail(TestBacktest01(), "Fail!");
return _result && GetLastError() == 0 ? INIT_SUCCEEDED : INIT_FAILED;
}

/**
* Implements Tick event handler.
*/
void OnTick() {}
1 change: 1 addition & 0 deletions Trade.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum ENUM_TRADE_CONDITION {
// Defines enumeration for trade parameters.
enum ENUM_TRADE_PARAM {
TRADE_PARAM_BARS_MIN = 0, // Bars minimum
TRADE_PARAM_DUMMY, // Dummy trading // @todo: Move to TRADE_MODE
TRADE_PARAM_LOG_LEVEL, // Log level
TRADE_PARAM_LOT_SIZE, // Lot size
TRADE_PARAM_MAGIC_NO, // Magic number
Expand Down
11 changes: 11 additions & 0 deletions Trade.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ HistorySelect(0, TimeCurrent()); // Select history for access.
break;
}
Order *_order = new Order(_request, _oparams);
OnOrderOpen(_oparams);
_result = OrderAdd(_order);
if (_result) {
OnOrderOpen(PTR_TO_REF(_order));
Expand Down Expand Up @@ -1761,6 +1762,16 @@ HistorySelect(0, TimeCurrent()); // Select history for access.

/* Event methods */


/**
* Event on trade's order open.
*
* @param
* _order OrderParams instance of order struct to be open.
*/
virtual void OnOrderOpen(const OrderParams &_oparams) {
}

/**
* Event on trade's order open.
*
Expand Down
3 changes: 3 additions & 0 deletions Trade.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ struct TradeStats {

/* Structure for trade parameters. */
struct TradeParams {
bool dummy; // Whether trades should be opened as dummy (fake orders).
float lot_size; // Default lot size.
float max_spread; // Maximum spread to trade (in pips).
float risk_margin; // Maximum account margin to risk (in %).
Expand All @@ -141,6 +142,7 @@ struct TradeParams {
// Constructors.
TradeParams(float _lot_size = 0, float _risk_margin = 1.0f, unsigned int _slippage = 0, ENUM_LOG_LEVEL _ll = V_INFO)
: bars_min(100),
dummy(false),
order_comment(""),
log_level(_ll),
lot_size(_lot_size),
Expand All @@ -151,6 +153,7 @@ struct TradeParams {
}
TradeParams(uint64 _magic_no, ENUM_LOG_LEVEL _ll = V_INFO)
: bars_min(100),
dummy(false),
lot_size(0),
order_comment(""),
log_level(_ll),
Expand Down

0 comments on commit cc93786

Please sign in to comment.