BtDonchian_PositionManagement.efs

ICE Data Services -


BtDonchian_PositionManagement.efs  EFSLibrary - Discussion Board
  

File Name: BtDonchian_PositionManagement.efs


Description:
This formula is a back testing example that illustrates how to code a strategy with complete position management with profit targets, stops and closing out positions with more than one closing trade.


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. To complete the position management for this strategy, the positions will also be partially (50%) closed at a fixed profit target. This target will initially be set to 1.5X the difference between the entry price and the initial stop price, which creates a minimum 1.5 reward risk ratio for the first half of the position. The second half of the position will be closed by the trailing stop.

Download File:
BtDonchian_PositionManagement.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 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.  To complete the position management for this strategy, the    positions will also be partially (50%) closed at a fixed profit target.  This target will    initially be set to 1.5X the difference between the entry price and the initial stop price,     which creates a minimum 1.5 reward risk ratio for the first half of the position.  The     second half of the position will be closed by the trailing stop.*********************************/function preMain() {    setPriceStudy(true);    setStudyTitle("Back Test: Donchian Position Management Example");    setCursorLabelName("Upper Donchian", 0);    setCursorLabelName("Lower Donchian", 1);    setCursorLabelName("Stop", 2);    setCursorLabelName("Target", 3);    setDefaultBarFgColor(Color.blue, 0);    setDefaultBarFgColor(Color.blue, 1);    setDefaultBarFgColor(Color.red, 2);   // Stop color    setDefaultBarFgColor(Color.khaki, 3); // Target color    setDefaultBarThickness(2, 2);         // Stop Thickness    setDefaultBarThickness(2, 3);         // Target Thickness    setPlotType(PLOTTYPE_FLATLINES, 2);   // Stop plot type    setPlotType(PLOTTYPE_FLATLINES, 3);   // Target plot type}// Global Variablesvar xUpper = null;var xMiddle = null;var xLower = null;var xATR  = null;var nStop  = null;var nTarget = null;var bTargetBreach = false;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);        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 and nTarget to null so that they do not plot if no longer in a position.    Also reset the flag for the target breach so that this will be enabled for the next        position taken.    *****/    if (Strategy.isInTrade() == false) {        nStop = null;        nTarget = null;        bTargetBreach = false;    } 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        }        /*****        If we are still in a position, check to see if the profit target was        executed.  The global bTargetBreach Boolean variable is used to track        this occurance.  If it is set to true, reset the nTarget to null so         that it will no longer plot on the chart.        *****/        if (bTargetBreach == true) {            nTarget = null;        }    }        // 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 and Profit Target 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);            } else if (bTargetBreach == false) { // Profit Target                /*****                If we get to this else statement, it means we did not get stopped out by                 the preceeding conditions and that we have not yet hit the profit target.                Now check for a breach of the profit target.                *****/                var nLot = Math.round(Strategy.getPositionSize()/2);                if (open(0) >= nTarget) {                    /*****                    Bar opened above the profit target level, close half of the position                    with a market order at the open.  To get the number of shares or contracts                    to sell, get the current position size, divide by 2 and round the result to                    ensure that we pass a whole number to the .doSell() call.  Also set the                    Boolean bTargetBreach variable to true to prevent subsequent bars from                     executing another profit target trade.  This is only allowed to occur once                    for each position taken.                    *****/                    bTargetBreach = true;                    Strategy.doSell("Profit Target", Strategy.MARKET, Strategy.THISBAR, nLot);                } else if (high(0) >= nTarget) {                    /*****                    If the bar did not open above the target level, check to see if the high of                    the bar breached the target level and exit half of the position with a limit                    order at the target price.  Again, set bTargetBreach to true.                    *****/                    bTargetBreach = true;                    Strategy.doSell("Profit Target", Strategy.LIMIT, Strategy.THISBAR, nLot, nTarget);                }            }        }        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 if (bTargetBreach == false) { // Profit Target                /*****                If we get to this else statement, it means we did not get stopped out by                 the preceeding conditions and that we have not yet hit the profit target.                Now check for a breach of the profit target.                *****/                var nLot = Math.abs(Math.round(Strategy.getPositionSize()/2));                if (open(0) <= nTarget) {                    /*****                    Bar opened below the profit target level, close half of the position                    with a market order at the open.  To get the number of shares or contracts                    to sell, get the current position size, divide by 2, round the result to                    ensure that we pass a whole number to the .doCover() call.  When the position                    is short, .getPositionSize() returns a negative number so the result above also                    needs to be converted to a positive number by taking the absolute value of the                     result (Math.abs() ).  Also set the Boolean bTargetBreach variable to true to                     prevent subsequent bars from executing another profit target trade.  This is                     only allowed to occur once for each position taken.                    *****/                    bTargetBreach = true;                    Strategy.doCover("Profit Target", Strategy.MARKET, Strategy.THISBAR, nLot);                } else if (low(0) <= nTarget) {                    /*****                    If the bar did not open below the target level, check to see if the low of                    the bar breached the target level and exit half of the position with a limit                    order at the target price.  Again, set bTargetBreach to true.                    *****/                    bTargetBreach = true;                    Strategy.doCover("Profit Target", Strategy.LIMIT, Strategy.THISBAR, nLot, nTarget);                }            }        }    } 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.            *****/                        var nEntry = Math.max(nU, open(0));            Strategy.doLong("Long Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, nEntry );                                    // Set the initial stop value at the previous bar's value of the              // Middle Donchian channel.            nStop = nM;                        // Set the profit target value at 1.5X the difference of the entry price              // and the initial stop price.            nTarget = nEntry + (1.5 * (nEntry - nStop));        } 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.            *****/                        var nEntry = Math.min(nL, open(0));            Strategy.doShort("Short Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, nEntry );                        // Set the initial stop value at the previous bar's value of the              // Middle Donchian channel.            nStop = nM;                        // Set the profit target value at 1.5X the difference of the entry price              // and the initial stop price.            nTarget = nEntry - (1.5 * (nStop - nEntry));        }    }    	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, nTarget);}