SmartQuant Discussion
http://www.smartquant.com/forums/

HMA (User Indicator) with Preloaded Bars
http://www.smartquant.com/forums/viewtopic.php?f=76&t=10170
Page 1 of 1

Author:  ivb73077 [ Wed May 09, 2012 8:45 pm ]
Post subject:  HMA (User Indicator) with Preloaded Bars

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.

Author:  Dr. Anton Fokin [ Wed May 09, 2012 11:16 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

Is this about Visual Quant ?

Author:  ivb73077 [ Wed May 09, 2012 11:22 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

no. i am adding a user HMA indicator to a strategy file.

Author:  Dr. Anton Fokin [ Wed May 09, 2012 11:35 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

This is VisualQuant -> New Features forum ;)

Author:  ivb73077 [ Wed May 09, 2012 11:38 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

i realize that. i posted this in error and subsequently reposted to the Strategy Programming forum.

Author:  ivb73077 [ Wed May 09, 2012 11:42 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

Could you please advise on this question either here or in the Strategy Programming forum post? Thank you.

Author:  Dr. Anton Fokin [ Wed May 09, 2012 11:42 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

ok, Sergey will look into this issue tomorrow in the office.

Author:  ivb73077 [ Wed May 09, 2012 11:44 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

thanks.

Author:  Baraz Sergey [ Thu May 10, 2012 10:33 am ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

Hi,

Please try to move ths following line to the end of the constructor code:
Code:
 this.Name = "HMA, Period = " + period;


It seems that the indicator is recalculated when you set the Name property. So it is better to do it after you initialize your indicator completely.

We will think if we need to keep this behaviour in next versions.

Regards,
Sergey.

Author:  ivb73077 [ Thu May 10, 2012 5:45 pm ]
Post subject:  Re: HMA (User Indicator) with Preloaded Bars

Removing the line corrected the problem. Thank you.

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/