Strategy.setStop() Back Testing Functions
setStop() is a method of the Strategy Object that submits a stop order at a specified price to close an existing position for back testing analysis through the Strategy Analyzer (Tools-->Back Testing). The Strategy Object is for back testing purposes only and is not intended for real time trading.
Strategy .setStop( nStop )
|nStop||Required. Number specifying the stop price level to close a position.||n/a|
The stop order will persist until the specified stop price is reached, which will close the entire position. Portions of a position may only be closed by specifying the specific lot size through the doCover() and doSell() methods. If a strategy requires this logic, additional conditions will be needed in the formula code. In this case, assign the stop price to a global variable and evaluate each bar against the global stop price variable and then exit the position using the doCover() or doSell() methods when a breach of the stop price has occured. There is a known problem with the setStop() method where bars that gap past the specified stop price level does not properly close the position. This problem is more prevalent for back tests on a daily interval. This issue will be corrected in a future version. Until that time, it is recommended to manage stops with the process described above using a global variable.
Use the clearStop() method to remove the current stop.
// Sets a stop order at the specified price. Strategy.setStop(low(-1));
// long stopStrategy.setStop( high(-1) ); // short stop // Long stop at current bar's low - 25% of the 10 period Average True Range.Strategy.setStop( low(0) - (0.25 * atr(10, 0)) );
// Short stop at current bar's high + 25% of the 10 period Average True Range.Strategy.setStop( high(0) + (0.25 * atr(10, 0)) );
// Assign a stop price to global variable (declared outside of main) // and pass the variable to setStop().nMyGlobalStopPrice = open(0) - 5.00;Strategy.setStop( nMyGlobalStopPrice ); // long stop