My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LessonTrackerRules  
Rules and actions
Updated May 13, 2011 by j...@pracplay.com

Introduction

You can use GenericTrackers to create trading rules.

Advantages :

  • always known clearly why a trade was entered or exited
  • minimizes troubleshooting of complex rule sets
  • minimizes typing
  • makes strategy more transparent
  • allows you to identify both positive and negative rule matches quickly

Pre-requisites :

  • you must have generic trackers of type 'bool' (true/false) configured for all your rule criteria/states
  • your criteria must all be expressed as positive with respect to the rule passing (iow only a 'true', or multiple 'trues' can trigger an action

In-practice

Tips :

  • you must name your indicators to get meaningful output

Lets take our example from lesson #2 in this series, except lets change the if statement to a rule.

here is the original :

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

}

here is this same thing converted to a rule (only the last line changes) :

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 (gt.rulepasses("sequential buy entry", symbol, senddebug, first1then2ok, indicatorcross2, indicatorcross1))
         sendorder(new BuyMarket(symbol,100));

}

So now when you run this strategy and get an entry, you will see something like this in the log :

IBM sequential buy entry, reason: FIRST1THEN2OK => TRUE, INDICATORCROSS2 => TRUE, INDICATORCROSS1 => TRUE

You can also provide an additional argument to 'rulepasses' by setting 'debugfailures' to true, and then you will get notifications when a rule failed and why :

IBM sequential buy entry FAILED, reason: FIRST1THEN2OK => FALSE

Sign in to add a comment
Powered by Google Project Hosting