paperone/paperone.py
Giulio De Pasquale 3ae586c39c init
2025-10-10 21:29:21 +01:00

94 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python
import requests
import math
from datetime import datetime
from dotenv import load_dotenv
from os import environ
load_dotenv()
API_KEY = environ.get("API_KEY")
def is_trading_day(date):
return date.weekday() not in [5, 6]
def calculate_candles_to_target_date(target_date, interval="1h", current_time=None):
"""
Calculate the number of candles needed to go back from now to the target date.
"""
# Parse target_date if it's a string
if isinstance(target_date, str):
try:
target_date = datetime.strptime(target_date, "%Y-%m-%d %H:%M:%S")
except ValueError:
target_date = datetime.strptime(target_date, "%Y-%m-%d")
# Set current_time to now if not provided
if current_time is None:
current_time = datetime.now()
# Calculate time difference
time_diff = current_time - target_date
time_diff_seconds = time_diff.total_seconds()
# Parse interval to get candle duration in seconds
interval_map = {
"1m": 60,
"5m": 300,
"15m": 900,
"30m": 1800,
"1h": 3600,
"2h": 7200,
"4h": 14400,
"12h": 43200,
"1d": 86400,
"1w": 604800,
}
candle_duration_seconds = interval_map[interval]
# Calculate number of candles (round up)
num_candles = math.ceil(time_diff_seconds / candle_duration_seconds)
return num_candles
url = "https://api.taapi.io/rsi"
target_date = "2025-08-11 14:30:00"
interval = "1d"
candles = calculate_candles_to_target_date(target_date, interval)
params = {
"secret": API_KEY,
"symbol": "AAPL",
"interval": interval,
"type": "stocks",
"gaps": "false",
"addResultTimestamp": "true",
"backtrack": candles,
"results": "14",
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
print("RSI Values for AAPL on August 11, 2025 (Hourly):")
for rsi_val, ts in zip(data["value"], data["timestamp"]):
dt = datetime.fromtimestamp(ts)
if not is_trading_day(dt):
continue
print(f"{dt.strftime('%Y-%m-%d %H:%M:%S')} | RSI: {rsi_val:.2f}")
else:
print(f"Error: {response.status_code}")
print(response.text)