Page 1 of 1

My First Script - Constant % ROI

Posted: Sun Aug 27, 2023 11:04 pm
by cageycruz
My first Pine Script.

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cageycruz 

// This draws a line overlaid on the chart showing a hypothetical asset returning a constant fixed rate of return. The annual percentage ROI can be entered as an input. 

// So for example you could enter a target ROI and see how an asset compares to it. 

// On a logarithmic chart this will plot a straight line. On linear charts it will plot an exponential curve. 

//@version=5
indicator("Const%ROI", overlay=true)

ROIInput = input(7, "Annual ROI (in percent)")
var leftTime = chart.left_visible_bar_time
var leftClose = close

leftClose := (time == leftTime) ? close : leftClose

plot(leftClose * math.pow((1+ROIInput/100), (time_close("") - leftTime) / 365.25 / 24 / 60 / 60 / 1000), color = color.rgb(246, 87, 246, 30))

This image shows the Constant % ROI indicator overlaid on a 30 year logarithmic chart of APPL showing how it's price compares to 7% annual return.
Image

Re: My First Script - Constant % ROI

Posted: Tue Aug 29, 2023 3:28 am
by DrVats
365.25 ? days ah?
But for leap year?

Re: My First Script - Constant % ROI

Posted: Thu Aug 31, 2023 10:59 pm
by cageycruz
365.25 yes is an approximation for leap year. The error is small, less then an pixel on charts at reasonable scales..