Positions - Partial Close event

Created at 11 Feb 2022, 17:04
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!
NC

ncel01

Joined 19.03.2020

Positions - Partial Close event
11 Feb 2022, 17:04


Hello,

 

As far as I've noticed, only fully closed positions do fall under Positions Closed event.

Thus, I would like to know what could be a solution to track this kind of event, when a position is not fully but only partially closed.

 

Thank you!


@ncel01
Replies

amusleh
14 Feb 2022, 09:01

Hi,

You can use Position Modified event for partial closes.


@amusleh

ncel01
14 Feb 2022, 14:10

RE:

amusleh said:

Hi,

You can use Position Modified event for partial closes.

Hi amusleh,

Thanks for your reply.

It makes sense in fact. I forgot about this event.

Is there a way to filter (within this event) a modified position for a partial close only?

My intention is to provide instructions in case of a partial close only, but it can also happen that a position has been modified for TP, SL, etc.

Thank you!


@ncel01

amusleh
15 Feb 2022, 09:11

Hi,

You can do something like this:

using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private Dictionary<int, double> _positionsVolume = new Dictionary<int, double>(100);

        protected override void OnStart()
        {
            foreach (var position in Positions)
            {
                _positionsVolume.Add(position.Id, position.VolumeInUnits);
            }

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
            Positions.Modified += Positions_Modified;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                _positionsVolume.Remove(obj.Position.Id);
            }
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
        }

        private void Positions_Modified(PositionModifiedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                var positionPreviousVolume = _positionsVolume[obj.Position.Id];

                if (positionPreviousVolume == obj.Position.VolumeInUnits)
                {
                    // It means volume desn't changed
                }
                // volume changed
                else
                {
                    // new volume added
                    if (positionPreviousVolume < obj.Position.VolumeInUnits)
                    {
                        Print("Position {0} volume increased, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                    // position partially closed
                    else
                    {
                        Print("Position {0} partially closed, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                }

                _positionsVolume[obj.Position.Id] = obj.Position.VolumeInUnits;
            }
            else
            {
                _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
            }
        }
    }
}

 


@amusleh

ncel01
15 Feb 2022, 23:04

RE:

amusleh said:

Hi,

You can do something like this:

using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private Dictionary<int, double> _positionsVolume = new Dictionary<int, double>(100);

        protected override void OnStart()
        {
            foreach (var position in Positions)
            {
                _positionsVolume.Add(position.Id, position.VolumeInUnits);
            }

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
            Positions.Modified += Positions_Modified;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                _positionsVolume.Remove(obj.Position.Id);
            }
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
        }

        private void Positions_Modified(PositionModifiedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                var positionPreviousVolume = _positionsVolume[obj.Position.Id];

                if (positionPreviousVolume == obj.Position.VolumeInUnits)
                {
                    // It means volume desn't changed
                }
                // volume changed
                else
                {
                    // new volume added
                    if (positionPreviousVolume < obj.Position.VolumeInUnits)
                    {
                        Print("Position {0} volume increased, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                    // position partially closed
                    else
                    {
                        Print("Position {0} partially closed, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                }

                _positionsVolume[obj.Position.Id] = obj.Position.VolumeInUnits;
            }
            else
            {
                _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
            }
        }
    }
}

 

amusleh,

That's clear. Thanks for the extra clarifications!


@ncel01

icollocollo
25 Apr 2022, 17:34

RE: RE:

ncel01 said:

amusleh said:

Hi,

You can do something like this:

using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private Dictionary<int, double> _positionsVolume = new Dictionary<int, double>(100);

        protected override void OnStart()
        {
            foreach (var position in Positions)
            {
                _positionsVolume.Add(position.Id, position.VolumeInUnits);
            }

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
            Positions.Modified += Positions_Modified;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                _positionsVolume.Remove(obj.Position.Id);
            }
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
        }

        private void Positions_Modified(PositionModifiedEventArgs obj)
        {
            if (_positionsVolume.ContainsKey(obj.Position.Id))
            {
                var positionPreviousVolume = _positionsVolume[obj.Position.Id];

                if (positionPreviousVolume == obj.Position.VolumeInUnits)
                {
                    // It means volume desn't changed
                }
                // volume changed
                else
                {
                    // new volume added
                    if (positionPreviousVolume < obj.Position.VolumeInUnits)
                    {
                        Print("Position {0} volume increased, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                    // position partially closed
                    else
                    {
                        Print("Position {0} partially closed, new volume: {1} | previous volume: {2}", obj.Position.Id, obj.Position.VolumeInUnits, positionPreviousVolume);
                    }
                }

                _positionsVolume[obj.Position.Id] = obj.Position.VolumeInUnits;
            }
            else
            {
                _positionsVolume.Add(obj.Position.Id, obj.Position.VolumeInUnits);
            }
        }
    }
}

 

amusleh,

That's clear. Thanks for the extra clarifications!

Is it possible to use this position modified in another Method? @amusleh


@icollocollo

amusleh
26 Apr 2022, 09:07

RE: RE: RE:

icollocollo said:

ncel01 said:

amusleh,

That's clear. Thanks for the extra clarifications!

Is it possible to use this position modified in another Method? @amusleh

Hi,

What do you mean by using the position on another method?


@amusleh

icollocollo
26 Apr 2022, 09:10

RE: RE: RE: RE:

amusleh said:

icollocollo said:

ncel01 said:

amusleh,

That's clear. Thanks for the extra clarifications!

Is it possible to use this position modified in another Method? @amusleh

Hi,

What do you mean by using the position on another method?

I was looking for a way to stop my positions from bwing modified twice in a cbot but i realized this is and event so i think i will try to use an if logic to implement that. Thank you


@icollocollo