Update data.py
Browse files
data.py
CHANGED
|
@@ -11,15 +11,16 @@ from datetime import datetime, timedelta
|
|
| 11 |
from config import CRYPTOCOMPARE_API_KEY, CRYPTOCOMPARE_BASE_URL
|
| 12 |
from utils import limpiar_texto, expand_money, descargar_tabla_web
|
| 13 |
|
| 14 |
-
# Diccionario global para almacenar datos históricos
|
| 15 |
-
all_historical_data = {}
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
|
|
|
|
|
|
| 23 |
end_time = datetime.now()
|
| 24 |
start_time = end_time - timedelta(days=10 * 365)
|
| 25 |
to_timestamp = int(end_time.timestamp())
|
|
@@ -31,12 +32,11 @@ def fetch_btc_data_cryptocompare():
|
|
| 31 |
"tsym": CURRENCY,
|
| 32 |
"limit": LIMIT_PER_CALL,
|
| 33 |
"toTs": to_timestamp,
|
| 34 |
-
"api_key":
|
| 35 |
}
|
| 36 |
try:
|
| 37 |
-
response = requests.get(
|
| 38 |
data = response.json()
|
| 39 |
-
|
| 40 |
if data['Response'] == 'Error':
|
| 41 |
print(f"API Error: {data['Message']}")
|
| 42 |
break
|
|
@@ -48,8 +48,10 @@ def fetch_btc_data_cryptocompare():
|
|
| 48 |
all_data.extend(ohlcv_data)
|
| 49 |
to_timestamp = ohlcv_data[0]['time'] - 1
|
| 50 |
oldest_data_time = datetime.fromtimestamp(ohlcv_data[0]['time'])
|
|
|
|
| 51 |
|
| 52 |
if oldest_data_time <= start_time:
|
|
|
|
| 53 |
break
|
| 54 |
|
| 55 |
time.sleep(1)
|
|
@@ -57,48 +59,30 @@ def fetch_btc_data_cryptocompare():
|
|
| 57 |
print(f"Network error: {e}")
|
| 58 |
time.sleep(5)
|
| 59 |
except Exception as e:
|
| 60 |
-
print(f"
|
| 61 |
break
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
btc_df.index = btc_df.index.date
|
| 71 |
-
btc_df.columns = btc_df.columns.str.title()
|
| 72 |
-
btc_df.index = pd.to_datetime(btc_df.index)
|
| 73 |
-
|
| 74 |
-
all_historical_data['BTC-USD'] = btc_df
|
| 75 |
-
return btc_df
|
| 76 |
-
|
| 77 |
-
def generar_tickers():
|
| 78 |
-
"""Genera una lista global de tickers a partir de las tablas de stocks y ETFs."""
|
| 79 |
-
tickers = []
|
| 80 |
-
url_stocks = "https://bitcointreasuries.net/"
|
| 81 |
-
url_etfs = "https://bitcointreasuries.net/etfs-and-exchanges"
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
df_stocks['Name'] = df_stocks[2].apply(limpiar_texto)
|
| 88 |
-
tickers.extend(df_stocks['Name'].dropna().tolist())
|
| 89 |
-
|
| 90 |
-
if df_etfs is not None and not df_etfs.empty:
|
| 91 |
-
df_etfs['Name'] = df_etfs[2].apply(limpiar_texto)
|
| 92 |
-
tickers.extend(df_etfs['Name'].dropna().tolist())
|
| 93 |
-
|
| 94 |
-
return tickers
|
| 95 |
|
|
|
|
| 96 |
def data_option(ticker, date, option_type, size_of_the_window=4):
|
| 97 |
-
"""Obtiene datos de opciones para un ticker en una fecha específica."""
|
| 98 |
price = yf.Ticker(ticker).info['regularMarketPrice']
|
| 99 |
chain = yf.Ticker(ticker).option_chain(date)
|
| 100 |
-
data = chain.puts if option_type == 'put' else chain.calls
|
| 101 |
-
data = data.iloc[:, [0, 2, 3, 8, 10, 11, 13]]
|
| 102 |
data['impliedVolatility'] = data['impliedVolatility'].apply(lambda x: f"{x*100:.2f}%")
|
| 103 |
data = data.sort_values(by='strike', key=lambda x: abs(x - price))
|
| 104 |
|
|
@@ -113,3 +97,4 @@ def data_option(ticker, date, option_type, size_of_the_window=4):
|
|
| 113 |
data = pd.concat([data.head(size_of_the_window), price_row]).sort_values(by='strike')
|
| 114 |
data = pd.concat([date_row, data])
|
| 115 |
return data.reset_index(drop=True)
|
|
|
|
|
|
| 11 |
from config import CRYPTOCOMPARE_API_KEY, CRYPTOCOMPARE_BASE_URL
|
| 12 |
from utils import limpiar_texto, expand_money, descargar_tabla_web
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Configuración de la API
|
| 16 |
+
API_KEY = "TU_API_KEY_DE_CRYPTOCOMPARE"
|
| 17 |
+
BASE_URL = "https://min-api.cryptocompare.com/data/v2/histohour"
|
| 18 |
+
SYMBOL = "BTC"
|
| 19 |
+
CURRENCY = "USD"
|
| 20 |
+
LIMIT_PER_CALL = 2000
|
| 21 |
|
| 22 |
+
# Descarga de datos históricos de BTC
|
| 23 |
+
def fetch_btc_hourly_data():
|
| 24 |
end_time = datetime.now()
|
| 25 |
start_time = end_time - timedelta(days=10 * 365)
|
| 26 |
to_timestamp = int(end_time.timestamp())
|
|
|
|
| 32 |
"tsym": CURRENCY,
|
| 33 |
"limit": LIMIT_PER_CALL,
|
| 34 |
"toTs": to_timestamp,
|
| 35 |
+
"api_key": API_KEY
|
| 36 |
}
|
| 37 |
try:
|
| 38 |
+
response = requests.get(BASE_URL, params=params)
|
| 39 |
data = response.json()
|
|
|
|
| 40 |
if data['Response'] == 'Error':
|
| 41 |
print(f"API Error: {data['Message']}")
|
| 42 |
break
|
|
|
|
| 48 |
all_data.extend(ohlcv_data)
|
| 49 |
to_timestamp = ohlcv_data[0]['time'] - 1
|
| 50 |
oldest_data_time = datetime.fromtimestamp(ohlcv_data[0]['time'])
|
| 51 |
+
print(f"Fetched data up to {oldest_data_time.strftime('%Y-%m-%d %H:%M:%S')}. Total records: {len(all_data)}")
|
| 52 |
|
| 53 |
if oldest_data_time <= start_time:
|
| 54 |
+
print(f"Reached desired start time ({start_time.strftime('%Y-%m-%d')}). Stopping.")
|
| 55 |
break
|
| 56 |
|
| 57 |
time.sleep(1)
|
|
|
|
| 59 |
print(f"Network error: {e}")
|
| 60 |
time.sleep(5)
|
| 61 |
except Exception as e:
|
| 62 |
+
print(f"An unexpected error occurred: {e}")
|
| 63 |
break
|
| 64 |
|
| 65 |
+
btc_hour_10y = pd.DataFrame(all_data)
|
| 66 |
+
btc_hour_10y['time'] = pd.to_datetime(btc_hour_10y['time'], unit='s')
|
| 67 |
+
btc_hour_10y.set_index('time', inplace=True)
|
| 68 |
+
btc_hour_10y.sort_index(inplace=True)
|
| 69 |
+
btc_hour_10y = btc_hour_10y[~btc_hour_10y.index.duplicated(keep='first')]
|
| 70 |
+
btc_hour_10y.to_csv('btc_hourly_cryptocompare_10_years.csv')
|
| 71 |
+
print("Data saved to btc_hourly_cryptocompare_10_years.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
btc_hour_10y.index = btc_hour_10y.index.tz_localize('UTC').tz_convert('America/New_York')
|
| 74 |
+
btc_hour_10y = btc_hour_10y[btc_hour_10y.index.hour == 16]
|
| 75 |
+
btc_hour_10y.index = btc_hour_10y.index.date
|
| 76 |
+
btc_hour_10y.columns = btc_hour_10y.columns.str.title()
|
| 77 |
+
btc_hour_10y.index = pd.to_datetime(btc_hour_10y.index)
|
| 78 |
|
| 79 |
+
return {'BTC-USD': btc_hour_10y}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# Función para obtener opciones financieras
|
| 82 |
def data_option(ticker, date, option_type, size_of_the_window=4):
|
|
|
|
| 83 |
price = yf.Ticker(ticker).info['regularMarketPrice']
|
| 84 |
chain = yf.Ticker(ticker).option_chain(date)
|
| 85 |
+
data = chain.puts.iloc[:, [0, 2, 3, 8, 10, 11, 13]] if option_type == 'put' else chain.calls.iloc[:, [0, 2, 3, 8, 10, 11, 13]]
|
|
|
|
| 86 |
data['impliedVolatility'] = data['impliedVolatility'].apply(lambda x: f"{x*100:.2f}%")
|
| 87 |
data = data.sort_values(by='strike', key=lambda x: abs(x - price))
|
| 88 |
|
|
|
|
| 97 |
data = pd.concat([data.head(size_of_the_window), price_row]).sort_values(by='strike')
|
| 98 |
data = pd.concat([date_row, data])
|
| 99 |
return data.reset_index(drop=True)
|
| 100 |
+
|