For that purpose i start developing my library of real-time technical indicators, that will when possible, do just additional calculation for new or changed value and not all that calculation for previous values. So I don't have static methods and instead have class for every indicator.
Here is source for one of the simplest indicators named Accumulation/Distribution indicator or AD.
Formula for this indicator is:.
PreviousValue + ((Close - Low) - (High - Close))/ (High - Low) * Volume
public class AD
{
private Decimal _Value;
private readonly String _Name;
private readonly BarDecimalCollection _PriceBars = new BarDecimalCollection();
private readonly DecimalCollection _ValueArray = new DecimalCollection();
/// <summary>
/// Class for calculating Accumulation/Distribution
/// </summary>
public AD()
{
_Name = GetType().Name;
}
public Decimal Value
{
get { return _Value; }
}
public String Name
{
get { return _Name; }
}
public DecimalCollection ValueArray
{
get { return _ValueArray; }
}
/// <summary>
/// Method for adding one BarDecimal struct instance
/// </summary>
/// <param name="priceBar"></param>
public void PriceBar(BarDecimal priceBar)
{
if (priceBar.AddBar)
AddPriceBar(priceBar);
else
EditPriceBar(priceBar);
}
/// <summary>
/// Method for adding collection of BarDecimal struct instances
/// </summary>
/// <param name="priceBars"></param>
public void PriceBars(BarDecimalCollection priceBars)
{
if (priceBars == null || priceBars.Count == 0)
return;
for (Int32 i = 0; i < priceBars.Count; i++)
{
AddPriceBar(priceBars[i]);
}
}
private void AddPriceBar(BarDecimal priceBar)
{
_PriceBars.Add(priceBar);
Decimal change = priceBar.HighPrice - priceBar.LowPrice;
if (change != 0)
{
_Value = ((priceBar.ClosePrice - priceBar.LowPrice) - (priceBar.HighPrice - priceBar.ClosePrice)) / change * priceBar.Volume + _Value;
}
ValueArray.Add(_Value);
}
private void EditPriceBar(BarDecimal priceBar)
{
_PriceBars[_PriceBars.Count - 1] = priceBar;
Decimal change = priceBar.HighPrice - priceBar.LowPrice;
if (change != 0)
{
Decimal prevValue = ValueArray.Count > 1 ? ValueArray[ValueArray.Count - 2] : 0;
_Value = ((priceBar.ClosePrice - priceBar.LowPrice) - (priceBar.HighPrice - priceBar.ClosePrice)) / change + prevValue;
}
ValueArray[ValueArray.Count - 1] = _Value;
}
}
1 comment:
Looks good. What about the classes: BarDecimal and BarDecimalCollection?
Post a Comment