Refer to last closed position result in OnBar or OnTick...

Created at 27 Nov 2018, 12:51
97

9718853

Joined 14.10.2014

Refer to last closed position result in OnBar or OnTick...
27 Nov 2018, 12:51


Hi,

Is it possible to refer to the last trade result without using OnPositionsClosed ?

Many thanks,
Ian


@9718853
Replies

PanagiotisCharalampous
27 Nov 2018, 12:58

Hi Ian,

You can access the position information from the Position parameter of the obj argument.

Let me know if this helps.

Best Regards,

Panagiotis


@PanagiotisCharalampous

9718853
27 Nov 2018, 13:17

Hi Panagiotis,

Thank you for your quick reply. I can see that the obj argument would allow me to refer to the current positions away from OnPositionsClosed..

However, I can't work out how to refer to the last closed position?

Any sample code you could offer would be greatly appreciated...

Many thanks,

Ian


@9718853

PanagiotisCharalampous
27 Nov 2018, 14:20

Hi Ian,

Here it is

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        Position _position;
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
        }
        
        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            _position = obj.Position;
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

marek.kysely.84@gmail.com
01 Dec 2018, 11:38

Hi Ian,

I think you can find the closed positions also through History...

HistoricalTrade lastTrade = History[History.Count - 1];

Do not forget to handle the case when Count is 0. History has also Find methods to allow searching for specific one. 


@marek.kysely.84@gmail.com