我正在尝试排除故障并理解为什么第一天[2024-03-27]打印了tr2和tr3的值,而它应该打印nan nan,因为2024-03-27没有前一天
我只希望它根据仅打印的天数计算数据
我最初认为shift(-1)可能有问题,但仍然可能存在问题。不过,正因为如此,计算才正确。
我还认为 API 收集的数据可能超过 4 天,这可以解释为什么没有 nan nan。
这是一个示例输出:
Date Open High Low Close TR1 TR2 TR3
2024-03-27 5.8100 6.0100 5.4200 5.9300 0.59 0.2 0.39 ( the first day should be nan nan)
2024-03-28 5.9400 6.3200 5.8301 5.8900 0.49 0.39 0.1
2024-04-01 5.9100 5.9400 5.5500 5.5600 0.39 0.05 0.34
2024-04-02 5.2800 5.4500 5.0900 5.3800 0.36 0.11 0.47
我的脚本:
import requests
import pandas as pd
from datetime import datetime, timedelta
import time
# Calculate start and end dates for daily historical data
end_date_daily = datetime.now().strftime('%Y-%m-%d')
start_date_daily = (datetime.now() - timedelta(days=4)).strftime('%Y-%m-%d')
# Construct the API URL for daily historical data
api_url_daily = f'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}&datatype=json&start_date={start_date_daily}&end_date={end_date_daily}'
# Fetch and process daily historical price data
response_daily = requests.get(api_url_daily)
if response_daily.status_code == 200:
historical_data_daily = response_daily.json()['Time Series (Daily)']
# Convert daily historical data to DataFrame
df_daily = pd.DataFrame(historical_data_daily).T
df_daily.index = pd.to_datetime(df_daily.index)
# Reverse the DataFrame to have oldest data at the top
df_daily = df_daily.sort_index(ascending=False)
# Convert column types to float
df_daily['High'] = df_daily['2. high'].astype(float)
df_daily['Low'] = df_daily['3. low'].astype(float)
df_daily['Close'] = df_daily['4. close'].astype(float)
# Function to calculate tr1, tr2, tr3
def calculate_tr(df):
df['tr1'] = df['High'] - df['Low']
df['tr2'] = abs(df['High'] - df['Close'].shift(-1))
df['tr3'] = abs(df['Low'] - df['Close'].shift(-1))
# Calculate tr1, tr2, tr3
calculate_tr(df_daily)
# Print DataFrame after calculating tr1, tr2, tr3
print("\nDataFrame after calculating tr1, tr2, tr3:")
print("Date Open High Low Close TR1 TR2 TR3")
for date in reversed(df_daily.head(4).index):
row = df_daily.loc[date]
tr1_rounded = round(row['tr1'], 4)
tr2_rounded = round(row['tr2'], 4)
tr3_rounded = round(row['tr3'], 4)
print(f"{date.strftime('%Y-%m-%d')} {row['1. open']} {row['2. high']} {row['3. low']} {row['4. close']} {round(tr1_rounded, 3)} {round(tr2_rounded, 3)} {round(tr3_rounded, 3)}")
IIUC,你可以这样做:
def calculate_tr(df):
high_shifted, close_shifted, low_shifted = (
df["High"].shift(),
df["Close"].shift(),
df["Low"].shift(),
)
df["tr1"] = df["High"] - df["Low"]
df["tr2"] = (high_shifted - close_shifted).abs()
df["tr3"] = (low_shifted - close_shifted).abs()
calculate_tr(df_daily)
print(df_daily)
打印:
Date Open High Low Close tr1 tr2 tr3
0 2024-03-27 5.81 6.01 5.4200 5.93 0.5900 NaN NaN
1 2024-03-28 5.94 6.32 5.8301 5.89 0.4899 0.08 0.5100
2 2024-04-01 5.91 5.94 5.5500 5.56 0.3900 0.43 0.0599
3 2024-04-02 5.28 5.45 5.0900 5.38 0.3600 0.38 0.0100