There is no indicator for H4 time frame EMAs before 261 working days.
There is no indicator for H4 time frame EMAs before 261 working days.
15 Nov 2020, 15:29
Dear Sir/Madam
I am using a custom indicator which is written by myself. It is simple indicator which uses 4 EMAs, two for M15 time frame and two for H4 time frame. Whenever I go back ward more than one year there is no H4 EMAs on the chart.
Please attach the indicator in M15 time frame and use the default settings. There is no H4 EMAs before Nov 15, 2019.
My Code is as below.
Please advise
Best regards
Nasser
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
public class NasserMTFnew : Indicator
{
[Parameter(DefaultValue = 15)]
public int Period1 { get; set; }
[Parameter(DefaultValue = 30)]
public int Period2 { get; set; }
[Parameter("EMAs Timeframe1", DefaultValue = "Minute15")]
public TimeFrame EMATimeframe1 { get; set; }
[Parameter("EMAs Timeframe2", DefaultValue = "Hour4")]
public TimeFrame EMATimeframe2 { get; set; }
[Output("EMA1", Color = Colors.Red)]
public IndicatorDataSeries EMA1 { get; set; }
[Output("EMA2", Color = Colors.ForestGreen)]
public IndicatorDataSeries EMA2 { get; set; }
[Output("EMA3", Color = Colors.BlueViolet)]
public IndicatorDataSeries EMA3 { get; set; }
[Output("EMA4", Color = Colors.Violet)]
public IndicatorDataSeries EMA4 { get; set; }
private MarketSeries seriesM15;
private MarketSeries seriesH4;
private ExponentialMovingAverage Ema1;
private ExponentialMovingAverage Ema2;
private ExponentialMovingAverage Ema3;
private ExponentialMovingAverage Ema4;
protected override void Initialize()
{
seriesM15 = MarketData.GetSeries(EMATimeframe1);
seriesH4 = MarketData.GetSeries(EMATimeframe2);
Ema1 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period1);
Ema2 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period2);
Ema3 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period1);
Ema4 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period2);
}
public override void Calculate(int index)
{
if (!IsLastBar)
{
var index1 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index1 != -1)
{
EMA1[index] = Ema1.Result[index1];
}
var index2 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index2 != -1)
{
EMA2[index] = Ema2.Result[index2];
}
var index3 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
if (index3 != -1)
{
EMA3[index] = Ema3.Result[index3];
}
var index4 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
if (index4 != -1)
{
EMA4[index] = Ema4.Result[index4];
}
}
else
{
EMA1[index] = Ema1.Result.LastValue;
EMA2[index] = Ema2.Result.LastValue;
EMA3[index] = Ema3.Result.LastValue;
EMA4[index] = Ema4.Result.LastValue;
}
}
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;
}
}
}
Replies
nh.zadeh
16 Nov 2020, 15:52
RE:
PanagiotisCharalampous said:
Hi Nasser,
To get more data you should use GetBars() instead of GetSeries() which is obsolete and then LoadMoreHistory() to retrieve more bars.
Best Regards,
Panagiotis
Dear Panagiotis
Many thanks for your feedback. I changed GetBars() and add LoadMoreHistory() method. When I delete LoadMoreHistory(), it works well with almost 261 days. But whenever I add LoadMoreHistory() it face the problem. Please find my codes as below:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
public class NasserMTFnewnew : Indicator
{
[Parameter(DefaultValue = 15)]
public int Period1 { get; set; }
[Parameter(DefaultValue = 30)]
public int Period2 { get; set; }
[Parameter("EMAs Timeframe1", DefaultValue = "Minute15")]
public TimeFrame EMATimeframe1 { get; set; }
[Parameter("EMAs Timeframe2", DefaultValue = "Hour4")]
public TimeFrame EMATimeframe2 { get; set; }
[Output("EMA1", Color = Colors.Red)]
public IndicatorDataSeries EMA1 { get; set; }
[Output("EMA2", Color = Colors.ForestGreen)]
public IndicatorDataSeries EMA2 { get; set; }
[Output("EMA3", Color = Colors.BlueViolet)]
public IndicatorDataSeries EMA3 { get; set; }
[Output("EMA4", Color = Colors.Violet)]
public IndicatorDataSeries EMA4 { get; set; }
private Bars seriesM15;
private Bars seriesH4;
private ExponentialMovingAverage Ema1;
private ExponentialMovingAverage Ema2;
private ExponentialMovingAverage Ema3;
private ExponentialMovingAverage Ema4;
protected override void Initialize()
{
seriesM15 = MarketData.GetBars(EMATimeframe1);
seriesH4 = MarketData.GetBars(EMATimeframe2);
//seriesM15.LoadMoreHistory()
seriesH4.LoadMoreHistory();
Ema1 = Indicators.ExponentialMovingAverage(seriesM15.ClosePrices, Period1);
Ema2 = Indicators.ExponentialMovingAverage(seriesM15.ClosePrices, Period2);
Ema3 = Indicators.ExponentialMovingAverage(seriesH4.ClosePrices, Period1);
Ema4 = Indicators.ExponentialMovingAverage(seriesH4.ClosePrices, Period2);
}
public override void Calculate(int index)
{
if (!IsLastBar)
{
var index1 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index1 != -1)
{
EMA1[index] = Ema1.Result[index1];
}
var index2 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index2 != -1)
{
EMA2[index] = Ema2.Result[index2];
}
var index3 = GetIndexByDate(seriesH4, Bars.OpenTimes[index]);
if (index3 != -1)
{
EMA3[index] = Ema3.Result[index3];
}
var index4 = GetIndexByDate(seriesH4, Bars.OpenTimes[index]);
if (index4 != -1)
{
EMA4[index] = Ema4.Result[index4];
}
}
else
{
EMA1[index] = Ema1.Result.LastValue;
EMA2[index] = Ema2.Result.LastValue;
EMA3[index] = Ema3.Result.LastValue;
EMA4[index] = Ema4.Result.LastValue;
}
}
private int GetIndexByDate(Bars series, DateTime time)
{
for (int i = series.ClosePrices.Count - 1; i > 0; i--)
{
if (time == series.OpenTimes[i])
return i;
}
return -1;
}
}
}
@nh.zadeh
PanagiotisCharalampous
16 Nov 2020, 16:16
Hi Nasser,
I did not understand what is the problem. Please explain.
Best Regards,
Panagiotis
@PanagiotisCharalampous
nh.zadeh
16 Nov 2020, 18:15
RE:
PanagiotisCharalampous said:
Hi Nasser,
I did not understand what is the problem. Please explain.
Best Regards,
Panagiotis
Dear Panagiotis
Thanks for your feedback.
In the first stage, for the getting candle information I use GetBars() and it worked properly for almost 261 days in chart backing. before 261 days there were not EMA for H4 (lbigger time frame).
Then I added
seriesH4.LoadMoreHistory();
After adding it not shows even current H4 EMAs.
The updated code is as below:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
public class NasserMTFnewnew : Indicator
{
[Parameter(DefaultValue = 15)]
public int Period1 { get; set; }
[Parameter(DefaultValue = 30)]
public int Period2 { get; set; }
[Parameter("EMAs Timeframe1", DefaultValue = "Minute15")]
public TimeFrame EMATimeframe1 { get; set; }
[Parameter("EMAs Timeframe2", DefaultValue = "Hour4")]
public TimeFrame EMATimeframe2 { get; set; }
[Output("EMA1", Color = Colors.Red)]
public IndicatorDataSeries EMA1 { get; set; }
[Output("EMA2", Color = Colors.ForestGreen)]
public IndicatorDataSeries EMA2 { get; set; }
[Output("EMA3", Color = Colors.BlueViolet)]
public IndicatorDataSeries EMA3 { get; set; }
[Output("EMA4", Color = Colors.Violet)]
public IndicatorDataSeries EMA4 { get; set; }
private Bars seriesM15;
private Bars seriesH4;
private ExponentialMovingAverage Ema1;
private ExponentialMovingAverage Ema2;
private ExponentialMovingAverage Ema3;
private ExponentialMovingAverage Ema4;
protected override void Initialize()
{
seriesM15 = MarketData.GetBars(EMATimeframe1);
seriesH4 = MarketData.GetBars(EMATimeframe2);
//seriesM15.LoadMoreHistory()
seriesH4.LoadMoreHistory();
Ema1 = Indicators.ExponentialMovingAverage(seriesM15.ClosePrices, Period1);
Ema2 = Indicators.ExponentialMovingAverage(seriesM15.ClosePrices, Period2);
Ema3 = Indicators.ExponentialMovingAverage(seriesH4.ClosePrices, Period1);
Ema4 = Indicators.ExponentialMovingAverage(seriesH4.ClosePrices, Period2);
}
public override void Calculate(int index)
{
if (!IsLastBar)
{
var index1 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index1 != -1)
{
EMA1[index] = Ema1.Result[index1];
}
var index2 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
if (index2 != -1)
{
EMA2[index] = Ema2.Result[index2];
}
var index3 = GetIndexByDate(seriesH4, Bars.OpenTimes[index]);
if (index3 != -1)
{
EMA3[index] = Ema3.Result[index3];
}
var index4 = GetIndexByDate(seriesH4, Bars.OpenTimes[index]);
if (index4 != -1)
{
EMA4[index] = Ema4.Result[index4];
}
}
else
{
EMA1[index] = Ema1.Result.LastValue;
EMA2[index] = Ema2.Result.LastValue;
EMA3[index] = Ema3.Result.LastValue;
EMA4[index] = Ema4.Result.LastValue;
}
}
private int GetIndexByDate(Bars series, DateTime time)
{
for (int i = series.ClosePrices.Count - 1; i > 0; i--)
{
if (time == series.OpenTimes[i])
return i;
}
return -1;
}
}
}
@nh.zadeh
PanagiotisCharalampous
17 Nov 2020, 08:24
Hi Nasser,
It looks fine to me. Make sure that you wait for the history to be retrieved.
Best Regards,
Panagiotis
@PanagiotisCharalampous
nh.zadeh
17 Nov 2020, 11:58
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi Nasser,
It looks fine to me. Make sure that you wait for the history to be retrieved.
Best Regards,
Panagiotis
Dear Panagiotis
Thanks for your feedback. You are right and it needs time for uploading. It works for me too.
Best regards,
Nasser
@nh.zadeh
PanagiotisCharalampous
16 Nov 2020, 10:47
Hi Nasser,
To get more data you should use GetBars() instead of GetSeries() which is obsolete and then LoadMoreHistory() to retrieve more bars.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous