-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdcacb.pine
384 lines (321 loc) · 17.9 KB
/
dcacb.pine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//@version=3
study("Dollar Cost Average Cost Basis", overlay=true)
// Shows the cost-basis for dollar cost averaging
// note: this code was generated using Bash because pinescript does not have arrays/collections
// and you can't run plot within a for loop :(
// code here: https://github.com/atomantic/pine_scripts
// Lightning Network Satoshi Tips Accepted https://tippin.me/@antic
price=input(close, title="Source")
// plot the price for use in color fills
plot_price = plot(price)
color_fill_sell = #a20000
color_fill_buy = #017d13
color_fill_transp = input(85, title="Fill Transparency")
rolling_window = input(defval=false, type=bool, title="Rolling Window?")
time_delta = (timenow - time)
milli_interval = 1000 * 60 * 60 * 24 * interval
dollars = 1000
milli_2190 = milli_interval * 2190
within_2190 = time_delta < milli_2190
total_2190 = 0.0
spent_2190 = 0
quant_2190 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_2190 := within_2190 ? nz(spent_2190[1])+dollars : 0
quant_2190 = within_2190 ? dollars/price : 0.0
total_2190 := nz(total_2190[1])+quant_2190
else
for i = 1 to 2190
if quant_2190[i]
spent_2190 := spent_2190+dollars
total_2190 := total_2190+nz(quant_2190[i], 0)
basis_2190 = spent_2190/total_2190
plot_2190 = plot(basis_2190, linewidth=8, color=#BB8CFF, title="2190 days of DCA")
fill(plot1=plot_2190, plot2=plot_price, color=price > basis_2190 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_1825 = milli_interval * 1825
within_1825 = time_delta < milli_1825
total_1825 = 0.0
spent_1825 = 0
quant_1825 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_1825 := within_1825 ? nz(spent_1825[1])+dollars : 0
quant_1825 = within_1825 ? dollars/price : 0.0
total_1825 := nz(total_1825[1])+quant_1825
else
for i = 1 to 1825
if quant_1825[i]
spent_1825 := spent_1825+dollars
total_1825 := total_1825+nz(quant_1825[i], 0)
basis_1825 = spent_1825/total_1825
plot_1825 = plot(basis_1825, linewidth=7, color=#AA7BEE, title="1825 days of DCA")
fill(plot1=plot_1825, plot2=plot_price, color=price > basis_1825 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_1460 = milli_interval * 1460
within_1460 = time_delta < milli_1460
total_1460 = 0.0
spent_1460 = 0
quant_1460 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_1460 := within_1460 ? nz(spent_1460[1])+dollars : 0
quant_1460 = within_1460 ? dollars/price : 0.0
total_1460 := nz(total_1460[1])+quant_1460
else
for i = 1 to 1460
if quant_1460[i]
spent_1460 := spent_1460+dollars
total_1460 := total_1460+nz(quant_1460[i], 0)
basis_1460 = spent_1460/total_1460
plot_1460 = plot(basis_1460, linewidth=6, color=#996ADD, title="1460 days of DCA")
fill(plot1=plot_1460, plot2=plot_price, color=price > basis_1460 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_1095 = milli_interval * 1095
within_1095 = time_delta < milli_1095
total_1095 = 0.0
spent_1095 = 0
quant_1095 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_1095 := within_1095 ? nz(spent_1095[1])+dollars : 0
quant_1095 = within_1095 ? dollars/price : 0.0
total_1095 := nz(total_1095[1])+quant_1095
else
for i = 1 to 1095
if quant_1095[i]
spent_1095 := spent_1095+dollars
total_1095 := total_1095+nz(quant_1095[i], 0)
basis_1095 = spent_1095/total_1095
plot_1095 = plot(basis_1095, linewidth=5, color=#8859CC, title="1095 days of DCA")
fill(plot1=plot_1095, plot2=plot_price, color=price > basis_1095 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_730 = milli_interval * 730
within_730 = time_delta < milli_730
total_730 = 0.0
spent_730 = 0
quant_730 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_730 := within_730 ? nz(spent_730[1])+dollars : 0
quant_730 = within_730 ? dollars/price : 0.0
total_730 := nz(total_730[1])+quant_730
else
for i = 1 to 730
if quant_730[i]
spent_730 := spent_730+dollars
total_730 := total_730+nz(quant_730[i], 0)
basis_730 = spent_730/total_730
plot_730 = plot(basis_730, linewidth=4, color=#7748BB, title="730 days of DCA")
fill(plot1=plot_730, plot2=plot_price, color=price > basis_730 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_365 = milli_interval * 365
within_365 = time_delta < milli_365
total_365 = 0.0
spent_365 = 0
quant_365 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_365 := within_365 ? nz(spent_365[1])+dollars : 0
quant_365 = within_365 ? dollars/price : 0.0
total_365 := nz(total_365[1])+quant_365
else
for i = 1 to 365
if quant_365[i]
spent_365 := spent_365+dollars
total_365 := total_365+nz(quant_365[i], 0)
basis_365 = spent_365/total_365
plot_365 = plot(basis_365, linewidth=3, color=#6637AA, title="365 days of DCA")
fill(plot1=plot_365, plot2=plot_price, color=price > basis_365 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_180 = milli_interval * 180
within_180 = time_delta < milli_180
total_180 = 0.0
spent_180 = 0
quant_180 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_180 := within_180 ? nz(spent_180[1])+dollars : 0
quant_180 = within_180 ? dollars/price : 0.0
total_180 := nz(total_180[1])+quant_180
else
for i = 1 to 180
if quant_180[i]
spent_180 := spent_180+dollars
total_180 := total_180+nz(quant_180[i], 0)
basis_180 = spent_180/total_180
plot_180 = plot(basis_180, linewidth=2, color=#552699, title="180 days of DCA")
fill(plot1=plot_180, plot2=plot_price, color=price > basis_180 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_120 = milli_interval * 120
within_120 = time_delta < milli_120
total_120 = 0.0
spent_120 = 0
quant_120 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_120 := within_120 ? nz(spent_120[1])+dollars : 0
quant_120 = within_120 ? dollars/price : 0.0
total_120 := nz(total_120[1])+quant_120
else
for i = 1 to 120
if quant_120[i]
spent_120 := spent_120+dollars
total_120 := total_120+nz(quant_120[i], 0)
basis_120 = spent_120/total_120
plot_120 = plot(basis_120, linewidth=1, color=#441588, title="120 days of DCA")
fill(plot1=plot_120, plot2=plot_price, color=price > basis_120 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
milli_90 = milli_interval * 90
within_90 = time_delta < milli_90
total_90 = 0.0
spent_90 = 0
quant_90 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_90 := within_90 ? nz(spent_90[1])+dollars : 0
quant_90 = within_90 ? dollars/price : 0.0
total_90 := nz(total_90[1])+quant_90
else
for i = 1 to 90
if quant_90[i]
spent_90 := spent_90+dollars
total_90 := total_90+nz(quant_90[i], 0)
basis_90 = spent_90/total_90
plot_90 = plot(basis_90, linewidth=1, color=#330477, title="90 days of DCA")
fill(plot1=plot_90, plot2=plot_price, color=price > basis_90 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
total_90_future_0 = total_90[0]-(total_90/90)+nz(quant_90[0],0)
basis_90_future_0 = spent_90/total_90_future_0
plot(basis_90_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#330477)
total_90_future_3 = total_90_future_0-(total_90_future_0[0]/90)+nz(quant_90[0],0)
basis_90_future_3 = spent_90/total_90_future_3
plot(basis_90_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray)
total_90_future_6 = total_90_future_3-(total_90_future_3[0]/90)+nz(quant_90[0],0)
basis_90_future_6 = spent_90/total_90_future_6
plot(basis_90_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray)
total_90_future_9 = total_90_future_6-(total_90_future_6[0]/90)+nz(quant_90[0],0)
basis_90_future_9 = spent_90/total_90_future_9
plot(basis_90_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray)
total_90_future_12 = total_90_future_9-(total_90_future_9[0]/90)+nz(quant_90[0],0)
basis_90_future_12 = spent_90/total_90_future_12
plot(basis_90_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray)
total_90_future_15 = total_90_future_12-(total_90_future_12[0]/90)+nz(quant_90[0],0)
basis_90_future_15 = spent_90/total_90_future_15
plot(basis_90_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray)
total_90_future_18 = total_90_future_15-(total_90_future_15[0]/90)+nz(quant_90[0],0)
basis_90_future_18 = spent_90/total_90_future_18
plot(basis_90_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray)
total_90_future_21 = total_90_future_18-(total_90_future_18[0]/90)+nz(quant_90[0],0)
basis_90_future_21 = spent_90/total_90_future_21
plot(basis_90_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray)
total_90_future_24 = total_90_future_21-(total_90_future_21[0]/90)+nz(quant_90[0],0)
basis_90_future_24 = spent_90/total_90_future_24
plot(basis_90_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray)
total_90_future_27 = total_90_future_24-(total_90_future_24[0]/90)+nz(quant_90[0],0)
basis_90_future_27 = spent_90/total_90_future_27
plot(basis_90_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray)
total_90_future_30 = total_90_future_27-(total_90_future_27[0]/90)+nz(quant_90[0],0)
basis_90_future_30 = spent_90/total_90_future_30
plot(basis_90_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray)
total_90_future_33 = total_90_future_30-(total_90_future_30[0]/90)+nz(quant_90[0],0)
basis_90_future_33 = spent_90/total_90_future_33
plot(basis_90_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray)
total_90_future_36 = total_90_future_33-(total_90_future_33[0]/90)+nz(quant_90[0],0)
basis_90_future_36 = spent_90/total_90_future_36
plot(basis_90_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray)
milli_60 = milli_interval * 60
within_60 = time_delta < milli_60
total_60 = 0.0
spent_60 = 0
quant_60 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_60 := within_60 ? nz(spent_60[1])+dollars : 0
quant_60 = within_60 ? dollars/price : 0.0
total_60 := nz(total_60[1])+quant_60
else
for i = 1 to 60
if quant_60[i]
spent_60 := spent_60+dollars
total_60 := total_60+nz(quant_60[i], 0)
basis_60 = spent_60/total_60
plot_60 = plot(basis_60, linewidth=1, color=#21F366, title="60 days of DCA")
fill(plot1=plot_60, plot2=plot_price, color=price > basis_60 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
total_60_future_0 = total_60[0]-(total_60/60)+nz(quant_60[0],0)
basis_60_future_0 = spent_60/total_60_future_0
plot(basis_60_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#21F366)
total_60_future_3 = total_60_future_0-(total_60_future_0[0]/60)+nz(quant_60[0],0)
basis_60_future_3 = spent_60/total_60_future_3
plot(basis_60_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray)
total_60_future_6 = total_60_future_3-(total_60_future_3[0]/60)+nz(quant_60[0],0)
basis_60_future_6 = spent_60/total_60_future_6
plot(basis_60_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray)
total_60_future_9 = total_60_future_6-(total_60_future_6[0]/60)+nz(quant_60[0],0)
basis_60_future_9 = spent_60/total_60_future_9
plot(basis_60_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray)
total_60_future_12 = total_60_future_9-(total_60_future_9[0]/60)+nz(quant_60[0],0)
basis_60_future_12 = spent_60/total_60_future_12
plot(basis_60_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray)
total_60_future_15 = total_60_future_12-(total_60_future_12[0]/60)+nz(quant_60[0],0)
basis_60_future_15 = spent_60/total_60_future_15
plot(basis_60_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray)
total_60_future_18 = total_60_future_15-(total_60_future_15[0]/60)+nz(quant_60[0],0)
basis_60_future_18 = spent_60/total_60_future_18
plot(basis_60_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray)
total_60_future_21 = total_60_future_18-(total_60_future_18[0]/60)+nz(quant_60[0],0)
basis_60_future_21 = spent_60/total_60_future_21
plot(basis_60_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray)
total_60_future_24 = total_60_future_21-(total_60_future_21[0]/60)+nz(quant_60[0],0)
basis_60_future_24 = spent_60/total_60_future_24
plot(basis_60_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray)
total_60_future_27 = total_60_future_24-(total_60_future_24[0]/60)+nz(quant_60[0],0)
basis_60_future_27 = spent_60/total_60_future_27
plot(basis_60_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray)
total_60_future_30 = total_60_future_27-(total_60_future_27[0]/60)+nz(quant_60[0],0)
basis_60_future_30 = spent_60/total_60_future_30
plot(basis_60_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray)
total_60_future_33 = total_60_future_30-(total_60_future_30[0]/60)+nz(quant_60[0],0)
basis_60_future_33 = spent_60/total_60_future_33
plot(basis_60_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray)
total_60_future_36 = total_60_future_33-(total_60_future_33[0]/60)+nz(quant_60[0],0)
basis_60_future_36 = spent_60/total_60_future_36
plot(basis_60_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray)
milli_30 = milli_interval * 30
within_30 = time_delta < milli_30
total_30 = 0.0
spent_30 = 0
quant_30 = dollars/price // how many fractions/units bought this period
if rolling_window
spent_30 := within_30 ? nz(spent_30[1])+dollars : 0
quant_30 = within_30 ? dollars/price : 0.0
total_30 := nz(total_30[1])+quant_30
else
for i = 1 to 30
if quant_30[i]
spent_30 := spent_30+dollars
total_30 := total_30+nz(quant_30[i], 0)
basis_30 = spent_30/total_30
plot_30 = plot(basis_30, linewidth=1, color=#10E255, title="30 days of DCA")
fill(plot1=plot_30, plot2=plot_price, color=price > basis_30 ? color_fill_sell : color_fill_buy, transp=color_fill_transp)
total_30_future_0 = total_30[0]-(total_30/30)+nz(quant_30[0],0)
basis_30_future_0 = spent_30/total_30_future_0
plot(basis_30_future_0, show_last=1, style=stepline, trackprice=false, offset=1, linewidth=2, color=#10E255)
total_30_future_3 = total_30_future_0-(total_30_future_0[0]/30)+nz(quant_30[0],0)
basis_30_future_3 = spent_30/total_30_future_3
plot(basis_30_future_3, show_last=1, style=line, trackprice=false, offset=3, linewidth=2, color=gray)
total_30_future_6 = total_30_future_3-(total_30_future_3[0]/30)+nz(quant_30[0],0)
basis_30_future_6 = spent_30/total_30_future_6
plot(basis_30_future_6, show_last=1, style=line, trackprice=false, offset=6, linewidth=2, color=gray)
total_30_future_9 = total_30_future_6-(total_30_future_6[0]/30)+nz(quant_30[0],0)
basis_30_future_9 = spent_30/total_30_future_9
plot(basis_30_future_9, show_last=1, style=line, trackprice=false, offset=9, linewidth=2, color=gray)
total_30_future_12 = total_30_future_9-(total_30_future_9[0]/30)+nz(quant_30[0],0)
basis_30_future_12 = spent_30/total_30_future_12
plot(basis_30_future_12, show_last=1, style=line, trackprice=false, offset=12, linewidth=2, color=gray)
total_30_future_15 = total_30_future_12-(total_30_future_12[0]/30)+nz(quant_30[0],0)
basis_30_future_15 = spent_30/total_30_future_15
plot(basis_30_future_15, show_last=1, style=line, trackprice=false, offset=15, linewidth=2, color=gray)
total_30_future_18 = total_30_future_15-(total_30_future_15[0]/30)+nz(quant_30[0],0)
basis_30_future_18 = spent_30/total_30_future_18
plot(basis_30_future_18, show_last=1, style=line, trackprice=false, offset=18, linewidth=2, color=gray)
total_30_future_21 = total_30_future_18-(total_30_future_18[0]/30)+nz(quant_30[0],0)
basis_30_future_21 = spent_30/total_30_future_21
plot(basis_30_future_21, show_last=1, style=line, trackprice=false, offset=21, linewidth=2, color=gray)
total_30_future_24 = total_30_future_21-(total_30_future_21[0]/30)+nz(quant_30[0],0)
basis_30_future_24 = spent_30/total_30_future_24
plot(basis_30_future_24, show_last=1, style=line, trackprice=false, offset=24, linewidth=2, color=gray)
total_30_future_27 = total_30_future_24-(total_30_future_24[0]/30)+nz(quant_30[0],0)
basis_30_future_27 = spent_30/total_30_future_27
plot(basis_30_future_27, show_last=1, style=line, trackprice=false, offset=27, linewidth=2, color=gray)
total_30_future_30 = total_30_future_27-(total_30_future_27[0]/30)+nz(quant_30[0],0)
basis_30_future_30 = spent_30/total_30_future_30
plot(basis_30_future_30, show_last=1, style=line, trackprice=false, offset=30, linewidth=2, color=gray)
total_30_future_33 = total_30_future_30-(total_30_future_30[0]/30)+nz(quant_30[0],0)
basis_30_future_33 = spent_30/total_30_future_33
plot(basis_30_future_33, show_last=1, style=line, trackprice=false, offset=33, linewidth=2, color=gray)
total_30_future_36 = total_30_future_33-(total_30_future_33[0]/30)+nz(quant_30[0],0)
basis_30_future_36 = spent_30/total_30_future_36
plot(basis_30_future_36, show_last=1, style=line, trackprice=false, offset=36, linewidth=2, color=gray)