{ NonlinearExits 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 mrb@BreakoutFutures.com Copyright 2008 Breakout Futures } Inputs: EntFr (1), { Fraction of ATR for entry } NChan (20), { Length of channel for breakout } NMA (30), { Length of moving average } NATRst (10), { Length of ATR, short-term } NATRlt (200), { Length of ATR, long-term } NBExit (20), { Number of bars from entry to exit } a0 (1), { constants for nonlinear exits } a1 (1), a2 (0.02), b0 (1), b1 (1), b2 (0.02); Var: EntLong (FALSE),{ Flag for entering long next bar } EntShort(FALSE),{ Flag for entering short next bar } ATRst (0), { Average true range, short-term } ATRlt (0), { Average true range, long-term } ExitSz1 (0), { Size in points of exit } ExitSz2 (0); { Size in points of exit } { Entry conditions } ATRst = Average(TrueRange, NATRst); ATRlt = Average(TrueRange, NATRlt); EntLong = C > Average(C, NMA) and C > (Highest(H, NChan)[1] + EntFr * ATRst); EntShort = C < Average(C, NMA) and C < (Lowest(L, NChan)[1] - EntFr * ATRst); { Place trades } If EntLong then Buy next bar at market Else If EntShort then Sell short next bar at market; { Exit orders } ExitSz1 = ATRlt * (a0 + a1 * (ATRst/ATRlt) + a2 * power((ATRst/ATRlt), 2)); ExitSz2 = ATRlt * (b0 + b1 * (ATRst/ATRlt) + b2 * power((ATRst/ATRlt), 2)); If MarketPosition > 0 then Begin { Time exit: bars from entry } If BarsSinceEntry = NBExit then Sell("NBar-L") this bar on close; { Money management stop } {Sell("MM-L") next bar at EntryPrice - ExitSz1 stop;} { Target exit } Sell("Targ-L") next bar at EntryPrice + ExitSz2 limit; End Else If MarketPosition < 0 then Begin { Time exit: bars from entry } If BarsSinceEntry = NBExit then Buy to cover("NBar-S") this bar on close; { Money management stop } {Buy to cover("MM-S") next bar at EntryPrice + ExitSz1 stop;} { Target exit } Buy to cover("Targ-S") next bar at EntryPrice - ExitSz2 limit; End;