File Name: BtDonchian_TrailingStops.efs
Description:
This formula is a back testing example of how to code a strategy that closes positions with trailing stops.
Formula Parameters:
NA
Notes:
This formula is a back testing formula to be used in conjunction with the Strategy Analyzer. It is not intended for real time analysis. This formula requires version 7.9.1 or later.
This strategy enters a position when a 10-period Donchian makes a new high on the upper channel or new low on the lower channel going long and short, respectively. The exit strategy for this sample uses the Average True Range study for the basis of the trailing stop logic. The initial stop is set to the Middle Donchian channel. The trailing stop will increment in the direction of the position by 25% of the previous bar's 10 period ATR.
Download File:
BtDonchian_TrailingStops.efs
EFS Code:
Description:
This formula is a back testing example of how to code a strategy that closes positions with trailing stops.
Formula Parameters:
NA
Notes:
This formula is a back testing formula to be used in conjunction with the Strategy Analyzer. It is not intended for real time analysis. This formula requires version 7.9.1 or later.
This strategy enters a position when a 10-period Donchian makes a new high on the upper channel or new low on the lower channel going long and short, respectively. The exit strategy for this sample uses the Average True Range study for the basis of the trailing stop logic. The initial stop is set to the Middle Donchian channel. The trailing stop will increment in the direction of the position by 25% of the previous bar's 10 period ATR.
Download File:
BtDonchian_TrailingStops.efs
EFS Code:
PHP Code:
/*********************************
Copyright © eSignal, a division of Interactive Data Corporation. 2006. All rights reserved.
This sample eSignal Formula Script (EFS) may be modified and saved under a new
filename; however, eSignal is no longer responsible for the functionality once modified.
eSignal reserves the right to modify and overwrite this EFS file with each new release.
This sample is for educational purposes only. It is not intended for trading.
Strategy Logic:
This Strategy example is a basic strategy that goes long when a new
10 period Donchian high occurs and goes short when a new 10 period Donchian
low occurs. The exit strategy for this sample uses the Average True Range study
for the basis of the trailing stop logic. The initial stop is set to the Middle Donchian
channel. The trailing stop will increment in the direction of the position by 25% of the
previous bar's 10 period ATR.
*********************************/
function preMain() {
setPriceStudy(true);
setStudyTitle("Back Test: Donchian with Trailing Stops");
setCursorLabelName("Upper Donchian", 0);
setCursorLabelName("Lower Donchian", 1);
setCursorLabelName("Stop", 2);
setDefaultBarFgColor(Color.blue, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.red, 2); // Stop color
setDefaultBarThickness(2, 2); // Stop Thickness
setPlotType(PLOTTYPE_FLATLINES, 2); // Stop plot type
}
// Global Variables
var xUpper = null;
var xMiddle = null;
var xLower = null;
var xATR = null;
var nStop = null;
var bInit = false; // initialization flag
function main() {
// Back Testing formulas are not for real time analysis.
// Therefore, prevent processing and exit at bar 0.
if (getCurrentBarIndex() == 0) return;
if(bInit == false) {
// This code block executes only once at the beginning of initialization.
// Initialize Donchian Series Objects.
xUpper = upperDonchian(10);
xMiddle = middleDonchian(10);
xLower = lowerDonchian(10);
xATR = atr(10);
bInit = true; // Prevents this code block from executing again.
}
// Retrieve previous bar's values of Donchian Series for back testing conditions.
var nU = xUpper.getValue(-1);
var nM = xMiddle.getValue(-1);
var nL = xLower.getValue(-1);
// Retrieve current bar's value of sar for the trailing stop condition.
var nATR = xATR.getValue(0);
var nInc = 0.25 * xATR.getValue(-1); // Stop increment.
// Validate the study variables to ensure they contain valid data.
if(nU == null || nM == null || nL == null || nATR == null || nInc == null) {
return; // Exit the formula if variables contain invalid data.
}
// Reset nStop to null so that it does not plot if no longer in a position.
if (Strategy.isInTrade() == false) {
nStop = null;
} else {
/*****
If the strategy is in a position, increment the trailing stop value.
*****/
if (Strategy.isLong() == true) {
nStop += nInc; // Moves up by nInc value
} else if (Strategy.isShort() == true) {
nStop -= nInc; // Moves down by nInc value
}
}
// Trailing Stop Exit Strategy
if (Strategy.isInTrade() == true) {
/*****
First check for a stop before looking for entry signals. If a stop
is found on the current bar, prevent a new entry from occuring on the
same bar. This is accomplished by placing the entry logic inside an else
statement that follows this trailing stop exit condition.
******/
if (Strategy.isLong() == true) { // Long Trailing Stop Exit
/*****
First check to see if the bar opened below the stop price. If so, exit
at the open of the bar.
*****/
if (open(0) <= nStop) {
Strategy.doSell("Long Stop", Strategy.MARKET, Strategy.THISBAR);
} else if (low(0) <= nStop) {
/*****
Next, check to see if the low of the bar breached the stop price.
If so, exit with a stop order at the stop price.
*****/
Strategy.doSell("Long Stop", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, nStop);
}
}
if (Strategy.isShort() == true) { // Short Trailing Stop Exit
/*****
First check to see if the bar opened above the stop price. If so, exit
at the open of the bar.
*****/
if (open(0) >= nStop) {
Strategy.doCover("Short Stop", Strategy.MARKET, Strategy.THISBAR);
} else if (high(0) >= nStop) {
/*****
Next, check to see if the high of the bar breached the stop price.
If so, exit with a stop order at the stop price.
*****/
Strategy.doCover("Short Stop", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, nStop);
}
}
} else { // Entry Strategy
/*****
Look for new entry only when the Strategy is not holding a position. By including
this entry logic in this else block, we ensure that we are not in a position when
these entry conditions are evaluated.
*****/
if (high(0) >= nU) { // Long Trade Signal
/*****
"if the current bar's high is equal to or greater than the previous bar's Upper
Donchian channel, go long with a limit order at the value of the previous bar's
Upper Donchian value or the open of the bar, which ever is greater.
*****/
Strategy.doLong("Long Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, Math.max(nU, open(0)) );
// Set the initial stop value at the previous bar's value of the
// Middle Donchian channel.
nStop = nM;
} else if (low(0) <= nL) { // Short Trade Signal
/*****
"if the current bar's low is equal to or lower than the previous bar's Lower
Donchian channel, go short with a limit order at the value of the previous bar's
Lower Donchian value or the open of the bar, which ever is smaller.
*****/
Strategy.doShort("Short Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, Math.min(nL, open(0)) );
// Set the initial stop value at the previous bar's value of the
// Middle Donchian channel.
nStop = nM;
}
}
if(Strategy.isLong()) {
setBarBgColor(Color.darkgreen);
} else if(Strategy.isShort()) {
setBarBgColor(Color.maroon);
}
// Plot the current bar's Donchian Channel values and the trailing stop.
return new Array(xUpper.getValue(0), xLower.getValue(0), nStop);
}