forked from marketcalls/tradingview-yahoo-finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (60 loc) · 2.14 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
from flask import Flask, render_template, jsonify
import yfinance as yf
import pandas as pd
import pandas_ta as ta
from datetime import datetime, timedelta
app = Flask(__name__)
def fetch_yahoo_data(ticker, interval, ema_period=20, rsi_period=14):
end_date = datetime.now()
if interval in ['1m', '5m']:
start_date = end_date - timedelta(days=7)
elif interval in ['15m', '60m']:
start_date = end_date - timedelta(days=60)
elif interval == '1d':
start_date = end_date - timedelta(days=365*5)
elif interval == '1wk':
start_date = end_date - timedelta(weeks=365*5)
elif interval == '1mo':
start_date = end_date - timedelta(days=365*5)
data = yf.download(ticker, start=start_date, end=end_date, interval=interval)
data['EMA'] = ta.ema(data['Close'], length=ema_period)
data['RSI'] = ta.rsi(data['Close'], length=rsi_period)
candlestick_data = [
{
'time': int(row.Index.timestamp()),
'open': row.Open,
'high': row.High,
'low': row.Low,
'close': row.Close
}
for row in data.itertuples()
]
ema_data = [
{
'time': int(row.Index.timestamp()),
'value': row.EMA
}
for row in data.itertuples() if not pd.isna(row.EMA)
]
rsi_data = [
{
'time': int(row.Index.timestamp()),
'value': row.RSI if not pd.isna(row.RSI) else 0 # Convert NaN to zero
}
for row in data.itertuples()
]
return candlestick_data, ema_data, rsi_data
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/data/<ticker>/<interval>/<int:ema_period>/<int:rsi_period>')
def get_data(ticker, interval, ema_period, rsi_period):
candlestick_data, ema_data, rsi_data = fetch_yahoo_data(ticker, interval, ema_period, rsi_period)
return jsonify({'candlestick': candlestick_data, 'ema': ema_data, 'rsi': rsi_data})
@app.route('/api/symbols')
def get_symbols():
with open('symbols.txt') as f:
symbols = [line.strip() for line in f]
return jsonify(symbols)
if __name__ == '__main__':
app.run()