Skip to content

Commit 86ded59

Browse files
temp
1 parent 070ae23 commit 86ded59

File tree

1 file changed

+152
-2
lines changed

1 file changed

+152
-2
lines changed

test.pinescript

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,155 @@
22
// © RicardoSantos
33

44
//@version=4
5-
study("this is a test file")
6-
plot(close)
5+
study(title='[RS]Bar Pattern Statistics Tool', shorttitle='bst', overlay=false)
6+
7+
// bar type placeholder names:
8+
string op_type_01 = 'inside bar'
9+
string op_type_02 = 'outside bar'
10+
string op_type_03 = 'up bar'
11+
string op_type_04 = 'down bar'
12+
string op_type_05 = 'up pin bar'
13+
string op_type_06 = 'down pin bar'
14+
string op_type_07 = 'any type of bar'
15+
// bar type enumeration functions:
16+
f_enum_type(_str)=>(_str==op_type_01?1:(_str==op_type_02?2:(_str==op_type_03?3:(_str==op_type_04?4:(_str==op_type_05?5:(_str==op_type_06?6:(_str==op_type_07?7:na)))))))
17+
f_enum_type_by_idx(_i)=>(_i==1?op_type_01:(_i==2?op_type_02:(_i==3?op_type_03:(_i==4?op_type_04:(_i==5?op_type_05:(_i==6?op_type_06:(_i==7?op_type_07:op_type_07)))))))
18+
//
19+
// current bar information:
20+
float body_range = abs(close - open)
21+
float hl_range = high - low
22+
float upperwick = high - max(close, open)
23+
float lowerwick = min(close, open) - low
24+
bool risinghigh = high > high[1]
25+
bool fallinghigh = high < high[1]
26+
bool risinglow = low > low[1]
27+
bool fallinglow = low < low[1]
28+
//
29+
// bar type information at position:
30+
f_isHighUp(_pos) => risinghigh[_pos]
31+
f_isHighDown(_pos) => fallinghigh[_pos]
32+
f_isLowUp(_pos) => risinglow[_pos]
33+
f_isLowDown(_pos) => fallinglow[_pos]
34+
35+
f_isInsideBar(_pos) => f_isHighDown(_pos) and f_isLowUp(_pos)
36+
f_isOutsideBar(_pos) => f_isHighUp(_pos) and f_isLowDown(_pos)
37+
f_isUpBar(_pos) => f_isHighUp(_pos) and f_isLowUp(_pos)
38+
f_isDownBar(_pos) => f_isHighDown(_pos) and f_isLowDown(_pos)
39+
f_isPinUp(_pos) => risinghigh[_pos] and body_range[_pos] < (hl_range[_pos] * 0.33) and (upperwick[_pos] * 0.66) > lowerwick[_pos]
40+
f_isPinDown(_pos) => fallinglow[_pos] and body_range[_pos] < (hl_range[_pos] * 0.33) and (lowerwick[_pos] * 0.66) > upperwick[_pos]
41+
f_isAny(_pos) => true
42+
43+
f_enum_BarPattern(_i, _pos)=>(_i==1?f_isInsideBar(_pos):(_i==2?f_isOutsideBar(_pos):(_i==3?f_isUpBar(_pos):(_i==4?f_isDownBar(_pos):(_i==5?f_isPinUp(_pos):(_i==6?f_isPinDown(_pos):(_i==7?f_isAny(_pos):na)))))))
44+
f_ident_bar(_pos)=>f_isInsideBar(_pos)?1:(f_isOutsideBar(_pos)?2:(f_isUpBar(_pos)?3:(f_isDownBar(_pos)?4:(f_isPinUp(_pos)?5:(f_isPinDown(_pos)?6:(f_isAny(_pos)?7:7))))))
45+
46+
bool automatic_last_n_bars = input(false)
47+
int n_bars_in_pattern = input(defval=3, title='How many bars pattern:', type=input.integer, minval=1, maxval=10)
48+
49+
string bar01_type = input(defval=op_type_01, title='#1 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
50+
string bar02_type = input(defval=op_type_01, title='#2 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
51+
string bar03_type = input(defval=op_type_01, title='#3 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
52+
string bar04_type = input(defval=op_type_01, title='#4 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
53+
string bar05_type = input(defval=op_type_01, title='#5 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
54+
string bar06_type = input(defval=op_type_01, title='#6 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
55+
string bar07_type = input(defval=op_type_01, title='#7 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
56+
string bar08_type = input(defval=op_type_01, title='#8 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
57+
string bar09_type = input(defval=op_type_01, title='#9 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
58+
string bar10_type = input(defval=op_type_01, title='#10 in pattern:', type=input.string, options=[op_type_01, op_type_02, op_type_03, op_type_04, op_type_05, op_type_06, op_type_07])
59+
60+
if automatic_last_n_bars
61+
bar01_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-0)))
62+
bar02_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-1)))
63+
bar03_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-2)))
64+
bar04_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-3)))
65+
bar05_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-4)))
66+
bar06_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-5)))
67+
bar07_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-6)))
68+
bar08_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-7)))
69+
bar09_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-8)))
70+
bar10_type := f_enum_type_by_idx(f_ident_bar(max(0, n_bars_in_pattern-9)))
71+
72+
f_get_nbar_type(_i)=>(_i==1?bar01_type:(_i==2?bar02_type:(_i==3?bar03_type:(_i==4?bar04_type:(_i==5?bar05_type:(_i==6?bar06_type:(_i==7?bar07_type:(_i==8?bar08_type:(_i==9?bar09_type:(_i==10?bar10_type:na))))))))))
73+
f_check_nbar(_i, _pos)=>f_enum_BarPattern(f_enum_type(f_get_nbar_type(_i)), _pos)
74+
75+
f_get_pattern_as_text()=>
76+
string _t = ''
77+
for _i = 1 to n_bars_in_pattern
78+
_t := _t + '(' + tostring(x=_i, y='#') + ': ' + f_get_nbar_type(_i) + ')'
79+
if _i != n_bars_in_pattern
80+
_t := _t + ' > '
81+
_t
82+
83+
84+
f_check_if_pattern(_length)=>
85+
int _count = 0
86+
bool _is_valid = false
87+
for _i = 1 to _length
88+
int _pos = _length - _i
89+
_count := _count + (f_check_nbar(_i, _pos) ? 1 : 0)
90+
if _count >= _length
91+
_is_valid := true
92+
_is_valid
93+
94+
t = f_check_if_pattern(n_bars_in_pattern) ? 1 : 0
95+
96+
var int total_bars = 0
97+
total_bars := total_bars + 1
98+
99+
f_perc_of(_fraction, _total)=>(_fraction / _total) * 100
100+
101+
//stats:
102+
103+
var int n_detected = 0
104+
var int n_bars_inside = 0
105+
var int n_bars_outside = 0
106+
var int n_bars_up = 0
107+
var int n_bars_down = 0
108+
var int n_bars_over_atr14x1 = 0
109+
var int n_bars_over_atr14x2 = 0
110+
var int n_bars_over_atr14x3 = 0
111+
112+
float atr = atr(14)
113+
if t[1] > 0
114+
n_detected := n_detected + 1
115+
if f_isInsideBar(0)
116+
n_bars_inside := n_bars_inside + 1
117+
if f_isOutsideBar(0)
118+
n_bars_outside := n_bars_outside + 1
119+
if f_isUpBar(0)
120+
n_bars_up := n_bars_up + 1
121+
if f_isDownBar(0)
122+
n_bars_down := n_bars_down + 1
123+
if high-low > atr * 1
124+
n_bars_over_atr14x1 := n_bars_over_atr14x1 + 1
125+
if high-low > atr * 2
126+
n_bars_over_atr14x2 := n_bars_over_atr14x2 + 1
127+
if high-low > atr * 3
128+
n_bars_over_atr14x3 := n_bars_over_atr14x3 + 1
129+
130+
string tex_size = input(defval=size.normal, title='Text size:', type=input.string, options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge])
131+
132+
string tex = 'Statistics:\n' +
133+
'Pattern: ' + f_get_pattern_as_text() + '\n' +
134+
'Nº of patterns:' + tostring(x=n_detected, y='#') + ' in ' + tostring(x=total_bars, y='#') + ' bars (' + tostring(x=f_perc_of(n_detected, total_bars), y='#.000') + '%)' + '\n' +
135+
'Nº of next bars, that are:\n' +
136+
' --- > Inside Bars: ' + tostring(x=n_bars_inside, y='#') + ' (' + tostring(x=f_perc_of(n_bars_inside, n_detected), y='#.000') + '%)' + '\n' +
137+
' --- > Outside Bars: ' + tostring(x=n_bars_outside, y='#') + ' (' + tostring(x=f_perc_of(n_bars_outside, n_detected), y='#.000') + '%)' + '\n' +
138+
' --- > Up Bars: ' + tostring(x=n_bars_up, y='#') + ' (' + tostring(x=f_perc_of(n_bars_up, n_detected), y='#.000') + '%)' + '\n' +
139+
' --- > Down Bars: ' + tostring(x=n_bars_down, y='#') + ' (' + tostring(x=f_perc_of(n_bars_down, n_detected), y='#.000') + '%)' + '\n' +
140+
' --- > Over ATR(14): ' + tostring(x=n_bars_over_atr14x1, y='#') + ' (' + tostring(x=f_perc_of(n_bars_over_atr14x1, n_detected), y='#.000') + '%)' + '\n' +
141+
' --- > Over ATR(14) x 2: ' + tostring(x=n_bars_over_atr14x2, y='#') + ' (' + tostring(x=f_perc_of(n_bars_over_atr14x2, n_detected), y='#.000') + '%)' + '\n' +
142+
' --- > Over ATR(14) x 3: ' + tostring(x=n_bars_over_atr14x3, y='#') + ' (' + tostring(x=f_perc_of(n_bars_over_atr14x3, n_detected), y='#.000') + '%)' + '\n'
143+
144+
145+
var label la = label.new(
146+
x=bar_index, y=0.5, text='',
147+
xloc=xloc.bar_index, yloc=yloc.price,
148+
color=color.white, style=label.style_label_left, textcolor=color.black,
149+
size=tex_size, textalign=text.align_left
150+
)
151+
152+
label.set_xy(id=la, x=bar_index, y=0.5)
153+
label.set_text(id=la, text=tex)
154+
plot(series=t, title='Event', color=color.black, style=plot.style_histogram, transp=0)
155+
hline(0)
156+
hline(1)

0 commit comments

Comments
 (0)