How to determine the last trade closed by stoploss or not?

 

Hello all,


I need a help to correctly determine whether the last trade closed by it's stoploss or not.

Is the code below enough?


bool closebysl=false;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)
 {
    OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
    if(OrderClosePrice()==OrderStopLoss()) closebysl=true;
    break;
 }


But since there's a possibility that there was a slippage so the closed price may not exactly at the stoploss price, I'm not sure about the code above.

Any suggestion is appreciated.


Thank you

 

Devilian,

Not really see your problem - well, yes I do and I can only fall back on what I always understood:

Slippage concerned with EA calling OrderSend() or OrderClose() with associated calcs on each being done with consideration of slippage.

Also, that trade closure by SL is just that - SL. ie, slippage not factored in, SL is absolute value.

Unless of course gapping then asap price will be taken as close price. of course what is loaded into OrderStopLoss()... I have not a clue.

Perhaps close price gets the gapped price and SL stays as specified in OrderSend() - if so, would be seen that close price is typically eg, below SL so this would be the hint that SL is over ruled.

I still think, forgetting about gapping stuff, that SL is SL is... and slippage is not part of the calcs. Buuut... :-)

Can you speak to broker?

I'd be very interested in your outcome Devilian, please post if you reach conclusion.

Best

 

You Can Try this also:

Inspect the OrderComment() and look for [sl] at the end of the string

if ProfitTarget was hit, the string will contain [tp] and will also be at the end of the strng

bool closebysl=false;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(StringFind(OrderComment(),"[sl]")!=-1) closebysl=true;
break;
}

FYI:

This code snippet does not determine if the last trade closed was closed by SL

It tell that a trade was closed by SLbut that trade is not necessarily the last trade

 

wadem000, the comment field is often talked about in matters of information relay.

Is their a cast iron Broker warrantee that this text will accurately relay order status and always be present?

What happens when partial take profit via OrderClose(..,partLots,..)? As new ticket is generated - I know that comment field is here talked about too, but...

For sure, magic# is absolute warrantee of ticket identity.

Regards

 

> As new ticket is generated - I know that comment field is here talked about too, but

IIRC - The MagicNumber carries across, the (original) Comment does not

This is really tricky as Devilian says - slippage could be quite large

Some brokers would add 'gap' to the comment of the market had jumped, others not

You certainly couldnt rely on Price()==OrderStopLoss()) to be absolute, but would it matter to the strategy if a near match was found?

FWIW

-BB-

 

this?

if a buy is stopped via gap, the close will be below the sl and the diff(sl-close) will be the gap

if a sell is stopped via gap, the close will be above the sl and the diff(close-sl) will be the gap

iow, if gap>0 then is a gapped out stopped out (spaced out dead order.... i got headache...

 

devilian...

Please kindly inform as to your solution - the collective 'we' would like to know

:O)

 

Hello All,


Thank you for your replies and attentions.

It's appear to be a problem for some of us, perhaps metaquotes need to add a fuction to determine it in the future.

For now, Phy's suggestion is likely the best we have now, it's like Wadem000's but covering a bit more since the comment is not always "[sl]".

It's here : https://forum.mql4.com/14865


Anymore idea perhaps?


Thank you

 

devilian - good to hear current status from you ;)

I remember that thread. Seems to me that like other platform areas, this is another that is subjective.

Comments generally may be reliable for a particular Broker and IF never use others then perhaps ok to trust this parsing of Comment().

.

My feeling is that ok... order is closed and there is a close price. Sometimes this close price could be tp or sl or 'something' which maybe was influenced by slippage or simply gapping AND/OR FreezeLevel influences or both.

So what? at least it is closed and contained and surely it must be general case that it was a tp or sl event. The NON-general case... well, is it worth attempting the [apparently] unfeasible fix?

Either it can be dealt with 100% or why bother with part of it with a part fix - just log() it and forget it...!

.

It is still not correct imho. This issue is just reflecting one of many design drop-offs in the product. A case of 'live with it' and find any work around which takes your fancy and you can live with :)

Is software = has design limits = step outside these limits = bugs/inconsistencies.

ALL software exhibits such behavior if pushed beyond design limits.

.

Some exhibit such behavior without being pushed...

Just look at Windoz!

LOL

 

Well.. I'm just thinking that I can live with Phy's suggetion, but still expecting some additional function added by the metaquotes if it's possible.


Thank you

 

Hey this is an old discussion but I found it really useful and wrote the section of code that will help others looking for something similar

Firstly Wadem000 's code section was correct, here is my version below which will determine if the previous order on the currency pair was closed by a Stop Loss or not, it won't find the last order closed on a Stop Loss:


for (int i=OrdersHistoryTotal()-1; i>=0; i--) //Cycles through the orders starting from the most recent one

{

OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);

if (OrderSymbol()==Symbol()) //Determines if this order was on this currency pair

{

i=-1; //Kills the for loop

if (StringFind(OrderComment(),"[sl] Long")!=-1) closebySL=true; //This comment string will vary from broker to broker, make sure you know what it is for yours

break;

}

}

This way you avoid trying to match up closing prices with Stop Loss levels, and do not have to worry about slip, just find out what your broker puts in the comment line.

Cheers, M