MetaEditor build 1490 - 页 2

 
Andrey Dik:

我如何知道在OnTradeTransaction()中已经触发了SL/TP?

为了理解OnTrade*中的信息逻辑,我在感兴趣的事件发生的时刻对输入参数进行了打印。

做到这一点,一切都会一下子变得清晰。

请记住,MT5的TP总是(与MT4相反)是一个市场订单,它挂在MT5服务器上,而不是挂在交易所/ECN上。因此,当它被触发时,会有延迟+任何标志的市场滑坡。在一些MT4的TP是直接在ECN上下的限价单。因此,那里没有延迟(MT-ECN),也没有负滑移,但有一个重定向。因此,TP是MT5比MT4强大的缺点之一。

 
fxsaber:

为了理解OnTrade*中的信息逻辑,我在感兴趣的事件发生的那一刻,对输入参数进行了打印。

做到这一点,一切都会一下子变得清晰。

试过了。在这种情况下,OnTrade()没有用处,甚至没有好处,因为我们必须为每个交易事件处理历史。

在OnTradeTransaction()中找出SL/TP的触发是有意义的,因为你可以通过事件类型 进行过滤。在这种情况下,我把事件类型定义为 "添加到历史",然后把交易类型定义为 "退出",然后是一个僵局。

 
Andrey Dik:

试过了。在这种情况下,OnTrade()是没有用的,甚至是有害的,因为它将不得不在每个交易事件中处理历史。

我指的是OnTrade*。
 
fxsaber:
这是关于OnTrade*。
是的,我知道。试过了。我还没能找到任何东西(从我需要的东西来看)。
 
#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

bool FirstRun = true;

void OnTick()
{  
  if (FirstRun)    
  {
    const double Price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    
    FirstRun = (OrderSend(_Symbol, OP_BUY, 1, Price, 0, 0, Price + 10 * _Point) <= 0);
  }
}

void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest &Request, const MqlTradeResult &Result )
{
  if (!FirstRun)
    Print(ToString(Trans) + ToString(Request) + ToString(Result));
}

#define TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define TOSTRING2(A) #A + " = " + EnumToString(A) + "\n"

string ToString( const MqlTradeTransaction &Trans )
{
  return(TOSTRING(Trans.deal) + TOSTRING(Trans.order) + TOSTRING(Trans.symbol) +
         TOSTRING2(Trans.type) + TOSTRING2(Trans.order_type) + TOSTRING2(Trans.order_state) +
         TOSTRING2(Trans.deal_type) + TOSTRING2(Trans.time_type) +
         TOSTRING(Trans.time_expiration) + TOSTRING(Trans.price) + TOSTRING(Trans.price_trigger) +
         TOSTRING(Trans.price_sl) + TOSTRING(Trans.price_tp) + TOSTRING(Trans.volume) +
         TOSTRING(Trans.position) + TOSTRING(Trans.position_by));
}

string ToString( const MqlTradeRequest &Request )
{
  return(TOSTRING2(Request.action) + TOSTRING(Request.magic) + TOSTRING(Request.order) +
         TOSTRING(Request.symbol) + TOSTRING(Request.volume) + TOSTRING(Request.price) +
         TOSTRING(Request.stoplimit) + TOSTRING(Request.sl) +  TOSTRING(Request.tp) +
         TOSTRING(Request.deviation) + TOSTRING2(Request.type) + TOSTRING2(Request.type_filling) +
         TOSTRING2(Request.type_time) + TOSTRING(Request.expiration) + TOSTRING(Request.comment) +
         TOSTRING(Request.position) + TOSTRING(Request.position_by));
}

string ToString( const MqlTradeResult &Result )
{
  return(TOSTRING(Result.retcode) + TOSTRING(Result.deal) + TOSTRING(Result.order) +
         TOSTRING(Result.volume) + TOSTRING(Result.price) + TOSTRING(Result.bid) +  
         TOSTRING(Result.ask) + TOSTRING(Result.comment) + TOSTRING(Result.request_id) +  
         TOSTRING(Result.retcode_external));
}
测试结果
2016.11.23 23:59:57   take profit triggered #2 buy 1.00 EURUSD 1.06235 tp: 1.06245 [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   deal #3 sell 1.00 EURUSD at 1.06245 done (based on order #3)
2016.11.23 23:59:57   deal performed [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   order performed sell 1.00 at 1.06245 [#3 sell 1.00 EURUSD at 1.06245]
2016.11.23 23:59:57   Trans.deal = 3
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_DEAL_ADD
2016.11.23 23:59:57   Trans.order_type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_STARTED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_SELL
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.time_expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.volume = 1.0
2016.11.23 23:59:57   Trans.position = 2
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.order = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.tp = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.type_time = ORDER_TIME_GTC
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.deal = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.bid = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57  
2016.11.23 23:59:57   Trans.deal = 0
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.symbol = EURUSD
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_ORDER_DELETE
2016.11.23 23:59:57   Trans.order_type = ORDER_TYPE_SELL
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_FILLED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_BUY
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.price_tp = 0.0
2016.11.23 23:59:57   Trans.volume = 1.0
2016.11.23 23:59:57   Trans.position = 2
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.stoplimit = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.tp = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Request.position_by = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.deal = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57   Result.retcode_external = 0
2016.11.23 23:59:57  
2016.11.23 23:59:57   Trans.deal = 0
2016.11.23 23:59:57   Trans.order = 3
2016.11.23 23:59:57   Trans.symbol = EURUSD
2016.11.23 23:59:57   Trans.type = TRADE_TRANSACTION_HISTORY_ADD
2016.11.23 23:59:57   Trans.order_state = ORDER_STATE_FILLED
2016.11.23 23:59:57   Trans.deal_type = DEAL_TYPE_BUY
2016.11.23 23:59:57   Trans.time_type = ORDER_TIME_GTC
2016.11.23 23:59:57   Trans.time_expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Trans.price = 1.06245
2016.11.23 23:59:57   Trans.price_trigger = 0.0
2016.11.23 23:59:57   Trans.price_sl = 0.0
2016.11.23 23:59:57   Trans.price_tp = 0.0
2016.11.23 23:59:57   Trans.volume = 0.0
2016.11.23 23:59:57   Trans.position_by = 0
2016.11.23 23:59:57   Request.action = ENUM_TRADE_REQUEST_ACTIONS::0
2016.11.23 23:59:57   Request.magic = 0
2016.11.23 23:59:57   Request.order = 0
2016.11.23 23:59:57   Request.symbol =
2016.11.23 23:59:57   Request.volume = 0.0
2016.11.23 23:59:57   Request.price = 0.0
2016.11.23 23:59:57   Request.stoplimit = 0.0
2016.11.23 23:59:57   Request.sl = 0.0
2016.11.23 23:59:57   Request.deviation = 0
2016.11.23 23:59:57   Request.type = ORDER_TYPE_BUY
2016.11.23 23:59:57   Request.type_filling = ORDER_FILLING_FOK
2016.11.23 23:59:57   Request.type_time = ORDER_TIME_GTC
2016.11.23 23:59:57   Request.expiration = 1970.01.01 00:00:00
2016.11.23 23:59:57   Request.comment =
2016.11.23 23:59:57   Request.position = 0
2016.11.23 23:59:57   Request.position_by = 0
2016.11.23 23:59:57   Result.retcode = 0
2016.11.23 23:59:57   Result.order = 0
2016.11.23 23:59:57   Result.volume = 0.0
2016.11.23 23:59:57   Result.price = 0.0
2016.11.23 23:59:57   Result.bid = 0.0
2016.11.23 23:59:57   Result.ask = 0.0
2016.11.23 23:59:57   Result.comment =
2016.11.23 23:59:57   Result.request_id = 0
2016.11.23 23:59:57   Result.retcode_external = 0
 

fxsaber:

测试者的结果

是的,但然后呢?

哪里显示触发了SL/TP?- 这就是困难所在))。

 

平仓 后,你没有办法通过MQL5知道其止盈和止损值。

这就是问题所在。但是MT5以某种方式收到了这些信息,从日志和平仓的画像来看。
 
fxsaber:
这就是问题所在。但MT5不知为何会得到这些信息,从日志和平仓的画像来看。

它不再像以前那样工作了吗?

关于交易、自动交易系统和策略测试的论坛

我如何知道是否触发了获利或止损?

Stanislav Korotky, 2014.10.16 12:06

我通过仓位(订单)的SL/TP检查,收盘价 优于或等于TP,差于或等于SL。

 
Stanislav Korotky:

这是不是已经不起作用了?

我可以拥有这个代码吗?

...是的是的,我可以抱着马什卡的大腿吗,对不起...请给我看看代码。

 
Stanislav Korotky:

它不再像以前那样工作了吗?

没有办法定义它。SL和TP只是一个MT服务器实体。