-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.py
236 lines (203 loc) · 8.22 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from flask import Flask, render_template, request, jsonify
from tradingview_ta import TA_Handler, get_multiple_analysis
import os
from dotenv import load_dotenv
load_dotenv() # .env dosyasını yükle
# API_KEY'i .env dosyasından al
API_KEY = os.getenv('API_KEY')
app = Flask(__name__)
file_dir='coinlist'
line_list = []
line_list1 = []
data = {}
element = {}
element.clear()
line_list.clear()
@app.route('/', methods=['GET', 'POST'])
def hours_store():
return render_template('index.html', Hourss=["4h","5m" ,"15m", "1h" , "1D", "1W", "1M"])
@app.route('/list', methods=['GET', 'POST'])
def scan():
element.clear()
line_list.clear()
hours = request.form.get("times")
bbw = request.form['bbw']
exchange = request.form['exchange']
striphours = hours.strip()
stripexchange = exchange.strip()
exchange_file = os.path.join(file_dir, f"{stripexchange}.txt")
with open(exchange_file) as file:
lines = file.read()
line = lines.split('\n')
exchange_screener_mapping = {
"all": "crypto",
"huobi": "crypto",
"kucoin": "crypto",
"coinbase": "crypto",
"gateio": "crypto",
"binance": "crypto",
"bitfinex": "crypto",
"bybit": "crypto",
"okx": "crypto",
"bist": "turkey",
"nasdaq": "america",
}
screener = exchange_screener_mapping.get(stripexchange, "crypto")
analysis = get_multiple_analysis(screener=screener, interval=striphours, symbols=line)
for key, value in analysis.items():
try:
if value != None:
open_price = value.indicators["open"]
close = value.indicators["close"]
change = ((close-open_price)/open_price)*100
sma = value.indicators["SMA20"]
bb_upper = value.indicators["BB.upper"]
bb_lower = value.indicators["BB.lower"]
bb_middle = sma
bb_upper_1 = bb_middle + ((bb_upper - bb_middle) / 2)
bb_lower_1 = bb_middle - ((bb_middle - bb_lower) / 2)
BBW = (bb_upper - bb_lower) / sma
rating = 0
if close > bb_upper:
rating = 3
elif close > bb_upper_1:
rating = 2
elif close > bb_middle:
rating = 1
elif close < bb_lower:
rating = -3
elif close < bb_lower_1:
rating = -2
elif close < bb_middle:
rating = -1
signal = "NEUTRAL"
if rating == 2:
signal = "BUY"
elif rating == -2:
signal = "SELL"
conditions = (
1 > BBW and BBW < float(bbw)
)
if BBW and value.indicators["EMA50"] and value.indicators["RSI"]:
if (conditions):
currency = key.split(":")
price = round(close, 4)
BBW = round(BBW, 4)
change = round(change, 3)
element[key] = [price, BBW, change, rating, signal]
except (TypeError):
print(key ," is not defined ")
except (ZeroDivisionError):
print(key," bbw value the is zero")
line_list.append(element)
return render_template('data.html', line_list=line_list, hours=hours, line_list1=line_list1, element=element)
@app.route('/getPrice', methods=['POST'])
def handle_list_request():
request_data = request.json
hours = request_data.get('hours')
symbol = request_data.get('symbol')
exchange = request_data.get('exchange')
scanForApi(hours, symbol, exchange)
return jsonify(element)
def scanForApi(hours, symbol, exchange):
element.clear()
striphours = hours.strip()
symbol_with_exchange = f"{exchange}:{symbol}"
if exchange == "kucoin":
analysis = get_multiple_analysis(screener="crypto", interval=striphours, symbols=[symbol_with_exchange])
elif exchange == "bist":
analysis = get_multiple_analysis(screener="turkey", interval=striphours, symbols=[symbol_with_exchange])
elif exchange == "nasdaq":
analysis = get_multiple_analysis(screener="america", interval=striphours, symbols=[symbol_with_exchange])
for key, value in analysis.items():
try:
if value is not None:
open_price = value.indicators["open"]
close = value.indicators["close"]
change = ((close - open_price) / open_price) * 100
price = round(close, 4)
change = round(change, 3)
element["name"] = key
element["current"] = price
element["change"] = change
element["open"] = open_price
element["close"] = close
except TypeError:
print(key, " is not defined ")
def check_auth_header(request):
auth_header = request.headers.get('Authorization')
if auth_header == API_KEY:
return True
return False
@app.errorhandler(404)
def pageNotFound(error):
return render_template('error.html')
@app.route('/api/scan', methods=['POST'])
def scan_api():
try:
request_data = request.json
hours = request_data.get('hours', '4h') # Default 4h
bbw = request_data.get('bbw', '0.04') # Default 0.04
exchange = request_data.get('exchange', 'binance') # Default binance
if not check_auth_header(request):
return jsonify({'error': 'Unauthorized access'}), 401
element.clear()
line_list.clear()
striphours = hours.strip()
stripexchange = exchange.strip()
exchange_file = os.path.join(file_dir, f"{stripexchange}.txt")
with open(exchange_file) as file:
lines = file.read()
line = lines.split('\n')
exchange_screener_mapping = {
"all": "crypto",
"huobi": "crypto",
"kucoin": "crypto",
"coinbase": "crypto",
"gateio": "crypto",
"binance": "crypto",
"bitfinex": "crypto",
"bybit": "crypto",
"okx": "crypto",
"bist": "turkey",
"nasdaq": "america"
}
screener = exchange_screener_mapping.get(stripexchange, "crypto")
analysis = get_multiple_analysis(screener=screener, interval=striphours, symbols=line)
result = []
for key, value in analysis.items():
try:
if value is not None:
open_price = value.indicators["open"]
close = value.indicators["close"]
change = ((close-open_price)/open_price)*100
sma = value.indicators["SMA20"]
bb_upper = value.indicators["BB.upper"]
bb_lower = value.indicators["BB.lower"]
bb_middle = sma
BBW = (bb_upper - bb_lower) / sma
if BBW and value.indicators["EMA50"] and value.indicators["RSI"]:
if 1 > BBW and BBW < float(bbw):
result.append({
"symbol": key,
"price": round(close, 4),
"bbw": round(BBW, 4),
"change": round(change, 3),
"rsi": value.indicators["RSI"],
"volume": value.indicators["volume"]
})
except (TypeError, ZeroDivisionError) as e:
continue
return jsonify({
"status": "success",
"timeframe": hours,
"exchange": exchange,
"data": result
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5001)))