Closed CharlieChi closed 1 year ago
Hi, you can try the following code to see if it can solve your problem.
positions_df = pd.DataFrame(columns=['symbol', 'amount', 'price'])
for date, position in positions.items():
for symbol, _position in position.position.items():
if symbol == 'yourSymbol':
positions_df = positions_df.append({
'price': _position['price'],
'symbol': symbol,
'amount': _position['amount']
}, ignore_index=True)
Hi, you can try the following code to see if it can solve your problem.
positions_df = pd.DataFrame(columns=['symbol', 'amount', 'price']) for date, position in positions.items(): for symbol, _position in position.position.items(): if symbol == 'yourSymbol': positions_df = positions_df.append({ 'price': _position['price'], 'symbol': symbol, 'amount': _position['amount'] }, ignore_index=True)
thanks with a little customization it works perfectly
positions_list = []
for date, position in positions.items():
for symbol, _position in position.position.items():
if symbol.startswith(("SH.", "SZ.")):
positions_list.append({
'date': date,
'price': _position['price'],
'symbol': symbol,
'amount': _position['amount']
})
positions_df = pd.DataFrame(positions_list)
❓ Questions and Help
positions = recorder.load_object("portfolio_analysis/positions_normal_1day.pkl")
the historical positions in backtest is a nested dict. how to transfer it into a dataframe or some sorts to give a better view?
We sincerely suggest you to carefully read the documentation of our library as well as the official paper. After that, if you still feel puzzled, please describe the question clearly under this issue.