-
Notifications
You must be signed in to change notification settings - Fork 2
/
GapDetect.pine
147 lines (128 loc) · 5.03 KB
/
GapDetect.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
//@version=6
// @description Helps in determining opening gap severity by measuring the standard deviation of the open minus the previous close.
library('GapDetect', false)
import Electrified/SessionInfo/11 as Session
///////////////////////////////////////////////////
//// Gap Detection ////////////////////////////////
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns today's overnight gap value.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export today(
simple string spec = session.regular,
simple string res = '1440') =>
var float value = na
var float previousClose = na
if timeframe.isdaily
value := open - close[1]
else if not timeframe.isintraday
value
else
if(Session.wasLastBar(spec, res))
previousClose := close[1]
if(Session.isFirstBar(spec, res))
value := open - previousClose
value
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns today's overnight gap (% move).
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export todayPercent(
simple string spec = session.regular,
simple string res = '1440') =>
var float value = na
var float previousClose = na
c1 = close[1]
if timeframe.isdaily
value := (open - c1) / c1
else if not timeframe.isintraday
value
else
if(Session.wasLastBar(spec, res))
previousClose := c1
if(Session.isFirstBar(spec, res))
value := (open - previousClose) / previousClose
value
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns the normal daily standard deviation of the overnight gap value.
// @param days Number of days back to measure.
// @param maxDeviation The limit of volatility before considered an outlier.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export normal(
simple int days,
simple float maxDeviation = 2,
simple string spec = session.regular,
simple string res = '1440') =>
var delta = array.new_float()
var result = array.new_float()
value = today(spec, res)
if(Session.isFirstBar(spec, res))
array.push(delta, value)
if(array.size(delta) > days)
array.shift(delta)
avg = array.avg(delta)
stdev = array.stdev(delta)
level = stdev * maxDeviation
hi = avg + level
lo = avg - level
if lo < value or value < hi
array.push(result, value)
if(array.size(result) > days)
array.shift(result)
array.stdev(result)
///////////////////////////////////////////////////
// @function Returns the normal daily standard deviation of the overnight gap (% move).
// @param days Number of days back to measure.
// @param maxDeviation The limit of volatility before considered an outlier.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export normalPercent(
simple int days,
simple float maxDeviation = 2,
simple string spec = session.regular,
simple string res = '1440') =>
var delta = array.new_float()
var result = array.new_float()
value = todayPercent(spec, res)
if(Session.isFirstBar(spec, res))
array.push(delta, value)
if(array.size(delta) > days)
array.shift(delta)
avg = array.avg(delta)
stdev = array.stdev(delta)
level = stdev * maxDeviation
hi = avg + level
lo = avg - level
if lo < value or value < hi
array.push(result, value)
if(array.size(result) > days)
array.shift(result)
array.stdev(result)
///////////////////////////////////////////////////
//////////////////////////////////////////////////
// @function Returns the severity of the overnight in ratios of the normal standard deviation.
// @param days Number of days back to measure.
// @param maxDeviation The limit of volatility before considered an outlier.
// @param spec session.regular (default), session.extended or other time spec.
// @param res The resolution (default = '1440').
export severity(
simple int days,
simple float maxDeviation = 2,
simple string spec = session.regular,
simple string res = '1440') =>
todayPercent(spec, res) / normalPercent(days, maxDeviation, spec, res)
///////////////////////////////////////////////////
hline(+2)
hline(+1)
hline(0, linestyle=hline.style_solid)
hline(-1)
hline(-2)
s = severity(input.int(50, 'Days to Measure', minval=3))
c = s < 0 ? color.red : color.green
plot(s, 'Gap Severity', c, style=plot.style_areabr)