-
Notifications
You must be signed in to change notification settings - Fork 294
/
app.py
52 lines (43 loc) · 1.62 KB
/
app.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
import os, csv
import talib
import yfinance as yf
import pandas
from flask import Flask, escape, request, render_template
from patterns import candlestick_patterns
app = Flask(__name__)
@app.route('/snapshot')
def snapshot():
with open('datasets/symbols.csv') as f:
for line in f:
if "," not in line:
continue
symbol = line.split(",")[0]
data = yf.download(symbol, start="2020-01-01", end="2020-08-01")
data.to_csv('datasets/daily/{}.csv'.format(symbol))
return {
"code": "success"
}
@app.route('/')
def index():
pattern = request.args.get('pattern', False)
stocks = {}
with open('datasets/symbols.csv') as f:
for row in csv.reader(f):
stocks[row[0]] = {'company': row[1]}
if pattern:
for filename in os.listdir('datasets/daily'):
df = pandas.read_csv('datasets/daily/{}'.format(filename))
pattern_function = getattr(talib, pattern)
symbol = filename.split('.')[0]
try:
results = pattern_function(df['Open'], df['High'], df['Low'], df['Close'])
last = results.tail(1).values[0]
if last > 0:
stocks[symbol][pattern] = 'bullish'
elif last < 0:
stocks[symbol][pattern] = 'bearish'
else:
stocks[symbol][pattern] = None
except Exception as e:
print('failed on filename: ', filename)
return render_template('index.html', candlestick_patterns=candlestick_patterns, stocks=stocks, pattern=pattern)