Offsetting a study

Created at 21 Feb 2013, 17:53
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!
RI

Rill

Joined 21.02.2013

Offsetting a study
21 Feb 2013, 17:53


Hi,

I am just a simple trader, not a programmer, but I am used to tweaking little bits of code here and there to get what I want. I wish to modify the "sample price channels" code so that it uses and calculates series offset by "x" bars. e.g. I want to display the 20 period high and low from "x" bars ago.  

I naievly presumed that like many other languages I could simply add in [x] or [-x]  at the end of an expression. However this won't work in this language.

Please could you suggest how to add 1-2 lines of code that enable me to specify an offset to "Upper" and "lower" in the following code:

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class SamplePriceChannels : Indicator
    {
        [Parameter(DefaultValue = 20)]
        public int Periods { get; set; }

        [Output("Upper", Color = Colors.Pink)]
        public IndicatorDataSeries Upper { get; set; }

        [Output("Lower", Color = Colors.Pink)]
        public IndicatorDataSeries Lower { get; set; }

        [Output("Center", Color = Colors.Pink)]
        public IndicatorDataSeries Center { get; set; }

        public override void Calculate(int index)
        {
            double upper = double.MinValue;
            double lower = double.MaxValue;

            for (int i = index - Periods; i <= index - 1; i++)
            {
                upper = Math.Max(MarketSeries.High[i], upper);
                lower = Math.Min(MarketSeries.Low[i], lower);
            }

            Upper[index] = upper;
            Lower[index] = lower;
            Center[index] = (upper + lower) / 2;
        }
    }
}
 

Thank you for any time you spend helping me with this.

 

 


@Rill
Replies

cAlgo_Fanatic
21 Feb 2013, 18:03

Hello,

The for loop means that it is looping from index - Periods (which is the current bar - Periods) to index - 1 (which is the previous to the last bar).

If you change this to be index - Periods - X to index - 1 - X, then you should get the desired outcome.

So,

for (int i = index - Periods - X; i <= index - 1 - X; i++)
{
       upper = Math.Max(MarketSeries.High[i], upper);
       lower = Math.Min(MarketSeries.Low[i], lower);
}




@cAlgo_Fanatic

Rill
26 Feb 2013, 10:50

RE:
cAlgo_Fanatic said:

Hello,

The for loop means that it is looping from index - Periods (which is the current bar - Periods) to index - 1 (which is the previous to the last bar).

If you change this to be index - Periods - X to index - 1 - X, then you should get the desired outcome.

So,

for (int i = index - Periods - X; i <= index - 1 - X; i++)
{
       upper = Math.Max(MarketSeries.High[i], upper);
       lower = Math.Min(MarketSeries.Low[i], lower);
}



beautiful. Thank you very much.


@Rill