Help with plotting ATR stop that matches ApplyStop
Hi Everyone,
First post, so be gentle
I just purchased AmiBroker and want to visualise stops on the chart for comparison. I seen to have it working okay for a fixed stop loss and a % trailing stop, however I can't get a match for an ATR stop.
Here is the code:
Any help would be greatly appreciated!
Hi Everyone,
First post, so be gentle
I just purchased AmiBroker and want to visualise stops on the chart for comparison. I seen to have it working okay for a fixed stop loss and a % trailing stop, however I can't get a match for an ATR stop.
Here is the code:
Code:
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Price Style" ) | GetPriceStyle() );
_SECTION_END();
//Trading System
TradeDelay = Param( "Trade Delay", 0, 1, 1 );
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay );
SetBacktestMode ( backtestregular );
SetOption( "InitialEquity", 20000 );
posqty = Param( "PosQty", 20, 1, 20, 1 );
SetOption( "MaxOpenPositions", posqty );
SetOption( "PriceBoundChecking", 1 ); // trade only within the chart bar's price range
SetOption( "CommissionMode", 2 ); // set commissions AND costs as $ per trade
SetOption( "CommissionAmount", 22.95 ); // commissions AND cost
SetOption( "UsePrevBarEquityForPosSizing", 1 ); // set the use of last bars Equity for trade size
SetOption( "EveryBarNullCheck", True ); //For graphing stop losses
RoundLotSize = 10;
PositionSize = -100 / posqty; // desired position size is 100% portfolio Equity
//PositionScore = Random(); Not necessary for this purpose // Set the order for which stock trades when get mulitple signals in one bar in backtesting
BuyPrice = IIf( TradeDelay > 0, O, C );;
SellPrice = O; //Exit on stops only
Buy = Cross( EMA( C, 9 ), EMA( C, 28 ) ); //This is arbitrary - I just want to plot stop lines that match ApplyStop
Sell = 0;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
ExitAtStop = IIf( TradeDelay > 0, 2, 1 ); //0 for ??, 1 for Intraday on Signal, 2 for open on next day
//StopLoss = Param( "Max loss %", 10, 2, 30, 1 );
//ApplyStop( stopTypeLoss, stopModePercent, StopLoss, ExitAtStop, False ); //Fixed % Stop Loss
//PercentTrailingStopLevel = Param( "Trailing %", 23, 23, 30, 1 );
//ApplyStop( stopTypeTrailing, stopModePercent, PercentTrailingStopLevel, ExitAtStop, False ); //Trailing % Stop Loss
ATRTrailingStopLevel = Param( "ATR Factor", 3, 0.5, 5, 0.5 ) * Ref( ATR( Param( "ATR Periods", 10, 2, 30, 1 ) ), -TradeDelay );
ApplyStop( stopTypeTrailing, stopModePoint, ATRTrailingStopLevel, ExitAtStop, True ); //Dynamic ATR Stop Loss
Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Ref( Buy, -TradeDelay ), Ref( Sell, -TradeDelay ) );
StopStyle = ParamStyle( "Stop Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine );
//StopLossLine = IIf( InTrade OR Ref(Sell,-TradeDelay), ValueWhen( Ref( Buy, -TradeDelay ), BuyPrice ) * ( 1 - StopLoss / 100 ), Null );
//Plot( StopLossLine, "Stop Loss", colorRed, StopStyle );
//PercentTrailingStopLine = IIf( InTrade OR Ref(Sell,-TradeDelay), HighestSince( Buy, Ref( H, -TradeDelay ) - ( Ref( H, -TradeDelay ) * PercentTrailingStopLevel / 100 ) ), Null );
//Plot( PercentTrailingStopLine, "Percent Trailing Stop Line", colorRed, StopStyle );
ATRTrailingStopLine = IIf( InTrade OR Ref( Sell, -TradeDelay ), HighestSince( Buy, Ref( H, -TradeDelay ) - ATRTrailingStopLevel ), Null );
Plot( ATRTrailingStopLine, "ATR Trailing Stop Line", colorRed, StopStyle );
PlotShapes( Buy*shapeUpArrow, colorBrightGreen, 0, Low );
PlotShapes( Sell*shapeDownArrow, colorRed, 0, High );
Any help would be greatly appreciated!