-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
142 lines (103 loc) · 4.76 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
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
139
140
141
142
from flask import Flask, render_template, request
import tradingview_ta as tv_ta
import os
app = Flask(__name__)
symbols_dir = 'exchanges/'
exchanges = ["NYSE", "NASDAQ", "BMFBOVESPA"]
intervals = ["5m" ,"15m", "30m", "1h", "2h", "4h", "1d", "1W", "1M"]
filtered_symbols = {}
@app.route('/', methods=['GET'])
def load():
"""Returns the rendered starting page with default values"""
filtered_symbols.clear()
return render_template('index.html', exchanges=exchanges, stock_symbols=filtered_symbols, intervals=intervals)
@app.route('/', methods=['POST'])
def scan():
"""Exibits the Bollinger Bands width, price and % change in price for multiple stocks at once"""
filtered_symbols.clear()
input_interval = request.form.get("intervals")
input_bbw = request.form.get("bbw")
input_exchange = request.form.get("exchanges")
available_symbols = open_file(input_exchange)
analysis = symbol_analysis(input_exchange, input_interval, available_symbols)
calculate_bbw(analysis, input_bbw)
calculate_current_price(analysis)
calculate_price_change(analysis)
recommend_symbols(analysis)
return render_template('index.html', exchanges=exchanges, stock_symbols=filtered_symbols, intervals=intervals)
def open_file(exchange):
"""Returns the stock ticker symbols available for the chosen exchange"""
exchange_file = symbols_dir + exchange + ".txt"
try:
with open(exchange_file) as file:
stock_symbols = file.read()
file.close
return stock_symbols.split('\n')
except:
print("Something went wrong: Unable to read file!")
def symbol_analysis(exchange, interval, symbols):
"""Returns a dictionary of a TradingView analysis of multiple stocks at once"""
screener = "brazil" if exchange == "BMFBOVESPA" else "america"
return tv_ta.get_multiple_analysis(screener=screener, interval=interval, symbols=symbols)
def calculate_bbw(analysis_dict, input_bbw):
"""Calculates the Bollinger Bands width for multiple stocks at once"""
for symbol, value in analysis_dict.items():
try:
if symbol or value is not None:
upper = value.indicators["BB.upper"]
lower = value.indicators["BB.lower"]
sma = value.indicators["SMA20"]
bbw = (upper - lower) / sma
if bbw < 1 and bbw < float(input_bbw):
bbw = round(bbw, 4)
filtered_symbols[symbol] = [bbw]
except TypeError:
print(symbol, "is not defined!")
except AttributeError:
print(symbol, "is missing bbw calculation values!")
except ZeroDivisionError:
print(symbol, "bbw division by SMA zero value!")
def calculate_current_price(analysis_dict):
"""Calculates the Closing price for multiple stocks at once"""
for symbol, value in analysis_dict.items():
try:
if symbol in filtered_symbols.keys():
close = value.indicators["close"]
closing_price = round(close, 2)
filtered_symbols[symbol].append(closing_price)
except AttributeError:
print(symbol, "is missing Closing price info!")
except KeyError:
print(symbol, "is not defined!")
def calculate_price_change(analysis_dict):
"""Calculates the percent change in price for multiple stocks at once"""
for symbol, value in analysis_dict.items():
try:
if symbol in filtered_symbols.keys():
open = value.indicators["open"]
close = value.indicators["close"]
price_change = ((close - open) / open) * 100
price_change = round(price_change, 2)
filtered_symbols[symbol].append(price_change)
except AttributeError:
print(symbol, "is missing Opening or Closing price info!")
except KeyError:
print(symbol, "is not defined!")
except ZeroDivisionError:
print(symbol, "Opening price set to zero value!")
def recommend_symbols(analysis_dict) -> int:
"""Filters for recomendations"""
for symbol, value in analysis_dict.items():
try:
recommendation = value.summary["RECOMMENDATION"]
filtered_symbols[symbol].append(recommendation.replace('_', ' '))
except AttributeError:
print(symbol, "does not have associated recommendation!")
except KeyError:
print(symbol, "is not defined!")
@app.errorhandler(404)
def pageNotFound(error):
"""Exibits the '404 error' page"""
return render_template('error.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))