# 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 );

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. I use IIf and AlmostEqual because if you just use `H / (L+1e-9)`
then you will get output in millions region  ``````///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
/// fix at
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 );