Position hits Stop Loss

Created at 07 Feb 2018, 14:57
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
alexander.n.fedorov's avatar

alexander.n.fedorov

Joined 02.01.2018

Position hits Stop Loss
07 Feb 2018, 14:57


Dear Panagiotis!

I could not find if was possible to find programmatically that a position is closed by hitting a Stop Loss?

If yes, what would be the code?

also : is it possible to find out if position was hitting a stop (or a take profit), or closed manually?

 


@alexander.n.fedorov
Replies

PanagiotisCharalampous
07 Feb 2018, 15:16

Hi Alexander,

Currently there is no way to know how a position was closed. You could post a suggestion in the Suggestions section.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jiri
07 Feb 2018, 17:09

Hi,

How about this? You might need to add some tolerance due to slippage though.

private void Positions_Closed(PositionClosedEventArgs e)
{
    var takeProfit = e.Position.TakeProfit;
    var stopLoss = e.Position.StopLoss;
    var closingPrice = History.First(x => x.PositionId == e.Position.Id).ClosingPrice;

    if (e.Position.TradeType == TradeType.Buy ? closingPrice >= takeProfit : closingPrice <= takeProfit)
    {
        Print("Position {0} closed by TP", e.Position.Id);
    }
    else if (position.TradeType == TradeType.Buy ? closingPrice <= stopLoss : closingPrice >= stopLoss)
    {
        Print("Position {0} closed by SL", position.Id);
    }
    else
    {
        Print("Position {0} closed manually", position.Id);
    }
}

 


@Jiri

Jiri
07 Feb 2018, 17:12

Sorry, there was a typo in the code.

private void Positions_Closed(PositionClosedEventArgs e)
{
    var takeProfit = e.Position.TakeProfit;
    var stopLoss = e.Position.StopLoss;
    var closingPrice = History.First(x => x.PositionId == e.Position.Id).ClosingPrice;
 
    if (e.Position.TradeType == TradeType.Buy ? closingPrice >= takeProfit : closingPrice <= takeProfit)
    {
        Print("Position {0} closed by TP", e.Position.Id);
    }
    else if (e.Position.TradeType == TradeType.Buy ? closingPrice <= stopLoss : closingPrice >= stopLoss)
    {
        Print("Position {0} closed by SL", e.Position.Id);
    }
    else
    {
        Print("Position {0} closed manually", e.Position.Id);
    }
}

 


@Jiri

PanagiotisCharalampous
07 Feb 2018, 17:19

Hi tmc,

Your proposal is a nice workaround and could work for most of the cases. However, since SL is a market order, the closing price could be both above and below the price. Therefore, the solution would not be 100% accurate (e.g. in case of positive slippage).

Best Regards,

Panagiotis 


@PanagiotisCharalampous

alexander.n.fedorov
07 Feb 2018, 21:05

Stop Loss

Yes, I also agree with Panagiotis, there must be a direct event, which actually is somewhere, cause every time the order is filled with SL or TP I got a mail, saying that my position was close due to .... (SL or TP) was filled. I will try to make them suggestion, but will they listen?

 

 


@alexander.n.fedorov

alexander.n.fedorov
07 Feb 2018, 21:10

In my bot the position is closed when it meets a cirtain criteria

In that case I do

....

Private bool ClosedByRobot=false

 

...//when the criteria is met then

ClosedByRobot=true

But for the SL there is no way I could do that, even to on the server they know it

 


@alexander.n.fedorov

alexander.n.fedorov
07 Feb 2018, 21:11

sorry for misprints

:)


@alexander.n.fedorov