To achieve the desired output in AmiBroker, you can use the BarsSince function in combination with conditional statements to track when the close price crosses the Opening Range (OR) high or low. Here's a sample AFL (AmiBroker Formula Language) code snippet that demonstrates how to implement this:
// Define the states
OR_state_bullish = 0;
OR_state_bearish = 1;
OR_state_neutral = 2;
OR_state_range = 3;
// Assuming OR_high and OR_low are defined somewhere in your code
// For demonstration, let's assume OR_high and OR_low are simple moving averages of High and Low for a certain period
// You should replace these with your actual OR_high and OR_low definitions
period = Param("OR Formation Period", 10, 1, 100);
OR_high = Ref(HHV(High, period), -1); // High of the formation period
OR_low = Ref(LLV(Low, period), -1); // Low of the formation period
// Determine OR_state
OR_state = IIf(C > OR_high, OR_state_bullish,
IIf(C < OR_low, OR_state_bearish,
IIf(C >= OR_low AND C <= OR_high, OR_state_neutral, OR_state_range)));
// Calculate BarsSince for crossing OR_high and OR_low
BarsSinceCrossedHigh = BarsSince(C < OR_high);
BarsSinceCrossedLow = BarsSince(C > OR_low);
// To show 0 if the close has never crossed OR high or OR low since the formation of OR
BarsSinceCrossedHigh = IIf(BarsSinceCrossedHigh == Null, 0, BarsSinceCrossedHigh);
BarsSinceCrossedLow = IIf(BarsSinceCrossedLow == Null, 0, BarsSinceCrossedLow);
// Display the values when OR_state is neutral
PlotShapes(IIf(OR_state == OR_state_neutral, shapeSmallCircle, shapeNone), colorCustom12);
BarsSinceHighText = "Bars Since High: " + IIf(OR_state == OR_state_neutral, BarsSinceCrossedHigh, Null);
BarsSinceLowText = "Bars Since Low: " + IIf(OR_state == OR_state_neutral, BarsSinceCrossedLow, Null);
PlotText(IIf(OR_state == OR_state_neutral, BarsSinceHighText, Null), BarCount - 1, L, colorBlack);
PlotText(IIf(OR_state == OR_state_neutral, BarsSinceLowText, Null), BarCount - 1, L + 1, colorBlack);
// Optionally, you can also explore using AddColumn or AddTextColumn for displaying these values in a scan or exploration
// For example:
// if (Status("action") == actionExplore) {
// AddColumn(BarsSinceCrossedHigh, "BarsSinceHigh", 1.0);
// AddColumn(BarsSinceCrossedLow, "BarsSinceLow", 1.0);
// }
This code snippet does the following:
- Defines the
OR_state variable based on the close price relative to OR_high and OR_low.
- Calculates the number of bars since the close was less than
OR_high and greater than OR_low using BarsSince.
- Adjusts
BarsSinceCrossedHigh and BarsSinceCrossedLow to show 0 if there's never been a cross (i.e., when BarsSince returns Null).
- Plots the values of
BarsSinceCrossedHigh and BarsSinceCrossedLow on the chart when OR_state is neutral.
You can adjust the OR_high and OR_low definitions according to your actual logic for determining the Opening Range. The example provided assumes a simple moving average for demonstration purposes.
To display the values in a more readable format, you might consider using AddColumn or AddTextColumn within an exploration (actionExplore) if you're analyzing the data in a tabular format.