Check if position has already been opened and closed?

Created at 18 Sep 2024, 15:55
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!
RI

rick2010

Joined 08.08.2024

Check if position has already been opened and closed?
18 Sep 2024, 15:55


Anyone know how to check if an order/position has already been opened and closed? TIA

Something along the lines of 

Position position = ClosedPositions.Find("some order name");

@rick2010
Replies

PanagiotisCharalampous
19 Sep 2024, 06:11

Hi there,

You can track the closed positions using the Positions.Closed event. There you can add the closed positions in a collection as the one you posted above.

Best regards,

Panagiotis


@PanagiotisCharalampous

rick2010
19 Sep 2024, 14:14 ( Updated at: 20 Sep 2024, 05:20 )

RE: Check if position has already been opened and closed?

PanagiotisCharalampous said: 

Hi there,

You can track the closed positions using the Positions.Closed event. There you can add the closed positions in a collection as the one you posted above.

Best regards,

Panagiotis

Thanks… I don't care about the event, I need to be able to look up a history of closed/filled trades. For example before placing a trade for id XYZ123 I first need to make sure that trade id XYZ123 hasn't already been opened and closed… this is to prevent duplicate trades from occurring. And if I just add them to a collection I'll lose that collection of closed trades next time the cBot is restarted which will allow duplicate trades to occur. TIA

 


@rick2010

PanagiotisCharalampous
20 Sep 2024, 05:31

RE: RE: Check if position has already been opened and closed?

rick2010 said: 

PanagiotisCharalampous said: 

Hi there,

You can track the closed positions using the Positions.Closed event. There you can add the closed positions in a collection as the one you posted above.

Best regards,

Panagiotis

Thanks… I don't care about the event, I need to be able to look up a history of closed/filled trades. For example before placing a trade for id XYZ123 I first need to make sure that trade id XYZ123 hasn't already been opened and closed… this is to prevent duplicate trades from occurring. And if I just add them to a collection I'll lose that collection of closed trades next time the cBot is restarted which will allow duplicate trades to occur. TIA

 

There is no option to access closed positions. You can only access past deals through History. Nevertheless if you want the information to persist between cBot restarts, you can always save it in a file


@PanagiotisCharalampous

rick2010
20 Sep 2024, 13:23

RE: RE: RE: Check if position has already been opened and closed?

PanagiotisCharalampous said: 

There is no option to access closed positions. You can only access past deals through History. Nevertheless if you want the information to persist between cBot restarts, you can always save it in a file

Do you have a code example of getting a trade out of history? Can it be done using a name or id? TIA

 

 

 


@rick2010

PanagiotisCharalampous
20 Sep 2024, 13:32

RE: RE: RE: RE: Check if position has already been opened and closed?

rick2010 said: 

PanagiotisCharalampous said: 

There is no option to access closed positions. You can only access past deals through History. Nevertheless if you want the information to persist between cBot restarts, you can always save it in a file

Do you have a code example of getting a trade out of history? Can it be done using a name or id? TIA

 

 

 

Check the link below

https://help.ctrader.com/ctrader-algo/references/Trading/History/History/#examples


@PanagiotisCharalampous

rick2010
20 Sep 2024, 15:10 ( Updated at: 21 Sep 2024, 06:09 )

RE: RE: RE: RE: RE: Check if position has already been opened and closed?

Thanks… so as soon as a trade is closed it is instantly added to the historical? If it's instant this solution will work…. but if there is a delay of say more than 1 second then I'll have to keep them in a list in my code.

PanagiotisCharalampous said: 

Check the link below

https://help.ctrader.com/ctrader-algo/references/Trading/History/History/#examples


@rick2010

PanagiotisCharalampous
21 Sep 2024, 06:12

RE: RE: RE: RE: RE: RE: Check if position has already been opened and closed?

rick2010 said: 

Thanks… so as soon as a trade is closed it is instantly added to the historical? If it's instant this solution will work…. but if there is a delay of say more than 1 second then I'll have to keep them in a list in my code.

PanagiotisCharalampous said: 

Check the link below

https://help.ctrader.com/ctrader-algo/references/Trading/History/History/#examples

Yes it should be added instantly


@PanagiotisCharalampous

rick2010
21 Sep 2024, 12:51

RE: RE: RE: RE: RE: RE: RE: Check if position has already been opened and closed?

PanagiotisCharalampous said: 

Yes it should be added instantly

Ok its exactly what I was looking for… Thanks

HistoricalTrade trade = History.FindLast(tradeLabel);

if(trade != null)
    return true; // trade is closed

 

 


@rick2010