【发布时间】:2019-09-27 17:50:33
【问题描述】:
我正在尝试创建一个 Python 数据框来跟踪我的股票买入、卖出和利润。现在我有一个熊猫数据框,其中一列中包含价格,另一列中包含买入-卖出-持有触发器以及时间戳。
我尝试创建一个数据框来跟踪买卖价格和交易利润,但没有成功,很难找到最后一笔交易来更新它。在这里,我正在尝试为交易创建一个数据框
Tdf.tail 的错误和当前输出
Empty DataFrame
Columns: [Buy, Sell, Profit]
Index: []
single positional indexer is out-of-bounds
df 是什么样子的
timestamp Price decide
2019-08-19 3.0000 1.0
2019-08-20 2.6200 0.0
2019-08-21 2.3200 0.0
2019-08-22 2.1400 0.0
2019-08-23 1.9500 -1.0
Price = [['2019-08-19',3.0000,1.0],['2019-08-20',2.6200,0.0],['2019-08-21',2.3200,0.0],['2019-08-22',2.1400,0.0],['2019-08-23',1.9500,-1.0]]
df = pd.DataFrame(Price,columns = ['timestamp','Price','decide'])
df.set_index('timestamp',inplace=True)
Trades = []
Tdf = pd.DataFrame(Trades,columns = ['Buy','Sell','Profit'])
for y in df.index.values:#Buy = 1.0 , Sell = -1.0
if df['decide'].iloc[y] == 1.0:
Tdf.append({'Buy' : df['Price'][y]},ignore_index=True)
if df['decide'].iloc[y] == -1.0:
profit = df['Price'].iloc[y] - Tdf['Buy'].iloc[-1]
Tdf.at[-1,'Sell'] = df['Price'].iloc[y]
Tdf.at[-1,'Profit'] = profit
预期是看到 Tdf 的样子
buy sell profit
0 3.00 1.95 -1.05
【问题讨论】:
-
你现在看到了什么?也请发布实际输出。
-
刚刚添加了实际输出
-
请看mcve。
-
这样更好吗?