Run for limit indexes or time ( from index X to index Y)

Created at 08 Nov 2017, 10:15
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!
itmfar's avatar

itmfar

Joined 08.11.2017

Run for limit indexes or time ( from index X to index Y)
08 Nov 2017, 10:15


Is it possible to limit a custom indicator by time or index (run for 2 last days only) ? for example it runs form 32 hours ago to 1 hour ago? or from index=X to index =Y?

in my sample code will draw 9 lines from 10 last index to 20 last index but it never update

        protected override void Initialize()
        {
            secondGap = MarketSeries.Close.Count - 10;
            firstGap = MarketSeries.Close.Count - 20;

        }
        public override void Calculate(int index)
        {
            if (index > first && index < second)
                ChartObjects.DrawVerticalLine("v" + index, index, Colors.White);       

}

in my code because I initialize my parameters in Initialize(), it will never update it by further index. what is the solution?


@itmfar
Replies

PanagiotisCharalampous
08 Nov 2017, 10:50 ( Updated at: 21 Dec 2023, 09:20 )

Hi itmfar,

I created an example based on your code and it seems to be working as expected. See code sample and screenshot below

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        private int secondGap;
        private int firstGap;

        protected override void Initialize()
        {
            secondGap = MarketSeries.Close.Count - 10;
            firstGap = MarketSeries.Close.Count - 20;
        }

        public override void Calculate(int index)
        {
            if (index > firstGap && index < secondGap)
                ChartObjects.DrawVerticalLine("v" + index, index, Colors.White);
        }
    }
}

Let me know if I misunderstood something.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
08 Nov 2017, 11:27 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi itmfar,

I created an example based on your code and it seems to be working as expected. See code sample and screenshot below

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        private int secondGap;
        private int firstGap;

        protected override void Initialize()
        {
            secondGap = MarketSeries.Close.Count - 10;
            firstGap = MarketSeries.Close.Count - 20;
        }

        public override void Calculate(int index)
        {
            if (index > firstGap && index < secondGap)
                ChartObjects.DrawVerticalLine("v" + index, index, Colors.White);
        }
    }
}

Let me know if I misunderstood something.

Best Regards,

Panagiotis

thanks. it is ok. but it wont update if new bar comes up ,these 9 lines remain in their pervious positions but i need them to be drawn from 20 - 10 last bars at the moment.


@itmfar

itmfar
08 Nov 2017, 12:16

the main purpose is that we able to make run our indicator for a period of time only. what would be the solution?


@itmfar

PanagiotisCharalampous
08 Nov 2017, 13:00

Hi itmfar,

Thanks now it is more clear what you are trying to achieve. You can try the following approach

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
          
        }

        public override void Calculate(int index)
        {
            if (IsLastBar)
            {
                ChartObjects.RemoveAllObjects();
                for (int i = MarketSeries.Close.Count - 20; i < MarketSeries.Close.Count - 10; i++)
                    ChartObjects.DrawVerticalLine("v" + i, i, Colors.White);
            }
        }
    }
}

Let me know if this works for you.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
08 Nov 2017, 13:25

RE:

Panagiotis Charalampous said:

Hi itmfar,

Thanks now it is more clear what you are trying to achieve. You can try the following approach

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
          
        }

        public override void Calculate(int index)
        {
            if (IsLastBar)
            {
                ChartObjects.RemoveAllObjects();
                for (int i = MarketSeries.Close.Count - 20; i < MarketSeries.Close.Count - 10; i++)
                    ChartObjects.DrawVerticalLine("v" + i, i, Colors.White);
            }
        }
    }
}

Let me know if this works for you.

Best Regards,

Panagiotis

thanks it works perfectly . is there any sensible solution for limiting time? such as modifying special date to start (start only after September 2017 to December 2017) ,instead of my method?


@itmfar

PanagiotisCharalampous
08 Nov 2017, 14:41

Hi itmfar,

With some more coding, you can develop something more proper, that could also take dates as input. But it will need somebody to devote time for this to developed. If i find some free time soon, then I might prepare something for you.

Best Regards,

Panagiotis 


@PanagiotisCharalampous

itmfar
08 Nov 2017, 15:03

RE:

Panagiotis Charalampous said:

Hi itmfar,

With some more coding, you can develop something more proper, that could also take dates as input. But it will need somebody to devote time for this to developed. If i find some free time soon, then I might prepare something for you.

Best Regards,

Panagiotis 

that's kind of you .


@itmfar