My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LessonGenericTrackers  
Customizeable trackers
Updated May 13, 2011 by j...@pracplay.com

Introduction

Generic Trackers allow you to :

  • track arbitrary types of data
  • track program or indicator states
  • track things where there is not an existing tracker
  • view internal state of your strategy/program after-the-fact
  • more quickly create new (non-generic) trackers and implement new functionality
  • initialize your trading application or strategy with per-symbol data
  • import and export data to excel-readable files

Overview

Trackers use Generics in .net2 (hence the name).

As such they can be used with any type.

Key things to understand :

  • each tracker can store one value per symbol of the type declared when you create tracker
  • trackers should be indexed together for maximum speed

State/boolean tracking

Trackers are very useful for tracking program states... eg :

GenericTracker<bool> indicatorcross1 = new GenericTracker<bool>();
GenericTracker<bool> indicatorcross2 = new GenericTracker<bool>();
GenericTracker<bool> first1then2ok = new GenericTracker<bool>();


BarListTracker blt = new BarListTracker(BarInterval.FiveMin, BarInterval.OneMin);

void GotNewBar(string symbol, int interval)
{
    // get current barlist for this symbol+interval
    BarList bl = blt[symbol,interval];
    // check for first cross on first interval
    if (interval==(int)BarInterval.OneMin)
       // update the cross state
          indicatorcross1[symbol] =  bl.RecentBar.Close > Calc.Avg(bl.Close());
    // check second cross 
    if (interval==(int)BarInterval.FiveMin)
       // update the cross state
          indicatorcross2[symbol] =  bl.RecentBar.Close > Calc.Avg(bl.Close());
    // update first1then2
    if (first1then2ok[symbol] && indicatorcross2[symbol] && !indicatorcross1[symbol])
        first1then2ok[symbol] = false;
    // send order if everything looks good
    if (first1then2ok[symbol] && indicatorcross2[symbol] && indicatorcross1[symbol])
        sendorder(new BuyMarket(symbol,100));

}

Decimal/integer tracking

GenericTracker<int> expectedsize = new GenericTracker<int>();
PositionTracker pt = new PositionTracker();

void GotTick(Tick k)
{
   // assumes that symbols have already been indexed

   // see if we're waiting for previous orders to fill
   if (expectedsize[k.symbol]!=pt[k.symbol].Size)
      // we're still waiting for an order, so wait till next tick
      return;
   // otherwise it's ok to send a new order
   sendorder(new BuyMarket(k.symbol,100));
}
override void sendorder(Order o)
{
   // track expected size before sending
   if (o.isMarket)
       expectedsize[o.symbol] += o.size;
   // pass order along
   base.sendorder(o);
}
void GotFill(Fill f)
{
   pt.Adjust(f);
}
void GotPosition(Position p)
{
   pt.Adjust(p);
}

tracker indexing

You can speed up tracker accesses by :

  • indexing trackers together
  • using the symbol index rather than symbol itself

To index trackers together :

  1. pick one tracker as the primary
  2. handle the NewTxt event on the tracker
  3. in NewTxt event, call the addindex method on every tracker. this will ensure they all have the same index
  4. call addindex at some point on primary tracker (eg in GotTick or in Reset, GotPosition, etc). this only needs to be done once per symbol (eg if there is no index for symbol)
  5. during normal processing, get index for symbol from any tracker... then just use index rather than symbol.

public class MyResponse : ResponseTemplate
{

 GenericTracker<string> PRIMARY = new  GenericTracker<string>();
 GenericTracker<int> expectedsize = new  GenericTracker<int>();
 GenericTracker<bool> entryok = new GenericTracker<bool>();

public MyResponse()
{ 
  PRIMARY.NewTxt+=new SymIdxDelegate(addindex);
}

void addindex(string txt, int idx)
{
  expectedsize.addindex(txt);
  entryok.addindex(txt);
  pt.addindex(txt);
}

void GotTick(Tick k)
{
   // get index from symbol, add if it doesn't exist
   int idx = PRIMARY.addindex(k.symbol);
   // use index
   entryok[idx] = expectedsize[idx]==pt[idx].Size;
   if (entryok[idx])
      sendorder(new BuyMarket(k.symbol,100));
}
override void sendorder(Order o)
{
   if (o.isMarket)
      expectedsize[o.symbol] += o.size;
}
PositionTracker pt = new PositionTracker();
void GotFill(Trade t) { pt.Adjust(t); }

Next Steps


Sign in to add a comment
Powered by Google Project Hosting