-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtechnicals.py
115 lines (89 loc) · 2.88 KB
/
technicals.py
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
def sma(price, length):
# price is list of prices for each day (ex. [10, 10.20, 10.30 ... ]
# length is length of sma (ex. 20 day sma)
sma = []
for day in range(0, len(price)):
sum=0
if day >= length-1: # day starts at zero and length starts at 1
for l in range(0,length):
sum += price[day-l]
sma.append(sum/length)
else:
sma.append(None)
return sma
def ema(price, length):
# EMAtoday = a * (p1 + (1-a)*p2 + (1-a)2 * p3 ... )
#(current day's closing price x Exponent) + (previous day's EMA x (1-Exponent))
ema = []
alpha = 2./(length+1)
for day in range(0, len(price)):
if day == length-1:
sum = 0
for l in range(0,length):
sum += price[day-l]
ema.append(sum/length)
elif day >= length:
ema.append((price[day]-ema[-1])*alpha + ema[-1])
else:
ema.append(None)
return ema
'''
for day in range(0, len(price)):
if day>=length-1:
EMAtoday=0
for l in range(0, length):
EMAtoday += alpha*((1-alpha)**l)*price[day-l]
ema.append(EMAtoday)
else:
ema.append(None)
print ema
'''
def simple_moving_average(STOCKAdj, startday, length):
# STOCKAdj list with all closing values
# days number of days (20, 50, 100)
# startday the beginning day (right now, numerical)
if startday < length:
return None
# stockprices fetches only the closing values we need, in reverse order
stockprices = STOCKAdj[startday:(startday-length):-1]
stockprices = STOCKAdj[(startday-length):startday]
stockprices.reverse()
SMA = sum(float(eachprice) for eachprice in stockprices)/len(stockprices)
return SMA
# input 1,2,3, [],
def exponential_moving_average(STOCKAdj, startday, length, oldEMA):
#EMA: {Close - EMA(previous day)} x multiplier + EMA(previous day).
# Multiplier is calculated like this: (2 / (Time periods + 1) )
# EMA,
# stockprices
if startday < length:
return None#float(STOCKAdj[startday]) #float(stockprices[1])
# stockprices fetches only the closing values we need, in reverse order
elif startday == length:
return simple_moving_average(STOCKAdj, startday, length)
else:
stockprices = STOCKAdj[(startday-length):startday]
stockprices.reverse()
for x in range(0, len(stockprices)):
stockprices[x]=float(stockprices[x])
alpha = 2./(length+1)
# stockprices, startday
# float(STOCKAdj[startday]) - oldEMA[len(oldEMA)-1]
## off by one error
# EMA = (float(STOCKAdj[startday])-oldEMA[len(oldEMA)-1])*alpha + oldEMA[len(oldEMA)-1]
EMA = (float(stockprices[0])-oldEMA[len(oldEMA)-1])*alpha + oldEMA[len(oldEMA)-1]
# Weight input EMA by day, farther back is multiplied by alpha^history
#EMA PREVIOUS DAY (SUM)
return EMA
"""
moving average convergence divergence
KDJ indicator
relative strength index
Williams %R
bias ratio
bollinger bands
fast stochastic oscillator
slow stochastic oscillator
commodity channel index
volume moving average
"""