【发布时间】:2022-01-18 21:06:56
【问题描述】:
我是编程和 python 的新手。经过数小时的研究,我想向社区寻求帮助。我想使用数据来回测基于伦敦时段开始的交易策略。
我有一个数据框:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 28766 entries, 0 to 28765
Data columns (total 6 columns):
| # | Column | Non-Null Count |Dtype |
|--- |------ |-------------- |----- |
| 0 |Time |28766 non-null |datetime64[ns]|
| 1 |Open |28766 non-null |float64 |
| 2 |...|
| |Time |Open | High | Low |Close |Volume |
|--- |--------------------- |------- |------- |------- |------- | ------|
|8 |2017-05-23 08:00:00 |2180.7 |2187.2 |2139.2 | 2170.2 | 97.0 |
现在我想定义一个函数,它可以识别其中包含 08:00 GMT 的每一行:
def LondonSession(df):
df = df.copy()
for t in df['Time']:
if df[df['Time'].dt.hour == (8)]:
df['StopLoss'] = technicals(df)['atr'] * (-1)
df['TakeProfit'] = technicals(df)['atr'] * (2)
else:
df['StopLoss'] = technicals(df)['atr'] * (0)
df['TakeProfit'] = technicals(df)['atr'] * (0)
return df
print(LondonSession(df)[0:10])
很遗憾,我对如何解决错误消息一无所知:
ValueError Traceback (most recent call last) Input In [204], in <module>
9 df['TakeProfit'] = technicals(df)['atr'] * (0)
10 return df
---> 11 print(LondonSession(df)[0:10])
Input In [204], in LondonSession(df)
2 df = df.copy()
3 for t in df['Time']:
----> 4 if df[df['Time'].dt.hour == (8)]:
5 df['StopLoss'] = technicals(df)['atr'] * (-1)
6 df['TakeProfit'] = technicals(df)['atr'] * (2)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py:1537, in NDFrame.__nonzero__(self) 1535 @final 1536 def
__nonzero__(self):
-> 1537 raise ValueError( 1538 f"The truth value of a {type(self).__name__} is ambiguous. " 1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 1540 )
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
建议使用任何输入。 提前致谢!
【问题讨论】:
标签: python pandas dataframe datetime finance