-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch_data.py
64 lines (57 loc) · 1.94 KB
/
fetch_data.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
import os
import io
from dotenv import load_dotenv
import pandas as pd
import requests
import json
import ta
from datetime import datetime, timedelta,date
load_dotenv()
BASE_URL = os.getenv("BASE_URL")
def fetch_daily_prices(symbol,API_KEY):
params = {
'function': 'TIME_SERIES_DAILY_ADJUSTED',
'symbol': symbol,
'apikey': API_KEY,
'outputsize': 'full',
'datatype': 'csv'
}
response = requests.get(BASE_URL, params=params)
data = pd.read_csv(io.StringIO(response.content.decode('utf-8')))
return data
def calculate_technical_indicators(data):
# Convert to pandas DataFrame
df = pd.DataFrame(data)
df = df.sort_values('timestamp')
df['20ma'] = ta.trend.sma_indicator(df['close'], window=20)
df['50ma'] = ta.trend.sma_indicator(df['close'], window=50)
# Calculate relative strength index (RSI)
df['rsi'] = ta.momentum.RSIIndicator(df['close']).rsi()
# Calculate volatility
df['volatility'] = ta.volatility.AverageTrueRange(df['high'], df['low'], df['close']).average_true_range()
# Calculate volume data
df['volume'] = ta.volume.on_balance_volume(df['close'], df['volume'])
# df = df.sort_values('timestamp', ascending=False)
return df
def get_api_key(api_counter):
API_KEY = os.getenv("API_KEY_1")
if api_counter == 1:
API_KEY = os.getenv("API_KEY_2")
if api_counter == 2:
API_KEY = os.getenv("API_KEY_3")
return API_KEY
def fetch_data(symbol,api_counter):
symbol += ".BSE"
API_KEY = get_api_key(api_counter)
filename = f"{symbol}_data_{date.today()}.csv"
stock_data=""
if not os.path.exists(filename):
daily_prices = fetch_daily_prices(symbol,API_KEY)
# data = daily_prices
data = calculate_technical_indicators(daily_prices)
data.to_csv(filename, index=False)
stock_data = data
else:
stock_data = pd.read_csv(filename)
print(stock_data)
return filename