Pandas 数据框交易 gain/loss
Pandas dataframe trading gain/loss
数据框
(忽略两个索引列)
level_0
index
Year
Month
Day
Open
High
Low
Close
Volume
Length
Polarity
Sentiment_Negative
Sentiment_Neutral
Sentiment_Positive
Target_variable
Predicted
0
0
0
2020
1
19
8941.45
9164.36
8620.08
8706.25
3.42173e+10
937.167
0.0884653
0
0
1
0
0
1
1
1
2020
1
18
8927.21
9012.2
8827.33
8942.81
3.23378e+10
1177.5
0.176394
0
0
1
1
1
2
2
2
2020
1
17
8725.21
8958.12
8677.32
8929.04
3.63721e+10
1580
0.216762
0
0
1
0
0
3
3
3
2020
1
16
8812.48
8846.46
8612.1
8723.79
3.1314e+10
1336.33
0.182707
0
0
1
0
0
描述
如果今天的收盘价大于昨天的收盘价,target_variable的值为1
如果今天的收盘价低于昨天的收盘价,target_variable的值为0
预测值是我的分类器的输出。
问题
我需要 运行 一些代码来跟踪如果我在分类器告诉我投资时投资能赚多少钱
我已经开始编写代码了
credit = 10000
for index, row in df.iterrows():
if row["Predicted"] == 1:
#print(row["Percentage_diff"])
credit = credit - 100
credit = credit + (100 * row["Percentage_diff"])
print(credit)
我的想法是,我从 10,000 的余额开始,每次分类器发出信号时投入 100。唯一的问题是,当我失去 8000 学分时。代码是否正确,分类器很差?
还是我代码有误?
我不是交易专家,所以我假设每天分类器告诉你交易,你会以开盘价买入,以收盘价卖出。
当分类器告诉您交易时,您可以从计算盈利或亏损的百分比开始。您可以通过从开盘价中减去收盘价并将其除以开盘价来做到这一点。
df["perc_diff"] = (df["Close"] - df["Open"])/df["open"]
当然,当分类器错误时,这将是负面的。要计算累积 profits/losses,您要做的就是迭代 add/subtract 您的 profit/loss 到您的资本。这意味着如果您投资 x
美元,在一天中有 profit/loss 百分比的 r
,您的新信用是 (1+r)*x
。所以一个简单的 for 循环可以这样做:
credit = 1 # your capital
for label, row in df.iterrows():
credit = (1 + row["Predicted"] * r) * row["perc_diff"]
print(credit)
编辑以解决您更新后的问题:
如果您想指定投资金额而不是全部资金,那么您可以使用:
credit = 1 # your capital
to_invest = 0.1 # money to invest
for label, row in df.iterrows():
# update invest
invest_update = (1 + row["Predicted"] * row["perc_diff"]) * to_invest
credit = credit - to_invest + invest_update
print(credit)
最后两行可以合为一行:
credit = credit + row["Predicted"] * row["perc_diff"] * to_invest
我认为代码是正确的,如果你输了,那可能是因为你的分类器性能不佳,但这应该从你对模型的评估(如准确性和精确度指标)中看出。另外,如果不是为时间序列做的分类器(比如逻辑回归),那么表现不好也是很合理的。
解决方案
df["Percentage_diff"] = (df["Close"] - df["Open"])/df["Open"]
credit = 10000
for index, row in df.iterrows():
if row["Predicted"] == 1:
#print(row["Percentage_diff"])
credit = credit - 100
credit = credit + ((100 * row["Percentage_diff"]) + 100)
print(credit)
感谢艾哈迈德,这是解决方案。
如果每次分类器发出投资信号时我的原始余额为 10000,我在开盘时投资 100 美元并在收盘时撤回,这会计算余额。
数据框
(忽略两个索引列)
level_0 | index | Year | Month | Day | Open | High | Low | Close | Volume | Length | Polarity | Sentiment_Negative | Sentiment_Neutral | Sentiment_Positive | Target_variable | Predicted | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 2020 | 1 | 19 | 8941.45 | 9164.36 | 8620.08 | 8706.25 | 3.42173e+10 | 937.167 | 0.0884653 | 0 | 0 | 1 | 0 | 0 |
1 | 1 | 1 | 2020 | 1 | 18 | 8927.21 | 9012.2 | 8827.33 | 8942.81 | 3.23378e+10 | 1177.5 | 0.176394 | 0 | 0 | 1 | 1 | 1 |
2 | 2 | 2 | 2020 | 1 | 17 | 8725.21 | 8958.12 | 8677.32 | 8929.04 | 3.63721e+10 | 1580 | 0.216762 | 0 | 0 | 1 | 0 | 0 |
3 | 3 | 3 | 2020 | 1 | 16 | 8812.48 | 8846.46 | 8612.1 | 8723.79 | 3.1314e+10 | 1336.33 | 0.182707 | 0 | 0 | 1 | 0 | 0 |
描述
如果今天的收盘价大于昨天的收盘价,target_variable的值为1
如果今天的收盘价低于昨天的收盘价,target_variable的值为0
预测值是我的分类器的输出。
问题
我需要 运行 一些代码来跟踪如果我在分类器告诉我投资时投资能赚多少钱
我已经开始编写代码了
credit = 10000
for index, row in df.iterrows():
if row["Predicted"] == 1:
#print(row["Percentage_diff"])
credit = credit - 100
credit = credit + (100 * row["Percentage_diff"])
print(credit)
我的想法是,我从 10,000 的余额开始,每次分类器发出信号时投入 100。唯一的问题是,当我失去 8000 学分时。代码是否正确,分类器很差?
还是我代码有误?
我不是交易专家,所以我假设每天分类器告诉你交易,你会以开盘价买入,以收盘价卖出。
当分类器告诉您交易时,您可以从计算盈利或亏损的百分比开始。您可以通过从开盘价中减去收盘价并将其除以开盘价来做到这一点。
df["perc_diff"] = (df["Close"] - df["Open"])/df["open"]
当然,当分类器错误时,这将是负面的。要计算累积 profits/losses,您要做的就是迭代 add/subtract 您的 profit/loss 到您的资本。这意味着如果您投资 x
美元,在一天中有 profit/loss 百分比的 r
,您的新信用是 (1+r)*x
。所以一个简单的 for 循环可以这样做:
credit = 1 # your capital
for label, row in df.iterrows():
credit = (1 + row["Predicted"] * r) * row["perc_diff"]
print(credit)
编辑以解决您更新后的问题:
如果您想指定投资金额而不是全部资金,那么您可以使用:
credit = 1 # your capital
to_invest = 0.1 # money to invest
for label, row in df.iterrows():
# update invest
invest_update = (1 + row["Predicted"] * row["perc_diff"]) * to_invest
credit = credit - to_invest + invest_update
print(credit)
最后两行可以合为一行:
credit = credit + row["Predicted"] * row["perc_diff"] * to_invest
我认为代码是正确的,如果你输了,那可能是因为你的分类器性能不佳,但这应该从你对模型的评估(如准确性和精确度指标)中看出。另外,如果不是为时间序列做的分类器(比如逻辑回归),那么表现不好也是很合理的。
解决方案
df["Percentage_diff"] = (df["Close"] - df["Open"])/df["Open"]
credit = 10000
for index, row in df.iterrows():
if row["Predicted"] == 1:
#print(row["Percentage_diff"])
credit = credit - 100
credit = credit + ((100 * row["Percentage_diff"]) + 100)
print(credit)
感谢艾哈迈德,这是解决方案。
如果每次分类器发出投资信号时我的原始余额为 10000,我在开盘时投资 100 美元并在收盘时撤回,这会计算余额。