-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
24 lines (24 loc) · 883 Bytes
/
Copy pathsample.py
File metadata and controls
24 lines (24 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def sar(s, af=0.02, amax=0.2):
high, low = s.high, s.low# Starting values
sig0, xpt0, af0 = True, high[0], af
sar = [low[0] - (high - low).std()]
for i in range(1, len(s)):
sig1, xpt1, af1 = sig0, xpt0, af0lmin = min(low[i - 1], low[i])
lmax = max(high[i - 1], high[i])
if sig1:
sig0 = low[i] > sar[-1]
xpt0 = max(lmax, xpt1)
else:
sig0 = high[i] >= sar[-1]
xpt0 = min(lmin, xpt1)if sig0 == sig1:
sari = sar[-1] + (xpt1 - sar[-1])*af1
af0 = min(amax, af1 + af)
if sig0:
af0 = af0 if xpt0 > xpt1 else af1
sari = min(sari, lmin)
else:
af0 = af0 if xpt0 < xpt1 else af1
sari = max(sari, lmax)
else:
af0 = af
sari = xpt0sar.append(sari)return sar