Skip to content

Commit 562c2b0

Browse files
beta.001
1 parent 94c475d commit 562c2b0

8 files changed

+288
-0
lines changed

beta/arrays/test-001.pinescript

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study("My Script")
6+
length = 5
7+
var float[] a = array.newfloat(length, close)
8+
_length = array.size(a)
9+
sum = 0
10+
for _i = _length-1 to 1
11+
// _e = array.get(a, _i-1)
12+
array.insert(a, _i, _i)
13+
// array.insert(a, 0, close)
14+
15+
16+
plot(array.get(a, 0))
17+
//plot(close)
18+
//plot(array.sum(a) / length)

beta/arrays/test-002-push.pinescript

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study("f_push()")
6+
7+
f_push(_id, _value)=>
8+
_e = array.pop(_id)
9+
array.insert(_id, 0, _value)
10+
11+
length = input(5)
12+
13+
var float[] a = array.newfloat(length, close)
14+
15+
f_push(a, close)
16+
17+
plot(array.get(a, 0))
18+
plot(array.get(a, 4), color=color.yellow, offset=-4)
19+
20+
plot(array.sum(a) / length, color=color.red)
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study("My Script")
6+
7+
f_push(_id, _value)=>
8+
_e = array.pop(_id)
9+
array.insert(_id, 0, _value)
10+
11+
f_push_array(_A, _B)=>
12+
for _i = array.size(_B)-1 to 0
13+
f_push(_A, array.get(_B, _i))
14+
15+
h = highest(10)
16+
l = lowest(10)
17+
avg = (h+l)/2
18+
newhigh = high >= h ? 1 : 0
19+
newlow = low <= l ? 1 : 0
20+
21+
length = input(6)
22+
var float[] A = array.newfloat(length, close)
23+
float[] B = array.newfloat(2, 0.0)
24+
25+
if change(newhigh) < 0 or change(newlow) < 0
26+
//array.set(B, 0, avg)
27+
array.set(B, 0, h)
28+
array.set(B, 1, l)
29+
f_push_array(A, B)
30+
31+
plot(array.sum(A) / length)
32+
plot(array.max(A), color=color.white)
33+
plot(array.min(A), color=color.white)
34+
plot(close, color=color.yellow)
35+
//plot(array.sum(A))
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study("My Script")
6+
7+
f_dot_product(_A, _B)=>
8+
int _Alength = array.size(_A)
9+
int _Blength = array.size(_B)
10+
_dot = 0.0
11+
if _Alength == _Blength
12+
for _i = 0 to _Alength-1
13+
_dot := _dot + array.get(_A, _i) * array.get(_B, _i)
14+
_dot
15+
16+
dotlength = input(6)
17+
float[] A = array.newfloat(dotlength, close)
18+
float[] B = array.newfloat(dotlength, 1.618)
19+
20+
float dot = f_dot_product(A, B)
21+
plot(dot)

beta/arrays/test-005-graph.pinescript

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study("My Script")
6+
7+
f_grad(_value)=>
8+
_value >= 11 ? #0002A2 :
9+
_value == 10 ? #0045A9 :
10+
_value == 09 ? #008EB0 :
11+
_value == 08 ? #00B690 :
12+
_value == 07 ? #00BD4A :
13+
_value == 06 ? #01C400 :
14+
_value == 05 ? #52CA00 :
15+
_value == 04 ? #A8D100 :
16+
_value == 03 ? #D7AC00 :
17+
_value == 02 ? #DE5800 :
18+
_value == 01 ? #E50000 : color.white
19+
20+
float feature01 = close-close[1]
21+
float feature02 = round(rsi(close, 14)[1]*10)
22+
23+
int data_range = 10
24+
int nclusters = 3
25+
26+
float[] X = array.newfloat(data_range, feature01)
27+
float[] Y = array.newfloat(data_range, feature02)
28+
29+
for _i = 0 to data_range-1
30+
array.set(id=X, index=_i, value=feature01[_i])
31+
array.set(id=Y, index=_i, value=feature02[_i])
32+
33+
plot(series=array.get(X, 0), color=f_grad(0), linewidth=10, style=plot.style_circles, offset=int(-1000+array.get(Y, 0)), show_last=1)
34+
plot(series=array.get(X, 1), color=f_grad(1), linewidth=9, style=plot.style_circles, offset=int(-1000+array.get(Y, 1)), show_last=1)
35+
plot(series=array.get(X, 2), color=f_grad(2), linewidth=8, style=plot.style_circles, offset=int(-1000+array.get(Y, 2)), show_last=1)
36+
plot(series=array.get(X, 3), color=f_grad(3), linewidth=7, style=plot.style_circles, offset=int(-1000+array.get(Y, 3)), show_last=1)
37+
plot(series=array.get(X, 4), color=f_grad(4), linewidth=6, style=plot.style_circles, offset=int(-1000+array.get(Y, 4)), show_last=1)
38+
plot(series=array.get(X, 5), color=f_grad(5), linewidth=5, style=plot.style_circles, offset=int(-1000+array.get(Y, 5)), show_last=1)
39+
plot(series=array.get(X, 6), color=f_grad(6), linewidth=4, style=plot.style_circles, offset=int(-1000+array.get(Y, 6)), show_last=1)
40+
plot(series=array.get(X, 7), color=f_grad(7), linewidth=3, style=plot.style_circles, offset=int(-1000+array.get(Y, 7)), show_last=1)
41+
plot(series=array.get(X, 8), color=f_grad(8), linewidth=2, style=plot.style_circles, offset=int(-1000+array.get(Y, 8)), show_last=1)
42+
plot(series=array.get(X, 9), color=f_grad(9), linewidth=1, style=plot.style_circles, offset=int(-1000+array.get(Y, 9)), show_last=1)
43+
44+
// plot different shapes:
45+
// plotshape(series=array.get(X, 9), title='', style=shape.cross, location=location.absolute, color=f_grad(9), transp=25, offset=int(-1000+array.get(Y, 9)), size=size.normal, show_last=1)
46+
// plotshape(series=array.get(X, 5), title='', style=shape.diamond, location=location.absolute, color=f_grad(5), transp=25, offset=int(-1000+array.get(Y, 5)), size=size.normal, show_last=1)
47+
48+
// not centered location
49+
//plotchar(series=array.get(X, 2), title='', char='*', location=location.absolute, color=f_grad(2), transp=25, offset=int(-1000+array.get(Y, 2)), size=size.normal, show_last=1)
50+
51+
var line box_top = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, xloc=xloc.bar_index, extend=extend.none, color=color.gray, style=line.style_solid, width=1)
52+
var line box_bot = line.new(x1=bar_index, y1=0., x2=bar_index, y2=0., xloc=xloc.bar_index, extend=extend.none, color=color.gray, style=line.style_solid, width=1)
53+
var line box_left = line.new(x1=bar_index, y1=0., x2=bar_index, y2=0., xloc=xloc.bar_index, extend=extend.none, color=color.gray, style=line.style_solid, width=1)
54+
var line box_right = line.new(x1=bar_index, y1=0., x2=bar_index, y2=0., xloc=xloc.bar_index, extend=extend.none, color=color.gray, style=line.style_solid, width=1)
55+
56+
var line box_ob = line.new(x1=bar_index, y1=0., x2=bar_index, y2=0., xloc=xloc.bar_index, extend=extend.none, color=color.olive, style=line.style_dashed, width=1)
57+
var line box_os = line.new(x1=bar_index, y1=0., x2=bar_index, y2=0., xloc=xloc.bar_index, extend=extend.none, color=color.orange, style=line.style_dashed, width=1)
58+
59+
var float top = 0.0
60+
var float bot = 0.0
61+
int left = bar_index-1000
62+
int right = bar_index
63+
int ob = bar_index-250
64+
int os = bar_index-750
65+
66+
top := cum(array.max(id=X)) / (bar_index + 1)//max(nz(top), array.max(id=X))
67+
bot := cum(array.min(id=X)) / (bar_index + 1)//min(nz(bot), array.min(id=X))
68+
69+
// plot(series=top)
70+
// plot(series=bot)
71+
hline(0)
72+
73+
line.set_xy1(id=box_top, x=left, y=top)
74+
line.set_xy2(id=box_top, x=right, y=top)
75+
line.set_xy1(id=box_bot, x=left, y=bot)
76+
line.set_xy2(id=box_bot, x=right, y=bot)
77+
line.set_xy1(id=box_left, x=left, y=top)
78+
line.set_xy2(id=box_left, x=left, y=bot)
79+
line.set_xy1(id=box_right, x=right, y=top)
80+
line.set_xy2(id=box_right, x=right, y=bot)
81+
line.set_xy1(id=box_ob, x=ob, y=top)
82+
line.set_xy2(id=box_ob, x=ob, y=bot)
83+
line.set_xy1(id=box_os, x=os, y=top)
84+
line.set_xy2(id=box_os, x=os, y=bot)
85+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study(title="test-006-initarray")
6+
7+
var color[] colors = array.newcolor(size=0, initial_value=#eeeeee)
8+
// if array has no elements, initialize the array with its elements:
9+
if array.size(id=colors) == 0
10+
array.push(id=colors, value=#000000)
11+
array.push(id=colors, value=#ffffff)
12+
13+
plot(series=0.0, color=array.get(id=colors, index=0))
14+
plot(series=1.0, color=array.get(id=colors, index=1))

beta/arrays/test-007-mode.pinescript

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study(title="test-007-mode")
6+
7+
var label debug = label.new(x=bar_index, y=0.0, text='', style=label.style_label_lower_left)
8+
f_print(_array, _label)=>
9+
int _size = array.size(id=_array) - 1
10+
string _t = ''
11+
if _size > 0
12+
for _i = 0 to _size
13+
_t := _t + tostring(_i, '# : ') + tostring(array.get(id=_array, index=_i)) + '\n'
14+
label.set_xy(id=_label, x=bar_index, y=0.0)
15+
label.set_text(id=_label, text=_t)
16+
17+
f_push(_id, _value)=>
18+
_e = array.pop(id=_id)
19+
array.insert(id=_id, index=0, value=_value)
20+
21+
int length = input(10)
22+
float src = close//bar_index % length
23+
var float[] prices = array.newfloat(size=length, initial_value=close)
24+
f_push(prices, src)
25+
26+
f_mode(_id)=>
27+
int _size = array.size(id=_id)
28+
int _target = _size % 2 == 0 ? _size / 2 : 1 + _size / 2
29+
float[] _sorted = array.copy(id=_id)
30+
array.sort(id=_sorted, order=true)
31+
//f_print(_sorted, debug)
32+
float _return = 0.
33+
if _size > 0
34+
_return := array.get(id=_sorted, index=_target)
35+
_return
36+
37+
float mode = f_mode(prices)
38+
plot(src, color=color.gray)
39+
plot(mode)
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
2+
// © RicardoSantos
3+
4+
//@version=4
5+
study(title='test-008-fusion', overlay=true)
6+
7+
var label debug_high = label.new(x=bar_index, y=0.0, text='', style=label.style_label_lower_left)
8+
var label debug_low = label.new(x=bar_index, y=0.0, text='', style=label.style_label_upper_left)
9+
var label debug_mid = label.new(x=bar_index, y=0.0, text='', style=label.style_label_right)
10+
f_print(_array, _label)=>
11+
int _size = array.size(id=_array) - 1
12+
string _t = ''
13+
if _size > 0
14+
for _i = 0 to _size
15+
_t := _t + tostring(_i, '# : ') + tostring(array.get(id=_array, index=_i)) + '\n'
16+
label.set_xy(id=_label, x=bar_index, y=0.0)
17+
label.set_text(id=_label, text=_t)
18+
19+
int length = input(10)
20+
int half = (1 + length) / 2
21+
// (1 + length) prioritizes the first half (lows) of the array so that at odd values it will have 1 more element.
22+
23+
var float[] extremes = array.newfloat(size=length, initial_value=open)
24+
25+
float[] lows = array.slice(id=extremes, index_from=0, index_to=half)
26+
float[] highs = array.slice(id=extremes, index_from=half, index_to=length)
27+
// odd slicing behaviour: when slicing the second part(highs) index_from should be half + 1?
28+
// right now it seems to be doing so automatically, or not being added into the first part value of "half".
29+
30+
float h = highest(length)
31+
float l = lowest(length)
32+
33+
bool isnewhigh = high >= h
34+
bool isnewlow = low <= l
35+
36+
if isnewhigh
37+
float _e = array.pop(id=highs)
38+
array.insert(id=highs, index=0, value=high)
39+
if isnewlow
40+
float _e = array.pop(id=lows)
41+
array.insert(id=lows, index=0, value=low)
42+
43+
float avg_extreme = array.sum(id=extremes) / array.size(id=extremes)
44+
45+
float avg_high = array.sum(id=highs) / array.size(id=highs)
46+
float avg_low = array.sum(id=lows) / array.size(id=lows)
47+
48+
plot(avg_high)
49+
plot(avg_low)
50+
plot(avg_extreme, color=color.silver)
51+
52+
bool show_debug = input(false)
53+
if show_debug
54+
f_print(highs, debug_high)
55+
f_print(lows, debug_low)
56+
f_print(extremes, debug_mid)

0 commit comments

Comments
 (0)