TradingView
logan3
2 Th05 2021 08:17

Return (Percent Change) 

Bitcoin / U.S. dollarBitstamp

Mô tả

This Script displays Regular or Log Returns as either a line or histogram and labels the current bar.
If something other than price is selected as the source, the result is percent change with a positive or negative slope.
If a moving average of price is used as the source, the result is analogous to a strength index
Other options include a look-back period adjustment (the default is 1),
smoothing results by converting to an EMA, and
Bollinger Bands with Length and Standard Deviation inputs.

Phát hành các Ghi chú

Name Shortened to "Return"

Phát hành các Ghi chú

precision increased to two decimal places

Phát hành các Ghi chú

// This Script displays Regular or Log Returns as either a line or histogram and labels the current bar.
// Additionally, an "ArcTan" method is available which allows the slope of a line that crosses zero to be displayed.
// If a moving average of price is used as the source, the result is analogous to a strength index
// Other options include smoothing by converting to an EMA of returns,
// Bollinger Bands with SMA for the Length, and Standard Deviation for the Width.
// When the BB Basis is positive the bands appear green, and when negative they are red.
// if you set the BB basis to one, the color of the BB histogram will indicate the slope only.

Phát hành các Ghi chú

Two Decimal Places for Absolute Change Label

Phát hành các Ghi chú

corrected log input type spelling

Phát hành các Ghi chú

...

Phát hành các Ghi chú

Option to display absolute values was added.

Bollinger Bands can be toggled on/off.

Phát hành các Ghi chú

Script has been updated to allow Return and Basis to be expressed as several different types of moving average. From the Pinescript Reference:

sma: The sma function returns the moving average, that is the sum of last y values of x, divided by y.
ema: The ema function returns the exponentially weighted moving average. In ema weighting factors decrease exponentially. It calculates by using a formula: EMA = alpha * x + (1 - alpha) * EMA[1], where alpha = 2 / (y + 1)
wma: The wma function returns weighted moving average of x for y bars back. In wma weighting factors decrease in arithmetical progression.
rma: Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length.
swma: Symmetrically weighted moving average with fixed length: 4. Weights: [1/6, 2/6, 2/6, 1/6].
vwma: The vwma function returns volume-weighted moving average of x for y bars back. It is the same as: sma(x * volume, y) / sma(volume, y)

Bands may be displayed as:
std dev: Standard Deviation (typical multiple value=2)
atr: Average True Range (typical multiple value = 1) RMA of true range. True range is max(high - low, abs(high - close[1]), abs(low - close[1]))

Here's an example show Bi-Weekly Log Returns of the entire history of bitcoin:


Here's an example of Daily Reg Returns in Absolute Price and the indicator being used to show the 30SMA Slope:

Phát hành các Ghi chú

Comments and Example updated

Corrected example of Daily Reg Returns in Absolute Price and the indicator being used to show the 30SMA Slope:

Phát hành các Ghi chú

Added functionality to magnify basis line (display only)

Phát hành các Ghi chú

corrected idea image

Phát hành các Ghi chú

formatting

Phát hành các Ghi chú

corrected versioning

Phát hành các Ghi chú

formatting

Phát hành các Ghi chú

Script has been updated to include wicks, so you can now see candles normalized to zero.

Two EMA's of the basis also have been added as an option, as well as the ability to magnify the plots (display only).
Bình luận
Bull_Bear003
This is a great script. Thank you for your work.

I have 1 question though, what does basis mean?
logan3
@Bull_Bear003, thanks, if Return MA Length = 1, basis is the average return. If Return MA Length > 1, basis is an average of the average return. Because it does not stray as far from zero, you could try magnifying it 5x or 10x to better visualize
Bull_Bear003
@logan3, I see. Help me to understand this, so if Return MA Length = 1 (Log Return), then basis length (EMA) of 10 means the arithmetic average return of the log returns of previous 10 periods?
logan3
@Bull_Bear003, if Return type is log, Return MA Length = 1, Basis Type = EMA, and Basis Length = 10, the basis displays the exponential moving average of the last 10 log returns. So, yes, except it's an exponential moving avg. I think of an arithmetic average as "SMA" or Simple Moving Average
Bull_Bear003
@logan3, Yes that's my bad. I meant Exponential Average Return of the log returns.

However, when I set both Return MA Length (EMA, Log type) and Basis Length (Type: EMA) to 1, the basis is showing a greater number on the positive side and a smaller number on the negative side than the 1-period EMA Return.
Correct me if I'm wrong but shouldn't both be the same since both are EMA type with period length of 1?
logan3
@Bull_Bear003, thanks, but I don't understand what you mean by "greater number on the positive side and a smaller number on the negative side." If you set both the Return MA length and Basis length to 1, there are no bands, and there's nothing to average. All you would see is normalized candles and the return of the current bar as it changes. Feel free to send me a private message if you like, and include a screen capture of your settings if the system lets you. Either way happy to take a closer look at something if it needs a second check
Bull_Bear003
@logan3, I can’t send you a private message. DM me?
logan3
SG_Investor
Thanks
Bull_Bear003
Hey again, Logan. I think there's something wrong with the script. Just wanted to bring your attention to the mismatch of log return of closing prices.

The settings I'm using for this is;
- Return Type: Log
- Return Length: 1
Other settings don't matter as no errors were found for High and Low log returns.

So I'm looking at GBPUSD (FXCM) daily closing data on Monday, 12th June 2023 and they are as follows:
Current close = 1.25053
Previous close = 1.25787
Simple % chg = -0.58%

If my math checks out, it should return -0.58524% but what your indicator is showing is -0.5660% which is wrong because log returns on the negative side is supposed to be a smaller number than simple returns ie. log r<-0.58%

Can you check on this issue?
Thêm nữa