How to count the number of bars printed after position is initiated

Created at 01 Mar 2018, 10:33
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!
MA

marwaha1@gmail.com

Joined 27.02.2018

How to count the number of bars printed after position is initiated
01 Mar 2018, 10:33


HI,

I am looking for the sample code for calculating the no of bars since last entry has taken place. This helps if we need to close a position after a fixed no of bars.

any help is greatly appreciated

thanks

 


@marwaha1@gmail.com
Replies

PanagiotisCharalampous
02 Mar 2018, 17:34

Hi marwaha1@gmail.com,

See below

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; }
        private int _barCount;
        private bool _startCounting;
        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            _startCounting = true;
        }

        protected override void OnBar()
        {
            if (_startCounting)
                _barCount++;
        }

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

Let me know if this helps,

Best Regards,

Panagiotis


@PanagiotisCharalampous

jani
07 Nov 2019, 16:32

RE:

Panagiotis Charalampous said:

Hi marwaha1@gmail.com,

See below

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; }
        private int _barCount;
        private bool _startCounting;
        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
        }

        void OnPositionsOpened(PositionOpenedEventArgs obj)
        {
            _startCounting = true;
        }

        protected override void OnBar()
        {
            if (_startCounting)
                _barCount++;
        }

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

Let me know if this helps,

Best Regards,

Panagiotis

Shouldn't the bar counter be after the actual trade execution logic? How about resetting the counter after the trade is closed?


@jani

PanagiotisCharalampous
07 Nov 2019, 16:38

Hi Jan,

Shouldn't the bar counter be after the actual trade execution logic? 

The counter starts after a position is opened as per the requirement

How about resetting the counter after the trade is closed?

This is just an example on how to implement such logic. Further to that, each one needs to adjust it on his cBot logic.

Best Regards,

Panagiotis


@PanagiotisCharalampous

jani
10 Nov 2019, 16:32

RE:

Panagiotis Charalampous said:

Hi Jan,

Shouldn't the bar counter be after the actual trade execution logic? 

The counter starts after a position is opened as per the requirement

How about resetting the counter after the trade is closed?

This is just an example on how to implement such logic. Further to that, each one needs to adjust it on his cBot logic.

Best Regards,

Panagiotis

Ok, Thank you,  I understand.

 


@jani

... Deleted by UFO ...