Skip to content

Commit

Permalink
Order: Adds support for order close based on the loss or profit in pips
Browse files Browse the repository at this point in the history
  • Loading branch information
kenorb committed Aug 1, 2021
1 parent 1efade4 commit e19b969
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 17 deletions.
11 changes: 11 additions & 0 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ struct DataParamEntry : public MqlParam {
return _dpe;
}

/**
* Gets DataParamEntry struct based on the value of float type.
*
*/
static DataParamEntry FromValue(float _value) {
DataParamEntry _dpe;
_dpe.type = TYPE_FLOAT;
_dpe.double_value = _value;
return _dpe;
}

/**
* Gets DataParamEntry struct based on the value of integer type.
*
Expand Down
2 changes: 2 additions & 0 deletions Order.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ enum ENUM_ORDER_PROPERTY_CUSTOM {
ORDER_PROP_PRICE_CURRENT, // Current price.
ORDER_PROP_PRICE_OPEN, // Open price.
ORDER_PROP_PRICE_STOPLIMIT, // The limit order price for the StopLimit order.
ORDER_PROP_PROFIT, // Current profit in price difference.
ORDER_PROP_PROFIT_PIPS, // Current profit in pips.
ORDER_PROP_REASON_CLOSE, // Reason or source for closing an order.
ORDER_PROP_TICKET, // Ticket number.
ORDER_PROP_TIME_CLOSED, // Closed time.
Expand Down
13 changes: 5 additions & 8 deletions Order.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ class Order : public SymbolInfo {
* Sets an order property custom value.
*/
template <typename T>
void Set(ENUM_ORDER_PARAM _param, T _value) {
oparams.Set<T>(_param, _value);
void Set(ENUM_ORDER_PARAM _param, T _value, int _index1 = 0, int _index2 = 0) {
oparams.Set<T>(_param, _value, _index1, _index2);
}

/**
Expand Down Expand Up @@ -621,10 +621,6 @@ class Order : public SymbolInfo {
return _result;
#endif
}
double GetProfit() {
Update(ORDER_PRICE_CURRENT);
return odata.profit;
}

/**
* Returns stop loss value of the currently selected order.
Expand Down Expand Up @@ -2642,11 +2638,12 @@ class Order : public SymbolInfo {
* Returns true when the condition is met.
*/
bool CheckCondition(ENUM_ORDER_CONDITION _cond, ARRAY_REF(DataParamEntry, _args)) {
float _profit = (float)Get<long>(ORDER_PROP_PROFIT_PIPS);
switch (_cond) {
case ORDER_COND_IN_LOSS:
return GetProfit() < (ArraySize(_args) > 0 ? DataParamEntry::ToDouble(_args[0]) : 0);
return Get<long>(ORDER_PROP_PROFIT_PIPS) < (ArraySize(_args) > 0 ? -DataParamEntry::ToDouble(_args[0]) : 0);
case ORDER_COND_IN_PROFIT:
return GetProfit() > (ArraySize(_args) > 0 ? DataParamEntry::ToDouble(_args[0]) : 0);
return Get<long>(ORDER_PROP_PROFIT_PIPS) > (ArraySize(_args) > 0 ? DataParamEntry::ToDouble(_args[0]) : 0);
case ORDER_COND_IS_CLOSED:
return IsClosed();
case ORDER_COND_IS_OPEN:
Expand Down
13 changes: 9 additions & 4 deletions Order.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "Data.struct.h"
#include "Order.enum.h"
#include "Serializer.mqh"
#include "SymbolInfo.static.h"
#include "Terminal.mqh"

#ifndef __MQL5__
Expand Down Expand Up @@ -136,16 +137,16 @@ struct OrderParams {
SetConditionClose(_cond, _args, ArraySize(cond_close));
}
template <typename T>
void Set(ENUM_ORDER_PARAM _param, T _value, int _index = 0) {
void Set(ENUM_ORDER_PARAM _param, T _value, int _index1 = 0, int _index2 = 0) {
switch (_param) {
case ORDER_PARAM_COLOR_ARROW:
color_arrow = (color)_value;
return;
case ORDER_PARAM_COND_CLOSE:
SetConditionClose((ENUM_ORDER_CONDITION)_value, _index);
SetConditionClose((ENUM_ORDER_CONDITION)_value, _index1);
return;
case ORDER_PARAM_COND_CLOSE_ARG_VALUE:
cond_close[_index].SetConditionArg(_value);
cond_close[_index1].SetConditionArg(_value, _index2);
return;
case ORDER_PARAM_DUMMY:
dummy = _value;
Expand Down Expand Up @@ -252,6 +253,10 @@ struct OrderData {
return (T)price_open;
case ORDER_PROP_PRICE_STOPLIMIT:
return (T)price_stoplimit;
case ORDER_PROP_PROFIT:
return (T)profit;
case ORDER_PROP_PROFIT_PIPS:
return (T)(profit * pow(10, SymbolInfoStatic::GetDigits(symbol)));
case ORDER_PROP_REASON_CLOSE:
return (T)reason_close;
case ORDER_PROP_TICKET:
Expand Down Expand Up @@ -496,7 +501,7 @@ struct OrderData {
ResetLastError();
last_error = ERR_NO_ERROR;
}
void UpdateProfit() { profit = price_open - price_current; }
void UpdateProfit() { profit = price_current - price_open; }
// Serializers.
SerializerNodeType Serialize(Serializer &s) {
s.Pass(THIS_REF, "magic", magic);
Expand Down
6 changes: 4 additions & 2 deletions Orders.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ class Orders {
// @todo: Implement different pools.
Order *_selected = SelectFirstOpen();
for (uint _pos = ArraySize(orders); _pos >= 0; _pos--) {
if (orders[_pos].IsOrderOpen() && orders[_pos].GetProfit() > _selected.GetProfit()) {
if (orders[_pos].IsOrderOpen() &&
orders[_pos].Get<float>(ORDER_PROP_PROFIT) > _selected.Get<float>(ORDER_PROP_PROFIT)) {
_selected = orders[_pos];
}
}
Expand All @@ -177,7 +178,8 @@ class Orders {
// @todo: Implement different pools.
Order *_selected = SelectFirstOpen();
for (uint _pos = ArraySize(orders); _pos >= 0; _pos--) {
if (orders[_pos].IsOrderOpen() && orders[_pos].GetProfit() < _selected.GetProfit()) {
if (orders[_pos].IsOrderOpen() &&
orders[_pos].Get<float>(ORDER_PROP_PROFIT) < _selected.Get<float>(ORDER_PROP_PROFIT)) {
_selected = orders[_pos];
}
}
Expand Down
2 changes: 2 additions & 0 deletions Strategy.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ enum ENUM_STRATEGY_PARAM {
STRAT_PARAM_LSF, // Lot size factor
STRAT_PARAM_MAX_RISK, // Max risk
STRAT_PARAM_MAX_SPREAD, // Max spread
STRAT_PARAM_OCL, // Order close loss
STRAT_PARAM_OCP, // Order close profit
STRAT_PARAM_OCT, // Order close time
STRAT_PARAM_PPL, // Signal profit level
STRAT_PARAM_PPM, // Signal profit method
Expand Down
18 changes: 16 additions & 2 deletions Strategy.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,27 @@ class Strategy : public Object {
// logger.Info(_order.ToString(), (string)_order.GetTicket()); // @fixme: memory leaks.
ResetLastError();
}
int _index = 0;
if (sparams.order_close_time != 0) {
long _close_time_arg = sparams.order_close_time > 0
? sparams.order_close_time * 60
: (int)round(-sparams.order_close_time *
ChartTf::TfToSeconds(trade.Get<ENUM_TIMEFRAMES>(CHART_PARAM_TF)));
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_LIFETIME_GT_ARG);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _close_time_arg);
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_LIFETIME_GT_ARG, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _close_time_arg, _index);
_index++;
}
if (sparams.order_close_loss != 0.0f) {
float _loss_limit = sparams.order_close_loss;
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_IN_LOSS, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _loss_limit, _index);
_index++;
}
if (sparams.order_close_profit != 0.0f) {
float _profit_limit = sparams.order_close_profit;
_order.Set(ORDER_PARAM_COND_CLOSE, ORDER_COND_IN_PROFIT, _index);
_order.Set(ORDER_PARAM_COND_CLOSE_ARG_VALUE, _profit_limit, _index);
_index++;
}
}

Expand Down
18 changes: 17 additions & 1 deletion Strategy.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ struct StgParams {
bool is_boosted; // State of the boost feature (to increase lot size).
long id; // Identification number of the strategy.
float weight; // Weight of the strategy.
long order_close_time; // Order close time in mins (>0) or bars (<0)
long order_close_time; // Order close time in mins (>0) or bars (<0).
float order_close_loss; // Order close loss (in pips).
float order_close_profit; // Order close profit (in pips).
int signal_open_method; // Signal open method.
float signal_open_level; // Signal open level.
int signal_open_filter; // Signal open filter method.
Expand Down Expand Up @@ -78,6 +80,8 @@ struct StgParams {
is_suspended(false),
is_boosted(true),
order_close_time(0),
order_close_loss(0.0f),
order_close_profit(0.0f),
weight(0),
signal_open_method(0),
signal_open_level(0),
Expand Down Expand Up @@ -151,6 +155,10 @@ struct StgParams {
return (T)price_profit_level;
case STRAT_PARAM_PSL:
return (T)price_stop_level;
case STRAT_PARAM_OCL:
return (T)order_close_loss;
case STRAT_PARAM_OCP:
return (T)order_close_profit;
case STRAT_PARAM_OCT:
return (T)order_close_time;
case STRAT_PARAM_SOM:
Expand Down Expand Up @@ -218,6 +226,12 @@ struct StgParams {
case STRAT_PARAM_PSL: // Price stop level
price_stop_level = (float)_value;
return;
case STRAT_PARAM_OCL: // Order close loss
order_close_loss = (float)_value;
return;
case STRAT_PARAM_OCP: // Order close profit
order_close_profit = (float)_value;
return;
case STRAT_PARAM_OCT: // Order close time
order_close_time = (long)_value;
return;
Expand Down Expand Up @@ -304,6 +318,8 @@ struct StgParams {
s.Pass(THIS_REF, "is_boosted", is_boosted);
s.Pass(THIS_REF, "id", id);
s.Pass(THIS_REF, "weight", weight);
s.Pass(THIS_REF, "ocl", order_close_loss);
s.Pass(THIS_REF, "ocp", order_close_profit);
s.Pass(THIS_REF, "oct", order_close_time);
s.Pass(THIS_REF, "shift", shift);
s.Pass(THIS_REF, "som", signal_open_method);
Expand Down

0 comments on commit e19b969

Please sign in to comment.