import yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
import _KEYS_DICT
def get_bars(symbol, start_date, end_date, interval='15m'):
start_date = datetime.strptime(start_date, "%Y-%m-%d")
end_date = datetime.strptime(end_date, "%Y-%m-%d")
all_data = []
while start_date < end_date:
temp_end_date = start_date + timedelta(days=7) # Fetching data in 7-day chunks
df = yf.download(symbol, start=start_date.strftime("%Y-%m-%d"), end=temp_end_date.strftime("%Y-%m-%d"), interval=interval)
all_data.append(df)
start_date = temp_end_date
final_df = pd.concat(all_data, axis=0)
return final_df[['Open', 'High', 'Low', 'Close', 'Volume']] # Only select the columns you need
Set your parameters
START_DATE = '2023-07-05'
END_DATE = '2023-09-01'
CSV_NAME = "@chill"
stocks_list = _KEYS_DICT.DICT_COMPANYS[CSV_NAME]
for symbol in stocks_list:
# Fetch the data
print("Starting data fetching process Stock: ", symbol)
df = get_bars(symbol, START_DATE, END_DATE)
print("Data fetching process completed df.shape: ", df.shape)
# Check if the dataframe is not empty and if there are no NaT values in the index
if not df.empty and not df.index.isna().any():
# Save the data as a CSV file
max_recent_date = df.index.max().strftime("%Y%m%d")
min_recent_date = df.index.min().strftime("%Y%m%d")
print("d_price/RAW_alpha/alpha_" + symbol + '_' + '1Min' + "_" + max_recent_date + "__" + min_recent_date + ".csv")
df.to_csv("d_price/RAW_alpha/alpha_" + symbol + '_' + '1Min' + "_" + max_recent_date + "__" + min_recent_date + ".csv",sep="\t", index=True)
print("\tSTART: ", str(df.index.min()), " END: ", str(df.index.max()) , " shape: ", df.shape, "\n")
else:
print ("error none in stock: ", symbol)
Files are attached, what am I doing wrong ?
(crypto_ml) C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow>python 1_Get_technical_indicators.py h5py._conv - [DEBUG]{MainThread} - <module>() - Creating converter from 7 to 5 h5py._conv - [DEBUG]{MainThread} - <module>() - Creating converter from 5 to 7 h5py._conv - [DEBUG]{MainThread} - <module>() - Creating converter from 7 to 5 h5py._conv - [DEBUG]{MainThread} - <module>() - Creating converter from 5 to 7 matplotlib - [DEBUG]{MainThread} - wrapper() - matplotlib data path: C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\matplotlib\mpl-data matplotlib - [DEBUG]{MainThread} - wrapper() - CONFIGDIR=C:\Users\dj_m0\.matplotlib matplotlib - [DEBUG]{MainThread} - <module>() - interactive is False matplotlib - [DEBUG]{MainThread} - <module>() - platform is win32 matplotlib - [DEBUG]{MainThread} - wrapper() - CACHEDIR=C:\Users\dj_m0\.matplotlib matplotlib.font_manager - [DEBUG]{MainThread} - _load_fontmanager() - Using fontManager instance from C:\Users\dj_m0\.matplotlib\fontlist-v330.json C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\py_ti\helper_loops.py:6: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details. def wilders_loop(data, n): C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\py_ti\helper_loops.py:18: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details. def kama_loop(data, sc, n_er, length): C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\py_ti\helper_loops.py:33: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details. def psar_loop(psar, high, low, af_step, max_af): C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\py_ti\helper_loops.py:100: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details. def supertrend_loop(close, basic_ub, basic_lb, n): C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\py_ti\helper_loops.py:138: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details. def fib_loop(n): yfinance - [DEBUG]{MainThread} - wrapper() - Entering history() yfinance - [DEBUG]{MainThread} - history() - AAPL: Yahoo GET parameters: {'range': '60d', 'interval': '15m', 'includePrePost': True, 'events': 'div,splits,capitalGains'} urllib3.connectionpool - [DEBUG]{MainThread} - _new_conn() - Starting new HTTPS connection (1): query2.finance.yahoo.com:443 urllib3.connectionpool - [DEBUG]{MainThread} - _make_request() - https://query2.finance.yahoo.com:443 "GET /v8/finance/chart/AAPL?range=60d&interval=15m&includePrePost=True&events=div%2Csplits%2CcapitalGains HTTP/1.1" 200 None yfinance - [DEBUG]{MainThread} - history() - AAPL: yfinance received OHLC data: 2023-06-13 08:00:00 -> 2023-09-07 23:45:00 yfinance - [DEBUG]{MainThread} - history() - AAPL: OHLC after cleaning: 2023-06-13 04:00:00-04:00 -> 2023-09-07 19:45:00-04:00 yfinance - [DEBUG]{MainThread} - history() - AAPL: OHLC after combining events: 2023-06-13 04:00:00-04:00 -> 2023-09-07 19:45:00-04:00 yfinance - [DEBUG]{MainThread} - history() - AAPL: yfinance returning OHLC: 2023-06-13 04:00:00-04:00 -> 2023-09-07 19:45:00-04:00 yfinance - [DEBUG]{MainThread} - wrapper() - Exiting history() MONTH_3_ADD_LO The action history is searched for Files: Read historical data from: d_price/RAW_alpha/alpha_AAPL_1Min_20230905__20230712.csv Traceback (most recent call last): File "1_Get_technical_indicators.py", line 46, in <module> df_download = yhoo_history_stock.get_favs_SCALA_csv_stocks_history_Download_list(list_stocks, CSV_NAME, opion, GENERATED_JSON_RELATIONS = GENERATED_JSON_RELATIONS) File "C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\yhoo_history_stock.py", line 221, in get_favs_SCALA_csv_stocks_history_Download_list df_all_generate_history ,df_l = get_favs_SCALA_csv_stocks_history_Download_One(df_all_generate_history, l, opion) File "C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\yhoo_history_stock.py", line 246, in get_favs_SCALA_csv_stocks_history_Download_One df_l, df_RAW = get_stock_history_Tech_download(l, opion, get_technical_data=True, File "C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\yhoo_history_stock.py", line 85, in get_stock_history_Tech_download df_his = __select_dowload_time_config(interval, opion, prepost, stockId) File "C:\Users\dj_m0\Documents\PythonScripts\stocks-prediction-Machine-learning-RealTime-TensorFlow\yhoo_history_stock.py", line 183, in __select_dowload_time_config df_path_raw = pd.read_csv("d_price/RAW_alpha/" + patH_raw, index_col='Date', sep='\t') File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\readers.py", line 912, in read_csv return _read(filepath_or_buffer, kwds) File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\readers.py", line 583, in _read return parser.read(nrows) File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\readers.py", line 1704, in read ) = self._engine.read( # type: ignore[attr-defined] File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 335, in read index, column_names = self._make_index(date_data, alldata, names) File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\base_parser.py", line 363, in _make_index simple_index = self._get_simple_index(alldata, columns) File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\base_parser.py", line 395, in _get_simple_index i = ix(idx) File "C:\Users\dj_m0\anaconda3\envs\crypto_ml\lib\site-packages\pandas\io\parsers\base_parser.py", line 390, in ix raise ValueError(f"Index {col} invalid") ValueError: Index Date invalid
alpha_AAPL_1Min_20230905__20230712.csv
alpha_TSLA_1Min_20230905__20230712.csv