根据数据框中日期时间的时间做出决定
Making decisions based on the times of datetime in a dataframe
我是编程新手 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().
推荐任何输入。
提前致谢!
这里的问题是您在第一个 if 条件中写的内容:df[df['Time'].dt.hour == (8)]
。此代码不是 return 布尔值,而是一个数据框,其中所选行是属性时间等于 8 的行。
如果您想保留 for 循环结构,则应使用 if t.hour == 8
更改条件。但请记住,对于 pandas,您应该避免使用 for 循环并使用行过滤(您可以在 pandas official tutorial 上阅读更多相关内容)。
我的解决方案是:
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)
我在这篇文章中读到:enter link description here
这篇文章描述了我的问题的更多选项。但是我第一次成功了。
我是编程新手 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().
推荐任何输入。 提前致谢!
这里的问题是您在第一个 if 条件中写的内容:df[df['Time'].dt.hour == (8)]
。此代码不是 return 布尔值,而是一个数据框,其中所选行是属性时间等于 8 的行。
如果您想保留 for 循环结构,则应使用 if t.hour == 8
更改条件。但请记住,对于 pandas,您应该避免使用 for 循环并使用行过滤(您可以在 pandas official tutorial 上阅读更多相关内容)。
我的解决方案是:
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)
我在这篇文章中读到:enter link description here
这篇文章描述了我的问题的更多选项。但是我第一次成功了。