Hello,
I am trying to preload historical data from a provider into a barseries to which live bars will be added as they come in. Note that I will have multiple time frames and want to use >1 bar series. For my 15-second series, I want to pass the barseries to the HMA user indicator (as posted by Sergey in
http://www.smartquant.com/forums/viewtopic.php?f=44&t=7185&hilit=+hma+indicator+)Below is the code for my 15-second bars:
Code:
DateTime startOfTrdDay = new DateTime(2012, 5, 9, 12,0,0);
Instrument baseInstrHist = InstrumentManager.Instruments["@ESM12"];
TradeSeries histTrdSeries = GetHistoricalTrades("NFIQFeed", baseInstrHist, startOfTrdDay,
DateTime.UtcNow.AddSeconds(-1));
BarSeries histBarSeries = DataManager.CompressBars(histTrdSeries, BarType.Time, 15);
BarSeries bs_15sec = GetBars(myInstr, BarType.Time, 15);
foreach (Bar bar in histBarSeries) bs_15sec.Add(bar);
This is working without problem.
Sergey's HMA indicator code is:
Code:
/// <summary>
/// HMA conversion.
/// </summary>
public class HMA: UserIndicator
{
private int period;
private BarData barData;
WMA wma1;
WMA wma2;
WMA diffWma;
BarSeries diffSeries;
public int Period
{
get { return period; }
set { period = value; }
}
public BarData BarData
{
get { return barData; }
set { barData = value; }
}
public HMA(ISeries series, int period, BarData barData) : base (series)
{
this.period = period;
this.barData = barData;
this.Name = "HMA, Period = " + period;
wma1 = new WMA((BarSeries)Input, (int)(period / 2) * 2);
wma2 = new WMA((BarSeries)Input , period);
diffSeries = new BarSeries();
diffWma = new WMA(diffSeries, (int)Math.Sqrt(period));
}
public HMA(ISeries series, int period)
: this(series, period, BarData.Close)
{
}
public override double Calculate(int index)
{
if (wma1.Count == 0 || wma2.Count == 0)
return double.NaN;
double hma = double.NaN;
if (index > period - 1)
{
double value1 = 2 * wma1.Last;
double value2 = wma2.Last;
diffSeries.Add(
Input.GetDateTime(index),
value1 - value2,
value1 - value2,
value1 - value2,
value1 - value2,
0,
0);
if (diffWma.Count == 0)
return double.NaN;
hma = diffWma.Last;
return hma;
}
else
return double.NaN;
}
}
My next line of code following the barseries assignment is:
Code:
userInd.HMA hma = new userInd.HMA(bs_15sec, 4, BarData.Close);
When the code enters this line, it correctly enter the constructor: HMA(ISeries series, int period, BarData barData). However, it executes the first 3 lines of code and then exits (entering into the Calculate method). I do not know why this is occurring and cannot resolve it.
If I call the HMA indicator on a barseries to which I have not appended historical bars (i.e. using GetBars methods without including any preloaded bars), the HMA code works properly and without incident. I gather from this that I must be doing something incorrect in my assignment of historical bars to the barseries. Can someone please advise what mistake I am making?
Thank you.