Capturing data from automated orders

To add comments, please log in or register
Matthew Dumas
13
Matthew Dumas  

I am new to MQL5, and I'm just coming back to trading after a few year break. I figured this time I would like to automate my old strategy and have it send me prompts to set orders, and then send me the results of those orders. I've been able to program most of it already, but I keep stumbling over the titular issue. 

As example, please look at the following journal entries from MetaTrader5 when I ran my EA: 

2020.06.15 21:01:27   take profit triggered #3382 buy 2 GBPUSD 1.25638 sl: 1.25538 tp: 1.25731 [#3390 sell 2 GBPUSD at 1.25731]
2020.06.15 21:01:27   deal #3264 sell 2 GBPUSD at 1.25731 done (based on order #3390)
2020.06.15 21:01:27   deal performed [#3264 sell 2 GBPUSD at 1.25731]
2020.06.15 21:01:27   order performed sell 2 at 1.25731 [#3390 sell 2 GBPUSD at 1.25731]
2020.06.15 21:01:28   BUY TP Triggered
2020.06.15 21:01:28       Trans Deal:             3264
2020.06.15 21:01:28       Trans Deal Type:        DEAL_TYPE_SELL
2020.06.15 21:01:28       Trans Order:            3390
2020.06.15 21:01:28       Trans Order State:      ORDER_STATE_STARTED
2020.06.15 21:01:28       Trans Order type:       ORDER_TYPE_BUY
2020.06.15 21:01:28       Trans position:         3382
2020.06.15 21:01:28       Trans time Type:        ORDER_TIME_GTC
2020.06.15 21:01:28       Trans type :            TRADE_TRANSACTION_DEAL_ADD
2020.06.15 21:01:28       Order Ticket #:           3390
2020.06.15 21:01:28       Order Initial Volume:     0.0
2020.06.15 21:01:28       Order Current Volume:     0.0
2020.06.15 21:01:28       Order Open Price:         0.0
2020.06.15 21:01:28       Order Take Profit:        0.0
2020.06.15 21:01:28       Order Stop Loss:          0.0
2020.06.15 21:01:28       Order Price Current:      0.0
2020.06.15 21:01:28       Position Volume:             0.0
2020.06.15 21:01:28       Position Open Price:         0.0
2020.06.15 21:01:28       Position Take Profit:        0.0
2020.06.15 21:01:28       Position Stop Loss:          0.0
2020.06.15 21:01:28       Position Swap:               0.0
2020.06.15 21:01:28       Position Profit:             0.0

Everything seems alright here, but no matter what I do, I can't ever seem to capture the information contained in the first line. As you can see, the parameters of the Order and Position (as well as Request and Result -- not pictured) objects are 0. What I'm trying to explicitly capture is the following: 

  1. The original order number (not the order generated by the take profit, but rather the initial order that instated the take profit to begin with) -- so I can relate this new order back to the ticket number I've already saved. 
  2. (If #1 isn't available at this point) The original open price of the originating order. (in this case: 1.25638) 
  3. The actual sell price (presumably in this case: 1.25731), not the current quote price, in case there is slippage.
  4. Both the stop loss and take profit values set in the original order (in this case sl: 1.25538, tp: 1.25731).  I'd only need this if #1 isn't available. 

Basically, I'd like to capture that first journal line. It is automatically generated by MetaTrader, so I thought the information would be available in the OnTradeTransaction() event. However, I can't seem to gain access to that information. 

The relevant section of my OnTradeTransaction() code is: 

Print("BUY TP Triggered");
Print("    Trans Deal:             ", trans.deal);
Print("    Trans Deal Type:        ", EnumToString(trans.deal_type));
Print("    Trans Order:            ", trans.order);
Print("    Trans Order State:      ", EnumToString(trans.order_state));
Print("    Trans Order type:       ", EnumToString(trans.order_type));
Print("    Trans position:         ", trans.position);
Print("    Trans time Type:        ", EnumToString(trans.time_type));
Print("    Trans type :            ", EnumToString(trans.type));
OrderSelect(trans.order);
PositionSelect(trans.position); 

Print("    Order Ticket #:           ", trans.order);
Print("    Order Initial Volume:     ", OrderGetDouble(ORDER_VOLUME_INITIAL));
Print("    Order Current Volume:     ", OrderGetDouble(ORDER_VOLUME_CURRENT));
Print("    Order Open Price:         ", OrderGetDouble(ORDER_PRICE_OPEN));
Print("    Order Take Profit:        ", OrderGetDouble(ORDER_TP));
Print("    Order Stop Loss:          ", OrderGetDouble(ORDER_SL));
Print("    Order Price Current:      ", OrderGetDouble(ORDER_PRICE_CURRENT));

Print("    Position Volume:             ", PositionGetDouble(POSITION_VOLUME));
Print("    Position Open Price:         ", PositionGetDouble(POSITION_PRICE_OPEN));
Print("    Position Take Profit:        ", PositionGetDouble(POSITION_TP));
Print("    Position Stop Loss:          ", PositionGetDouble(POSITION_SL));
Print("    Position Swap:               ", PositionGetDouble(POSITION_SWAP));
Print("    Position Profit:             ", PositionGetDouble(POSITION_PROFIT));

The intent behind this is to have a live check on my trading strategy as orders execute. 

What am I missing here? I feel like this should be a simple and basic task.


Thanks for your help, in advance.

WindmillMQL
241
WindmillMQL  
Are you talking about the Strategy Tester (i.e. backtesting)? Or are you using a forward test?
Enrique Dangeroux
645
Enrique Dangeroux  

Backwards or forward and tester has nothing to do with it.

Read the manual and examine the example code provided here https://www.mql5.com/en/docs/constants/structures/mqltradetransaction

To add comments, please log in or register