Thursday, May 17, 2007

Real-Time Technical Indicator

I develop trading systems. In most of systems, technical indicators are used. What is very important when you run hundreds of trading systems that do very intensive calculations on price change, is that executions must be very efficient and quick. I was using some library, and what was bothering me is that all such indicators library are static. What i mean by static is that you have a function for calculating some indicator and you need to send array of input values, and to receive a value or array as output. But if you receive a new value or a change of last value in the array, you need to send the whole array of values again, and calculations to be done once again for all that data. Not just that array of prices and volumes can be big in number (hundreds) but also the number of times when you need calculation can be a problem. Sometimes you receive very frequent price events and you need all that calculations for all systems.

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:

Unknown said...

Looks good. What about the classes: BarDecimal and BarDecimalCollection?