My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LessonTradingRules  
Trading with Rules = Lesson 1 + Lesson 2
Updated Jan 1, 2011 by j...@pracplay.com

Introduction

Trading 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

  1. strategies are implemented inside of a Response
  2. The vast majority of your logic will be responding to market ticks
  3. tick response actions go in the GotTick function.
  4. fill response actions go in the GotFill function, etc
  5. You can see the response documentation for the entire Response reference

Considerations

  1. Populating working variables (eg feeding ticks to a moving average)
  2. Deciding whether you want to trade (or you have enough data; eg if your 10bar MA only has 5 bars of data so far)\
  3. Deciding whether to go long/short and how much
  4. whether to trade on bars or intra bar

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:

  1. populate the indicators we created in lesson2
  2. trade based upon those indicators
Lets say we want to add an additional indicator which computes a moving average.

Trading Rules aka If/else statements

  1. ignore tick when HL average is too big (market to volatile)
  2. otherwise, buy when current bar crosses above MA

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.


Sign in to add a comment
Powered by Google Project Hosting