[BUG] Historical trade registered after Position.Modified event

Created at 14 Jan 2019, 17:32
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!
Jiri's avatar

Jiri

Joined 31.08.2015

[BUG] Historical trade registered after Position.Modified event
14 Jan 2019, 17:32


Hi Spotware,

When a position is partially closed and triggers Position.Modified event it happens before historical trade is registered. You can see the sample below to reproduce the issue. Hope you can fix this as soon as possible.

 

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BugDemonstration : Robot
    {
        protected override void OnStart()
        {
            // executes market order
            var marketOrder = ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.VolumeInUnitsMin * 2);
            if (marketOrder.IsSuccessful)
            {
                var position = marketOrder.Position;

                // closes half of the position
                var partialClose = position.ModifyVolume(position.VolumeInUnits / 2);
                if (partialClose.IsSuccessful)
                {
                    // tries to find historical trade of partial close
                    var historicalTrade = History.FirstOrDefault(trade => trade.PositionId == position.Id);

                    // returns null, historical trade not found
                    Print("Sync trade = {0}", historicalTrade);

                    // async task trying every 50ms to find the trade
                    var task = new Task(() =>
                    {
                        while (historicalTrade == null)
                        {
                            Thread.Sleep(50);

                            historicalTrade = History.FirstOrDefault(trade => trade.PositionId == position.Id);

                            // when found
                            if (historicalTrade != null)
                            {
                                BeginInvokeOnMainThread(() =>
                                {
                                    Print("Async trade = {0}", historicalTrade);
                                    ClosePosition(position);
                                    Stop();
                                });
                            }
                        }
                    });
                    task.Start();
                }
            }
        }
    }
}


@Jiri
Replies

Jiri
24 Jan 2019, 01:52

Just a friendly follow-up, is this going to be fixed any time soon?


@Jiri

PanagiotisCharalampous
24 Jan 2019, 10:37

Hi Jiri,

It is a known issue and will be fixed in one of the upcoming updates.

Best Regards,

Panagiotis


@PanagiotisCharalampous