湖南省交通建设质安监督局网站,上海备案证查询网站查询系统,文件外链网站,大型的网站建设公司交易中的胜率和盈亏比估算
1.定义
胜率是指交易者在一定时间内成功交易的次数占总交易次数的比例。例如#xff0c;如果交易者在10次交易中成功了6次#xff0c;那么他的胜率就是60%。
盈亏比是指交易者每笔成功交易的盈利与每笔失败交易的亏损之间的比例。例如#xff0…交易中的胜率和盈亏比估算
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], sizev_size, pprobabilities, replaceTrue) # 将随机数组转换为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] seriesi 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 1print(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], sizev_size, pprobabilities, replaceTrue) # 将随机数组转换为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] seriesi 0 # df遍历赋值如果是1盈利赋值如果是0 亏损赋值。
for index, row in df_profit.iterrows(): if row[probability] 1 :# 如果是首次交易if i 0 :df_profit.loc[i,cash] init_cashdf_profit.loc[i,position] init_cash * positiondf_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] * positiondf_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_cashdf_profit.loc[i,position] init_cash * positiondf_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] * positiondf_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