AMIBROKER: THE TRUTH ABOUT VOLATILITY
In “The Truth About Volatility,” Jim Berg presents how to use several well-known volatility measures such as average true range (ATR) to calculate entry, trailing stop, and profit-taking levels. Implementing techniques presented in the article is very simple using the AmiBroker Formula Language (Afl), and takes just a few lines of code.
Listing 1 shows the formula that the plots color-coded price chart, trailing stop, and profit-taking lines, as well as a colored ribbon showing volatility-based entry and exit signals. The relative strength index (RSI) used by Berg is a built-in indicator in AmiBroker, so no additional code is necessary. See Figure 3 for an example.
FIGURE: AMIBROKER, VOLATILITY SYSTEM. Here is a sample AmiBroker chart demonstrating the techniques from Jim Berg’s article in this issue.
EntrySignal = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) );
ExitSignal = C < ( HHV( H, 20 ) – 2 * ATR( 10 ) );
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorOrange, colorGrey50 ));
TrailStop = HHV( C – 2 * ATR(10), 15 );
ProfitTaker = EMA( H, 13 ) + 2 * ATR(10);
/* plot price chart and stops */
Plot( TrailStop, “Trailing stop”, colorBrown, styleThick | styleLine );
Plot( ProfitTaker, “Profit taker”, colorLime, styleThick );
Plot( C, “Price”, Color, styleBar | styleThick );
/* plot color ribbon */
Plot( 1, “”, Color, styleArea | styleOwnScale | styleNoLabel, -0.1, 50 );
–Tomasz Janeczko, AmiBroker.com