Delete data.py
Browse files
data.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
# Módulo para descarga y preparación de datos desde APIs y web
|
| 3 |
-
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import numpy as np
|
| 6 |
-
import requests
|
| 7 |
-
import time
|
| 8 |
-
import yfinance as yf
|
| 9 |
-
from datetime import datetime, timedelta
|
| 10 |
-
|
| 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())
|
| 27 |
-
all_data = []
|
| 28 |
-
|
| 29 |
-
while True:
|
| 30 |
-
params = {
|
| 31 |
-
"fsym": SYMBOL,
|
| 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
|
| 43 |
-
|
| 44 |
-
ohlcv_data = data['Data']['Data']
|
| 45 |
-
if not ohlcv_data:
|
| 46 |
-
break
|
| 47 |
-
|
| 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)
|
| 58 |
-
except requests.exceptions.RequestException as e:
|
| 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 |
-
|
| 89 |
-
price_row = pd.DataFrame([[
|
| 90 |
-
'░░░░░░░ Spot Price ░░░░░░░', price, '░'*13, '░'*10, '░'*18, '░'*11, '░'*10
|
| 91 |
-
]], columns=data.columns)
|
| 92 |
-
|
| 93 |
-
date_row = pd.DataFrame([[
|
| 94 |
-
f"▓▓▓▓▓▓▓ {date} ▓▓▓▓▓▓▓", '▓'*7, '▓'*13, '▓'*10, '▓'*18, '▓'*11, '▓'*10
|
| 95 |
-
]], columns=data.columns)
|
| 96 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|