交易中的胜率和盈亏比估算

交易中的胜率和盈亏比估算

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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-02-14 00:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-14 00:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-14 00:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-14 00:38:01       20 阅读

热门阅读

  1. 数据结构-树

    2024-02-14 00:38:01       28 阅读
  2. day2-理解 linux 云计算

    2024-02-14 00:38:01       32 阅读
  3. C#中 Combine 静态方法

    2024-02-14 00:38:01       29 阅读
  4. STM32 与 ARM 谁比较强大?

    2024-02-14 00:38:01       28 阅读
  5. ndk-r20b 编译 boost 1.74。

    2024-02-14 00:38:01       36 阅读
  6. 遗传算法实现

    2024-02-14 00:38:01       27 阅读
  7. 安卓termux mosh配置nvim远程开发

    2024-02-14 00:38:01       36 阅读
  8. A股上市以来涨幅排行榜

    2024-02-14 00:38:01       34 阅读
  9. 202401 卓越学院转专业-上机测试

    2024-02-14 00:38:01       32 阅读
  10. UVA489 - Hangman Judge

    2024-02-14 00:38:01       24 阅读
  11. 运维面试题

    2024-02-14 00:38:01       31 阅读