Close Time Issue

Created at 11 Sep 2020, 05:42
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!
FI

findsameh

Joined 21.06.2015

Close Time Issue
11 Sep 2020, 05:42


Dear all,

I'm trying to retrieve a position's close time using an OnPositionsClosed event:


        protected override void OnStart()
        {

            Positions.Closed += OnPositionsClosed;
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            Print("Pos ID: {0} ... Close Time: {1}", position.Id, TimeInUtc);
        }

When 2 positions (ID 13 & 14, ID 15 & 16 in this case) are closed at the same time, the above returns close times for other positions.

Attached are screenshots from History and Log highlighting the discrepancy.

Pls let me know what I'm doing wrong.

Thank you,

Sameh 


@findsameh
Replies

PanagiotisCharalampous
11 Sep 2020, 09:17

Hi findsameh,

The position id is not the same with the closing deal id that is displayed in History. You are associating different trades.

Best Regards,

Panagiotis 

Join us on Telegram 

 


@PanagiotisCharalampous

findsameh
11 Sep 2020, 12:42 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi findsameh,

The position id is not the same with the closing deal id that is displayed in History. You are associating different trades.

Best Regards,

Panagiotis 

Join us on Telegram 

 

Dear Panagiotis,

Thank you for your reply. I tried to associate ID with Closing Deal ID with this:

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            var lastTrade = History[History.Count - 1];
            Print("Pos ID: {0} ... Close Time: {1}", lastTrade.ClosingDealId, TimeInUtc);
        }

But again when there are 2 positions (13 & 14) closing at the same time, only the latter position's Closing Deal ID is returned (14) for both 13 & 14, as shown in attached.

Pls let me know what can be done.

Thanks,

Sameh


@findsameh

PanagiotisCharalampous
11 Sep 2020, 12:47

Hi findsameh,

It would be better to use the associated PositionID to make the association instead of the index.

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

findsameh
11 Sep 2020, 13:43 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi findsameh,

It would be better to use the associated PositionID to make the association instead of the index.

Best Regards,

Panagiotis 

Join us on Telegram 

Dear Panagiotis, 

I used the "better" approach, but that didn't work either:

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            var lastTrade = History[History.Count - 1];
            Print("Pos ID: {0} ... Close Time: {1}", lastTrade.PositionId, TimeInUtc);
        }

Is there a "right" way as opposed to a "better" way?


@findsameh

PanagiotisCharalampous
11 Sep 2020, 13:46

Hi findsameh,

That's not what I meant. You assume that the last deal is the deal associated to the position which might not always be true. The right way is to make sure that the printed deal is the closing deal of the associated position. If you post the complete code, I can try and fix this for you.

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

findsameh
11 Sep 2020, 14:12

RE:

PanagiotisCharalampous said:

Hi findsameh,

That's not what I meant. You assume that the last deal is the deal associated to the position which might not always be true. The right way is to make sure that the printed deal is the closing deal of the associated position. If you post the complete code, I can try and fix this for you.

Best Regards,

Panagiotis 

Join us on Telegram 

I can't see the point in sharing the code for the whole bot, which I can't.

Instead, something as simple as this has the same issue, I can apply your fix on my bot.

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        int barCounter;
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
        }
        protected override void OnBar()
        {
            barCounter++;
            if (barCounter == 1)
            {
                ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1000, "label", 10, 10);
            }
            if (barCounter == 2)
            {
                ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1000, "label", 10, 10);
            }
            if (barCounter == 5)
            {
                foreach (var pos in Positions)
                {
                    ClosePosition(pos);
                }
            }
        }
        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var lastTrade = History[History.Count - 1];
            Print("Pos ID: {0} ... Close Time: {1}", lastTrade.PositionId, TimeInUtc);
        }
    }
}

Thanks,

Sameh


@findsameh

PanagiotisCharalampous
11 Sep 2020, 14:19

Hi Sameh,

I asked for the cBot code so that I can reproduce what you are seeing. I cannot reproduce such an issue with the cBot you posted. See below

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

findsameh
11 Sep 2020, 14:32 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi Sameh,

I asked for the cBot code so that I can reproduce what you are seeing. I cannot reproduce such an issue with the cBot you posted. See below

Best Regards,

Panagiotis 

Join us on Telegram 

These are the results I got. Meaning the date/time of trade has something to do with it.


@findsameh

findsameh
14 Sep 2020, 10:29 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

findsameh said:

PanagiotisCharalampous said:

Hi Sameh,

I asked for the cBot code so that I can reproduce what you are seeing. I cannot reproduce such an issue with the cBot you posted. See below

Best Regards,

Panagiotis 

Join us on Telegram 

These are the results I got. Meaning the date/time of trade has something to do with it.

Hello Panagiotis,

Any thoughts on this? As I mentioned on Friday, if you can fix the issue with the bot I shared, I can apply it mine. The issue arises at certain dates/times and it doesn't on others, as shown in my last post.

Thanks,

Sameh


@findsameh

PanagiotisCharalampous
14 Sep 2020, 10:41

Hi findsameh,

Here is the correct way to do this.

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var trade = History.Where(x => x.PositionId == args.Position.Id).First();
            Print("Pos ID: {0} ... Close Time: {1}", trade.PositionId, TimeInUtc);
        }

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

findsameh
14 Sep 2020, 10:47

RE:

PanagiotisCharalampous said:

Hi findsameh,

Here is the correct way to do this.

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var trade = History.Where(x => x.PositionId == args.Position.Id).First();
            Print("Pos ID: {0} ... Close Time: {1}", trade.PositionId, TimeInUtc);
        }

Best Regards,

Panagiotis 

Join us on Telegram 

Hello Panagiotis,

This is perfect! Thank you very much.

Best regards,

Sameh


@findsameh