How can I know when the stop loss is triggered

Created at 04 Aug 2024, 07:48
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!
EY

eynt

Joined 08.05.2020

How can I know when the stop loss is triggered
04 Aug 2024, 07:48


Hi

 

I'm openning a position and change its stop loss using the code below. How can I know when the stop loss is triggered?

 

TradeResult tradeResult1 = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);

TradeResult tradeResult2 = tradeResult1.Position.ModifyStopLossPips(100);

 

Thanks


@eynt
Replies

PanagiotisCharalampous
05 Aug 2024, 05:50

Hi there,

See an example below

using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class StopLoss : Robot
    {
         protected override void OnStart()
          {
              Positions.Closed += PositionsOnClosed;
          }
          
          private void PositionsOnClosed(PositionClosedEventArgs args)
          {
                if(args.Reason == PositionCloseReason.StopLoss)
                {
                    Print("Stop loss triggered for position " + args.Position.Id);
                }
          }
    }
}

Best regards,

Panagiotis


@PanagiotisCharalampous

eynt
06 Aug 2024, 07:24

RE: How can I know when the stop loss is triggered

Hi

 

It seems the Positions.Closed event is triggered somehow late, meaning AFTER OnTick. So when I run the OnTick the cBot thinks the position is still open (and the position still appears on the Positions property).

How can I know from the OnTick that the position is already closed?

 

Thanks


@eynt

PanagiotisCharalampous
06 Aug 2024, 08:22

RE: RE: How can I know when the stop loss is triggered

eynt said: 

Hi

 

It seems the Positions.Closed event is triggered somehow late, meaning AFTER OnTick. So when I run the OnTick the cBot thinks the position is still open (and the position still appears on the Positions property).

How can I know from the OnTick that the position is already closed?

 

Thanks

You can't and probably that will be the case in live environments too. It takes some time for the server to notify the client application that a position was closed.


@PanagiotisCharalampous

eynt
06 Aug 2024, 11:02

RE: RE: RE: How can I know when the stop loss is triggered

 

You can't and probably that will be the case in live environments too. It takes some time for the server to notify the client application that a position was closed.

 

Is there a way via code to “ask” the broker directly and not via cTrader properties/events?


@eynt

PanagiotisCharalampous
07 Aug 2024, 05:29

RE: RE: RE: RE: How can I know when the stop loss is triggered

eynt said: 

 

You can't and probably that will be the case in live environments too. It takes some time for the server to notify the client application that a position was closed.

 

Is there a way via code to “ask” the broker directly and not via cTrader properties/events?

I am not sure what do you mean…


@PanagiotisCharalampous

eynt
07 Aug 2024, 11:42

RE: RE: RE: RE: RE: How can I know when the stop loss is triggered

 

 

You can't and probably that will be the case in live environments too. It takes some time for the server to notify the client application that a position was closed.

 

Is there a way via code to “ask” the broker directly and not via cTrader properties/events?

I am not sure what do you mean…

As far as i know the only way to know about the positions states is using Positions which is a cTrader's object and is not necessarily updated.

Is there a way via code to communicate instead directly with the broker and get the updated positions status from there?

 

Thanks


@eynt

PanagiotisCharalampous
07 Aug 2024, 13:39

RE: RE: RE: RE: RE: RE: How can I know when the stop loss is triggered

eynt said: 

 

 

You can't and probably that will be the case in live environments too. It takes some time for the server to notify the client application that a position was closed.

 

Is there a way via code to “ask” the broker directly and not via cTrader properties/events?

I am not sure what do you mean…

As far as i know the only way to know about the positions states is using Positions which is a cTrader's object and is not necessarily updated.

Is there a way via code to communicate instead directly with the broker and get the updated positions status from there?

 

Thanks

Positions information for cTrader accounts is stored in cTrader platform. The phrase “directly with the broker” doesn't make much sense.


@PanagiotisCharalampous

eynt
11 Aug 2024, 14:18

RE: RE: RE: RE: RE: RE: RE: How can I know when the stop loss is triggered

It makes a lot of sense if cTrader cant provide accurate real time data.

Is there a way via code to tell cTrader at OnTick to check *now* from the broker the positions status instead of waiting for the Positions.Closed event to be triggered?

 

Thanks


@eynt

PanagiotisCharalampous
12 Aug 2024, 05:20

RE: RE: RE: RE: RE: RE: RE: RE: How can I know when the stop loss is triggered

eynt said: 

It makes a lot of sense if cTrader cant provide accurate real time data.

Is there a way via code to tell cTrader at OnTick to check *now* from the broker the positions status instead of waiting for the Positions.Closed event to be triggered?

 

Thanks

Broker is using the cTrader platfrom. The positions information is stored in cTrader Server. cTrader Desktop gets its information from cTrader Server. This information is available on Positions collection. There is no other source of positions. There is no way to know that a position has closed before the Positions.Closed event is triggered.


@PanagiotisCharalampous