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
http://www.breakoutfutures.com/Newsl...letter0608.htm
www.BreakoutFutures.com


Download File:
NonLinearExits.efs



EFS Code:
PHP 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.blue0);
    
setPlotType(PLOTTYPE_FLATLINES0); 
    
setDefaultBarThickness(10);
    
setCursorLabelName("Take Profit Price"1);   
    
setDefaultBarFgColor(Color.green1);
    
setPlotType(PLOTTYPE_FLATLINES1); 
    
setDefaultBarThickness(11);
    
setCursorLabelName("Stop Loss Price"2);   
    
setDefaultBarFgColor(Color.red2);
    
setPlotType(PLOTTYPE_FLATLINES2); 
    
setDefaultBarThickness(12);
    
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(EntFrNChanNMANATRstNATRltNBExita0a1a2b0b1b2) {
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 == nullEntFr 1;
        if (
NChan == nullNChan 20;
        if (
NMA == nullNMA 30;
        if (
NATRst == nullNATRst 10;
        if (
NATRlt == nullNATRlt 200;
        if (
NBExit == nullNBExit 20;
        if (
a0 == nulla0 1;
        if (
a1 == nulla1 1;
        if (
a2 == nulla2 0.02;
        if (
b0 == nullb0 1;
        if (
b1 == nullb1 1;
        if (
b2 == nullb2 0.02;
    }    
    if (
bInit == false) { 
        
xATRst sma(NATRstatr(1));
        
xATRlt sma(NATRltatr(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.MARKETStrategy.THISBAR);
        
nEntryBar getCurrentBarCount();
        
nPriceProfit nClose ExitSz2;
        
nPriceLoss nClose ExitSz1;
        
nPriceEntry nClose;        
        
setPriceBarColor(Color.green);
    } else {  
        if (
EntShort && !Strategy.isShort()) {
            
Strategy.doShort("Short"Strategy.MARKETStrategy.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.CLOSEStrategy.THISBAR);
                } else {
                    
Strategy.doSell("Targ-L"Strategy.LIMITStrategy.THISBARStrategy.ALLMath.max(nPriceProfitnOpen));
                    
Strategy.doSell("MM-L"Strategy.STOPStrategy.THISBARStrategy.ALLMath.min(nPriceLossnOpen));
                }    
                if (!
Strategy.isLong()) bOut true;
            } else {
                if(
Strategy.isShort()) {
                    
setPriceBarColor(Color.red);
                    if (
getCurrentBarCount() - nEntryBar >= NBExit) {
                        
Strategy.doCover("NBar-S"Strategy.CLOSEStrategy.THISBAR);
                    } else {
                        
Strategy.doCover("Targ-S"Strategy.LIMITStrategy.THISBARStrategy.ALLMath.min(nPriceProfitnOpen));
                        
Strategy.doCover("MM-S"Strategy.STOPStrategy.THISBARStrategy.ALLMath.max(nPriceLossnOpen));
                    }
                    if (!
Strategy.isShort()) bOut true;
                }               
            }   
        } 
    }        
    if (
bOut == true) {
        
nPriceEntry 0;
        
nPriceProfit 0;
        
nPriceLoss 0;
        
bOut false;
    }    
    if (
nPriceEntry != 0) {
        return new Array(
nPriceEntrynPriceProfitnPriceLoss);
    } else {
        return;
    }