1

I've tried to ask this question at "quantitive finance" but it seems this is better place because the question more about programing than trading

How do you declare Indicator interface? What would be the correct way to model an "Indicator"?

I'm using c# and I want to declare Indicator interface like this:

interface Indicator
{
    double Value { get; }
    event Action<Void> ValueUpdated;
}

or probably even like this:

interface Indicator
{
    event Action<Double> ValueUpdated;
}

I consider "pure price" also as trivial indicator:

class PriceIndicator : Indicator {
    PriceIndicator(string ticker) {
        ....
    }
}

Example of MA:

class MovingAverage : Indicator {
    private PriceIndicator price;

    public MovingAverage(PriceIndicator price, int period) {
        ....
    }

    // when price.ValueUpdated event occurs need to recalculate MA and raise ValueUpdated event
}

What do you think? Any suggestions are welcome!

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • Ok, but what is your question really? Does your code not work? – Tudor Dec 11 '11 at 13:40
  • I must be missing something - what is the actual issue? The interface declarations you posted are fine. – Oded Dec 11 '11 at 13:40
  • the question is how to declare `Indicator` interface and corresponding implementation (for example `Moving Average`). is it good or not to consider "pure price" as an Indicator. Is it good to use `event` to notify that value is updated or it is better to use sort of timer or other techniques – Oleg Vazhnev Dec 11 '11 at 13:43
  • if someone know links to some code that already declares `Indicator` `Moving average` etc. on C# links are very welcome – Oleg Vazhnev Dec 11 '11 at 13:44
  • 1
    You might want to look at Ninjatrader to see how it is implemented there. Instead of making the indicator autoupdate itself because of an event, I'd have another class that does it, the abstraction of indicator means that it knows how to calculate a value, but it doesn't need the responsability of knowing when to do it. But really without more context on your solution is hard to tell – Sebastian Piu Dec 11 '11 at 14:27
  • @SebastianPiu the issue is - indicator can be built on top of another indicator. for example I can built MA on MA or MA above just Price (that would be typical MA). So Indicator need to know when it's aggregated indicators updated so it can recalculate own value – Oleg Vazhnev Dec 11 '11 at 14:48
  • Again I don't have any context to know that, but If an indicator is composition of other indicators, then you can resolve that just that way, with a class that is composite by others. – Sebastian Piu Dec 11 '11 at 14:54

3 Answers3

0

As far as my understanding goes an Indicator should perform some action after something interesting as happened in market data. This means the interface must be alerted on all interesting events, these might be pure market data events or events triggered by other indicators. I'd just have a very simple interface that takes in a market data event and returns some event to interpreted by some other part of the system. This event would be dispatched to a large internal event queue.

 interface Indicator {
     Event processEvent(MarketDataEvent e);
 } 

So a stream of MarketDataEvents come in either directly form the market or from some other indicator, and then once a certain threshold or condition is meet within the processEvent method is met, the method would return a non-null Event to be deployed to the internal event queue, otherwise this method would just return null values.

newlogic
  • 807
  • 8
  • 25
0

First, I would start by figuring out IF you actually have a problem that needs a solution. An interface is a contract that guarantees specific functionality exists (methods, properties, etc.) when called by a consumer.

So if you don't actually need an interface, I wouldn't use one. However, as a working example in NinjaTrader, we created a custom DrawingTool called "TradeZone". This drawing tool has specific functionality that needs to be available to other indicators.

The only way to guarantee that the drawing tool being inspected has the functionality needed, is by using an interface. For example, look at the following code that iterates over the objects within a chart.

for (int i = chartControl.ChartObjects.Count - 1; i >= 0; i--)
{
    Gui.NinjaScript.IChartObject chartObject = chartControl.ChartObjects[i];

    if ((chartObject is DrawingTool) == false) continue;

    if (chartObject is ITradeZone)
    {

        ITradeZone tradeZone = chartObject as ITradeZone;
        if (tradeZone.CreatedByTradeManager)
        {
            // Do stuff with the tradeZone object...
        }
    }
}

We first check to see if it's a DrawingTool. If not, we ignore it.

Then we check to see if it implements our ITradeZone interface. If so, then we know we can call specific functionality as guaranteed by that interace, in this case the CreatedByTradeManager property.

(Note that one could also check the type name or even the tag value of the object instance, but the tag could be changed by the user and if the name of the object ever changes, then that might also be a failed condition.)

Mark Wise
  • 21
  • 4
0

I would have something like this

public interface IIndicator
{
      double Calculate();
}

So an indicator that is composed could be

public class CompositeIndicator: IIndicator
{
     private MovingAverage _ma;

     public CompositeIndicator(/* your parameters here */)
     {
         _ma = new MovingAverage();
     }

     public double Calculate()
     {
         var mavalue = _ma.Calculate();
         //do more stuff
         return mavalue;
     }
}

Then a component that knows all indicators that need to be calculated would call this method every time the price is changed, and reflect that in a chart or somewhere else.

Really this problem has been resolved in a numerous existing applications already, some examples you could check are Ninjatrader (implemented in C#) or Metatrader (implemented with c)

Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • thanks! but how to solve "overcalculate" problem? Assume I have 100 strategies each using the same indicator (MA on sp500 is a good example). when new bunch of data arrived all 100 strategies need fresh value, and this will cause 100 calculations of the same data what is pretty bad in hft.. – Oleg Vazhnev Dec 11 '11 at 15:13
  • Well then you can send the instance in the constructor as you had in your example and use only one for all indicators. Or you could use a cache (so the MA would only recalculate itself if there is not a value already for the current bar) – Sebastian Piu Dec 11 '11 at 15:15
  • also in case of MA it is not clear how calculate works. when I update value when new value received I only need to discard the oldest value and to add new value this is `O(1)` operation. In your case MA will be `O(n)` and with much worse const - actually `100 * O(n)` instead of `1 * O(1)` – Oleg Vazhnev Dec 11 '11 at 15:15
  • I just posted an example, of course an indicator will need price series data shared somehow. Again I recommend you seeing how an indicator is implemented in metatrader for example. – Sebastian Piu Dec 11 '11 at 15:17
  • i don't know where to download metatrader or ninjatrader source files, i was thinking this is private projects with private sources – Oleg Vazhnev Dec 11 '11 at 15:21
  • I don't mean the sources, I mean you check one implementation of a custom indicator, with that you will have an insight on how to layout yours. If you want an opensource one check tradestation (although it is implmented in Java) – Sebastian Piu Dec 11 '11 at 15:25