What causes a position to close?

 

In my EA I open a position with both tp and sl.

When the position closes, I want to know if it was due to the tp or the sl.

What code can do this?

Thanks 

 
roamso:

In my EA I open a position with both tp and sl.

When the position closes, I want to know if it was due to the tp or the sl.

What code can do this?

Thanks 

Check the history and compare closing with your sl/tp.
 
angevoyageur:
Check the history and compare closing with your sl/tp.

Thanks for the reply angevoyageur

 So, using a long as an example, do you mean to check the history to see if close price was above tp, then tp was used and if close price was below sl, then sl was used?

I thought of this, but didn't think this logic was solid because the price can spike past the sl/tp threshold and go below again. I.e. the sl/tp could have been used but price changed by the time position closed, right?

But if this is the only way to check, then I don't have a choice.

Any other thoughts?

Thanks 

 
roamso:

Thanks for the reply angevoyageur

 So, using a long as an example, do you mean to check the history to see if close price was above tp, then tp was used and if close price was below sl, then sl was used?

I thought of this, but didn't think this logic was solid because the price can spike past the sl/tp threshold and go below again. I.e. the sl/tp could have been used but price changed by the time position closed, right?

But if this is the only way to check, then I don't have a choice.

Any other thoughts?

Thanks 

For a long, if the closeprice is >=tp, your trade was close by tp. If the closeprice is <=sl your trade was closed by sl. It's valid in all cases.
 
Thanks, appreciate the help
Reason: