Click here to Login





                                                   Can REF's period be dynamic?

  0

0
Win
2015-11-07 22:57:08


Hi,

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.

Thank you!
Weimin



QuantShare
2015-11-09 03:21:20

  0

Hi Weimin,

It does work for me. It returns today's volume on the day the crossover happens. Otherwise, it returns the volume on the crossover day.



Win
2015-11-09 20:12:56

  0

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"

// 2)
PlotArrow(JXHH, "JXHH: ".Ref(Volume, 18), BelowLow, colorGreen); // " Return 423900, correct

//3)
PlotArrow(JXHH, "JXHH: ".vol, BelowLow, colorGreen); // " Return 507700, volume on Nov. 21, 2014, incorrect"



QuantShare
2015-11-10 02:54:20

  0

Just make it simple and test something like:

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.




Win
2015-11-10 19:25:51

  0

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!



QuantShare
2015-11-11 02:56:47

  0

Do you mean that when plotting "vol", you are getting a different value on each bar and each bar value is exactly the same as volume?



Win
2015-11-11 11:31:29

  0

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.


QuantShare
2015-11-13 02:44:20

  0

Yes, it should be steps. What if you plot "n_days_ago"? What do you get?


Win
2015-11-13 09:03:01

  0

I get triangles if I plot "n_days_ago". By the way, my QS version is 3.1.1.


QuantShare
2015-11-14 03:31:27

  0

That explains everything. You need a recent version for the "ref" to properly handle dynamic periods.


No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Holding period return - classifying positions
Inside Period
Highest and Lowest value over a dynamic period
Triple Period Indicator
Short Indicator

How-to Lessons
How to optimize an indicator in your trading system
How to plot a stock using different periods in the same chart
How to dynamically update the settings of an indicator
How to create a screen
How to detect stocks trading near all time highs

Related Forum Threads
How can i download additional data not within the standard time p...
Can you share your main (candlestick chart) formula? Here's mine!
Can I update the sector and industry information in the stock dat...
9 period sma added willr indicator
How can I incorporate earnings release data into my backtests scr...

Blog Posts
5 position sizing techniques you can use in your trading system
Statistical data analysis of time series
How to Calculate and Display the Bid/Ask Spread on a Chart
Backtesting All Candlestick Patterns in 10 Minutes
Backtesting a Strategy Based on Bond and Stock Index ETFs









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



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.