Skip to content

Commit

Permalink
Working MQL code after partial MQL/C++ refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Jul 23, 2021
1 parent 048708e commit c684e77
Show file tree
Hide file tree
Showing 28 changed files with 297 additions and 235 deletions.
11 changes: 11 additions & 0 deletions Array.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,14 @@ static int ArraySize(const ARRAY_REF(X, array)) {
}
}
;

template <typename X>
void ArrayPush(ARRAY_REF(X, array), X value) {
ArrayResize(ArraySize(array) + 1);
array[ArraySize(array) - 1] = value;
}
template <typename X>
void ArrayPushObject(ARRAY_REF(X, array), X& value) {
ArrayResize(array, Array::ArraySize(array) + 1);
array[Array::ArraySize(array) - 1] = value;
}
60 changes: 44 additions & 16 deletions Config.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ string ToJSON(const MqlParam& param, bool, int) {
switch (param.type) {
case TYPE_BOOL:
// boolean
return Serializer::ValueToString((bool)param.integer_value);
return SerializerConversions::ValueToString((bool)param.integer_value);
case TYPE_INT:
return Serializer::ValueToString((int)param.integer_value);
return SerializerConversions::ValueToString((int)param.integer_value);
break;
case TYPE_DOUBLE:
case TYPE_FLOAT:
return Serializer::ValueToString(param.double_value);
return SerializerConversions::ValueToString(param.double_value);
break;
case TYPE_CHAR:
case TYPE_STRING:
return Serializer::ValueToString(param.string_value, true);
return SerializerConversions::ValueToString(param.string_value, true);
break;
case TYPE_DATETIME:
#ifdef __MQL5__
return Serializer::ValueToString(TimeToString(param.integer_value), true);
return SerializerConversions::ValueToString(TimeToString(param.integer_value), true);
#else
return Serializer::ValueToString(TimeToStr(param.integer_value), true);
return SerializerConversions::ValueToString(TimeToStr(param.integer_value), true);
#endif
break;
}
Expand All @@ -81,6 +81,38 @@ struct ConfigEntry : public MqlParam {
string_value == _s.string_value;
}

ConfigEntry() { type = (ENUM_DATATYPE)-1; }
ConfigEntry(const ConfigEntry& _r) { THIS_REF = _r; }

ConfigEntry(long _value) {
type = ENUM_DATATYPE::TYPE_LONG;
integer_value = _value;
}
ConfigEntry(int _value) {
type = ENUM_DATATYPE::TYPE_INT;
integer_value = _value;
}
ConfigEntry(bool _value) {
type = ENUM_DATATYPE::TYPE_BOOL;
integer_value = _value ? 1 : 0;
}
ConfigEntry(float _value) {
type = ENUM_DATATYPE::TYPE_FLOAT;
double_value = (double)_value;
}
ConfigEntry(double _value) {
type = ENUM_DATATYPE::TYPE_DOUBLE;
double_value = _value;
}
ConfigEntry(string _value) {
type = ENUM_DATATYPE::TYPE_STRING;
string_value = _value;
}
ConfigEntry(datetime _value) {
type = ENUM_DATATYPE::TYPE_DATETIME;
integer_value = _value;
}

SerializerNodeType Serialize(Serializer& s) {
s.PassEnum(THIS_REF, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);

Expand Down Expand Up @@ -155,38 +187,34 @@ class Config : public DictStruct<string, ConfigEntry> {
Config(const Config& r) : DictStruct<string, ConfigEntry>(r) {}

bool Set(string key, bool value) {
ConfigEntry param = {TYPE_BOOL, 0, 0, ""};
ConfigEntry param = value;
param.integer_value = value;
return Set(key, param);
}

bool Set(string key, int value) {
ConfigEntry param = {TYPE_INT, 0, 0, ""};
ConfigEntry param = value;
param.integer_value = value;
return Set(key, param);
}

bool Set(string key, long value) {
ConfigEntry param = {TYPE_LONG, 0, 0, ""};
param.integer_value = value;
ConfigEntry param = value;
return Set(key, param);
}

bool Set(string key, double value) {
ConfigEntry param = {TYPE_DOUBLE, 0, 0, ""};
param.double_value = value;
ConfigEntry param = value;
return Set(key, param);
}

bool Set(string key, string value) {
ConfigEntry param = {TYPE_STRING, 0, 0, ""};
param.string_value = value;
ConfigEntry param = value;
return Set(key, param);
}

bool Set(string key, datetime value) {
ConfigEntry param = {TYPE_DATETIME, 0, 0, ""};
param.integer_value = value;
ConfigEntry param = value;
return Set(key, param);
}

Expand Down
6 changes: 6 additions & 0 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ double MqlParamToDouble(MqlParam &param) {
struct DataParamEntry : public MqlParam {
public:
DataParamEntry() { type = (ENUM_DATATYPE)WRONG_VALUE; }
DataParamEntry(ENUM_DATATYPE _type, long _integer_value, double _double_value, string _string_value) {
type = _type;
integer_value = _integer_value;
double_value = _double_value;
string_value = _string_value;
}
DataParamEntry(const DataParamEntry &_r) { ASSIGN_TO_THIS(MqlParam, _r); }
// Struct operators.
void operator=(const bool _value) {
Expand Down
12 changes: 6 additions & 6 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -710,16 +710,16 @@ class EA {
}
bool ExecuteAction(ENUM_EA_ACTION _action, long _arg1) {
ARRAY(DataParamEntry, _args);
_args[0].type = TYPE_INT;
_args[0].integer_value = _arg1;
DataParamEntry _param1 = _arg1;
ArrayPushObject(_args, _param1);
return EA::ExecuteAction(_action, _args);
}
bool ExecuteAction(ENUM_EA_ACTION _action, long _arg1, long _arg2) {
ARRAY(DataParamEntry, _args);
_args[0].type = TYPE_INT;
_args[0].integer_value = _arg1;
_args[1].type = TYPE_INT;
_args[1].integer_value = _arg2;
DataParamEntry _param1 = _arg1;
DataParamEntry _param2 = _arg2;
ArrayPushObject(_args, _param1);
ArrayPushObject(_args, _param2);
return EA::ExecuteAction(_action, _args);
}

Expand Down
3 changes: 2 additions & 1 deletion Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ class Indicator : public Chart {
}
bool ExecuteAction(ENUM_INDICATOR_ACTION _action, long _arg1) {
ARRAY(DataParamEntry, _args);
_args[0].type = TYPE_LONG;
DataParamEntry _param1 = _arg1;
ArrayPushObject(_args, _param1);
_args[0].integer_value = _arg1;
return ExecuteAction(_action, _args);
}
Expand Down
3 changes: 3 additions & 0 deletions Market.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* Includes Market's structs.
*/

// Includes.
#include "Std.h"

// Structure for trade time static methods.
struct MarketTimeForex : MqlDateTime {
// Market sessions for trading Forex.
Expand Down
44 changes: 22 additions & 22 deletions Order.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1125,31 +1125,31 @@ class Order : public SymbolInfo {
if (Order::OrderModify(_request.position, _request.price, _request.sl, _request.tp, _request.expiration,
_color)) {
// @see: https://www.mql5.com/en/docs/constants/structures/mqltraderesult
_result.ask = SymbolInfo::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfo::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.order = _request.position; // Order ticket.
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
_result.ask = SymbolInfoStatic::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfoStatic::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.order = _request.position; // Order ticket.
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
_result.retcode = TRADE_RETCODE_DONE;
//_result.comment = TODO; // The broker comment to operation (by default it is filled by description of trade
// server return code).
}
} else if (_request.action == TRADE_ACTION_CLOSE_BY) {
if (Order::OrderCloseBy(_request.position, _request.position_by, _color)) {
// @see: https://www.mql5.com/en/docs/constants/structures/mqltraderesult
_result.ask = SymbolInfo::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfo::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.ask = SymbolInfoStatic::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfoStatic::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.retcode = TRADE_RETCODE_DONE;
}
} else if (_request.action == TRADE_ACTION_DEAL || _request.action == TRADE_ACTION_REMOVE) {
// @see: https://docs.mql4.com/trading/orderclose
if (Order::OrderClose(_request.position, _request.volume, _request.price, (int)_request.deviation, _color)) {
// @see: https://www.mql5.com/en/docs/constants/structures/mqltraderesult
_result.ask = SymbolInfo::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfo::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.order = _request.position; // Order ticket.
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
_result.ask = SymbolInfoStatic::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfoStatic::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.order = _request.position; // Order ticket.
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
_result.retcode = TRADE_RETCODE_DONE;
//_result.comment = TODO; // The broker comment to operation (by default it is filled by description of trade
// server return code).
Expand All @@ -1175,10 +1175,10 @@ class Order : public SymbolInfo {

if (_request.order > 0) {
// @see: https://www.mql5.com/en/docs/constants/structures/mqltraderesult
_result.ask = SymbolInfo::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfo::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
_result.ask = SymbolInfoStatic::GetAsk(_request.symbol); // The current market Bid price (requote price).
_result.bid = SymbolInfoStatic::GetBid(_request.symbol); // The current market Ask price (requote price).
_result.price = _request.price; // Deal price, confirmed by broker.
_result.volume = _request.volume; // Deal volume, confirmed by broker (@fixme?).
//_result.comment = TODO; // The broker comment to operation (by default it is filled by description of trade
// server return code).
}
Expand Down Expand Up @@ -2169,7 +2169,7 @@ class Order : public SymbolInfo {
_result = ::OrderTakeProfit();
break;
case ORDER_PRICE_CURRENT:
_result = SymbolInfo::GetBid(Order::OrderSymbol());
_result = SymbolInfoStatic::GetBid(Order::OrderSymbol());
break;
case ORDER_PRICE_STOPLIMIT:
SetUserError(ERR_INVALID_PARAMETER);
Expand Down Expand Up @@ -2608,13 +2608,13 @@ class Order : public SymbolInfo {
bool ProcessConditions() {
bool _result = true;
if (IsOpen() && ShouldCloseOrder()) {
ARRAY(DataParamEntry, _args);
DataParamEntry _cond;
_cond.type = TYPE_STRING;
_cond.string_value = "Close condition";
string _reason = "Close condition";
#ifdef __MQL__
_args[0].string_value += StringFormat(": %s", EnumToString(oparams.cond_close));
_reason += StringFormat(": %s", EnumToString(oparams.cond_close));
#endif
ARRAY(DataParamEntry, _args);
DataParamEntry _cond = _reason;
ArrayPushObject(_args, _cond);
_result &= Order::ExecuteAction(ORDER_ACTION_CLOSE, _args);
}
return _result;
Expand Down
13 changes: 8 additions & 5 deletions Order.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
#pragma once
#endif

#include "Serializer.mqh"
// Includes.
#include "Data.struct.h"
#include "Order.enum.h"
#include "Serializer.mqh"
#include "Terminal.mqh"

#ifndef __MQL5__
/**
Expand All @@ -56,10 +59,10 @@ struct MqlTradeCheckResult {
* The structure for order parameters.
*/
struct OrderParams {
bool dummy; // Whether order is dummy (fake) or not (real).
color color_arrow; // Color of the opening arrow on the chart.
unsigned short refresh_rate; // How often to refresh order values (in secs).
ENUM_ORDER_CONDITION cond_close; // Close condition.
bool dummy; // Whether order is dummy (fake) or not (real).
color color_arrow; // Color of the opening arrow on the chart.
unsigned short refresh_rate; // How often to refresh order values (in secs).
ENUM_ORDER_CONDITION cond_close; // Close condition.
ARRAY(DataParamEntry, cond_close_args); // Close condition argument.
// Special struct methods.
OrderParams() : dummy(false), color_arrow(clrNONE), refresh_rate(10), cond_close(ORDER_COND_NONE){};
Expand Down
11 changes: 6 additions & 5 deletions Orders.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ class Orders {
* @return
* Returns true on success.
*/
bool OrdersCloseAll(const string _symbol = NULL, const ENUM_POSITION_TYPE _type = (ENUM_POSITION_TYPE)-1, const int _magic = -1) {
bool OrdersCloseAll(const string _symbol = NULL, const ENUM_POSITION_TYPE _type = (ENUM_POSITION_TYPE)-1,
const int _magic = -1) {
#ifdef __MQL4__

//---
Expand All @@ -372,7 +373,7 @@ class Orders {
(_magic == -1 || OrderMagicNumber() == _magic)) {
string o_symbol = OrderSymbol();

uint _digits = SymbolInfo::GetDigits(o_symbol);
uint _digits = SymbolInfoStatic::GetDigits(o_symbol);
bool res_one = false;
int attempts = 10;
while (attempts > 0) {
Expand All @@ -388,14 +389,14 @@ class Orders {

double close_price = 0.0;
if (order_type == OP_BUY) {
close_price = SymbolInfo::GetBid(o_symbol);
close_price = SymbolInfoStatic::GetBid(o_symbol);
}
if (order_type == OP_SELL) {
close_price = SymbolInfo::GetAsk(o_symbol);
close_price = SymbolInfoStatic::GetAsk(o_symbol);
}

//---
uint slippage = SymbolInfo::GetSpread(o_symbol);
uint slippage = SymbolInfoStatic::GetSpread(o_symbol);

//---
if (OrderClose(OrderTicket(), OrderLots(), close_price, slippage)) {
Expand Down
1 change: 1 addition & 0 deletions Refs.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// Includes.
#include "Refs.struct.h"
#include "Std.h"

/**
* For explanation about difference between strong(Ref) and weak(WeakRef) references please look at:
Expand Down
Loading

0 comments on commit c684e77

Please sign in to comment.