Detect stop trigger in OnTradeTransaction backtest

 

Hello,

i try to detect a stop execution in my EA. Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction.

This seems only to be the case in backtest.

Anyone know how to reliable detect a triggered stop in backtest - best along with price and (partial-) profit ?

Thank you 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties - Documentation on MQL5
 
chinaski:

Hello,

i try to detect a stop execution in my EA. Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction.

This seems only to be the case in backtest.

Anyone know how to reliable detect a triggered stop in backtest - best along with price and (partial-) profit ?

Thank you 

In OnTradeTransaction, you have to check for :

trans.order_state = ORDER_STATE_PLACED;

Followed by another transaction with :

trans.type = TRADE_TRANSACTION_DEAL_ADD;
 
angevoyageur:

In OnTradeTransaction, you have to check for :

Followed by another transaction with :

Hello Angevoyageur,

please have a look onto my dump. This dump prints ALL transactions into a file. My EA is very simple. It buys or sells along with stop and target. Unfortunately, i can't find any transactions in the log, that

are related to triggered stop or target:

 

 

20130507 00:26:59:672 entry short at 1.32176 <- entry short
20130507 00:26:59:672 symbol=EURUSD price=1.3217600 volume=0.01 sl=1.3229200 tp=1.3202000 order=2 deal=2 entry-type=IN trans-type=DEAL_ADD order_type=BUY deal_type=DEAL_TYPE_SELL type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:672 symbol=EURUSD price=1.3217600 volume=0.01 sl=1.3229200 tp=1.3202000 order=2 deal=0 entry-type=IN trans-type=ORDER_DELETE order_type=SELL deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:672 symbol=EURUSD price=1.3217600 volume=0.00 sl=1.3229200 tp=1.3202000 order=2 deal=0 entry-type=IN trans-type=HISTORY_ADD order_type=SELL deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:672 symbol= price=0.0000000 volume=0.00 sl=0.0000000 tp=0.0000000 order=0 deal=0 entry-type=IN trans-type=REQUEST order_type=BUY deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00

/// before the entry long, the stop or target of entry short should be triggerd.
/// The EA does not allow to enter new trade before latest is closed, simply by check current positions for symbol before open an entry tradde.

20130507 00:26:59:674 entry long at 1.32140 <- entry long
20130507 00:26:59:674 symbol=EURUSD price=1.3214000 volume=0.01 sl=1.3200900 tp=1.3231100 order=3 deal=3 entry-type=IN trans-type=DEAL_ADD order_type=BUY deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:674 symbol=EURUSD price=1.3214000 volume=0.01 sl=1.3200900 tp=1.3231100 order=3 deal=0 entry-type=IN trans-type=ORDER_DELETE order_type=BUY deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:674 symbol=EURUSD price=1.3214000 volume=0.00 sl=1.3200900 tp=1.3231100 order=3 deal=0 entry-type=IN trans-type=HISTORY_ADD order_type=BUY deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00
20130507 00:26:59:674 symbol= price=0.0000000 volume=0.00 sl=0.0000000 tp=0.0000000 order=0 deal=0 entry-type=IN trans-type=REQUEST order_type=BUY deal_type=DEAL_TYPE_BUY type_time=0 expiration=1970.01.01 00:00 price_trigger=0.0000000 profit=0.00

/// As you can see, my entry short shows transaction BUY, SELL, SELL what ever the purpose of this confusing transactions is.
/// No a bit about stop or target orders

So, how to get transactions of stop and target ? I Talk about backtest here. Not sure, if this is also the case under live conditions.

 
chinaski:

Hello Angevoyageur,

please have a look onto my dump. This dump prints ALL transactions into a file. My EA is very simple. It buys or sells along with stop and target. Unfortunately, i can't find any transactions in the log, that

are related to triggered stop or target:

 

 

So, how to get transactions of stop and target ? I Talk about backtest here. Not sure, if this is also the case under live conditions.

Ok, sorry I misunderstood your first question, I was thinking of a pending order (buy stop or sell stop) when you said "triggered stop".

Are you sure you need to monitor transactions ? If you only place one trade at a time, you can simply check your opened position with  PositionSelect(_Symbol).

 
angevoyageur:

Ok, sorry I misunderstood your first question, I was thinking of a pending order (buy stop or sell stop) when you said "triggered stop".

Are you sure you need to monitor transaction ? If you only place one trade at a time, you can simple check your opened position with  PositionSelect(_Symbol).

Hello,

in order to check if a position is open or not, i do this already.CPositionInfo.Select.

But how to get the executed stop or target price, once a stop or target got triggered ? Is there a smart method ?

 
chinaski:

Hello,

in order to check if a position is open or not, i do this already.CPositionInfo.Select.

But how to get the executed stop or target price, once a stop or target got triggered ? Is there a smart method ?

Why not check the deals history ?
 
angevoyageur:
Why not check the deals history ?

Can you give more details please. I need the executed stop or target price. Have no deal id or ticket of triggered target or stop (not passed as transaction).

The History provides DEAL_PRICE but i need a deal # or ticket. Which ticket or deal to take ? Thank you. 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties - Documentation on MQL5
 
chinaski:

Can you give more details please. I need the executed stop or target price. Have no deal id or ticket of triggered target or stop (not passed as transaction).

The History provides DEAL_PRICE but i need a deal # or ticket. Which ticket or deal to take ? Thank you. 

When a sl or tp is triggered, you have a deal added with the opposite side of your trade. For example, you have a opened position of BUY 0.25 lots. When your sl/tp is triggered you will have a deal of type SELL, you can use OnTradeTransaction to monitor that or simply check the history of deals once you know your position is closed.

See the code example here.

 
angevoyageur:

When a sl or tp is triggered, you have a deal added with the opposite side of your trade. For example, you have a opened position of BUY 0.25 lots. When your sl/tp is triggered you will have a deal of type SELL, you can use OnTradeTransaction to monitor that or simply check the history of deals once you know your position is closed.

See the code example here.

Hello angev,

this is my proble: obviously i can't use OnTradeTransaction. Please see my logs above. No stop or target order listed there - so NO deal related to a stop or target executed The dump is created for EACH transaction passed to OnTradeTransaction.

Again, i talk about backtest. 

 
chinaski:

Hello angev,

this is my proble: obviously i can't use OnTradeTransaction. Please see my logs above. No stop or target order listed there - so NO deal related to a stop or target executed The dump is created for EACH transaction passed to OnTradeTransaction.

Again, i talk about backtest. 

Only think I can do is to test your code if you provide it. I don't have such an issue.
 
angevoyageur:
Only think I can do is to test your code if you provide it. I don't have such an issue.

Hello angevoyageur,

find attached a simple EA which demonstrates the problem. The EA opens long along with stop and target then short, then long and so on.

A dump method is also contained. Just apply EUR.USD 1 minute 1 week and watch the transaction dumps in backtest log.

Thank you for help. 

Files:
tc_ea_trans.mq5  11 kb
Reason: