How can i check if a stop loss was executed
How can i check if a stop loss was executed
17 Oct 2017, 04:32
Hi everyone and sorry for a newbie.
What I am trying to achieve is that if a specified open position(within multiple open positions) hit the stop loss then do a bunch of other stuff (set a pending order somewhere else, close some of the profit, etc)
is there a way to check precisely this?
It looks like an easy task but i have struggled for the past 2 weeks without solution
I hope someone can help me
Thank you
Replies
frapetim
18 Oct 2017, 12:52
Hi and thanks for your interest.
Basically, I need to the algo to react whenever a position is closed with a stop loss. As soon as this happens the algo must do a series of other action.
I have looked into
PositionClosedEventArgs
that seems to be the closest thing i could use but i haven't been able to figure out how to use it to scan if a stop loss has been hit.
I hope it makes sense to you.
Thank you
Fran
@frapetim
PanagiotisCharalampous
18 Oct 2017, 15:36
Hi frapetim,
Currently there is no easy way in cAlgo to know if a position was closed because a stop loss was triggered. You will need to write custom code yourself to track this. You can add a suggestion in the Suggestions section of the forum for the product team to consider.
Best Regards,
Panagiotis
@PanagiotisCharalampous
lakshanperera1313
18 Jul 2020, 13:12
Now there is a way with new cAlgo API updates
protected override void OnStart()
{
// Subscribe to the Trade Closing event
Positions.Closed += OnPositionsClosed;
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
// check if the position has been closed due to stoploss or takeprofit or any other(stop out etc)
if(args.Reason == PositionCloseReason.StopLoss)
{
Print("Position has been closed due to Stop Loss");
}
else if(args.Reason == PositionCloseReason.TakeProfit)
{
Print("Position has been closed due to Take Profit");
}
}
With the new cAlgo API, the reason for position closing can be checked as shown above.
Hope this helps someone who has the same question.
All the best!
@lakshanperera1313
PanagiotisCharalampous
17 Oct 2017, 09:49
Hi frapetim,
Thanks for posting in our forum. What exactly do you need to check? That the position was closed in general or that it was closed because SL was triggered?
Best Regards,
Panagiotis
@PanagiotisCharalampous