I want to find Up-Down Volume ratio for last 50 days. The ratio is derived from the past 50 trading days by dividing the total volume on up days by the total volume on down days.
So far I tried to code something like this which is improper syntax as Volume is not a function:
Thanks for this conversation. As an addition or idea, here is my solution how I solved that:
I'm not sure if the function with Sum is appropriate in this case. But would also not say that my solution is better. I know there is a big mistake in it (you should not assume no. of bars in "barcount"). But it works for me and has an advantage that it also works on IPO's if there are not already 50 bars available. I also define upday/downday slightly different.
upday = O < C;
downday = O > C;
upvol = 0;
downvol = 0;
for ( i = Max(0, BarCount-50); i < BarCount; i++ )
{
if(upday[i]) {
upvol += V;
} else if (downday[i]) {
downvol += V;
}
udRatio = upvol / downvol;
}
Is this some kind of troll attempt of the month?
Why is the Sum function in post #2 not supposed to be appropriate in case of post #1?
Are you kidding me?
Here is some enlightenment to you... your code is complete applesauce. It is one of the most awful things I have seen in months.
BTW, that code is doing same thing as following one just without pointless barcount loop:
upday = O < C;
downday = O > C;
upvol = LastValue(Sum(upday,50))*V;
downvol = LastValue(Sum(downday,50))*V;
udratio = upvol/downvol;
Do even understand what it is doing?
I'm telling you... what it does is that it simply multiplies a single SAME number with ENTIRE Volume array. What kind of sense is that one supposed to make?
Instead of playing Mr. Smart who (falsely) thinks that he has found out something being extremely awesome (while it is complete opposite of it) and believing that code of post #2 is not appropriate you should rather study that code of post #2 carefully in what it is actually doing.
Wow.... I think I put myself wrong or used the wrong words. I'm sorry, english is not my mother tongue. Otherwise sorry for sharing.
I did not pretend to be a genius! The word "appropriate" was meant in relation to the idea of IBD and not of how you solved it.
As mentioned, I know my solution is not optimal. I tried a different approach similar to your last code. I only switched to the cumbersome for-loop, because I received an "inf" amount for IPO stocks.
I would appreciate it if we could continue to discuss creatively and politely. I will also pay more attention to my choice of words.
Thanks for your post but the code written by @fxshrat is tidy and elegant. It just multiplies the volume array with boolean condition array and that solves it.
For IPO stocks, I added few lines. Pls check if that serves the purpose.
if( BarCount < 50 )
period = BarCount;
else
period = 50;
up = ROC(C,1)>0;
dn = NOT up;
upvol = Sum(up*V,period);
downvol = Sum(dn*V,period);
udratio = upvol/downvol;
Absolutely, the way @fxshrat did, is a much more elegant solution!
The thing about what an update is "O < C" or "ROC(C, 1)>0" is more of a philosophical question.
I like your simple approach with "if" and have adapted mine accordingly.
To get the latest bar, just use "barcount -1". With this change it should also fit for IPO's.
if( BarCount-1 < 50 )
period = BarCount-1;
else
period = 50;
Your loop code is not a solution (neither is my non loop response to your loop version).
A solution is something that makes sense and works (everywhere) and is optimal.
You still don't seem to understand what your loop (and my non-loop code response) are doing.
Both just add up same volume element n-times for entire volume array. n -> up/down occurrences at last bar. (Note: there may occur small differences between loop and non loop code because of floating point rounding).
Anyway here is picture. Maybe then you understand that it makes no sense and is off topic to 1st post in addition.
If you got inf then denominator was zero because of zero down bar occurrences.
So to prevent inf we may use SafeDivide() function of AB 6.35, for example.
You do not need if-else statement.
If you have less bars than set period then: period = Min(50,BarCount-1);
or you may use
period = Min(50,BarIndex()); as once posted by @Tomasz (note: BarIndex() is different to BarCount).
/// @link https://forum.amibroker.com/t/how-to-find-up-down-volume-ratio/22470/12
Version(6.35);// because of SafeDivide function
period = Min(50,BarCount-1);
up = ROC(C,1)>0;
dn = NOT up;
upvol = Sum(up*V,period);
downvol = Sum(dn*V,period);
udratio = SafeDivide(upvol,downvol);
Plot(udratio, "udratio", colorRed);