-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtilities.py
96 lines (76 loc) · 2.19 KB
/
Utilities.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
# -*- coding: utf-8 -*-
"""
Some methods
from 2001 to 2013
"""
__author__ = 'chen hsueh-min'
import datetime
import numpy as np
# from RawDataProcessing import *
from scipy.stats.stats import pearsonr
def str2date(dateStr):
return datetime.datetime.strptime(dateStr, '%Y/%m/%d').date()
def returnRate(data):
return np.diff(data) / map(float, data[:len(data) - 1])
def nCorrelation(x, y, n=None, pValue=False):
if n is None:
if pValue:
return pearsonr(x, y)
else:
return pearsonr(x, y)[0]
else:
if pValue:
return [pearsonr(x[k - n:k], y[k - n:k]) for k in range(n, len(x) + 1)]
else:
return [pearsonr(x[k - n:k], y[k - n:k])[0] for k in range(n, len(x) + 1)]
# from matplotlib example
def movingAverage(x, n, type='simple'):
"""
compute an n period moving average.
type is 'simple' | 'exponential'
"""
x = np.asarray(x)
if type == 'simple':
weights = np.ones(n)
else:
weights = np.exp(np.linspace(-1., 0., n))
weights /= weights.sum()
a = np.convolve(x, weights, mode='full')[:len(x)]
a[:n] = a[n]
return a
# from matplotlib example
def relativeStrength(prices, n=14):
"""
compute the n period relative strength indicator
http://stockcharts.com/school/doku.php?id=chart_school:glossary_r#relativestrengthindex
http://www.investopedia.com/terms/r/rsi.asp
"""
deltas = np.diff(prices)
seed = deltas[:n + 1]
up = seed[seed >= 0].sum() / n
down = -seed[seed < 0].sum() / n
rs = up / down
rsi = np.zeros_like(prices)
rsi[:n] = 100. - 100. / (1. + rs)
for i in range(n, len(prices)):
delta = deltas[i - 1] # cause the diff is 1 shorter
if delta > 0:
upval = delta
downval = 0.
else:
upval = 0.
downval = -delta
up = (up * (n - 1) + upval) / n
down = (down * (n - 1) + downval) / n
rs = up / down
rsi[i] = 100. - 100. / (1. + rs)
return rsi
# from matplotlib example
def movingAverageConvergence(x, nslow=26, nfast=12):
"""
compute the MACD (Moving Average Convergence/Divergence) using a fast and slow exponential moving avg'
return value is emaslow, emafast, macd which are len(x) arrays
"""
emaslow = movingAverage(x, nslow, type='exponential')
emafast = movingAverage(x, nfast, type='exponential')
return emaslow, emafast, emafast - emaslow