Back Testing Operations
Strategy Object for Back Testing
Fill Type Constants
- Strategy.CLOSE -- uses the closing price as the fill price
- Strategy.LIMIT -- uses the StopOrLimit price as the fill price
- Strategy.MARKET -- uses the open price as the fill price
- Strategy.STOP -- uses the StopOrLimit price as the fill price
Fill Bar Constants
- Strategy.THISBAR -- uses the OHLC values of the current bar to determine the fill price in conjunction with the Fill Type
- Strategy.NEXTBAR -- uses the OHLC values of the next bar to determine the fill price in conjunction with the Fill Type
LotSize Constants
- Strategy.DEFAULT -- uses the Default LotSize as the number of shares
- Strategy.ALL -- typically used with Strategy.doSell or Strategy.doCover; specifies that all shares being held to be sold/covered
Strategy Functions
- Strategy.doLong(Description, Fill Type, Fill Bar, LotSize, StopOrLimit) -- returns true if filled, false if not filled
- Strategy.doSell(Description, Fill Type, Fill Bar, LotSize, StopOrLimit) -- returns true if filled, false if not filled
- Strategy.doCover(Description, Fill Type, Fill Bar, LotSize, StopOrLimit) -- returns true if filled, false if not filled
- Strategy.doShort(Description, Fill Type, Fill Bar, LotSize, StopOrLimit) -- returns true if filled, false if not filled
- Strategy.isInTrade() -- returns true if currently in a trade (long or short); otherwise, false
- Strategy.isLong() -- returns true if currently long; otherwise, false
- Strategy.isShort() -- returns true if currently short; otherwise, false
- Strategy.setStop(dStop) -- sets a stop order; this will close out the active position if the stop order price is reached or exceeded
- Strategy.clearStop() -- clears (removes) the current stop order
- Strategy.getPositionSize() -- returns the number of shares currently held; this number is negative if short, positive if long (e.g., -100 is short 100 shares. 100 is long 100 shares.)
- Strategy.getDefaultLotSize() -- returns the default lot size as set by the user before starting the backtest
Notes about Strategy Functions
- If calling Strategy.doLong, Strategy.doShort, Strategy.doSell, or Strategy.doCover and a Fill Type of Strategy.LIMIT or Strategy.STOP is provided, the StopOrLimit price must be specified.
- You must be long to use Strategy.doSell.
- You must be short to use Strategy.doCover.
- If a long position is currently held, calling Strategy.doShort will close the long position and enter a short position.
- If a short position is currently held, calling Strategy.doLong will close the short position and enter a long position.
Examples: (assumes default lotsize is 100)
Strategy.doLong("Go Long", Strategy.CLOSE, Strategy.THISBAR);
//Go long 100 shares at the closing value of the current bar.
Strategy.doLong("Go Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
//Go long 100 shares at the closing value of the current bar.
Strategy.doLong("Go Long", Strategy.LIMIT, Strategy.THISBAR, null, 20);
//Limit order to go long 100 shares @ $20
//if the low of the current bar is <= 20.
if (Strategy.isLong()){
Strategy.doSell("Close Long", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, 30);
}
//If long, issue a stop sell order
//if high of the current bar >= 30.
Strategy.setStop(30);
//Set a stop order to sell all shares
//if high of any bar at or after the current bar >= 30.