时间戳和int进行比较问题
This commit is contained in:
parent
dee9cdb16a
commit
b14ed06579
@ -1577,21 +1577,24 @@ class FreqaiPrimer(IStrategy):
|
||||
trend_scores.append(50)
|
||||
continue
|
||||
|
||||
# 获取时间戳 - 统一使用无时区的时间戳
|
||||
# 获取时间戳 - 统一使用整数时间戳
|
||||
try:
|
||||
last_idx = hist_df.index[-1]
|
||||
if isinstance(last_idx, pd.Timestamp):
|
||||
# 确保时间戳是无时区的
|
||||
# 确保时间戳是无时区的整数
|
||||
ts = last_idx.tz_localize(None) if last_idx.tz else last_idx
|
||||
timestamp = int(ts.timestamp())
|
||||
elif isinstance(last_idx, (int, np.integer)):
|
||||
# 如果索引已经是整数,直接使用
|
||||
timestamp = int(last_idx)
|
||||
elif hasattr(last_idx, 'timestamp'):
|
||||
timestamp = int(last_idx.timestamp())
|
||||
else:
|
||||
# 使用UTC时间避免时区问题
|
||||
timestamp = int(pd.Timestamp.utcnow().timestamp()) + i
|
||||
# 使用当前时间的整数时间戳
|
||||
timestamp = int(pd.Timestamp.utcnow().timestamp())
|
||||
except Exception as e:
|
||||
# 使用UTC时间避免时区问题
|
||||
timestamp = int(pd.Timestamp.utcnow().timestamp()) + i
|
||||
# 使用当前时间的整数时间戳作为fallback
|
||||
timestamp = int(pd.Timestamp.utcnow().timestamp())
|
||||
|
||||
# 获取趋势得分
|
||||
score = self.get_trend_score_with_cache(
|
||||
|
||||
37
test_timestamp.py
Normal file
37
test_timestamp.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
测试时间戳处理的脚本
|
||||
"""
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
|
||||
def test_timestamp_handling():
|
||||
"""测试时间戳处理逻辑"""
|
||||
|
||||
# 创建测试数据
|
||||
dates = pd.date_range('2024-01-01', periods=100, freq='1min')
|
||||
df = pd.DataFrame({
|
||||
'close': np.random.randn(100),
|
||||
'volume': np.random.randn(100)
|
||||
}, index=dates)
|
||||
|
||||
print("测试数据创建成功")
|
||||
print(f"数据类型: {type(df.index[0])}")
|
||||
print(f"索引示例: {df.index[0]}")
|
||||
|
||||
# 测试时间戳转换
|
||||
for i in range(-10, 0):
|
||||
last_idx = df.index[i]
|
||||
if isinstance(last_idx, pd.Timestamp):
|
||||
ts = last_idx.tz_localize(None) if last_idx.tz else last_idx
|
||||
timestamp = int(ts.timestamp())
|
||||
print(f"索引 {i}: {last_idx} -> {timestamp}")
|
||||
else:
|
||||
timestamp = int(last_idx) if isinstance(last_idx, (int, np.integer)) else int(pd.Timestamp.utcnow().timestamp())
|
||||
print(f"索引 {i}: {last_idx} -> {timestamp}")
|
||||
|
||||
print("时间戳处理测试完成")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_timestamp_handling()
|
||||
Loading…
x
Reference in New Issue
Block a user