BtDonchian_FixedStops.efs

ICE Data Services -

BtDonchian_FixedStops.efs                                                                    EFSLibrary - Discussion Board

File Name: BtDonchian_FixedStops.efs


Description:
This formula is a back testing example of how to code a strategy that closes positions with fixed 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 a fixed stop that is set equal to the Middle Donchian channel. Positions will also be closed upon a reversing trade signal.

 

Download File:
BtDonchian_FixedStops.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 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 a fixed stop that is set equal 
    to the Middle Donchian channel.  Positions will also be closed upon a reversing
    trade signal.
*********************************/

function preMain() {
    setPriceStudy(true);
    setStudyTitle("Back Test: Donchian with Fixed 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 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);
        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);
    // Set Boolean exit flag to flase to indicate that a stop has not occured on this bar.
    var bExitFlag = false;
	
	// Validate the study variables to ensure they contain valid data.
	if(nU == null || nM == null || nL == 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;
    }

    // Fixed 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 setting the value of the bExitFlag
        to true if an exit trade occurs on the current bar.  The flag is then
        used in the entry logic that follows.  If the flag is true, no new entry
        conditions will be evaluated until the next bar, or execution of main.
        ******/
        if (Strategy.isLong() == true) {  // Long Fixed Stop Exit
            /*****
            First check to see if the bar opened below the stop price.  If so, exit
            at the open of the bar and set the bExitFlag to true.
            *****/
            if (open(0) <= nStop) {
                Strategy.doSell("Long Stop", Strategy.MARKET, Strategy.THISBAR);
                bExitFlag = true;  // Exit trade occured on the current bar.
            } 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);
                bExitFlag = true;  // Exit trade occured on the current bar.
            }
        }
        if (Strategy.isShort() == true) { // Short Fixed 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);
                bExitFlag = true;  // Exit trade occured on the current bar.
            } 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);
                bExitFlag = true;  // Exit trade occured on the current bar.
            }
        }
    } 
    
    // Entry Strategy
    if (bExitFlag == false) {
        /*****
        Look for new entry only if a stop exit has not occured on this bar.  By including
        this entry logic inside this condition, we ensure that we do not enter a new position
        on the same bar that an exit occurs.
        *****/
        if (Strategy.isLong() == false && 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.
            If the strategy is currently short, doLong() will also close the short position 
            at the same price as the long entry.
            *****/
            
            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 (Strategy.isShort() == false && 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.
            If the strategy is currently long, doShort() will also close the long position 
            at the same price as the short entry.
            *****/
            
            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);
}