Click here to Login

                                                   Plotting the closing value one year ago? Two years ago? Three?


Josh McCormick
2020-11-24 17:21:11

I'm banging my head against the wall on this one. I'm wanting to average the current symbol's closing price over the past six years. But to start, I'm just trying to plot the current closing price, the previous year's closing price, and so on. But I can't seem to come up with the function which says, "take the current date, subtract a year, and plot the close on that date". How should I be coding this?

// Plots the stock price of the current year and the past five years.

Y1_close=ValueOn (close,Day(),Month(),Year()-1);
Y2_close=ValueOn (close,Day(),Month(),Year()-2);
Y3_close=ValueOn (close,Day(),Month(),Year()-3);
Y4_close=ValueOn (close,Day(),Month(),Year()-4);
Y5_close=valuewhen(stringequal(date(), month()."/".day()."/".year()-5), close);

Plot (Y0_close,"Year 0",colorBlack,ChartSmoothLine,StyleWidth2);
Plot (Y1_close,"Year -1",colorRed,ChartSmoothLine,StyleWidth2);
Plot (Y2_close,"Year -2",colorOrange,ChartSmoothLine,StyleWidth2);
Plot (Y3_close,"Year -3",colorGreen,ChartSmoothLine,StyleWidth2);
Plot (Y4_close,"Year -4",colorBlue,ChartSmoothLine,StyleWidth2);
Plot (Y5_close,"Year -5",colorViolet,ChartSmoothLine,StyleWidth2);

2020-12-03 12:58:59


Best Answer
Here is how:
b1 = barssince(year() != ref(year(), 1));
y1 = close[b1 + 1];
plot(y1, "");

If you want to plot the previous year value for every bar. Say on May 1, 2020, you want to plot the value of May 1, 2019 then you will need to create a custom function that would search for the previous year index within the date array and then use "ref" to reference the previous close.

Josh McCormick
2020-12-03 15:20:02


That's smart. I get what you're doing there.

Josh McCormick

No more messages


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"


Trading Items
PREV - Previous value of the same formula
Measure the value of the stock market using the Q-Ratio valuation...
Foreign Exchange Value of the Dollar - Nominal Indexes
Output metric for each trading year
Year to Date Change Indicator

How-to Lessons
How to quickly select stocks based on the last value of a databas...
How to plot the ratio of two stocks
How to plot two different time frames in the same chart
How to display two stocks on the same chart
How to change volume bar colors when the price closes down

Related Forum Threads
One result by the screener
Replacing value for the last 100 bars (end of day data)
Plotting the data from custom -Intraday data base
How to get the median closing price?
STOP/Profit targets based on the indicator value at the time of e...

Blog Posts
How to Select the Best Market Indicator for your Trading System
How to download the history of dividend payments for stocks in th...
How to Use the Percentage Scale
Introduction to the Grid Tool
Optimizing the Strategy Equity Curve

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2023
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
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.