Skip to content

Commit

Permalink
Merge pull request #621 from EA31337/dev-exchange
Browse files Browse the repository at this point in the history
Initial Exchange class
  • Loading branch information
kenorb authored Jan 15, 2022
2 parents 66d435a + a94b5c4 commit 35af70c
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 2 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/test-exchange.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: Test Exchange

# yamllint disable-line rule:truthy
on:
pull_request:
paths:
- 'Exchange/**.h'
- 'Exchange/**.mq?'
- '.github/workflows/test-exchange.yml'
push:
paths:
- 'Exchange/**.h'
- 'Exchange/**.mq?'
- '.github/workflows/test-exchange.yml'

jobs:

Compile:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Compile
uses: fx31337/mql-compile-action@master
with:
init-platform: true
path: 'Exchange/tests'
verbose: true
- name: Print compiled files
run: '(Get-ChildItem -Recurse -Path . -Include *.ex[45]).fullname'
shell: powershell
- name: Upload artifacts (MQL4)
uses: actions/upload-artifact@v2
with:
name: files-ex4
path: '**/*.ex4'
- name: Upload artifacts (MQL5)
uses: actions/upload-artifact@v2
with:
name: files-ex5
path: '**/*.ex5'

Exchange-Tests-MQL4:
defaults:
run:
shell: bash
working-directory: Exchange/tests
needs: Compile
runs-on: ubuntu-latest
strategy:
matrix:
test:
- Exchange.test
steps:
- uses: actions/download-artifact@v2
with:
name: files-ex4
- name: Run ${{ matrix.test }}
uses: fx31337/mql-tester-action@master
with:
Script: ${{ matrix.test }}
timeout-minutes: 10
5 changes: 5 additions & 0 deletions Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class Account {
*/
Account() : init_balance(CalcInitDeposit()), start_balance(GetBalance()), start_credit(GetCredit()) {}

/**
* Class copy constructor.
*/
Account(const Account &_account) {}

/**
* Class deconstructor.
*/
Expand Down
83 changes: 83 additions & 0 deletions Exchange/Exchange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

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

/**
* Exchange class.
*/
#ifndef EXCHANGE_H
#define EXCHANGE_H

// Includes.
#include "../Account.mqh"
#include "../DictObject.mqh"
#include "../SymbolInfo.mqh"
#include "../Trade.mqh"
#include "Exchange.struct.h"

class Exchange {
protected:
DictObject<string, Account> accounts;
DictObject<string, SymbolInfo> symbols;
DictObject<string, Trade> trades;
ExchangeParams eparams;

public:
/**
* Class constructor without parameters.
*/
Exchange(){};

/**
* Class constructor with parameters.
*/
Exchange(ExchangeParams &_eparams) : eparams(_eparams){};

/**
* Class deconstructor.
*/
~Exchange() {}

/* Adders */

/**
* Adds account to the list.
*/
void AccountAdd(Account &_account, string _name) { accounts.Set(_name, _account); }

/**
* Adds symbol to the list.
*/
void SymbolAdd(SymbolInfo &_sinfo, string _name) { symbols.Set(_name, _sinfo); }

/* Removers */

/**
* Removes account from the list.
*/
void AccountRemove(string _name) { accounts.Unset(_name); }

/**
* Removes symbol from the list.
*/
void SymbolRemove(string _name) { symbols.Unset(_name); }
};
#endif // EXCHANGE_H
42 changes: 42 additions & 0 deletions Exchange/Exchange.struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* 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
* Includes Exchange's structs.
*/

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

// Forward class declaration.
class Exchange;

/* Defines struct for Exchange parameters. */
struct ExchangeParams {
// Constructors.
ExchangeParams() {}
ExchangeParams(const ExchangeParams &_eparams) {}
long id;
};
28 changes: 28 additions & 0 deletions Exchange/tests/Exchange.test.mq4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

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

// Includes.
#include "Exchange.test.mq5"
43 changes: 43 additions & 0 deletions Exchange/tests/Exchange.test.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

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

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

// Test classes.
class ExchangeDummy : public Exchange {};

// Global variables.
ExchangeDummy ex_dummy;

/**
* Implements OnInit().
*/
int OnInit() {
bool _result = true;
return _result && GetLastError() == ERR_NO_ERROR ? INIT_SUCCEEDED : INIT_FAILED;
}
12 changes: 11 additions & 1 deletion SymbolInfo.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SymbolInfo : public Object {

public:
/**
* Implements class constructor with a parameter.
* Class constructor given a symbol string.
*/
SymbolInfo(string _symbol = NULL) : symbol(_symbol), pip_size(GetPipSize()), symbol_digits(GetDigits()) {
Select();
Expand All @@ -73,6 +73,16 @@ class SymbolInfo : public Object {
}
}

/**
* Class constructor with symbol properties.
*/
SymbolInfo(const SymbolInfoProp &_sip) : sprops(_sip) {}

/**
* Class copy constructor.
*/
SymbolInfo(const SymbolInfo &_si) : s_entry(_si.s_entry), sprops(_si.sprops) {}

~SymbolInfo() {}

/**
Expand Down
7 changes: 6 additions & 1 deletion SymbolInfo.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct SymbolInfoEntry
double last; // Price of the last deal.
double spread; // Current spread.
unsigned long volume; // Volume for the current last price.
// Constructor.
// Constructors.
SymbolInfoEntry() : bid(0), ask(0), last(0), spread(0), volume(0) {}
SymbolInfoEntry(const MqlTick& _tick, const string _symbol = "") {
bid = _tick.bid;
Expand All @@ -55,6 +55,8 @@ struct SymbolInfoEntry
volume = _tick.volume;
spread = (unsigned int)round((ask - bid) * pow(10, SymbolInfoStatic::SymbolInfoInteger(_symbol, SYMBOL_DIGITS)));
}
// Copy constructor.
SymbolInfoEntry(const SymbolInfoEntry& _sie) { this = _sie; }
// Getters
string ToCSV() { return StringFormat("%g,%g,%g,%g,%d", bid, ask, last, spread, volume); }
// Serializers.
Expand All @@ -72,6 +74,9 @@ struct SymbolInfoProp {
unsigned int pip_digits; // Pip digits (precision).
unsigned int pts_per_pip; // Points per pip.
unsigned int vol_digits; // Volume digits.
// Constructors.
SymbolInfoProp() {}
SymbolInfoProp(const SymbolInfoProp& _sip) {}
// Serializers.
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {}
SerializerNodeType Serialize(Serializer& _s);
Expand Down

0 comments on commit 35af70c

Please sign in to comment.