BtDonchian_StopReverse.efs

ICE Data Services -


BtDonchian_StopReverse.efs  EFSLibrary - Discussion Board
  

File Name: BtDonchian_StopReverse.efs


Description:
This formula is a back testing example of how to code a strategy that closes positions with reversing trades.


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 stop and reverse 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 strategy is always holding a position, which reverses when the opposite trade signal occurs.

Download File:
BtDonchian_StopReverse.efs




EFS 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 Stop and Reverse strategy that goes long when a new    10 period Donchian high occurs and goes short when a new 10 period Donchian    low occurs.  The strategy is always in a position and reverses when the opposite    trade signal occurs.*********************************/function preMain() {    setPriceStudy(true);    setStudyTitle("Back Test: Donchian Stop and Reverse");    setCursorLabelName("Upper Donchian", 0);    setCursorLabelName("Lower Donchian", 1);    setDefaultBarFgColor(Color.blue, 0);    setDefaultBarFgColor(Color.blue, 1);}// Global Variablesvar xUpper = null;var xLower = null;var bInit  = false;  // initialization flagfunction 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);        xLower = lowerDonchian(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 nL = xLower.getValue(-1);		// Validate the study variables to ensure they contain valid data.	if(nU == null || nL == null) return;  // Exit the formula if invalid data.    // Long Trade Signal    if (Strategy.isLong() == false && high(0) >= nU) {        /*****        If the strategy is not long and the current bar's high            is equal to or greater than the previous bar's Upper Donchian channel,            go long at the value of the previous bar's Upper Donchian value or the            open, which ever is greater.        If the strategy is short at this instance, .doLong() automaticly exits             the short position and then enters the long position at the same price.            There is no need to call .doCover() to close the short position.        *****/                Strategy.doLong("Long Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, Math.max(nU, open(0)) );    } else if (Strategy.isShort() == false && low(0) <= nL) {  // Short Trade Signal        /*****        If the strategy is not short and the current bar's low            is equal to or lower than the previous bar's Lower Donchian channel,             go short at the previous bar's Lower Donchian value or the open,             which ever is lower.        If the strategy is long at this instance, .doShort() automaticly exits               the long position and then enters the short position at the same price.            There is no need to call .doSell() to close the long position.        *****/                Strategy.doShort("Short Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, Math.min(nL, open(0)) );            }    	if(Strategy.isLong()) {        setBarBgColor(Color.darkgreen);	} else if(Strategy.isShort()) {        setBarBgColor(Color.maroon);	}    // Plot the current bar's Donchian Channel values.	return new Array(xUpper.getValue(0), xLower.getValue(0));}