I need to get Volume of the day when Close Price crossed SMA50. I use the following formula, but it does not work. It always return current day's volume.
Ref( Volume, BarsSince(Cross(Close, sma(50))) )
Details:
BarsSince(Cross(Close, sma(50))) is to get number of days since the Close price went up and crossed SMA50. This part work fine, results are correct, for instance, 15 days).
if I hard-code Ref(Volume,15), works fine. But if I replace "15" with "BarsSince(Cross(Close, sma(50)))", it will return today's volume.
Please help.
I simplified code and copied below. It still not work for me.
If SMA34 cross SMA55 (when the price at or above SMA55 ) and I need to get volume on the day when the close price went-up and crossed SMA55.
Below example, I know 18 days ago close price crossed SMA55 by counting chart, I hard-coded 18, gets correct volume. However, if I pass the variable (n_days_ago), it return signal day's volume.
May be I do understand conditions of BarsSince or REF well. Please help.
Thanks in advance.
// Signal name: JXHH // "SMA34 Cross SMA55"
JX1 = Ref(high,1) > SMA(55) and Close > SMA(55); //price stable at/above SMA55
JX3 = CROSS(SMA(34),SMA(55)); // A34 Crosses A55
n_days_ago = BarsSince( Cross(Close, SMA(55)) ); // "N days ago that close price crossover SMA55"
//vol = Ref(Volume, BarsSince( Cross(Close, SMA(55)) ) ); //"I need to retreive Volume on the day when close price crossover SMA55"
vol = Ref(Volume, n_days_ago ); //"I need to retreive Volume on the day when price crossover SMA55"
JXHH = JX1 AND JX3;
// "JXHH signal is on US Stock ACXM on Nov. 21, 2014, 18 days ago is Oct.28, 2014"
// 1)
PlotArrow(JXHH, "JXHH: ".n_days_ago, BelowLow, colorGreen); // "Return 18, it is correct"
n_days_ago = BarsSince( Cross(Close, SMA(55)) ); // "N days ago that close price crossover SMA55"
vol = Ref(Volume, n_days_ago ); //"I need to retreive Volume on the day when price crossover SMA55"
plot(vol, "");
Check the "vol" plot on your chart and you will see that it is getting the correct result.
Now, your formula is correct. I am getting correct result. Just double check that the first PlotArrow is returning 18.
Code 1)
n_days_ago = BarsSince( Cross(Close, SMA(55)) ); // "N days ago that close price crossover SMA55"
vol = Ref(Volume, n_days_ago );
plot(n_days_ago, "n_days_ago" , colorBlue, ChartBar, StyleSymbolNone);
Code 2)
n_days_ago = BarsSince( Cross(Close, SMA(55)) ); // "N days ago that close price crossover SMA55"
vol = Ref(Volume, n_days_ago );
plot(vol, "Vol");
I tested plotting both n_days_ago and Vol using above two sets of code. Vol is still always plotting the current day's volume, not as what expected n_days_ago's volume. Use AAPL as example: on Oct. 23, 2015 n_days_ago is 3, code2 should plot 3 days ago's (Oct.20) volume (48778800) rather than current date's (Oct.23) volume (59139600). I'll send you an image file of AAPL.
It is not urgent. I hope that it will not cause too much of your time. Thank you!
I got each days' volume when I plot "vol". I anticipate to get the same Volume for most of days, but I did not. The chart should be like steps rather than curve.
Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.