# Weekly Breakout Trading Strategy Amibroker AFL Code

The Weekly Breakout Trading Strategy is a popular technical analysis trading strategy that aims to take advantage of the market’s tendency to experience a significant price movement following a period of consolidation. This strategy involves identifying key price levels where the market is likely to experience a breakout, and then entering a long or short position when the price breaks out above or below these levels. The weekly breakout trading strategy can be applied to a wide range of financial markets, including stocks, commodities, and forex. In this post, we’ll go through a Weekly Breakout Trading Strategy Amibroker AFL Code. The AFL code can be downloaded free of cost and imported into Amibroker.

## Weekly Breakout Trading Strategy Amibroker AFL Screenshot

Check out the screenshot of this AFL below

## AFL Explanation

Here is the explanation of each line of code:

```// Define Inputs
WeeksBack = Param("Weeks Back", 20, 1, 100, 1);
Threshold = Param("Threshold", 1, 0.1, 10, 0.1);```

`Param` function defines the inputs for the trading strategy. In this case, we have two inputs: `WeeksBack` which is the number of weeks to look back for the previous high and low, and `Threshold` which is the percentage distance from the previous high/low to define the breakout level.

```// Define Variables
PrevHigh = Ref(HHV(H, WeeksBack), -1);
PrevLow = Ref(LLV(L, WeeksBack), -1);```

`Ref` function is used to reference a previous bar’s value. `HHV` and `LLV` functions are used to calculate the highest and lowest values over a specified period. In this case, we’re using `Ref` to get the previous high and low values, respectively.

```// Calculate Breakout Levels
BreakoutLong = PrevHigh + Threshold * (PrevHigh - PrevLow);
BreakoutShort = PrevLow - Threshold * (PrevHigh - PrevLow);```

The `BreakoutLong` and `BreakoutShort` variables are calculated using the `PrevHigh` and `PrevLow` values, and the `Threshold` input. `BreakoutLong` is the previous high plus `Threshold` times the difference between the previous high and low, while `BreakoutShort` is the previous low minus `Threshold` times the difference between the previous high and low.

```// Define Trading Signals
SellSignal = C < BreakoutShort;```

`BuySignal` is true when the closing price is above the `BreakoutLong` level, while `SellSignal` is true when the closing price is below the `BreakoutShort` level

```// Plot Breakout Levels
Plot(PrevHigh, "Previous High", colorGreen, styleLine);
Plot(PrevLow, "Previous Low", colorRed, styleLine);
Plot(BreakoutLong, "Long Breakout", colorBlue, styleLine);
Plot(BreakoutShort, "Short Breakout", colorBlue, styleLine);

PlotShapes(IIf(BuySignal, shapeUpArrow, shapeNone), colorGreen, 0, Low);
PlotShapes(IIf(SellSignal, shapeDownArrow, shapeNone), colorRed, 0, High);

• The first 4 lines use the `Plot` function to plot the previous high and low levels, as well as the `BreakoutLong` and `BreakoutShort` levels on the chart. `Plot` function takes 4 arguments: the value to be plotted, the name of the plot, the color of the plot, and the style of the plot.
• The next 2 lines use the `PlotShapes` function to plot arrows on the chart when a buy or sell signal is generated. `PlotShapes` takes 4 arguments: the shape to be plotted (in this case, an up arrow for a buy signal or a down arrow for a sell signal), the color of the shape, the layer (0 means the default layer), and the price level at which the shape should be plotted (in this case, the low for a buy signal or the high for a sell signal).
• The last 2 lines use the `BuySignal` and `SellSignal` variables to generate trading signals. When `BuySignal` is true, the `Buy` variable is set to true, indicating a long position should be taken. Similarly, when `SellSignal` is true, the `Sell` variable is set to true, indicating a short position should be taken. These variables can be used later in the code to generate trades using the `Buy` and `Sell` functions.