admin管理员组文章数量:1648944
交易中的胜率和盈亏比估算
1.定义
胜率是指交易者在一定时间内成功交易的次数占总交易次数的比例。例如,如果交易者在10次交易中成功了6次,那么他的胜率就是60%。
盈亏比是指交易者每笔成功交易的盈利与每笔失败交易的亏损之间的比例。例如,如果交易者每笔成功交易盈利100元,而每笔失败交易亏损50元,那么他的盈亏比就是2:1。
2.计算逻辑
假设一个交易者在100次交易中的胜率为60%,盈亏比为2:1。模拟计算他的总盈利情况:
(1)确定交易者在100次交易中的成功次数和失败次数。根据胜率60%,成功次数为60次,失败次数为40次。
(2)计算每次成功交易和失败交易的盈亏情况。根据盈亏比2:1,每次成功交易盈利2单位,每次失败交易亏损1单位。
(3)将成功交易的盈利和失败交易的亏损相加,得到总盈利情况。即:60次成功交易×2单位盈利 - 40次失败交易×1单位亏损 = 120单位盈利 - 40单位亏损 = 80单位总盈利 。
3.简单模拟计算
实际交易中,胜率设为55%,盈亏比设为4:3,比较合理的范围。
代码:
import numpy as np
import pandas as pd
import random
#定义维度,交易数量
v_size = 1000
# 定义概率分布 ,设置胜率
probabilities = [0.45, 0.55]
# 定义盈亏比 4:3
v_win = 400.0
v_loss = -300.0
# 生成随机数组
arr_probability = np.random.choice([0,1], size=v_size, p=probabilities, replace=True)
# 将随机数组转换为pandas Series
series_probability = pd.Series(arr_probability)
df_profit = pd.DataFrame()
df_profit['probability'] = series_probability
# 使用numpy生成一个长度为v_size的数组,全部填充0.0,然后转换为Series
series = pd.Series(np.zeros(v_size))
# 增加profit 列
df_profit['profit'] = series
i = 0
# df遍历赋值,如果是1,盈利赋值,如果是0, 亏损赋值。
for index, row in df_profit.iterrows():
if row['probability'] == 1 :
df_profit.loc[i,'profit'] = random.uniform(0.0, v_win)
else :
df_profit.loc[i,'profit'] = random.uniform(v_loss, 0.0)
i += 1
print('Profit: ',round(df_profit['profit'].sum(),2))
print('Win times: ',df_profit['probability'].sum())
print('Test times:',len(df_profit))
执行结果:
Profit: 51270.84
Win times: 583
Test times: 1000
交易次数:1000
盈利 51270.84 。
胜率是:58.3% 。
还是比较可观的。
当然,随着交易的次数增加,胜率会更接近55% 。
交易次数 :100000 。
Profit: 4283618.09
Win times: 55098
Test times: 100000
可以看到,胜率55.098% 。
4.模拟交易
上面的数据不容易看出投资本金,利润之间的关系。
将改进程序,更接近模拟交易过程,看看胜率和盈亏比对最终利润的影响。
import numpy as np
import pandas as pd
import random
#定义维度,交易数量
v_size = 100
# 定义概率分布 ,设置胜率
probabilities = [0.45, 0.55]
# 定义盈亏比 4:3
v_win = 400.0
v_loss = -300.0
# 初始资金
init_cash = 10000.0
# 手续费 万二
v_comm = 0.0002
# 当前现金 ,每次交易的仓位
position = 0.6
# 生成随机数组
arr_probability = np.random.choice([0,1], size=v_size, p=probabilities, replace=True)
# 将随机数组转换为pandas Series
series_probability = pd.Series(arr_probability)
df_profit = pd.DataFrame()
df_profit['probability'] = series_probability
# 使用numpy生成一个长度为v_size的数组,全部填充0.0,然后转换为Series
series = pd.Series(np.zeros(v_size))
# 每次交易的利润,含参与交易的本金
df_profit['profit'] = series
# 每次交易的初始资金
df_profit['cash'] = series
# 每次的仓位值
df_profit['position'] = series
# 每次交易的佣金
df_profit['comm'] = series
i = 0
# df遍历赋值,如果是1,盈利赋值,如果是0, 亏损赋值。
for index, row in df_profit.iterrows():
if row['probability'] == 1 :
# 如果是首次交易
if i == 0 :
df_profit.loc[i,'cash'] = init_cash
df_profit.loc[i,'position'] = init_cash * position
df_profit.loc[i,'profit'] = random.uniform(0.0, v_win) / 10000 * df_profit.loc[i,'position'] + df_profit.loc[i,'position'] # 盈利
df_profit.loc[i,'comm'] = abs(df_profit.loc[i,'profit']) * v_comm # 总是正值
df_profit.loc[i,'profit'] = df_profit.loc[i,'profit'] - df_profit.loc[i,'comm']
#非首次交易
else :
df_profit.loc[i,'cash'] = df_profit.loc[i-1,'cash'] - df_profit.loc[i-1,'position'] + df_profit.loc[i-1,'profit']
df_profit.loc[i,'position'] = df_profit.loc[i,'cash'] * position
df_profit.loc[i,'profit'] = random.uniform(0.0, v_win) / 10000 * df_profit.loc[i,'position'] + df_profit.loc[i,'position'] # 盈利
df_profit.loc[i,'comm'] = abs(df_profit.loc[i,'profit']) * v_comm # 总是正值
df_profit.loc[i,'profit'] = df_profit.loc[i,'profit'] - df_profit.loc[i,'comm']
else :
# 如果是首次交易
if i == 0 :
df_profit.loc[i,'cash'] = init_cash
df_profit.loc[i,'position'] = init_cash * position
df_profit.loc[i,'profit'] = random.uniform(v_loss, 0.0) / 10000 * df_profit.loc[i,'position'] + df_profit.loc[i,'position'] # 亏损
df_profit.loc[i,'comm'] = abs(df_profit.loc[i,'profit']) * v_comm # 总是正值
df_profit.loc[i,'profit'] = df_profit.loc[i,'profit'] - df_profit.loc[i,'comm']
#非首次交易
else :
df_profit.loc[i,'cash'] = df_profit.loc[i-1,'cash'] - df_profit.loc[i-1,'position'] + df_profit.loc[i-1,'profit']
df_profit.loc[i,'position'] = df_profit.loc[i,'cash'] * position
df_profit.loc[i,'profit'] = random.uniform(v_loss, 0.0) / 10000 * df_profit.loc[i,'position'] + df_profit.loc[i,'position'] # 亏损
df_profit.loc[i,'comm'] = abs(df_profit.loc[i,'profit']) * v_comm # 总是正值
df_profit.loc[i,'profit'] = df_profit.loc[i,'profit'] - df_profit.loc[i,'comm']
i += 1
#print('Profit: ',round(df_profit['profit'].sum(),2))
print('Win times: ',df_profit['probability'].sum())
print('Test times: ',len(df_profit))
print('Profit ratio %: ',round((df_profit.loc[v_size -1,'cash']/init_cash - 1)*100,2))
print('Last trade cash: ',round(df_profit.loc[v_size-1,'cash'],2))
print('Sum trade comm: ',round(df_profit['comm'].sum(),2))
# df_profit
结果:
Win times: 48
Test times: 100
Profit ratio %: 7.52
Last trade cash: 10752.08
Sum trade comm: 120.04
如果把交易次数设置1000
Win times: 550
Test times: 1000
Profit ratio %: 965.35
Last trade cash: 106534.91
Sum trade comm: 3919.97
版权声明:本文标题:交易中的胜率和盈亏比估算 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729490081a1202466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论