Offsetting a study
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.
Replies
Rill
26 Feb 2013, 10:50
RE:
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
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,
@cAlgo_Fanatic