-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsi.py
executable file
·138 lines (109 loc) · 3.45 KB
/
tsi.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# Author: Chico
# Version: 0.1.0
# test
#
#
import sys;
import csv;
#import pandas as pd;
from datetime import datetime;
#import pandas_datareader.data as web;
def getList ():
#lista = ('rent3', 'klbn4', 'abev3', 'bvmf3', 'embr3', 'hype3')
lista = ('vale5',)
return lista
def downloadCSV ():
# Code Fount at: http://www.theodor.io/scraping-google-finance-data-using-pandas/
# Specify Date Range
start = datetime(2015, 8, 1)
end = datetime.today()
# Specify symbol
symbol = 'rent3'
#aapl_from_google = web.DataReader("%s" % symbol, 'google', start, end)
#aapl_from_google.to_csv('%s.csv' % symbol)
def openCsv (stock_name):
csvFile = open('sampleCSV/' + stock_name + '.csv')
csvReader = csv.reader(csvFile)
return csvReader
def closeList (csv):
tempList = []
for row in csv:
if csv.line_num == 1:
continue
tempList.append(row[4])
tempList.reverse()
return tempList
def momentum (closeList):
momentumList = []
for i in range(len(closeList)):
if i == 0:
continue
momentumList.append(round(float(closeList[i])-float(closeList[i-1]),2))
return momentumList
def momentumAbs (closeList):
momentumAbsList = []
for i in range(len(closeList)):
if i == 0:
continue
momentumAbsList.append(abs(round(float(closeList[i])-float(closeList[i-1]),2)))
return momentumAbsList
def ema(s, n):
"""
Function found at http://stackoverflow.com/questions/488670/calculate-exponential-moving-average-in-python
returns an n period exponential moving average for
the time series s
s is a list ordered from oldest (index 0) to most
recent (index -1)
n is an integer
returns a numeric array of the exponential
moving average
"""
#s = array(s)
ema = []
j = 1
#get n sma first and calculate the next n period ema
sma = sum(s[:n]) / n
multiplier = 2 / float(1 + n)
ema.append(sma)
#EMA(current) = ( (Price(current) - EMA(prev) ) x Multiplier) + EMA(prev)
ema.append(( (s[n] - sma) * multiplier) + sma)
#now calculate the rest of the values
for i in s[n+1:]:
tmp = ( (i - ema[j]) * multiplier) + ema[j]
j = j + 1
ema.append(tmp)
return ema
def emapandas(s, n):
data = { 'stock' : s}
df = pd.DataFrame(data)
emapandas = df.ewm(span=25).mean()
return emapandas
def tsi (emaList, emaAbsList):
tsiList = []
for i in range(len(emaList)):
tsiList.append(100*(emaList[i]/emaAbsList[i]))
return tsiList
def main():
stockList = getList()
for i in range(len(stockList)):
csvReader = openCsv(stockList[i])
ListValues = closeList(csvReader)
momentumValues = momentum(ListValues)
momentumAbsValues = momentumAbs(ListValues)
ema25Momentum = ema(momentumValues,25)
ema13ema25 = ema(ema25Momentum,13)
ema25MomentumAbs = ema(momentumAbsValues,25)
ema13ema25Abs = ema(ema25MomentumAbs,13)
tsiList = tsi(ema13ema25, ema13ema25Abs)
tsi7List = ema(tsiList, 7)
print tsiList
if tsiList[-1] > tsi7List[-1] and tsiList[-1] > 0:
print (stockList[i] + ': ' + 'Compra')
elif tsiList[-1] < tsi7List[-1] and tsi7List < 0:
print (stockList[i] + ': ' + 'Vende')
else:
print (stockList[i] + ': ' + 'Faz nada')
downloadCSV()
if __name__ == '__main__':
sys.exit(main())