|
LessonTrackerRules
Rules and actions
IntroductionYou can use GenericTrackers to create trading rules.
Advantages :
Pre-requisites :
In-practiceTips :
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