Closing PriceOr

Created at 26 Aug 2015, 12:26
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!
YA

Yaseen19

Joined 26.08.2015

Closing PriceOr
26 Aug 2015, 12:26


Does anyone know how to get the closing price of an order?? Or even the closing time of an order?

 


@Yaseen19
Replies

Spotware
26 Aug 2015, 12:43

Dear Trader,

Please have a look at the HistoricalTrade section of our API reference and at the following code snippet.

        Position pos;
        HistoricalTrade histrade;

        protected override void OnStart()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "label");
            pos = Positions.Find("label");
            ClosePosition(pos);

            histrade = History.FindLast("label");
            Print("Closing Price ", histrade.ClosingPrice);

        }

 


@Spotware

Yaseen19
26 Aug 2015, 13:02

RE:

Spotware said:

Dear Trader,

Please have a look at the HistoricalTrade section of our API reference and at the following code snippet.

        Position pos;
        HistoricalTrade histrade;

        protected override void OnStart()
        {

            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "label");
            pos = Positions.Find("label");
            ClosePosition(pos);

            histrade = History.FindLast("label");
            Print("Closing Price ", histrade.ClosingPrice);

        }

Thank you for the quick response. However i recieve this error "Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object." Any idea on why this happens? 

 


@Yaseen19

Spotware
26 Aug 2015, 13:18

Dear Trader,

Please take a look at the following article regarding NullReferenceExceptions: https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx

This happens if you still try to access the position after it is closed. A simple solution to avoid such errors is to check if the position is not null prior trying to access it.

...

if (pos != null)
	ClosePosition(pos);

...


@Spotware

Yaseen19
26 Aug 2015, 13:58

RE:

Spotware said:

Dear Trader,

Please take a look at the following article regarding NullReferenceExceptions: https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx

This happens if you still try to access the position after it is closed. A simple solution to avoid such errors is to check if the position is not null prior trying to access it.

...

if (pos != null)
	ClosePosition(pos);

Thank you once again for the quick responce. The only problem I have is that im working with a trailing stop loss and im not sure how to access the position once it has closed.

 


@Yaseen19

Spotware
27 Aug 2015, 07:13

Dear Trader,

Once a position is closed it becomes a historical trade. You can't access a position that does not exist anymore. 


@Spotware