本文介绍了如何形成一个新的数据框,以反映原始数据框中值的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您可以窥视未来,并且在整个价格历史记录中都准确地知道要买卖哪些股票,那么在给定Shares
的唯一值是1000
,0
,和-1000
?您还从1000
共享开始.您还可以交易2000
份额,将其从1000
直接获取到-1000
,反之亦然.
If you could peek into the future and know exactly what stocks to buy and sell given their entire price history, how would you implement it, given constraints that your only values of Shares
is 1000
, 0
, and -1000
? You also start with 1000
shares. You can also trade 2000
shares to get from 1000
directly to -1000
and vice versa.
我想要这样一个数据帧df_prices
:
I want to take a dataframe df_prices
like this:
JPM
2008-01-02 38.47
2008-01-03 38.20
2008-01-04 37.33
2008-01-07 37.71
2008-01-08 36.21
2008-01-09 36.72
并将其转换为df_trades
:
Symbol Order Shares
2008-01-02 JPM NOTHING 1000
2008-01-03 JPM HOLD 1000
2008-01-04 JPM HOLD 1000
2008-01-07 JPM BUY 0
2008-01-08 JPM SELL -1000
2008-01-09 JPM HOLD 0
使用此代码:
if df_prices['JPM'].shift(1) > df_prices['JPM'] & curr_hold == 0:
df_orders['Order'] = 'SELL'
df_orders['Shares'] += 1000
elif df_prices['JPM'].shift(1) < df_prices['JPM'] & curr_hold == 0:
df_orders['Order'] = 'BUY'
df_orders['Shares'] -= 1000
elif df_prices['JPM'].shift(1) > df_prices['JPM'] & curr_hold == -1000:
df_orders['Order'] = 'HOLD'
elif df_prices['JPM'].shift(1) < df_prices['JPM'] & curr_hold == 1000:
df_orders['Order'] = 'HOLD'
但是它不起作用.我收到错误消息:
It doesn't work however. I get the error:
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
推荐答案
我能够弄清楚.
这足够了:
for i in range(len(df_prices)):
if i == len(df_prices) - 1:
df_orders.set_value(i, 'Date', df_prices.index[i])
df_orders.set_value(i, 'Order', 'BUY')
df_orders.set_value(i, 'Shares', 0)
df_orders.set_value(i, 'Symbol', symbol)
break
if df_prices.loc[df_prices.index[i + 1]][symbol] > df_prices.loc[df_prices.index[i]][symbol] and curr_hold == 0:
curr_hold += 1000
df_orders.set_value(i, 'Date', df_prices.index[i])
df_orders.set_value(i, 'Order', 'BUY')
df_orders.set_value(i, 'Shares', 1000)
df_orders.set_value(i, 'Symbol', 'JPM')
elif df_prices.loc[df_prices.index[i + 1]][symbol] < df_prices.loc[df_prices.index[i]][symbol] and curr_hold == 0:
curr_hold -= 1000
df_orders.set_value(i, 'Date', df_prices.index[i])
df_orders.set_value(i, 'Order', 'SELL')
df_orders.set_value(i, 'Shares', 1000)
df_orders.set_value(i, 'Symbol', symbol)
elif df_prices.loc[df_prices.index[i + 1]][symbol] > df_prices.loc[df_prices.index[i]][symbol] and curr_hold == -1000:
curr_hold += 2000
df_orders.set_value(i, 'Date', df_prices.index[i])
df_orders.set_value(i, 'Order', 'BUY')
df_orders.set_value(i, 'Shares', 2000)
df_orders.set_value(i, 'Symbol', symbol)
elif df_prices.loc[df_prices.index[i + 1]][symbol] < df_prices.loc[df_prices.index[i]][symbol] and curr_hold == 1000:
curr_hold -= 2000
df_orders.set_value(i, 'Date', df_prices.index[i])
df_orders.set_value(i, 'Order', 'SELL')
df_orders.set_value(i, 'Shares', 2000)
df_orders.set_value(i, 'Symbol', symbol)
尽管我可能可以模块化很多.
although I could probably modularize a lot of it.
这篇关于如何形成一个新的数据框,以反映原始数据框中值的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!