|
LessonGenericTrackers
Customizeable trackers
Introduction
Generic Trackers allow you to :
OverviewTrackers use Generics in .net2 (hence the name). As such they can be used with any type. Key things to understand :
State/boolean trackingTrackers 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 indexingYou can speed up tracker accesses by :
To index trackers together :
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