Good Introduction to Object Oriented Programming in MQL5.
With MT5 build 1347 the first code example fails to compile with error "illegal switch expression type".
I had to add casting to int in switch statements in AccountInfo.mqh to make it work, like:
switch((int)MarginMode())
Good Introduction to Object Oriented Programming in MQL5.
With MT5 build 1347 the first code example fails to compile with error "illegal switch expression type".
I had to add casting to int in switch statements in AccountInfo.mqh to make it work, like:
switch((int)MarginMode())
Hi, Thanks for this very helpful post and please help me resolve this. I am new to MT5 and am learning to create EAs so I copied sample code to execute Ctrade.Buy but the backtest failed. Here's more info:
1) Account: Its a live account with base currency as NZD
2) MetaEditor settings for backtest:
3) Code: Copied from https://www.mql5.com/en/articles/481:
//+------------------------------------------------------------------+
//| demo.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include<Trade\Trade.mqh>
//--- object for performing trade operations
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- set MagicNumber for your orders identification
int MagicNumber=123456;
trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
int deviation=10;
trade.SetDeviationInPoints(deviation);
//--- order execution mode
trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
trade.SetAsyncMode(true);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
BuySample1();
}
//--- Buy sample
//+------------------------------------------------------------------+
//| Buying a specified volume at the current symbol |
//+------------------------------------------------------------------+
void BuySample1()
{
//--- 1. example of buying at the current symbol
if(!trade.Buy(0.1))
{
//--- failure message
Print("Buy() method failed. Return code=",trade.ResultRetcode(),
". Code description: ",trade.ResultRetcodeDescription());
}
else
{
Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
" (",trade.ResultRetcodeDescription(),")");
}
//---
}
4) Error log (Please note that I am testing only on EUR/USD):
GJ 0 19:36:44.410 127.0.0.1 login (build 1730)
HH 0 19:36:44.420 Network 38520 bytes of account info loaded
JO 0 19:36:44.420 Network 1482 bytes of tester parameters loaded
QE 0 19:36:44.420 Network 188 bytes of input parameters loaded
FR 0 19:36:44.421 Network 443 bytes of symbols list loaded
IF 0 19:36:44.421 Tester expert file added: Experts\demo.ex5. 46684 bytes loaded
QH 0 19:36:44.433 Tester initial deposit 10000.00 NZD, leverage 1:100
JN 0 19:36:44.437 Tester successfully initialized
ES 0 19:36:44.437 Network 46 Kb of total initialization data received
PP 0 19:36:44.437 Tester Intel Core i7-4510U @ 2.00GHz, 8103 MB
RJ 0 19:36:44.799 Symbols EURUSD: symbol to be synchronized
HR 0 19:36:44.800 Symbols EURUSD: symbol synchronized, 3624 bytes of symbol info received
NJ 0 19:36:44.800 History EURUSD: history synchronization started
GO 0 19:36:44.856 History EURUSD: load 27 bytes of history data to synchronize in 0:00:00.000
RQ 0 19:36:44.856 History EURUSD: history synchronized from 2012.01.01 to 2017.11.15
EF 0 19:36:44.993 History EURUSD,Daily: history cache allocated for 1010 bars and contains 312 bars from 2014.01.01 00:00 to 2014.12.31 00:00
ND 0 19:36:44.993 History EURUSD,Daily: history begins from 2014.01.01 00:00
OL 0 19:36:44.996 Tester EURUSD,Daily (HalifaxPlus-Live): every tick generating
GN 0 19:36:44.996 Tester EURUSD,Daily: testing of Experts\demo.ex5 from 2015.01.01 00:00 to 2017.11.15 00:00 started
CK 0 19:36:56.288 Symbols NZDUSD: symbol to be synchronized
IS 0 19:36:56.288 Symbols NZDUSD: symbol synchronized, 3624 bytes of symbol info received
JL 0 19:36:56.288 History NZDUSD: history synchronization started
HJ 0 19:36:56.575 History NZDUSD: load 14 Kb of history data to synchronize in 0:00:00.078
LS 0 19:36:56.575 History NZDUSD: history synchronized from 2013.01.01 to 2017.11.15
CO 0 19:36:56.579 Symbols EURNZD: symbol to be synchronized
OJ 0 19:36:56.580 Symbols EURNZD: symbol synchronized, 3624 bytes of symbol info received
DL 0 19:36:56.580 History EURNZD: history synchronization started
MK 0 19:36:56.656 History EURNZD: load 27 bytes of history data to synchronize in 0:00:00.000
OD 0 19:36:56.656 History EURNZD: history synchronized from 2013.01.01 to 2017.11.15
IN 0 19:36:56.665 Trade 2015.01.02 03:00:00 market buy 0.10 EURUSD (1.20538 / 1.20549 / 1.20538)
PE 0 19:36:56.665 Trades 2015.01.02 03:00:00 deal #2 buy 0.10 EURUSD at 1.20549 done (based on order #2)
FH 0 19:36:56.666 Trade 2015.01.02 03:00:00 deal performed [#2 buy 0.10 EURUSD at 1.20549]
OG 0 19:36:56.666 Trade 2015.01.02 03:00:00 order performed buy 0.10 at 1.20549 [#2 buy 0.10 EURUSD at 1.20549]
FO 0 19:36:56.670 demo (EURUSD,D1) 2015.01.02 03:00:00 Buy() method executed successfully. Return code=10009 (done at 1.20549)
NM 2 19:37:15.823 History NZDUSD 2016.09.21 23:01:00: corrupted history detected (s:-73370, o:73433, h:+48, l:-123, c:-117 -- tv:63, rv:11250111)
JF 2 19:37:15.823 History NZDUSD 2016.09.21, bad container found, must be resynchronized
LQ 2 19:37:16.106 Tester history error 9 in undefined function
OH 2 19:37:16.106 Tester stopped on 0% of testing interval with error '20 NZDUSD'
Please tell me whats wrong and how do I resolve this?
Good, a nice one indeed..
Hi, Thanks for this very helpful post and please help me resolve this. I am new to MT5 and am learning to create EAs so I copied sample code to execute Ctrade.Buy but the backtest failed. Here's more info:
1) Account: Its a live account with base currency as NZD
2) MetaEditor settings for backtest:
3) Code: Copied from https://www.mql5.com/en/articles/481:
//+------------------------------------------------------------------+
//| demo.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include<Trade\Trade.mqh>
//--- object for performing trade operations
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- set MagicNumber for your orders identification
int MagicNumber=123456;
trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
int deviation=10;
trade.SetDeviationInPoints(deviation);
//--- order execution mode
trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
trade.SetAsyncMode(true);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
BuySample1();
}
//--- Buy sample
//+------------------------------------------------------------------+
//| Buying a specified volume at the current symbol |
//+------------------------------------------------------------------+
void BuySample1()
{
//--- 1. example of buying at the current symbol
if(!trade.Buy(0.1))
{
//--- failure message
Print("Buy() method failed. Return code=",trade.ResultRetcode(),
". Code description: ",trade.ResultRetcodeDescription());
}
else
{
Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
" (",trade.ResultRetcodeDescription(),")");
}
//---
}
4) Error log (Please note that I am testing only on EUR/USD):
GJ 0 19:36:44.410 127.0.0.1 login (build 1730)
HH 0 19:36:44.420 Network 38520 bytes of account info loaded
JO 0 19:36:44.420 Network 1482 bytes of tester parameters loaded
QE 0 19:36:44.420 Network 188 bytes of input parameters loaded
FR 0 19:36:44.421 Network 443 bytes of symbols list loaded
IF 0 19:36:44.421 Tester expert file added: Experts\demo.ex5. 46684 bytes loaded
QH 0 19:36:44.433 Tester initial deposit 10000.00 NZD, leverage 1:100
JN 0 19:36:44.437 Tester successfully initialized
ES 0 19:36:44.437 Network 46 Kb of total initialization data received
PP 0 19:36:44.437 Tester Intel Core i7-4510U @ 2.00GHz, 8103 MB
RJ 0 19:36:44.799 Symbols EURUSD: symbol to be synchronized
HR 0 19:36:44.800 Symbols EURUSD: symbol synchronized, 3624 bytes of symbol info received
NJ 0 19:36:44.800 History EURUSD: history synchronization started
GO 0 19:36:44.856 History EURUSD: load 27 bytes of history data to synchronize in 0:00:00.000
RQ 0 19:36:44.856 History EURUSD: history synchronized from 2012.01.01 to 2017.11.15
EF 0 19:36:44.993 History EURUSD,Daily: history cache allocated for 1010 bars and contains 312 bars from 2014.01.01 00:00 to 2014.12.31 00:00
ND 0 19:36:44.993 History EURUSD,Daily: history begins from 2014.01.01 00:00
OL 0 19:36:44.996 Tester EURUSD,Daily (HalifaxPlus-Live): every tick generating
GN 0 19:36:44.996 Tester EURUSD,Daily: testing of Experts\demo.ex5 from 2015.01.01 00:00 to 2017.11.15 00:00 started
CK 0 19:36:56.288 Symbols NZDUSD: symbol to be synchronized
IS 0 19:36:56.288 Symbols NZDUSD: symbol synchronized, 3624 bytes of symbol info received
JL 0 19:36:56.288 History NZDUSD: history synchronization started
HJ 0 19:36:56.575 History NZDUSD: load 14 Kb of history data to synchronize in 0:00:00.078
LS 0 19:36:56.575 History NZDUSD: history synchronized from 2013.01.01 to 2017.11.15
CO 0 19:36:56.579 Symbols EURNZD: symbol to be synchronized
OJ 0 19:36:56.580 Symbols EURNZD: symbol synchronized, 3624 bytes of symbol info received
DL 0 19:36:56.580 History EURNZD: history synchronization started
MK 0 19:36:56.656 History EURNZD: load 27 bytes of history data to synchronize in 0:00:00.000
OD 0 19:36:56.656 History EURNZD: history synchronized from 2013.01.01 to 2017.11.15
IN 0 19:36:56.665 Trade 2015.01.02 03:00:00 market buy 0.10 EURUSD (1.20538 / 1.20549 / 1.20538)
PE 0 19:36:56.665 Trades 2015.01.02 03:00:00 deal #2 buy 0.10 EURUSD at 1.20549 done (based on order #2)
FH 0 19:36:56.666 Trade 2015.01.02 03:00:00 deal performed [#2 buy 0.10 EURUSD at 1.20549]
OG 0 19:36:56.666 Trade 2015.01.02 03:00:00 order performed buy 0.10 at 1.20549 [#2 buy 0.10 EURUSD at 1.20549]
FO 0 19:36:56.670 demo (EURUSD,D1) 2015.01.02 03:00:00 Buy() method executed successfully. Return code=10009 (done at 1.20549)
NM 2 19:37:15.823 History NZDUSD 2016.09.21 23:01:00: corrupted history detected (s:-73370, o:73433, h:+48, l:-123, c:-117 -- tv:63, rv:11250111)
JF 2 19:37:15.823 History NZDUSD 2016.09.21, bad container found, must be resynchronized
LQ 2 19:37:16.106 Tester history error 9 in undefined function
OH 2 19:37:16.106 Tester stopped on 0% of testing interval with error '20 NZDUSD'
Please tell me whats wrong and how do I resolve this?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article Trade Operations in MQL5 - It's Easy is published:
Almost all traders come to market to make money but some traders also enjoy the process itself. However, it is not only manual trading that can provide you with an exciting experience. Automated trading systems development can also be quite absorbing. Creating a trading robot can be as interesting as reading a good mystery novel.
When developing a trading algorithm, we have to deal with plenty of technical issues including the most important ones:
We need to answer the first question to choose the most suitable symbol. Our choice can be affected by many factors including the ability to automate our trading system for the market. The second question involves elaboration of the trading rules clearly indicating deals' direction, as well as entry and exit points. The third question seems to be relatively simple: how to buy and sell using some definite programming language?
In this article we will consider how to implement trade operations in algorithmic trading using MQL5 language.
Author: MetaQuotes