indicator using different timeframe
indicator using different timeframe
10 Jan 2018, 20:14
Hey guys.
I have created an indicator. I want Open (2h timeframe) and Close 2h timeframe) lines to be outlined on 1h chart. So here's what I did:
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Indicators { [Indicator("High Minus Low", IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)] public class HighMinusLow : Indicator { [Output("Main", Color = Colors.Orange)] public IndicatorDataSeries Result { get; set; } [Parameter(DefaultValue = 14)] public int Periods { get; set; } [Output("Upper", Color = Colors.Aquamarine)] public IndicatorDataSeries Upper { get; set; } [Output("Lower", Color = Colors.Aquamarine)] public IndicatorDataSeries Lower { get; set; } private MarketSeries h2; protected override void Initialize() { h2 = MarketData.GetSeries(TimeFrame.Hour2); //ma5 = Indicators.MovingAverage(h2.Close Period, MovingAverageType.Triangular); } public override void Calculate(int index) { double open = h2.Open[index - 1]; double close = h2.Close[index - 1]; Upper[index] = open; Lower[index] = close; } } }
---
No errors. However when I add an EURUSD instance the the indicator in my cAlgo, those lines show up for like a second or two and the vanish.
What am I doing wrong?
Thanks.
Replies
PanagiotisCharalampous
11 Jan 2018, 10:11
Hi irmscher9,
Can you please try the version below and let me know if it works for you?
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Indicators { [Indicator("High Minus Low", IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)] public class HighMinusLow : Indicator { [Output("Main", Color = Colors.Orange)] public IndicatorDataSeries Result { get; set; } [Parameter(DefaultValue = 14)] public int Periods { get; set; } [Output("Upper", Color = Colors.Aquamarine)] public IndicatorDataSeries Upper { get; set; } [Output("Lower", Color = Colors.Aquamarine)] public IndicatorDataSeries Lower { get; set; } private MarketSeries h2; protected override void Initialize() { h2 = MarketData.GetSeries(TimeFrame.Hour2); //ma5 = Indicators.MovingAverage(h2.Close Period, MovingAverageType.Triangular); } public override void Calculate(int index) { double open = h2.Open[h2.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index])]; double close = h2.Close[h2.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index])]; Upper[index] = open; Lower[index] = close; } } }
Best Regards,
Panagiotis
@PanagiotisCharalampous
irmscher9
11 Jan 2018, 13:40
Hey Panagiotis,
Yes, it works better. Thanks.
Could you please explain what these things:
[h2.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index])]
Thanks a lot!
@irmscher9
PanagiotisCharalampous
11 Jan 2018, 14:58
Hi irmscher9,
The problem with your indicator was that you were not using the correct indexes for the h2 series. The GetIndexByExactTime() function allows you to get the index for a specified MarketSeries based purely on time, which is a common reference between MarketSeries of different timeframes. Let me know if my explanation helps you.
Best Regards,
Panagiotis
@PanagiotisCharalampous
irmscher9
10 Jan 2018, 21:21
Ok, when I'm using
It works just fine.
Does that mean it's NOT possible to use a different timeframe data in indicators?
@irmscher9