我是编程和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 ~AppDataLocalProgramsPythonPython310libsite-packagespandascoregeneric.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().
建议进行任何输入。提前感谢!
这里的问题是您在第一个if条件中写了什么:df[df['Time'].dt.hour == (8)]
。这段代码不返回布尔值,而是返回一个数据帧,其中所选的行是属性Time等于8的行。
如果您希望保留for循环结构,则应该使用if t.hour == 8
更改条件。但请记住,对于panda,您应该避免使用for循环并使用行过滤(您可以在panda官方教程中阅读更多关于这一问题的内容(。
我的解决方案是:
test2.loc[test2['Time'].dt.hour == (8), 'TakeProfit'] = (test2['atr'] * (1.5)) test2.loc[test2['Time'].dt.hour == (8), 'StopLoss'] = (test2['atr'] * (-1)) test2.loc[test2['Time'].dt.hour != (8), 'TakeProfit'] = (0) test2.loc[test2['Time'].dt.hour != (8), 'StopLoss'] = (0)
我在这篇文章中读到:在这里输入链接描述
这篇文章描述了我的问题的更多选项。但我第一次就成功了。