Python 如何在一个数据帧中组合多个库存ohlc数据?
我正在尝试建立一个涉及多个外汇对和自定义指数的交易策略。所有这些都有OHLC数据,我需要将特定指标应用于某些方面,而不是其他方面 每个标记和标记对包含以下列:Python 如何在一个数据帧中组合多个库存ohlc数据?,python,pandas,dataframe,trading,ohlc,Python,Pandas,Dataframe,Trading,Ohlc,我正在尝试建立一个涉及多个外汇对和自定义指数的交易策略。所有这些都有OHLC数据,我需要将特定指标应用于某些方面,而不是其他方面 每个标记和标记对包含以下列: date, open, high, low, close 我希望所有这些外汇对和指数在一个数据帧内。我首先尝试使用以下方法: index_list = ['index_1', 'index_2', ...] pair_list = ['pair_1', 'pair_2', ...] df = pd.read_csv('index_1.
date, open, high, low, close
我希望所有这些外汇对和指数在一个数据帧内。我首先尝试使用以下方法:
index_list = ['index_1', 'index_2', ...]
pair_list = ['pair_1', 'pair_2', ...]
df = pd.read_csv('index_1.csv')
df['type'] = 'index'
df['name'] = index_list[0]
for index in index_list[1:]:
df1 = pd.read_csv(f'{index}.csv')
df1['type'] = 'index'
df1['name'] = index
df.append(df1)
for pair in pair_list:
df1 = pd.read_csv(f'{pair}.csv')
df1['type'] = 'pair'
df1['name'] = pair
df.append(df1)
df = df.set_index(['date', 'type', 'name']).sort_index()
这将按预期工作并返回以下数据帧:
date type name open high low close
2020-01-01 00:00:00 index index_1 0.60866 0.60906 0.60834 0.60900
index_2 0.69038 0.69040 0.69011 0.69030
pair pair_1 0.73149 0.73151 0.73131 0.73140
pair_2 1.03914 1.03945 1.03900 1.03924
... ... ... ... ... ... ...
当我想将某些特定指标应用于“货币”而不是“配对”(type列的值)时,问题就开始了
我使用ta来实施这些指标。例如,我曾用RSI尝试过以下方法:
df.loc[(df.index.get_level_values('type') == 'currency')].ta.rsi(close=df.loc[(df.index.get_level_values('type') == 'currency')].close, length=14, append=True)
但我有以下错误:
/home/user/.local/lib/python3.8/site-packages/pandas/core/frame.py:4300: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
return super().rename(
/home/user/miniconda3/lib/python3.8/site-packages/pandas_ta/core.py:387: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[ind_name] = result
尽管有“append=True”,RSI列也不会出现在数据帧中。有人能告诉我我的方法有什么问题吗
更一般地说,对于熊猫的单一交易策略,是否有更好/更聪明的方法处理多个OHLC数据?我应该使用多个数据帧还是将所有数据帧放在一个数据帧中是一种有效的方法?如果是后者,你有什么建议