Number of bars it takes for the price to reach the previous high

I'm trying to write an AFL code that calculates the following:

  • Number of bars it takes for the price to reach the previous high after the RSI(14) dips below 30 (oversold condition).

**I currently have the following code, but I'm encountering errors .

// Define variables
// Removed "var" keyword
int prevHigh = Highest(1); // Stores the previous high price
double rsi = RSI(Close, 14); // Calculates the RSI value
int barsSinceLow = 0; // Tracks bars since the RSI dipped below 30

// Identify oversold condition
if (rsi < 30) {
barsSinceLow = 0; // Reset counter when RSI goes oversold
} else if (Close > prevHigh) {
// Update previous high if a new high is reached
prevHigh = Close;
}

// Track bars since low EVERY bar
barsSinceLow++;

// Display the number of bars on the chart (modify as needed)
if (Close > prevHigh) {
Print("Bars to regain high: " + barsSinceLow, Color.BLUE);
}

this code is generated by a bot I do tried to correct it however it seems there is some understanding of IF is missing Sad

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

1 Like

It appears that your bot has absolutely no knowledge of AFL., because nearly every line of code that you posted includes an error of some sort. Your best bet would be to discard the code here, take some time to go through the tutorials in the AmiBroker Help pages, and try again.

4 Likes

please help me in writing the code I have already tried myself however I get lost , maybe its the age which is hampering me

I have asked the bot again and here is the modified version of the code however i am still getting one error please help fix it


// Calculate RSI
myrsi = RSI(14);

// Initialize array to store bar counts
barCounts = Null;

// Loop over bars
for(i = 0; i < BarCount; i++) {
    // Check if RSI has gone below 30
    if(myrsi[i] < 30) {
        // Find the previous high
        prevHigh = HHV(H,i+1);
        
        // Count the number of bars since the previous high
        for(j = i; j >= 0; j--) {
            if(H[j] == prevHigh) {
                barCounts[i] = i - j;
                break;
            }
        }
    }
}

// Plot the bar counts
Plot(barCounts, "Bar Counts", colorYellow);

// Plot the candlestick chart
Plot(C, "Close", colorDefault, styleCandle);

@mradtke informed you of what best to do but for some reason you have chosen not to take his advice - which makes no sense!

Yet...

Ok please create

Create what?

1 Like

There is no looping required.
All it needs is array processing.

/// @link https://forum.amibroker.com/t/number-of-bars-it-takes-for-the-price-to-reach-the-previous-high/37960/9
function BarsSincePrevHigh(cond, array) {
  // by fxshrat@gmail.com
  valHigh = ValueWhen(cond,array);
  bars = BarsSinceCompare(array, ">", valHigh)-1;
  bars_new = IIf(SumSince(cond, bars==0)==0, bars-ValueWhen(cond,bars), 0);
  NOT_reached = Reverse(ValueWhen(Ref(Reverse(cond),-1), Reverse(bars_new)>0));
  return IIf(Nz(NOT_reached)==0, bars_new, 0);
}

Version(6.40);// minimum AB version

cond = Cross(30, RSI(14));
x = BarsSincePrevHigh(cond, H);

Plot( RSI(14), "RSI(14)", colorRed, styleLine );
PlotGrid(30, colorRed);

Plot( Iif(cond, H, Null), "RSI dip marker", colorRed, styleDots | styleLeftAxisScale, Null, Null, 0, 0 , 10 );
Plot( H, "H-Price", colorGreen, styleLine | styleleftaxisScale );

Plot( x, "bars", colorYellow, styleLine | styleOwnScale);

2 Likes

Thank you so much for the effort
I have a little issue I have only version 6.30 so it does not work
I have not updated and it looks like i am stuck with this version for sometime
it would be great if the code is made compatiable with previous versions as well.

Thank you

So upgrade - issue fixed!

As I mentioned I am stuck with this version as of now so try to address the issue than so obvious SOLUTION

How are you 'stuck' with your current version?

The world is changing - specially when it comes to programming
From 1957 fotran to modern day python nothing has changed it still takes a nerd to understand what is written the code and we are very lazy in documentation .

Now I see Amibroker getting the power of AI code generators in less than a year and it is going to be a huge shift from the current version .

I am keeping my powder dry to upgrade amibroker to that version in the meanwhile for any ideas which I am not able to code you guys have excellent coding skills and I am a fan of you guys.

I have discussed the silver breakout with bard and bing in october and it gave me the excact timeframe of 6 months based on last 100 years of data to touch the previous highs or there abouts .

Technical trading is simply based on what has happened before to "predict" what may happen next .

I cannot underestimate the power of these generative ai bots and in less than couple of years I see Amibroker coming up with ai powered version and that will be move I am waiting for.

In the meanwhile let me tell you that find the finding the number of bars of previous hhv from the rsi going below xx is just a part of that prompt which made bard and bing to compute with accuracy about the price movement of silver.

Holy grail isn;t it ?

What nonsense!

There is no Holy Grails in trading! They simply don't exist! Good news is one can still make good money without them - that's why we have AmiBroker.

The thing is that each version makes things easier than before. @fxshrat generous code contribution takes advantage of BarsSinceCompare function that was added in 6.40.
Implementing the same functionality without that function is combersome and slow (as it would involve double nested loop).

New functions that arrive with each version are great incentive to upgrade. At the cost of one dinner at fancy restaurant you save yourself from headache

2 Likes

So anyone from old school (version) who would like to make it work (the code) the old version way?

Can we not use barssince().?

You have already been given the solution...

I appreciate your response however as I am looking for a solution on older versions since I am stuck with version 6.3 so it would be great that someone takes pain to rewrite it in older version

Thanks