NonLinearExits.efs EFSLibrary - Discussion Board
File Name: NonLinearExits.efs
Description:
NonLinear Exits
Formula Parameters:
Fraction of ATR for entry : 1
Length of channel for breakout : 20
Length of moving average : 30
Length of ATR, short-term : 10
Length of ATR, long-term : 200
Number of bars from entry to exit : 20
Constants for nonlinear exits a0 : 1
Constants for nonlinear exits a2 : 1
Constants for nonlinear exits a3 : 0.02
Constants for nonlinear exits b0 : 1
Constants for nonlinear exits b2 : 1
Constants for nonlinear exits b3 : 0.02
Notes:
This system illustrates different kinds of nonlinear
exits based on volatility, as given by the average
true range (ATR).
Mike Bryant
Breakout Futures
www.breakoutfutures.com/Newsletters/Newsletter0608.htm
www.BreakoutFutures.com
Download File:
NonLinearExits.efs
EFS Code:
/********************************* Provided By: eSignal (Copyright c eSignal), a division of Interactive Data Corporation. 2009. All rights reserved. This sample eSignal Formula Script (EFS) is for educational purposes only and may be modified and saved under a new file name. eSignal is not responsible for the functionality once modified. eSignal reserves the right to modify and overwrite this EFS file with each new release. Description: NonLinear Exits Version: 1.01 11/16/2009 Formula Parameters: Default: Fraction of ATR for entry 1 Length of channel for breakout 20 Length of moving average 30 Length of ATR, short-term 10 Length of ATR, long-term 200 Number of bars from entry to exit 20 Constants for nonlinear exits a0 1 Constants for nonlinear exits a2 1 Constants for nonlinear exits a3 0.02 Constants for nonlinear exits b0 1 Constants for nonlinear exits b2 1 Constants for nonlinear exits b3 0.02 Notes: This system illustrates different kinds of nonlinear exits based on volatility, as given by the average true range (ATR). Mike Bryant Breakout Futures www.breakoutfutures.com/Newsletters/Newsletter0608.htm www.BreakoutFutures.com **********************************/ var fpArray = new Array(); var bInit = false; function preMain() { setPriceStudy(true); setShowCursorLabel(true); setShowTitleParameters(false); setStudyTitle("NonLinear Exits"); setDefaultPriceBarColor(Color.black); setCursorLabelName("Open Price", 0); setDefaultBarFgColor(Color.blue, 0); setPlotType(PLOTTYPE_FLATLINES, 0); setDefaultBarThickness(1, 0); setCursorLabelName("Take Profit Price", 1); setDefaultBarFgColor(Color.green, 1); setPlotType(PLOTTYPE_FLATLINES, 1); setDefaultBarThickness(1, 1); setCursorLabelName("Stop Loss Price", 2); setDefaultBarFgColor(Color.red, 2); setPlotType(PLOTTYPE_FLATLINES, 2); setDefaultBarThickness(1, 2); setColorPriceBars(true); askForInput(); var x=0; fpArray[x] = new FunctionParameter("EntFr", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Fraction of ATR for entry"); setLowerLimit(1); setDefault(1); } fpArray[x] = new FunctionParameter("NChan", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Length of channel for breakout"); setLowerLimit(1); setDefault(20); } fpArray[x] = new FunctionParameter("NMA", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Length of moving average"); setLowerLimit(1); setDefault(30); } fpArray[x] = new FunctionParameter("NATRst", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Length of ATR, short-term"); setLowerLimit(1); setDefault(10); } fpArray[x] = new FunctionParameter("NATRlt", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Length of ATR, long-term"); setLowerLimit(1); setDefault(200); } fpArray[x] = new FunctionParameter("NBExit", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Number of bars from entry to exit"); setLowerLimit(1); setDefault(20); } fpArray[x] = new FunctionParameter("a0", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits a0"); setLowerLimit(0); setDefault(1); } fpArray[x] = new FunctionParameter("a1", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits a1"); setLowerLimit(0); setDefault(1); } fpArray[x] = new FunctionParameter("a2", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits a2"); setLowerLimit(0); setDefault(0.02); } fpArray[x] = new FunctionParameter("b0", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits b0"); setLowerLimit(0); setDefault(1); } fpArray[x] = new FunctionParameter("b1", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits b1"); setLowerLimit(0); setDefault(1); } fpArray[x] = new FunctionParameter("b2", FunctionParameter.NUMBER); with(fpArray[x++]){ setName("Constants for nonlinear exits b2"); setLowerLimit(0); setDefault(0.02); } } var xATRst = null; var xATRlt = null; var xMA = null; var xHH = null; var xLL = null; var xClose = null; var xOpen = null; var nEntryBar = 0; var nPriceEntry = 0; var nPriceProfit = 0; var nPriceLoss = 0; var bOut = false; function main(EntFr, NChan, NMA, NATRst, NATRlt, NBExit, a0, a1, a2, b0, b1, b2) { var nBarState = getBarState(); var EntLong = false; var EntShort = false; var nMA = 0; var nHH1 = 0; var nLL1 = 0; var nClose = 0; var nOpen = 0; var ExitSz1 = 0; var ExitSz2 = 0; var ATRst = 0; var ATRlt = 0; if (nBarState == BARSTATE_ALLBARS) { if (EntFr == null) EntFr = 1; if (NChan == null) NChan = 20; if (NMA == null) NMA = 30; if (NATRst == null) NATRst = 10; if (NATRlt == null) NATRlt = 200; if (NBExit == null) NBExit = 20; if (a0 == null) a0 = 1; if (a1 == null) a1 = 1; if (a2 == null) a2 = 0.02; if (b0 == null) b0 = 1; if (b1 == null) b1 = 1; if (b2 == null) b2 = 0.02; } if (bInit == false) { xATRst = sma(NATRst, atr(1)); xATRlt = sma(NATRlt, atr(1)); xMA = sma(NMA); xHH = upperDonchian(NChan); xLL = lowerDonchian(NChan); xClose = close(); xHigh = high(); xLow = low(); xOpen = open(); bInit = true; } nMA = xMA.getValue(-1); nHH1 = xHH.getValue(-2); nLL1 = xLL.getValue(-2); nClose = xClose.getValue(-1); ATRst = xATRst.getValue(-1); ATRlt = xATRlt.getValue(-1); nOpen = xOpen.getValue(0); if (getCurrentBarIndex() == 0) return; if (nHH1 == null || nMA == null || ATRst == null || ATRlt == null) return; if (nClose > nMA && nClose > (nHH1 + EntFr * ATRst )) { EntLong = true; } else { EntLong = false; } if (nClose < nMA && nClose < (nLL1 - EntFr * ATRst )) { EntShort = true; } else { EntShort = false; } ExitSz1 = ATRlt * (a0 + a1 * (ATRst / ATRlt) + a2 * Math.pow((ATRst / ATRlt), 2)); ExitSz2 = ATRlt * (b0 + b1 * (ATRst / ATRlt) + b2 * Math.pow((ATRst / ATRlt), 2)); if (EntLong && !Strategy.isLong()) { Strategy.doLong("Long", Strategy.MARKET, Strategy.THISBAR); nEntryBar = getCurrentBarCount(); nPriceProfit = nClose + ExitSz2; nPriceLoss = nClose - ExitSz1; nPriceEntry = nClose; setPriceBarColor(Color.green); } else { if (EntShort && !Strategy.isShort()) { Strategy.doShort("Short", Strategy.MARKET, Strategy.THISBAR); nEntryBar = getCurrentBarCount(); nPriceProfit = nClose - ExitSz2; nPriceLoss = nClose + ExitSz1; nPriceEntry = nClose; setPriceBarColor(Color.red); } else { if(Strategy.isLong()) { setPriceBarColor(Color.green); if (getCurrentBarCount() - nEntryBar >= NBExit) { Strategy.doSell("NBar-L", Strategy.CLOSE, Strategy.THISBAR); } else { Strategy.doSell("Targ-L", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, Math.max(nPriceProfit, nOpen)); Strategy.doSell("MM-L", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, Math.min(nPriceLoss, nOpen)); } if (!Strategy.isLong()) bOut = true; } else { if(Strategy.isShort()) { setPriceBarColor(Color.red); if (getCurrentBarCount() - nEntryBar >= NBExit) { Strategy.doCover("NBar-S", Strategy.CLOSE, Strategy.THISBAR); } else { Strategy.doCover("Targ-S", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, Math.min(nPriceProfit, nOpen)); Strategy.doCover("MM-S", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, Math.max(nPriceLoss, nOpen)); } if (!Strategy.isShort()) bOut = true; } } } } if (bOut == true) { nPriceEntry = 0; nPriceProfit = 0; nPriceLoss = 0; bOut = false; } if (nPriceEntry != 0) { return new Array(nPriceEntry, nPriceProfit, nPriceLoss); } else { return; } }