Multi Time Frame EMA
Multi Time Frame EMA
22 Oct 2018, 19:10
Hi everybody,
It 's EMA(20) of 1 Hour Period on 5 min chart. Does anybody know why the last segment of EMA is not straight like previous segments? That is the code (it is written like one of indicators of API Guide):
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.FLEStandardTime, AccessRights = AccessRights.None)]
public class MTF_EMA_1H : Indicator
{
[Parameter(DefaultValue = 20)]
public int Period { get; set; }
[Output("EMA 1H", Color = Colors.OrangeRed)]
public IndicatorDataSeries EMAH { get; set; }
private MarketSeries seriesHour;
private MovingAverage emaH;
protected override void Initialize()
{
seriesHour = MarketData.GetSeries(TimeFrame.Hour);
emaH = Indicators.MovingAverage(seriesHour.Close, Period, MovingAverageType.Exponential);
}
public override void Calculate(int index)
{
var indexH = seriesHour.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
if (indexH != -1)
EMAH[index] = emaH.Result[indexH];
}
}
}
Replies
Symposium
23 Oct 2018, 01:09
Freeman is right, the longer the timeframe the more jaggered the line will appear in the lower TF's. ie: the weekly would look like a ZigZg Indicator..........
I have added an Oscillating version of a MA MTF Indicator (completely adjustable) which will smooth out the Moving Averages for you.....
If you want to place the chart change the line (8) of code below.
[Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
Hope this is of some help...... Cheers
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = false, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)] public class MovingAverageMTFOscillator : Indicator { [Parameter(DefaultValue = 14)] public int Periods { get; set; } [Parameter("Timeframe 1", DefaultValue = "Minute15")] public TimeFrame MATimeframe1 { get; set; } [Parameter("Timeframe 2", DefaultValue = "Hour")] public TimeFrame MATimeframe2 { get; set; } [Parameter("Timeframe 3", DefaultValue = "Hour4")] public TimeFrame MATimeframe3 { get; set; } [Parameter("Fast MA Type", DefaultValue = MovingAverageType.Weighted)] public MovingAverageType MAType { get; set; } [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)] public IndicatorDataSeries MA1 { get; set; } [Output("Timeframe 2", Color = Colors.Red, Thickness = 1)] public IndicatorDataSeries MA2 { get; set; } [Output("Timeframe 3", Color = Colors.White, Thickness = 1)] public IndicatorDataSeries MA3 { get; set; } private MarketSeries series1; private MarketSeries series2; private MarketSeries series3; private MovingAverage Ma1; private MovingAverage Ma2; private MovingAverage Ma3; protected override void Initialize() { series1 = MarketData.GetSeries(MATimeframe1); series2 = MarketData.GetSeries(MATimeframe2); series3 = MarketData.GetSeries(MATimeframe3); Ma1 = Indicators.MovingAverage(series1.Close, Periods, MAType); Ma2 = Indicators.MovingAverage(series2.Close, Periods, MAType); Ma3 = Indicators.MovingAverage(series3.Close, Periods, MAType); } public override void Calculate(int index) { { var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]); if (index1 != -1) { MA1[index] = Ma1.Result[index1]; } var index2 = GetIndexByDate(series2, MarketSeries.OpenTime[index]); if (index2 != -1) { MA2[index] = Ma2.Result[index2]; } var index3 = GetIndexByDate(series3, MarketSeries.OpenTime[index]); if (index3 != -1) { MA3[index] = Ma3.Result[index3]; } } } private int GetIndexByDate(MarketSeries series, DateTime time) { for (int i = series.Close.Count - 1; i > 0; i--) { if (time == series.OpenTime[i]) return i; } return -1; } } }
@Symposium
Mastah1238
09 Nov 2018, 16:30
Hello
How can I change the code to show me two EMA 20 and 40 on the H4 chart with the D1 timeframe?
Best Regards
@Mastah1238
Symposium
10 Nov 2018, 03:11
( Updated at: 21 Dec 2023, 09:20 )
Hi Mastah1238, Parameters are shown in the image (Please find Code Below) Hope this is what you have asked to be displayed.
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = false, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)] public class MovingAverageMTFOscillator : Indicator { [Parameter("Timeframe 1", DefaultValue = "Minute15")] public TimeFrame MATimeframe1 { get; set; } [Parameter("Period TF 1", DefaultValue = 14)] public int Periods1 { get; set; } [Parameter("Timeframe 2", DefaultValue = "Hour")] public TimeFrame MATimeframe2 { get; set; } [Parameter("Period TF 2", DefaultValue = 14)] public int Periods2 { get; set; } [Parameter("Timeframe 3", DefaultValue = "Hour4")] public TimeFrame MATimeframe3 { get; set; } [Parameter("Period TF 3", DefaultValue = 14)] public int Periods3 { get; set; } [Parameter("Fast MA Type", DefaultValue = MovingAverageType.Weighted)] public MovingAverageType MAType { get; set; } [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)] public IndicatorDataSeries MA1 { get; set; } [Output("Timeframe 2", Color = Colors.Red, Thickness = 1)] public IndicatorDataSeries MA2 { get; set; } [Output("Timeframe 3", Color = Colors.White, Thickness = 1)] public IndicatorDataSeries MA3 { get; set; } private MarketSeries series1; private MarketSeries series2; private MarketSeries series3; private MovingAverage Ma1; private MovingAverage Ma2; private MovingAverage Ma3; protected override void Initialize() { series1 = MarketData.GetSeries(MATimeframe1); series2 = MarketData.GetSeries(MATimeframe2); series3 = MarketData.GetSeries(MATimeframe3); Ma1 = Indicators.MovingAverage(series1.Close, Periods1, MAType); Ma2 = Indicators.MovingAverage(series2.Close, Periods2, MAType); Ma3 = Indicators.MovingAverage(series3.Close, Periods3, MAType); } public override void Calculate(int index) { { var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]); if (index1 != -1) { MA1[index] = Ma1.Result[index1]; } var index2 = GetIndexByDate(series2, MarketSeries.OpenTime[index]); if (index2 != -1) { MA2[index] = Ma2.Result[index2]; } var index3 = GetIndexByDate(series3, MarketSeries.OpenTime[index]); if (index3 != -1) { MA3[index] = Ma3.Result[index3]; } } } private int GetIndexByDate(MarketSeries series, DateTime time) { for (int i = series.Close.Count - 1; i > 0; i--) { if (time == series.OpenTime[i]) return i; } return -1; } } }
@Symposium
Symposium
10 Nov 2018, 04:09
( Updated at: 21 Dec 2023, 09:20 )
RE:
Symposium said:
Hi Mastah1238, Parameters are shown in the image (Please find Code Below) Hope this is what you have asked to be displayed.
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = false, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)] public class MovingAverageMTFOscillator : Indicator { [Parameter("Timeframe 1", DefaultValue = "Minute15")] public TimeFrame MATimeframe1 { get; set; } [Parameter("Period TF 1", DefaultValue = 14)] public int Periods1 { get; set; } [Parameter("Timeframe 2", DefaultValue = "Hour")] public TimeFrame MATimeframe2 { get; set; } [Parameter("Period TF 2", DefaultValue = 14)] public int Periods2 { get; set; } [Parameter("Timeframe 3", DefaultValue = "Hour4")] public TimeFrame MATimeframe3 { get; set; } [Parameter("Period TF 3", DefaultValue = 14)] public int Periods3 { get; set; } [Parameter("Fast MA Type", DefaultValue = MovingAverageType.Weighted)] public MovingAverageType MAType { get; set; } [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)] public IndicatorDataSeries MA1 { get; set; } [Output("Timeframe 2", Color = Colors.Red, Thickness = 1)] public IndicatorDataSeries MA2 { get; set; } [Output("Timeframe 3", Color = Colors.White, Thickness = 1)] public IndicatorDataSeries MA3 { get; set; } private MarketSeries series1; private MarketSeries series2; private MarketSeries series3; private MovingAverage Ma1; private MovingAverage Ma2; private MovingAverage Ma3; protected override void Initialize() { series1 = MarketData.GetSeries(MATimeframe1); series2 = MarketData.GetSeries(MATimeframe2); series3 = MarketData.GetSeries(MATimeframe3); Ma1 = Indicators.MovingAverage(series1.Close, Periods1, MAType); Ma2 = Indicators.MovingAverage(series2.Close, Periods2, MAType); Ma3 = Indicators.MovingAverage(series3.Close, Periods3, MAType); } public override void Calculate(int index) { { var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]); if (index1 != -1) { MA1[index] = Ma1.Result[index1]; } var index2 = GetIndexByDate(series2, MarketSeries.OpenTime[index]); if (index2 != -1) { MA2[index] = Ma2.Result[index2]; } var index3 = GetIndexByDate(series3, MarketSeries.OpenTime[index]); if (index3 != -1) { MA3[index] = Ma3.Result[index3]; } } } private int GetIndexByDate(MarketSeries series, DateTime time) { for (int i = series.Close.Count - 1; i > 0; i--) { if (time == series.OpenTime[i]) return i; } return -1; } } }
To modify Indicator so it overlays the chart (not display as an Oscillator) paste the line of code below (Overlay = true) in the code and recompile.
Line 8 [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
Change Timeframe 1 to Daily1, Period TF 1 to 20 | Timeframe 2 to Daily1, Period TF 2 to 40 | Timeframe 3 to Daily1, Period TF 3 to 200 (For reference)
This gives the result you requested........ Zoomed out, Looks terrific for long term position...... Good Luck
@Symposium
freemangreat
23 Oct 2018, 00:05
Because the last 1H bar is not closed and the EMA value on it is recalculated with each tick.
The correct value will be the last on the 12th closed bar (60 min / 5 min = 12 bars).
@freemangreat