Calculating and Plotting a Ratio Using AddtoComposite and PlotForeign

I am having an issue with an AFL where I am calculation the ratio between two numbers. In essence, I am looking for the ratio of stocks that are up 25% in the quarter to stocks that are down 25% in the quarter. I have been able to successfully create a plot of each of the numbers (i.e. chart showing the number of stocks up 25% in the quarter). However, when I try to use AddtoComposite with a PlotForeign on the ratio, I keep getting no results. I do get the 505 warning, and have added +1e-9 to the denominator but that does not seem to help. I can't seem to figure out what I am doing wrong after scouring this site and reading the knowledge base and other areas.

I run this code as an explore and then want to use the foreign symbol created to plot the ratio on a chart. On the chart, the values are all -nan(ind), even with the +1e-9.

Looking to the experts on the forum here for help with what I am doing wrong.

//Market Monitor - 25% Quarterly Ratio
//Run as Explore going back for 4-months; apply chart
//Ratio of the 25% Up in QTR and 25% Down in QTR


Ratio25 = ((100 * ((Close - LLV(Close, 63)) / LLV(Close, 63)) >= 25 AND MA(Close, 20) * MA(Volume, 20) >=250000)) 
			/ (((100+1e-9) * ((Close - HHV(Close, 63)) / HHV(Close, 63)) <= (-25) AND MA(Close, 20) * MA(Volume, 20) >=250000));

AddToComposite(Ratio25, "~~25%QtrRatio", "C", atcFlagEnableInExplore | atcFlagResetValues );

PlotForeign("~~25%QtrRatio", "25% Up/Down Ratio", colorGreen, styleLine | styleDots);```

Make a cleaner code to see the light at the end of the tunnel.
And you should rather store numerator and denominator to separate fields and then in the end via SetForeign calculating Ratio. Otherwise you would get wrong ratio.

Now it should work.

//Market Monitor - 25% Quarterly Ratio
//Run as Explore going back for 4-months; apply chart
//Ratio of the 25% Up in QTR and 25% Down in QTR
ma_cond = MA(Close*V, 20)  >= 25000;

hh = HHV(Close, 63);
ll = LLV(Close, 63);

num = 100 * (C - ll) / ll >= 25 AND ma_cond;
denom = 100 * (C - hh) / hh <= -25 AND ma_cond;

AddToComposite(num, atcname = "~~25%QtrRatio", "H", flags = atcFlagEnableInExplore | atcFlagResetValues );
AddToComposite(denom, atcname, "L", flags );

SetForeign(atcname, 2);
Ratio25 = Nz(H / L); 
RestorePriceArrays();

Plot( Ratio25, "25% Up/Down Ratio", colorGreen, styleLine | styleDots );
3 Likes

Thank you so much - Iā€™m new to coding having only dabbled starting in my 40ā€™s. Help is much appreciated - I have learned a lot just by looking at that code and how you have organized it. Thanks again for the help.

@Jeremy

Here is a short update to remove division by zero warning (note: warning is not equal to error) once ATC is created.
38

I use IIf and AlmostEqual because if you just use H / (L+1e-9)
then you will get output in millions region
34

instead of

00

///Market Monitor - 25% Quarterly Ratio
///Run as Explore going back for 4-months; apply chart
///Ratio of the 25% Up in QTR and 25% Down in QTR
/// Original at 
/// @link https://forum.amibroker.com/t/calculating-and-plotting-a-ratio-using-addtocomposite-and-plotforeign/11477
/// fix at
/// @link https://forum.amibroker.com/t/calculating-and-plotting-a-ratio-using-addtocomposite-and-plotforeign/11477/4
ma_cond = MA(C*V, 20) >= 25000;

hh = HHV(Close, 63);
ll = LLV(Close, 63);

num = 100 * (C - ll) / ll >= 25 AND ma_cond;
denom = 100 * (C - hh) / hh <= -25 AND ma_cond;

AddToComposite(num, atcname = "~~25%QtrRatio", "H", flags = atcFlagEnableInExplore | atcFlagResetValues );
AddToComposite(denom, atcname, "L", flags );

SetForeign(atcname, 2);
denom = L+1e-9;
is_equal = AlmostEqual(denom, 1e-9);
Ratio25 = IIf(is_equal, 0, H / denom); 
RestorePriceArrays();

Plot( Ratio25, "25% Up/Down Ratio", colorGreen, styleLine | styleDots );
3 Likes