├── .gitignroe ├── LICENSE ├── README.md ├── ausrod.pine ├── dcacb.pine ├── gen.dcacb.sh ├── img ├── ausrod.png ├── dcacb_btcusd_continuous.png ├── dcacb_btcusd_continuous_log.png ├── dcacb_btcusd_rolling.png ├── dcacb_vti_continuous.png └── dcacb_vti_rolling.png └── temp.pine /.gitignroe: -------------------------------------------------------------------------------- 1 | temp.pine -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018, Adam Eivy 2 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 3 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TradingView Custom Pine Scripts 2 | 3 | ## DCACB - Dollar Cost Average Cost Basis 4 | > Shows the cumulative cost-basis for buying a fixed $ quantity of the asset over various plots: `30 days`, `60`, `90`, `120`, `180`, and `1-6 years` 5 | > - when the current price is profitable for the DCA plot, fills red 6 | > - when it will reduce the cost-basis for a DCA investor to buy more, turns green 7 | > - draws heavier support lines for longer term DCA investors 8 | > - intensifies colors to show where larger pools of players stand with DCA profitability 9 | > - has a rolling window option (default is false/static) 10 | 11 | Note: since pinescript doesn't have arrays/collections and you can't plot within a for loop anyway, this repo contains `gen.dcacb.sh`, which generates the repetitive pinescript using `bash`. 12 | 13 | ![VTI Continous](img/dcacb_vti_continuous.png) 14 | ![VTI Rolling](img/dcacb_vti_rolling.png) 15 | ![BTCUSD Continous](img/dcacb_btcusd_continuous.png) 16 | ![BTCUSD Continous Log Scale](img/dcacb_btcusd_continuous_log.png) 17 | ![BTCUSD Rolling](img/dcacb_btcusd_rolling.png) 18 | 19 | ## AUSROD - atomantic ultimate stochastic RSI of Destiny 20 | > A combined indicator, overlapping the following three indicators 21 | > - RSI (with option to add second timeframe) 22 | > - Stochastic RSI 23 | > - Ultimate Oscillator 24 | ![Alt text](img/ausrod.png) 25 | 26 | # Making Money From My Work? 27 | ⚡ Lightning Network Satoshi Tips Accepted https://tippin.me/@antic 28 | > With the advent of Bitcoin Lightning Network, you can tip me as little as 1 satoshi (.00000001 $BTC). 1000 satoshi is about $.04 as of this writing. Any amount is appreciated :) 29 | 30 | Don't have Lightning? 31 | - [Blue Wallet (for iOS and Android)](https://bluewallet.io/) 32 | - [Eclair Wallet (for Android)](https://play.google.com/store/apps/details?id=fr.acinq.eclair.wallet.mainnet2) 33 | 34 | 35 | # Contributing 36 | I welcome pull-requests. Feel free to submit Issues on this project or submit your own modifications or scripts -------------------------------------------------------------------------------- /ausrod.pine: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study(title="atomantic ultimate stochastic RSI oscillator of destiny", precision=0) 3 | 4 | // taken from https://github.com/atomantic/pine_scripts 5 | // A combined indicator, overlapping the following three indicators 6 | // - RSI (with option to add second timeframe) 7 | // - Stochastic RSI 8 | // - Ultimate Oscillator 9 | // Lightning Network Satoshi Tips Accepted https://tippin.me/@antic 10 | 11 | // Configurable Inputs 12 | src = input(close, title="Source") 13 | len = input(14, minval=1, title="Length") 14 | upLine = input(70, minval=50, maxval=90, title="Upper Line Value?") 15 | lowLine = input(30, minval=10, maxval=50, title="Lower Line Value?") 16 | sml = input(true, title="Show Mid Line?") 17 | sbh = input(true, title="Show Background Highlights When RSI is Above/Below High/Low Lines?") 18 | sch = input(true, title="Show Background Highlights When RSI Cross?") 19 | sl = input(true, title="Show 'B' and 'S' Letters When RSI Crosses High/Low Line?") 20 | useCurrentRes = input(true, title="Use Current Chart Resolution?") 21 | resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="60") 22 | ssRSI = input(false, title="Show 2nd RSI?") 23 | resCustom2 = input(title="Use 2nd RSI? Check Box Above", type=resolution, defval="D") 24 | useCurrentRes2 = input(false, title="Use 2nd RSI Plot On Samet Timeframe?") 25 | len2 = input(14, minval=1, title="2nd RSI Length") 26 | 27 | res = useCurrentRes ? period : resCustom 28 | res2 = useCurrentRes2 ? period : resCustom2 29 | 30 | up = rma(max(change(src), 0), len) 31 | down = rma(-min(change(src), 0), len) 32 | rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) 33 | outRSI = security(tickerid, res, rsi) 34 | 35 | up2 = rma(max(change(src), 0), len2) 36 | down2 = rma(-min(change(src), 0), len2) 37 | rsi2 = down2 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up2 / down2)) 38 | outRSI2 = security(tickerid, res2, rsi2) 39 | 40 | aboveLine = outRSI > upLine ? 1 : 0 41 | belowLine = outRSI < lowLine ? 1 : 0 42 | crossUp = outRSI[1] < lowLine and outRSI > lowLine ? 1 : 0 43 | crossDn = outRSI[1] > upLine and outRSI < upLine ? 1 : 0 44 | 45 | bgcolor(sbh and aboveLine ? color(red,70) : na, title="RSA Above Line") 46 | bgcolor(sbh and belowLine ? color(green,70) : na, title="RSA Below Line") 47 | bgcolor(sch and crossUp ? color(#33dd88,40) : na, title="RSA Cross Up") 48 | bgcolor(sch and crossDn ? color(red,40) : na, title="RSA Cross Down") 49 | 50 | plot(outRSI, title="RSI", style=line, linewidth=3, color=purple) 51 | plot(ssRSI and outRSI2 ? outRSI2 : na, title="2nd RSI - Different Time Frame?", style=linebr, linewidth=4, color=orange) 52 | p1 = plot(upLine, title= "Upper Line", style=solid, linewidth=3, color=red) 53 | p2 = plot(lowLine, title= "Lower Line", style=solid, linewidth=3, color=green) 54 | plot(sml and 50 ? 50 : na, title="Mid Line", style=linebr, linewidth=2, color=gray) 55 | plotchar(sl and crossUp ? crossUp : na, title="Buy Signal", char='B', location=location.bottom, color=white, transp=0, offset=0) 56 | plotchar(sl and crossDn ? crossDn : na, title="Sell Signal", char='S', location=location.top, color=red, transp=0, offset=0) 57 | fill(p1, p2, color=silver, transp=60) 58 | 59 | 60 | // Stochastic RSI 61 | smoothK = input(3, minval=1) 62 | smoothD = input(3, minval=1) 63 | lengthRSI = input(14, minval=1) 64 | lengthStoch = input(14, minval=1) 65 | stochastic_src = input(close, title="RSI Source") 66 | 67 | rsi1 = rsi(stochastic_src, lengthRSI) 68 | k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) 69 | d = sma(k, smoothD) 70 | plot(k, color=blue) 71 | plot(d, color=red) 72 | h0 = hline(80) 73 | h1 = hline(20) 74 | fill(h0, h1, color=silver, transp=60) 75 | 76 | // Ultimate Oscillator 77 | length7 = input(7, minval=1), length14 = input(14, minval=1), length28 = input(28, minval=1) 78 | average(bp, tr_, length) => sum(bp, length) / sum(tr_, length) 79 | high_ = max(high, close[1]) 80 | low_ = min(low, close[1]) 81 | bp = close - low_ 82 | tr_ = high_ - low_ 83 | avg7 = average(bp, tr_, length7) 84 | avg14 = average(bp, tr_, length14) 85 | avg28 = average(bp, tr_, length28) 86 | out = 100 * (4*avg7 + 2*avg14 + avg28)/7 87 | plot(out, color=red, title="UO", linewidth = 4) -------------------------------------------------------------------------------- /dcacb.pine: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study("Dollar Cost Average Cost Basis", overlay=true) 3 | // Shows the cost-basis for dollar cost averaging 4 | // note: this code was generated using Bash because pinescript does not have arrays/collections 5 | // and you can't run plot within a for loop :( 6 | // code here: https://github.com/atomantic/pine_scripts 7 | // Lightning Network Satoshi Tips Accepted https://tippin.me/@antic 8 | 9 | price=input(close, title="Source") 10 | // plot the price for use in color fills 11 | plot_price = plot(price) 12 | color_fill_sell = #a20000 13 | color_fill_buy = #017d13 14 | color_fill_transp = input(85, title="Fill Transparency") 15 | rolling_window = input(defval=false, type=bool, title="Rolling Window?") 16 | 17 | time_delta = (timenow - time) 18 | milli_interval = 1000 * 60 * 60 * 24 * interval 19 | dollars = 1000 20 | 21 | milli_2190 = milli_interval * 2190 22 | within_2190 = time_delta < milli_2190 23 | total_2190 = 0.0 24 | spent_2190 = 0 25 | quant_2190 = dollars/price // how many fractions/units bought this period 26 | if rolling_window 27 | spent_2190 := within_2190 ? nz(spent_2190[1])+dollars : 0 28 | quant_2190 = within_2190 ? dollars/price : 0.0 29 | total_2190 := nz(total_2190[1])+quant_2190 30 | else 31 | for i = 1 to 2190 32 | if quant_2190[i] 33 | spent_2190 := spent_2190+dollars 34 | total_2190 := total_2190+nz(quant_2190[i], 0) 35 | basis_2190 = spent_2190/total_2190 36 | plot_2190 = plot(basis_2190, linewidth=8, color=#BB8CFF, title="2190 days of DCA") 37 | fill(plot1=plot_2190, plot2=plot_price, color=price > basis_2190 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 38 | 39 | 40 | milli_1825 = milli_interval * 1825 41 | within_1825 = time_delta < milli_1825 42 | total_1825 = 0.0 43 | spent_1825 = 0 44 | quant_1825 = dollars/price // how many fractions/units bought this period 45 | if rolling_window 46 | spent_1825 := within_1825 ? nz(spent_1825[1])+dollars : 0 47 | quant_1825 = within_1825 ? dollars/price : 0.0 48 | total_1825 := nz(total_1825[1])+quant_1825 49 | else 50 | for i = 1 to 1825 51 | if quant_1825[i] 52 | spent_1825 := spent_1825+dollars 53 | total_1825 := total_1825+nz(quant_1825[i], 0) 54 | basis_1825 = spent_1825/total_1825 55 | plot_1825 = plot(basis_1825, linewidth=7, color=#AA7BEE, title="1825 days of DCA") 56 | fill(plot1=plot_1825, plot2=plot_price, color=price > basis_1825 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 57 | 58 | 59 | milli_1460 = milli_interval * 1460 60 | within_1460 = time_delta < milli_1460 61 | total_1460 = 0.0 62 | spent_1460 = 0 63 | quant_1460 = dollars/price // how many fractions/units bought this period 64 | if rolling_window 65 | spent_1460 := within_1460 ? nz(spent_1460[1])+dollars : 0 66 | quant_1460 = within_1460 ? dollars/price : 0.0 67 | total_1460 := nz(total_1460[1])+quant_1460 68 | else 69 | for i = 1 to 1460 70 | if quant_1460[i] 71 | spent_1460 := spent_1460+dollars 72 | total_1460 := total_1460+nz(quant_1460[i], 0) 73 | basis_1460 = spent_1460/total_1460 74 | plot_1460 = plot(basis_1460, linewidth=6, color=#996ADD, title="1460 days of DCA") 75 | fill(plot1=plot_1460, plot2=plot_price, color=price > basis_1460 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 76 | 77 | 78 | milli_1095 = milli_interval * 1095 79 | within_1095 = time_delta < milli_1095 80 | total_1095 = 0.0 81 | spent_1095 = 0 82 | quant_1095 = dollars/price // how many fractions/units bought this period 83 | if rolling_window 84 | spent_1095 := within_1095 ? nz(spent_1095[1])+dollars : 0 85 | quant_1095 = within_1095 ? dollars/price : 0.0 86 | total_1095 := nz(total_1095[1])+quant_1095 87 | else 88 | for i = 1 to 1095 89 | if quant_1095[i] 90 | spent_1095 := spent_1095+dollars 91 | total_1095 := total_1095+nz(quant_1095[i], 0) 92 | basis_1095 = spent_1095/total_1095 93 | plot_1095 = plot(basis_1095, linewidth=5, color=#8859CC, title="1095 days of DCA") 94 | fill(plot1=plot_1095, plot2=plot_price, color=price > basis_1095 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 95 | 96 | 97 | milli_730 = milli_interval * 730 98 | within_730 = time_delta < milli_730 99 | total_730 = 0.0 100 | spent_730 = 0 101 | quant_730 = dollars/price // how many fractions/units bought this period 102 | if rolling_window 103 | spent_730 := within_730 ? nz(spent_730[1])+dollars : 0 104 | quant_730 = within_730 ? dollars/price : 0.0 105 | total_730 := nz(total_730[1])+quant_730 106 | else 107 | for i = 1 to 730 108 | if quant_730[i] 109 | spent_730 := spent_730+dollars 110 | total_730 := total_730+nz(quant_730[i], 0) 111 | basis_730 = spent_730/total_730 112 | plot_730 = plot(basis_730, linewidth=4, color=#7748BB, title="730 days of DCA") 113 | fill(plot1=plot_730, plot2=plot_price, color=price > basis_730 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 114 | 115 | 116 | milli_365 = milli_interval * 365 117 | within_365 = time_delta < milli_365 118 | total_365 = 0.0 119 | spent_365 = 0 120 | quant_365 = dollars/price // how many fractions/units bought this period 121 | if rolling_window 122 | spent_365 := within_365 ? nz(spent_365[1])+dollars : 0 123 | quant_365 = within_365 ? dollars/price : 0.0 124 | total_365 := nz(total_365[1])+quant_365 125 | else 126 | for i = 1 to 365 127 | if quant_365[i] 128 | spent_365 := spent_365+dollars 129 | total_365 := total_365+nz(quant_365[i], 0) 130 | basis_365 = spent_365/total_365 131 | plot_365 = plot(basis_365, linewidth=3, color=#6637AA, title="365 days of DCA") 132 | fill(plot1=plot_365, plot2=plot_price, color=price > basis_365 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 133 | 134 | 135 | milli_180 = milli_interval * 180 136 | within_180 = time_delta < milli_180 137 | total_180 = 0.0 138 | spent_180 = 0 139 | quant_180 = dollars/price // how many fractions/units bought this period 140 | if rolling_window 141 | spent_180 := within_180 ? nz(spent_180[1])+dollars : 0 142 | quant_180 = within_180 ? dollars/price : 0.0 143 | total_180 := nz(total_180[1])+quant_180 144 | else 145 | for i = 1 to 180 146 | if quant_180[i] 147 | spent_180 := spent_180+dollars 148 | total_180 := total_180+nz(quant_180[i], 0) 149 | basis_180 = spent_180/total_180 150 | plot_180 = plot(basis_180, linewidth=2, color=#552699, title="180 days of DCA") 151 | fill(plot1=plot_180, plot2=plot_price, color=price > basis_180 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 152 | 153 | 154 | milli_120 = milli_interval * 120 155 | within_120 = time_delta < milli_120 156 | total_120 = 0.0 157 | spent_120 = 0 158 | quant_120 = dollars/price // how many fractions/units bought this period 159 | if rolling_window 160 | spent_120 := within_120 ? nz(spent_120[1])+dollars : 0 161 | quant_120 = within_120 ? dollars/price : 0.0 162 | total_120 := nz(total_120[1])+quant_120 163 | else 164 | for i = 1 to 120 165 | if quant_120[i] 166 | spent_120 := spent_120+dollars 167 | total_120 := total_120+nz(quant_120[i], 0) 168 | basis_120 = spent_120/total_120 169 | plot_120 = plot(basis_120, linewidth=1, color=#441588, title="120 days of DCA") 170 | fill(plot1=plot_120, plot2=plot_price, color=price > basis_120 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 171 | 172 | 173 | milli_90 = milli_interval * 90 174 | within_90 = time_delta < milli_90 175 | total_90 = 0.0 176 | spent_90 = 0 177 | quant_90 = dollars/price // how many fractions/units bought this period 178 | if rolling_window 179 | spent_90 := within_90 ? nz(spent_90[1])+dollars : 0 180 | quant_90 = within_90 ? dollars/price : 0.0 181 | total_90 := nz(total_90[1])+quant_90 182 | else 183 | for i = 1 to 90 184 | if quant_90[i] 185 | spent_90 := spent_90+dollars 186 | total_90 := total_90+nz(quant_90[i], 0) 187 | basis_90 = spent_90/total_90 188 | plot_90 = plot(basis_90, linewidth=1, color=#330477, title="90 days of DCA") 189 | fill(plot1=plot_90, plot2=plot_price, color=price > basis_90 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 190 | 191 | total_90_future_0 = total_90[0]-(total_90/90)+nz(quant_90[0],0) 192 | basis_90_future_0 = spent_90/total_90_future_0 193 | plot(basis_90_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#330477) 194 | 195 | total_90_future_3 = total_90_future_0-(total_90_future_0[0]/90)+nz(quant_90[0],0) 196 | basis_90_future_3 = spent_90/total_90_future_3 197 | plot(basis_90_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray) 198 | 199 | total_90_future_6 = total_90_future_3-(total_90_future_3[0]/90)+nz(quant_90[0],0) 200 | basis_90_future_6 = spent_90/total_90_future_6 201 | plot(basis_90_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray) 202 | 203 | total_90_future_9 = total_90_future_6-(total_90_future_6[0]/90)+nz(quant_90[0],0) 204 | basis_90_future_9 = spent_90/total_90_future_9 205 | plot(basis_90_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray) 206 | 207 | total_90_future_12 = total_90_future_9-(total_90_future_9[0]/90)+nz(quant_90[0],0) 208 | basis_90_future_12 = spent_90/total_90_future_12 209 | plot(basis_90_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray) 210 | 211 | total_90_future_15 = total_90_future_12-(total_90_future_12[0]/90)+nz(quant_90[0],0) 212 | basis_90_future_15 = spent_90/total_90_future_15 213 | plot(basis_90_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray) 214 | 215 | total_90_future_18 = total_90_future_15-(total_90_future_15[0]/90)+nz(quant_90[0],0) 216 | basis_90_future_18 = spent_90/total_90_future_18 217 | plot(basis_90_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray) 218 | 219 | total_90_future_21 = total_90_future_18-(total_90_future_18[0]/90)+nz(quant_90[0],0) 220 | basis_90_future_21 = spent_90/total_90_future_21 221 | plot(basis_90_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray) 222 | 223 | total_90_future_24 = total_90_future_21-(total_90_future_21[0]/90)+nz(quant_90[0],0) 224 | basis_90_future_24 = spent_90/total_90_future_24 225 | plot(basis_90_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray) 226 | 227 | total_90_future_27 = total_90_future_24-(total_90_future_24[0]/90)+nz(quant_90[0],0) 228 | basis_90_future_27 = spent_90/total_90_future_27 229 | plot(basis_90_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray) 230 | 231 | total_90_future_30 = total_90_future_27-(total_90_future_27[0]/90)+nz(quant_90[0],0) 232 | basis_90_future_30 = spent_90/total_90_future_30 233 | plot(basis_90_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray) 234 | 235 | total_90_future_33 = total_90_future_30-(total_90_future_30[0]/90)+nz(quant_90[0],0) 236 | basis_90_future_33 = spent_90/total_90_future_33 237 | plot(basis_90_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray) 238 | 239 | total_90_future_36 = total_90_future_33-(total_90_future_33[0]/90)+nz(quant_90[0],0) 240 | basis_90_future_36 = spent_90/total_90_future_36 241 | plot(basis_90_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray) 242 | 243 | 244 | milli_60 = milli_interval * 60 245 | within_60 = time_delta < milli_60 246 | total_60 = 0.0 247 | spent_60 = 0 248 | quant_60 = dollars/price // how many fractions/units bought this period 249 | if rolling_window 250 | spent_60 := within_60 ? nz(spent_60[1])+dollars : 0 251 | quant_60 = within_60 ? dollars/price : 0.0 252 | total_60 := nz(total_60[1])+quant_60 253 | else 254 | for i = 1 to 60 255 | if quant_60[i] 256 | spent_60 := spent_60+dollars 257 | total_60 := total_60+nz(quant_60[i], 0) 258 | basis_60 = spent_60/total_60 259 | plot_60 = plot(basis_60, linewidth=1, color=#21F366, title="60 days of DCA") 260 | fill(plot1=plot_60, plot2=plot_price, color=price > basis_60 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 261 | 262 | total_60_future_0 = total_60[0]-(total_60/60)+nz(quant_60[0],0) 263 | basis_60_future_0 = spent_60/total_60_future_0 264 | plot(basis_60_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#21F366) 265 | 266 | total_60_future_3 = total_60_future_0-(total_60_future_0[0]/60)+nz(quant_60[0],0) 267 | basis_60_future_3 = spent_60/total_60_future_3 268 | plot(basis_60_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray) 269 | 270 | total_60_future_6 = total_60_future_3-(total_60_future_3[0]/60)+nz(quant_60[0],0) 271 | basis_60_future_6 = spent_60/total_60_future_6 272 | plot(basis_60_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray) 273 | 274 | total_60_future_9 = total_60_future_6-(total_60_future_6[0]/60)+nz(quant_60[0],0) 275 | basis_60_future_9 = spent_60/total_60_future_9 276 | plot(basis_60_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray) 277 | 278 | total_60_future_12 = total_60_future_9-(total_60_future_9[0]/60)+nz(quant_60[0],0) 279 | basis_60_future_12 = spent_60/total_60_future_12 280 | plot(basis_60_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray) 281 | 282 | total_60_future_15 = total_60_future_12-(total_60_future_12[0]/60)+nz(quant_60[0],0) 283 | basis_60_future_15 = spent_60/total_60_future_15 284 | plot(basis_60_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray) 285 | 286 | total_60_future_18 = total_60_future_15-(total_60_future_15[0]/60)+nz(quant_60[0],0) 287 | basis_60_future_18 = spent_60/total_60_future_18 288 | plot(basis_60_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray) 289 | 290 | total_60_future_21 = total_60_future_18-(total_60_future_18[0]/60)+nz(quant_60[0],0) 291 | basis_60_future_21 = spent_60/total_60_future_21 292 | plot(basis_60_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray) 293 | 294 | total_60_future_24 = total_60_future_21-(total_60_future_21[0]/60)+nz(quant_60[0],0) 295 | basis_60_future_24 = spent_60/total_60_future_24 296 | plot(basis_60_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray) 297 | 298 | total_60_future_27 = total_60_future_24-(total_60_future_24[0]/60)+nz(quant_60[0],0) 299 | basis_60_future_27 = spent_60/total_60_future_27 300 | plot(basis_60_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray) 301 | 302 | total_60_future_30 = total_60_future_27-(total_60_future_27[0]/60)+nz(quant_60[0],0) 303 | basis_60_future_30 = spent_60/total_60_future_30 304 | plot(basis_60_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray) 305 | 306 | total_60_future_33 = total_60_future_30-(total_60_future_30[0]/60)+nz(quant_60[0],0) 307 | basis_60_future_33 = spent_60/total_60_future_33 308 | plot(basis_60_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray) 309 | 310 | total_60_future_36 = total_60_future_33-(total_60_future_33[0]/60)+nz(quant_60[0],0) 311 | basis_60_future_36 = spent_60/total_60_future_36 312 | plot(basis_60_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray) 313 | 314 | 315 | milli_30 = milli_interval * 30 316 | within_30 = time_delta < milli_30 317 | total_30 = 0.0 318 | spent_30 = 0 319 | quant_30 = dollars/price // how many fractions/units bought this period 320 | if rolling_window 321 | spent_30 := within_30 ? nz(spent_30[1])+dollars : 0 322 | quant_30 = within_30 ? dollars/price : 0.0 323 | total_30 := nz(total_30[1])+quant_30 324 | else 325 | for i = 1 to 30 326 | if quant_30[i] 327 | spent_30 := spent_30+dollars 328 | total_30 := total_30+nz(quant_30[i], 0) 329 | basis_30 = spent_30/total_30 330 | plot_30 = plot(basis_30, linewidth=1, color=#10E255, title="30 days of DCA") 331 | fill(plot1=plot_30, plot2=plot_price, color=price > basis_30 ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 332 | 333 | total_30_future_0 = total_30[0]-(total_30/30)+nz(quant_30[0],0) 334 | basis_30_future_0 = spent_30/total_30_future_0 335 | plot(basis_30_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#10E255) 336 | 337 | total_30_future_3 = total_30_future_0-(total_30_future_0[0]/30)+nz(quant_30[0],0) 338 | basis_30_future_3 = spent_30/total_30_future_3 339 | plot(basis_30_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray) 340 | 341 | total_30_future_6 = total_30_future_3-(total_30_future_3[0]/30)+nz(quant_30[0],0) 342 | basis_30_future_6 = spent_30/total_30_future_6 343 | plot(basis_30_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray) 344 | 345 | total_30_future_9 = total_30_future_6-(total_30_future_6[0]/30)+nz(quant_30[0],0) 346 | basis_30_future_9 = spent_30/total_30_future_9 347 | plot(basis_30_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray) 348 | 349 | total_30_future_12 = total_30_future_9-(total_30_future_9[0]/30)+nz(quant_30[0],0) 350 | basis_30_future_12 = spent_30/total_30_future_12 351 | plot(basis_30_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray) 352 | 353 | total_30_future_15 = total_30_future_12-(total_30_future_12[0]/30)+nz(quant_30[0],0) 354 | basis_30_future_15 = spent_30/total_30_future_15 355 | plot(basis_30_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray) 356 | 357 | total_30_future_18 = total_30_future_15-(total_30_future_15[0]/30)+nz(quant_30[0],0) 358 | basis_30_future_18 = spent_30/total_30_future_18 359 | plot(basis_30_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray) 360 | 361 | total_30_future_21 = total_30_future_18-(total_30_future_18[0]/30)+nz(quant_30[0],0) 362 | basis_30_future_21 = spent_30/total_30_future_21 363 | plot(basis_30_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray) 364 | 365 | total_30_future_24 = total_30_future_21-(total_30_future_21[0]/30)+nz(quant_30[0],0) 366 | basis_30_future_24 = spent_30/total_30_future_24 367 | plot(basis_30_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray) 368 | 369 | total_30_future_27 = total_30_future_24-(total_30_future_24[0]/30)+nz(quant_30[0],0) 370 | basis_30_future_27 = spent_30/total_30_future_27 371 | plot(basis_30_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray) 372 | 373 | total_30_future_30 = total_30_future_27-(total_30_future_27[0]/30)+nz(quant_30[0],0) 374 | basis_30_future_30 = spent_30/total_30_future_30 375 | plot(basis_30_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray) 376 | 377 | total_30_future_33 = total_30_future_30-(total_30_future_30[0]/30)+nz(quant_30[0],0) 378 | basis_30_future_33 = spent_30/total_30_future_33 379 | plot(basis_30_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray) 380 | 381 | total_30_future_36 = total_30_future_33-(total_30_future_33[0]/30)+nz(quant_30[0],0) 382 | basis_30_future_36 = spent_30/total_30_future_36 383 | plot(basis_30_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray) 384 | -------------------------------------------------------------------------------- /gen.dcacb.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | color=0xBB8CFF 4 | alter=0x111111 5 | futures=(30 60 90) 6 | 7 | echo -n "//@version=3 8 | study(\"Dollar Cost Average Cost Basis\", overlay=true) 9 | // Shows the cost-basis for dollar cost averaging 10 | // note: this code was generated using Bash because pinescript does not have arrays/collections 11 | // and you can't run plot within a for loop :( 12 | // code here: https://github.com/atomantic/pine_scripts 13 | // Lightning Network Satoshi Tips Accepted https://tippin.me/@antic 14 | 15 | price=input(close, title=\"Source\") 16 | // plot the price for use in color fills 17 | plot_price = plot(price) 18 | color_fill_sell = #a20000 19 | color_fill_buy = #017d13 20 | color_fill_transp = input(85, title=\"Fill Transparency\") 21 | rolling_window = input(defval=false, type=bool, title=\"Rolling Window?\") 22 | 23 | time_delta = (timenow - time) 24 | milli_interval = 1000 * 60 * 60 * 24 * interval 25 | dollars = 1000" > dcacb.pine 26 | 27 | linewidth=8 28 | for period in 2190 1825 1460 1095 730 365 180 120 90 60 30 29 | do 30 | color_string=$(echo "$color" | sed 's/0x/#/') 31 | echo -n " 32 | 33 | milli_${period} = milli_interval * ${period} 34 | within_${period} = time_delta < milli_${period} 35 | total_${period} = 0.0 36 | spent_${period} = 0 37 | quant_${period} = dollars/price // how many fractions/units bought this period 38 | if rolling_window 39 | spent_${period} := within_${period} ? nz(spent_${period}[1])+dollars : 0 40 | quant_${period} = within_${period} ? dollars/price : 0.0 41 | total_${period} := nz(total_${period}[1])+quant_${period} 42 | else 43 | for i = 1 to ${period} 44 | if quant_${period}[i] 45 | spent_${period} := spent_${period}+dollars 46 | total_${period} := total_${period}+nz(quant_${period}[i], 0) 47 | basis_${period} = spent_${period}/total_${period} 48 | plot_${period} = plot(basis_${period}, linewidth=${linewidth}, color=${color_string}, title=\"${period} days of DCA\") 49 | fill(plot1=plot_${period}, plot2=plot_price, color=price > basis_${period} ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 50 | " >> dcacb.pine 51 | 52 | if [[ " ${futures[@]} " =~ " ${period} " ]]; then 53 | # note: this isn't quite perfect. ideally, we would want to pop the oldest value from total_${period} 54 | # and add a new value that is the quantity purchased at the realtime price 55 | # this would create a sliding window that gets closer to the current closing price 56 | # however, I haven't found a way to do this, so we are simply subtracting the average quantity 57 | # then adding a new quantity based on the latest (it's not quite right but it's close if the price is somewhat stable) 58 | echo -n " 59 | total_${period}_future_0 = total_${period}[0]-(total_${period}/$period)+nz(quant_${period}[0],0) 60 | basis_${period}_future_0 = spent_${period}/total_${period}_future_0 61 | plot(basis_${period}_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=${color_string}) 62 | " >> dcacb.pine 63 | 64 | for i in {3..36} 65 | do 66 | if [[ $((i % 3)) == 0 ]]; then 67 | prev="$(($i-3))" 68 | echo -n " 69 | total_${period}_future_${i} = total_${period}_future_${prev}-(total_${period}_future_${prev}[0]/${period})+nz(quant_${period}[0],0) 70 | basis_${period}_future_${i} = spent_${period}/total_${period}_future_${i} 71 | plot(basis_${period}_future_${i}, show_last=1, style=line, trackprice=false, offset=${i}, linewidth=2, color=gray) 72 | " >> dcacb.pine 73 | fi 74 | done 75 | fi 76 | 77 | if (( linewidth > 1 )); then 78 | ((linewidth--)) 79 | fi 80 | new_color_dec=$(( $color - $alter )) 81 | color=`printf "0x%X\n" $new_color_dec` 82 | 83 | done -------------------------------------------------------------------------------- /img/ausrod.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/ausrod.png -------------------------------------------------------------------------------- /img/dcacb_btcusd_continuous.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/dcacb_btcusd_continuous.png -------------------------------------------------------------------------------- /img/dcacb_btcusd_continuous_log.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/dcacb_btcusd_continuous_log.png -------------------------------------------------------------------------------- /img/dcacb_btcusd_rolling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/dcacb_btcusd_rolling.png -------------------------------------------------------------------------------- /img/dcacb_vti_continuous.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/dcacb_vti_continuous.png -------------------------------------------------------------------------------- /img/dcacb_vti_rolling.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomantic/pine_scripts/d9ea4ff17f2c7cdfa6d2d658705304ac7fc0a4a0/img/dcacb_vti_rolling.png -------------------------------------------------------------------------------- /temp.pine: -------------------------------------------------------------------------------- 1 | 2 | plot(n, color=na, style=line) 3 | AB(x1,x2,y1,y2) => n < x1 or n > x2 ? na : y1 + (y2 - y1) / (x2 - x1) * (n - x1) 4 | plot(AB(valuewhen(barstate.islast, close, 0),10136,3819,3893), color=#ff00ff, linewidth=1, style=line, transp=0) 5 | plot(AB(valuewhen(barstate.islast, close, 0),10136,3966.5,3931), color=#ff00ff, linewidth=1, style=line, transp=0) 6 | 7 | current_price = price 8 | total_1825_future = nz(valuewhen(barstate.islast, close, 0)) 9 | spent_1825_future = nz(valuewhen(barstate.islast, close, 0)) 10 | if(barstate.islast) 11 | 12 | 13 | // draw the future 14 | // assuming the price stays the same 15 | // but extended from where all the DCA lines left off 16 | quant_1825_future = dollars/price // how many fractions/units bought this period 17 | for i = 0 to 1825 18 | spent_1825_future := spent_1825_future+dollars 19 | total_1825_future := total_1825_future+quant_1825_future 20 | 21 | basis_1825_future = spent_1825_future/total_1825_future 22 | plot_1825_future = plot(basis_1825_future, linewidth=10, color=#ff00ff, title="1825_future") 23 | 24 | 25 | //spent_all := nz(spent_all[1])+dollars 26 | //quant_all = dollars/price 27 | //total_all := nz(total_all[1])+quant_all 28 | //basis_all = spent_all/total_all 29 | //plot_all = plot(basis_all, linewidth=10, color=blue, title=\"DCA: All Time\") 30 | //fill(plot1=plot_all, plot2=plot_price, color=price > basis_all ? color_fill_sell : color_fill_buy, transp=color_fill_transp) 31 | 32 | 33 | /////////////////////////////////////////////////// 34 | 35 | 36 | _src = close 37 | change = (_src[0] - _src[1])/_src[0] 38 | linear_predicted_price_f1 = (change*_src[0]) + _src[0] 39 | 40 | linear_predicted_price_f2 = (change*linear_predicted_price_f1[0] + linear_predicted_price_f1[0]) 41 | linear_predicted_price_f3 = (change*linear_predicted_price_f2[0] + linear_predicted_price_f2[0]) 42 | linear_predicted_price_f4 = (change*linear_predicted_price_f3[0] + linear_predicted_price_f3[0]) 43 | linear_predicted_price_f5 = (change*linear_predicted_price_f4[0] + linear_predicted_price_f4[0]) 44 | linear_predicted_price_f6 = (change*linear_predicted_price_f5[0] + linear_predicted_price_f5[0]) 45 | linear_predicted_price_f7 = (change*linear_predicted_price_f6[0] + linear_predicted_price_f6[0]) 46 | linear_predicted_price_f8 = (change*linear_predicted_price_f7[0] + linear_predicted_price_f7[0]) 47 | linear_predicted_price_f9 = (change*linear_predicted_price_f8[0] + linear_predicted_price_f8[0]) 48 | linear_predicted_price_f10 = (change*linear_predicted_price_f9[0] + linear_predicted_price_f9[0]) 49 | linear_predicted_price_f11 = (change*linear_predicted_price_f10[0] + linear_predicted_price_f10[0]) 50 | 51 | plot(linear_predicted_price_f1, show_last=1, offset=1, linewidth=1, color=red) 52 | plot(linear_predicted_price_f2, show_last=1, offset=2, linewidth=1, color=red) 53 | plot(linear_predicted_price_f3, show_last=1, offset=3, linewidth=1, color=red) 54 | plot(linear_predicted_price_f4, show_last=1, offset=4, linewidth=1, color=red) 55 | plot(linear_predicted_price_f5, show_last=1, offset=5, linewidth=1, color=red) 56 | plot(linear_predicted_price_f6, show_last=1, offset=6, linewidth=1, color=red) 57 | plot(linear_predicted_price_f7, show_last=1, offset=7, linewidth=1, color=red) 58 | plot(linear_predicted_price_f8, show_last=1, offset=8, linewidth=1, color=red) 59 | plot(linear_predicted_price_f9, show_last=1, offset=9, linewidth=1, color=red) 60 | plot(linear_predicted_price_f10, show_last=1, offset=10, linewidth=1, color=red) 61 | plot(linear_predicted_price_f11, show_last=1, offset=11, linewidth=1, color=blue) 62 | 63 | plot(_src) --------------------------------------------------------------------------------