trading: how do you declare `Indicator` interface

900 Views Asked by At

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!

3

There are 3 best solutions below

6
On

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)

0
On

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.)

0
On

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.