|
LessonTradingRules
Trading with Rules = Lesson 1 + Lesson 2
IntroductionTrading rules are just the set of instructions that say 'if this happens' => 'do this'. As such, we call these rules Responses. Responses are created by inheriting from an existing response, or from a ResponseTemplate. A typical response would do something like, 'if some indicator crosses another indicator (or another value)' => 'buy or sell' Basic Response Information
Considerations
Generally you need to handle these three things in this order... ie populate your indicators... check to make sure they're good... then trade on them. You can look at SMAResponse in the ResponseExamples for an example. The first couple of lines of GotTick will be populating variables and checking to make sure they're good. Then the trading rules come at the end. The Response
In Lesson 2 we created an example for tracking HL range, we'll be extending this to:
Trading Rules aka If/else statements
Assignment :
You will need to reference the PositionTracker component as well as the SendOrder command. Answer :
public class LessonTradingRules : ResponseTemplate
{
BarListTracker _blt = new BarListTracker();
PositionTracker _pt = new PositionTracker();
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);
// ignore volatile symbols
if (avghl > MAXVOL) return;
// compute MA
decimal ma = Calc.Avg(_blt[tick.symbol].Close());
// trading rule
if (_pt[tick.symbol].isFlat && (_blt[tick.symbol].RecentBar.Close > ma))
sendorder(new BuyMarket(tick.symbol, 100));
// exit rule
if (_pt[tick.symbol].isLong && (_blt[tick.symbol].RecentBar.Close < ma))
sendorder(new MarketOrderFlat(_pt[tick.symbol]));
}
public override void GotFill(Trade fill)
{
_pt.Adjust(fill);
}
public override void GotPosition(Position p)
{
_pt.Adjust(p);
}
}
You can test in gauntlet, kadina or ASP to make sure it's doing what you'd expect.
|