My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LessonIndicators  
Create simple indicators
Updated Nov 4, 2011 by j...@pracplay.com

Basics :

  • Most indicators are created off ticks or bars.
  • Indicators can be as simple as embedded in your response
  • Complex indicators might have their own class

Simple indicator

The simplest indicators are created from bars.

Assignment

  1. Create a LessonIndicatorSimple response from a response template (as in lesson1)
  2. Look at the barlisttracker component.
  3. Amend your simple indicator response to use barlist trackers
  4. Ensure that bars are being created from ticks
  5. Ensure that no calculations are performed without at least 10 bars of data
  6. get highs and lows from bar
  7. compute all high-low range values using Calc.Subtract
  8. compute single average high-low range using Calc.Avg
  9. Output this value to screen using senddebug or D

Extra-credit : Only output debug message when HL range exceeds a volatility of 50cents. Extra-extra-credit : Using SMAResponse example, update indicator table in kadina/gauntlet and plot HL range on chart

Here is what you should get :

    public class LessonIndicatorSimple : ResponseTemplate
    {
        BarListTracker _blt = new BarListTracker();
        int MINBARS = 10;
        decimal MAXVOL = .5m;
        public override void GotTick(Tick tick)
        {
            // create bars from ticks
            _blt.newTick(tick);
            // make sure we have enough bars our indicator
            if (!_blt[tick.symbol].Has(MINBARS)) return;
            // get highs from our bar
            decimal [] highs = _blt[tick.symbol].High();
            // get lows
            decimal [] lows = _blt[tick.symbol].Low();
            // compute high low ranges
            decimal [] hlrange = Calc.Subtract(highs,lows);
            // compute average range
            decimal avghl = Calc.Avg(hlrange););
            // debug it
            if (avghl > MAXVOL)
                D(tick.symbol + " exceeded max volatility "+tick.time);
            // display it (gauntlet and kadina only)
            sendindicators(new string[] { tick.time.ToString(), avghl.ToString("N2") } );
            // plot it (in kadina only)
            sendchartlabel(avghl+_blt[tick.symbol].RecentBar.Close, _blt[tick.symbol].Last);

        }

        public LessonIndicatorSimple()
        {
            Indicators = new string[] { "Time","AVGHL" };
        }
    }

Testing the Example

Complex example

When you create your own indicator, often times you need to track information between ticks.

This usually means you have to keep track of symbol state. The simplest way to do this is with dictionaries.

Assignment :

  1. Create an empty response called LessonIndicatorComplex
  2. create a seperate class called ComplexIndicator that will hold spreads for symbols
  3. Pass ticks as they arrive to your indicator and have it track spreads
  4. Ensure that your response does not process symbols unless a spread is present
  5. After a spread is present, have response obtain spread
  6. If spread is bigger than 20 cents, send a debug alert

Extra-credit : send spread to indicator tab in kadina/gauntlet

You should get something like this

public class LessonIndicatorComplex : ResponseTemplate
    {
        ComplexSpreadIndicator _csi = new ComplexSpreadIndicator();
        decimal BIGSPREAD = .2m;
        public override void GotTick(Tick tick)
        {
            // update our indicator
            _csi.newTick(tick);
            // verify our indicator has enough data
            if (!_csi.hasSpread(tick.symbol)) return;
            // when we have enough data, we can now get spread
            decimal spread = _csi[tick.symbol];
            // debug it
            if (spread> BIGSPREAD)
                senddebug(tick.symbol+" too big to trade "+tick.time);
            // display our spread(gauntlet and kadina only)
            sendindicators(new string[] { tick.time.ToString(), spread.ToString("N2") });
        }

        public LessonIndicatorComplex()
        {
            Indicators = new string[] { "Time", "Spread" };
        }
    }


    public class ComplexSpreadIndicator : GotTickIndicator
    {
        // some dictionaries which hold bid and ask for each symbol
        Dictionary<string, decimal> _bid = new Dictionary<string, decimal>();
        Dictionary<string, decimal> _ask = new Dictionary<string, decimal>();
        // allows us to test if we have a full spread
        public bool hasSpread(string sym) 
        { 
            // make sure we have bid and ask values
            return _bid.ContainsKey(sym) && _ask.ContainsKey(sym);
        }
        // tracks spreads from ticks
        public bool newTick(Tick k)
        {
            // create a temp value so we can quickly query dictionaries
            decimal v = 0;
            // if we have a bid
            if (k.hasBid)
                // if it's not our first bid
                if (_bid.TryGetValue(k.symbol, out v))
                    _bid[k.symbol] = k.bid; // we can update it
                else // otherwise we must add it
                    _bid.Add(k.symbol,k.bid);
            // do same for ask
            if (k.hasAsk)
                if (_ask.TryGetValue(k.symbol, out v))
                    _ask[k.symbol] = k.ask;
                else 
                    _ask.Add(k.symbol,k.ask);
            // return whether spread is good
            return hasSpread(k.symbol);
        }

        public decimal Spread(string sym)
        {
            return hasSpread(sym) ? _ask[sym] - _bid[sym] : 0;
        }

        public decimal this[string sym] { get { return Spread(sym); } }

the next step is to send orders based on your indictors


Sign in to add a comment
Powered by Google Project Hosting