Closing price and time

Created at 29 Jun 2023, 22:22
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!
AL

AlexFrei

Joined 05.06.2023

Closing price and time
29 Jun 2023, 22:22


Hello all,

please how can I get closing price and time of position ? 


@AlexFrei
Replies

firemyst
30 Jun 2023, 03:33

You can do it in a position closing event method. Rough example below:

 

//In the OnStart method:
Positions.Closed += Positions_Closed;


//In the method itself once the event is defined:
private void Positions_Closed(PositionClosedEventArgs args)
{
     Position p1 = args.Position;
    //do whatever you have to do

    //Now get the historical trade stuff.
    HistoricalTrade ht = History.FindLast(p1.Label, p1.SymbolName, p1.TradeType);
    Print("Position \"{0} {1}\" closed for reason \"{2}\" with {3} profit. Entry Price {4}, Closing Price {5}, StopLoss {6}, ClosingTime {7}", p1.Id, p1.Label, args.Reason, String.Format("{0:$#,###.00}", p1.GrossProfit), p1.EntryPrice, p1.ClosingPrice, p1.StopLoss, ht.ClosingTime);

    //finish off whatever you have to do
}

 


@firemyst

AlexFrei
30 Jun 2023, 09:38

RE:

firemyst said:

You can do it in a position closing event method. Rough example below:

 

//In the OnStart method:
Positions.Closed += Positions_Closed;


//In the method itself once the event is defined:
private void Positions_Closed(PositionClosedEventArgs args)
{
     Position p1 = args.Position;
    //do whatever you have to do

    //Now get the historical trade stuff.
    HistoricalTrade ht = History.FindLast(p1.Label, p1.SymbolName, p1.TradeType);
    Print("Position \"{0} {1}\" closed for reason \"{2}\" with {3} profit. Entry Price {4}, Closing Price {5}, StopLoss {6}, ClosingTime {7}", p1.Id, p1.Label, args.Reason, String.Format("{0:$#,###.00}", p1.GrossProfit), p1.EntryPrice, p1.ClosingPrice, p1.StopLoss, ht.ClosingTime);

    //finish off whatever you have to do
}

 

 

Hi Firemyst, thank you so much !! I didnt know about History collection. Great. Thanks ! Have a great day


@AlexFrei

firemyst
30 Jun 2023, 09:42

RE: RE:

AlexFrei said:

 

Hi Firemyst, thank you so much !! I didnt know about History collection. Great. Thanks ! Have a great day

 

No worries. That's what the forums are for :-)


@firemyst

jim.tollan
30 Jun 2023, 11:32

RE: RE: RE:

firemyst said:

AlexFrei said:

 

Hi Firemyst, thank you so much !! I didnt know about History collection. Great. Thanks ! Have a great day

 

No worries. That's what the forums are for :-)

also thank you so much for this nugget. i've been wrestling with as issue regarding completed orders and this looks to address the problem i was facing... ;)


@jim.tollan

AlexFrei
30 Jun 2023, 12:10

RE: RE: RE:

firemyst said:

AlexFrei said:

 

Hi Firemyst, thank you so much !! I didnt know about History collection. Great. Thanks ! Have a great day

 

No worries. That's what the forums are for :-)

Thank you for having people like you.  Many others usually make me look like an idiot if I ask some basics 


@AlexFrei