import numpy as np
import pandas as pd
jiacang = 0.5 # 加仓比例
zhangdiefu = [-0.5, 0.5, 0.5, 0.5, 0.5]
jiacang_arr = [] # 加仓比例
benjin_arr = [100] # 本金
shouyi_arr = [] # 收益
shengyu_arr = [] # 剩余持仓
jc_money_arr = [] # 加仓金额
zongjine_arr = [] # 总金额
chicang_arr = [] # 持仓
zongshouyi_arr = [] # 总收益比例
def get_shengyu(m, zd):
shouyi = m * zd
shengyu = m + shouyi
yinkuibili = (shengyu - m) / m
return shengyu, shouyi, yinkuibili
def get_cc_total(m,shengyu, jc):
jc_money = m * jc
chicang = shengyu + m * jc # 目前持仓
zongjine = m + m*jc
return chicang, zongjine, jc_money
for key,value in enumerate(zhangdiefu):
if (key > 0):
benjin_arr.append(money)
jiacang_arr.append(jiacang)
benjin = benjin_arr[key]
shengyu, shouyi, yinkuibili = get_shengyu(benjin, value)
chicang, zongjine, jc_money = get_cc_total(benjin, shengyu, jiacang)
money = chicang
shengyu_arr.append(shengyu)
shouyi_arr.append(shouyi)
chicang_arr.append(chicang)
zongjine_arr.append(zongjine)
jc_money_arr.append(jc_money)
zongshouyi_arr.append(yinkuibili)
data = {
'本金': benjin_arr,
'涨跌幅': zhangdiefu,
'剩余': shengyu_arr,
'加仓比例': jiacang_arr, # 回测变量
'加仓金额': jc_money_arr, # 回测变量
'持仓金额': chicang_arr,
'总金额': zongjine_arr,
'总收益': zongshouyi_arr
}
df = pd.DataFrame(data)
# print(df)
def per(x):
return f'{x * 100}%'
df['总收益'] = df['总收益'].map(per)
df['涨跌幅'] = df['涨跌幅'].map(per)
df['加仓比例'] = df['加仓比例'].map(per)
print(df)
# zhangdie_arr = np.array(zhangdiefu)
# print(zhangdie_arr)
# zhangdie_arr = 100 * zhangdie_arr
# zhangdie_arr = zhangdie_arr + '%'
# zhangdie_arr = zhangdie_arr.tolist()
# print(zhangdie_arr)
本金 涨跌幅 剩余 加仓比例 加仓金额 持仓金额 总金额 总收益
0 100.0 -50.0% 50.0 50.0% 50.0 100.0 150.0 -50.0%
1 100.0 50.0% 150.0 50.0% 50.0 200.0 150.0 50.0%
2 200.0 50.0% 300.0 50.0% 100.0 400.0 300.0 50.0%
3 400.0 50.0% 600.0 50.0% 200.0 800.0 600.0 50.0%
4 800.0 50.0% 1200.0 50.0% 400.0 1600.0 1200.0 50.0%
def get_shengyu(m, zd):
shouyi = m * zd
shengyu = m + shouyi
yinkuibili = (shengyu - m) / m
return shengyu, shouyi, yinkuibili
def get_cc_total(m, shengyu, jc):
jc_money = m * jc
chicang = shengyu + m * jc # 目前持仓
zongjine = m + m*jc
return chicang, zongjine, jc_money
def per(x):
return f'{x * 100}%'
def back_value(first=100, zhangdiefu=[], jiacang=0.5, format=True):
jiacang = jiacang # 加仓比例
zhangdiefu = zhangdiefu
jiacang_arr = [] # 加仓比例
benjin_arr = [first] # 第一次持仓金额
shouyi_arr = [] # 收益
shengyu_arr = [] # 剩余持仓
jc_money_arr = [] # 加仓金额
zongjine_arr = [] # 总金额
chicang_arr = [] # 持仓
zongshouyi_arr = [] # 总收益比例
for key, value in enumerate(zhangdiefu):
if (key > 0):
benjin_arr.append(money)
jiacang_arr.append(jiacang)
benjin = benjin_arr[key]
shengyu, shouyi, yinkuibili = get_shengyu(benjin, value)
chicang, zongjine, jc_money = get_cc_total(benjin, shengyu, jiacang)
money = chicang
shengyu_arr.append(shengyu)
shouyi_arr.append(shouyi)
chicang_arr.append(chicang)
zongjine_arr.append(zongjine)
jc_money_arr.append(jc_money)
zongshouyi_arr.append(yinkuibili)
data = {
'本金': benjin_arr,
'涨跌幅': zhangdiefu,
'剩余': shengyu_arr,
'加仓比例': jiacang_arr, # 回测变量
'加仓金额': jc_money_arr, # 回测变量
'持仓金额': chicang_arr,
'总金额': zongjine_arr,
'总收益': zongshouyi_arr
}
df = pd.DataFrame(data)
if format == True:
df['总收益'] = df['总收益'].map(per)
df['涨跌幅'] = df['涨跌幅'].map(per)
df['加仓比例'] = df['加仓比例'].map(per)
return df
zhangdiefu = [-0.5, -0.5, -0.5, -0.5, 0.5]
jiacang = 0
first_money = 10000
back_value(first_money,zhangdiefu, jiacang)
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
本金 |
涨跌幅 |
剩余 |
加仓比例 |
加仓金额 |
持仓金额 |
总金额 |
总收益 |
0 |
10000.0 |
-50.0% |
5000.0 |
0% |
0.0 |
5000.0 |
10000.0 |
-50.0% |
1 |
5000.0 |
-50.0% |
2500.0 |
0% |
0.0 |
2500.0 |
5000.0 |
-50.0% |
2 |
2500.0 |
-50.0% |
1250.0 |
0% |
0.0 |
1250.0 |
2500.0 |
-50.0% |
3 |
1250.0 |
-50.0% |
625.0 |
0% |
0.0 |
625.0 |
1250.0 |
-50.0% |
4 |
625.0 |
50.0% |
937.5 |
0% |
0.0 |
937.5 |
625.0 |
50.0% |
from prettytable import PrettyTable
table = PrettyTable()
# table.add_column('本金', benjin_arr)
# table.add_column('涨跌', [f'{zhangdie * 100}%'])
# table.add_column('收益', [shouyi])
# table.add_column('剩余', [shengyu])
# table.add_column('加仓', [f'{jiacang * 100}%'])
# table.add_column('加仓金额', [jc_money])
# table.add_column('持仓', [chicang])
# table.add_column('总计', [zongjine])
# print(table)
import math
import pandas as pd
gujia = [1, 0.5]
bishu = []
shizhi = []
zhangdie = []
jiacang = []
jiacangjinge = []
jiacangbishu = []
benjin = []
zongjinge = []
zongbishu = []
chenbenjia = []
data = {}
# gujia = 1 # 当前股价
# bishu = 100 # 笔数
# shizhi = 10000 # 当前市值 = 股价 * 笔数
gujia_arr = [1, 0.5]
shizhi_arr = [] # 实际市值
bishu_arr = []
zhangdiefu_arr = [0] # 涨跌幅 = 现价-上一次价格/上一次价格
jiacang_arr = [] # 加仓比例
jiacangjine_arr = [0] # 加仓金额 = 加仓比例 * 上一次本金
jiacangbishu_arr = [0] # 加仓笔数 = 加仓金额 / 股价
benjin_arr = [] # 本金 = 加仓金额 + 市值
zongjine_arr = [] # 总金额 = 上一次总金额 + 这一次加仓金额
zongbishu_arr = [] # 总笔数 = 上一次总笔数 + 这一次总笔数
chengben_arr = [] # 成本 = 本金/总笔数
# 根据笔数与股价获取市值与笔数
def get_shizhi(bishu, gujia):
# 笔数要大于100 并且是 10 的整数
if bishu >= 100 and bishu % 10 == 0:
shizhi = bishu * gujia
return bishu,shizhi
else:
bishu += 1
return get_shizhi(bishu, gujia)
# print(get_shizhi(1001, 19))
# 根据市值,获取对应笔数和对应市值
def get_bishu(shizhi, gujia):
bishu = shizhi / gujia
if bishu < 100 and bishu % 10 == 0:
shizhi += 10
return get_bishu(shizhi, gujia)
else:
# 向上取整
bishu = math.ceil(bishu)
if bishu % 10 != 0:
shizhi += 1
return get_bishu(shizhi, gujia)
else:
return bishu, shizhi
# print(get_bishu(10000, 19.2))
def back_value(prices=[100, 50, 25], first_qiwang_shizhi=10000, jiacang = 1):
for key, value in enumerate(prices):
if key == 0:
jiacang_arr.append(0)
bishu, shizhi = get_bishu(first_qiwang_shizhi, value)
bishu_arr.append(bishu)
benjin_arr.append(shizhi) # 第一次本金=市值
shizhi_arr.append(shizhi)
zongjine_arr.append(shizhi)
zongbishu_arr.append(bishu) # 总笔数=笔数
chengben_arr.append(value)
if key > 0:
pre_value = prices[key-1]
if (value <= pre_value):
zhangdiefu = (value - pre_value) / prices[key-1]
else:
zhangdiefu = value / pre_value
bishu_arr.append(zongbishu_arr[key-1])
pre_bishu = zongbishu_arr[key-1]
shizhi = value * pre_bishu
shizhi_arr.append(shizhi)
zhangdiefu_arr.append(zhangdiefu)
jiacang_arr.append(jiacang)
jiacangjine = jiacang * benjin_arr[key-1] # 期望金额
jiacangbishu, jiacangjine = get_bishu(jiacangjine, value) # 实际加仓金额
jiacangbishu_arr.append(jiacangbishu)
jiacangjine_arr.append(jiacangjine)
benjin = jiacangjine + shizhi
benjin_arr.append(benjin)
zongjine = zongjine_arr[key-1] + jiacangjine
zongjine_arr.append(zongjine)
zongbishu = zongbishu_arr[key-1] + jiacangbishu
zongbishu_arr.append(zongbishu)
chengben = benjin / zongbishu
chengben_arr.append(chengben)
data = {
"股价": prices,
"笔数": bishu_arr,
"市值": shizhi_arr,
"涨跌幅": zhangdiefu_arr,
"加仓": jiacang_arr,
"加仓金额": jiacangjine_arr,
"加仓笔数": jiacangbishu_arr,
"持仓金额": benjin_arr,
"总金额": zongjine_arr,
"总笔数": zongbishu_arr,
"成本价": chengben_arr
}
df = pd.DataFrame(data)
return df
back_value()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
股价 |
笔数 |
市值 |
涨跌幅 |
加仓 |
加仓金额 |
加仓笔数 |
持仓金额 |
总金额 |
总笔数 |
成本价 |
0 |
100 |
100 |
10000 |
0.0 |
0 |
0 |
0 |
10000 |
10000 |
100 |
100.0 |
1 |
50 |
100 |
5000 |
-0.5 |
1 |
10000 |
200 |
15000 |
20000 |
300 |
50.0 |
2 |
25 |
300 |
7500 |
-0.5 |
1 |
15000 |
600 |
22500 |
35000 |
900 |
25.0 |
import math
math.ceil(526.0)