美文网首页
python 积累4

python 积累4

作者: 黄yy家的jby | 来源:发表于2018-12-19 16:21 被阅读0次

摘要

1.时间序列用字典存储

dic ={}
for date in close.index:
    ***
    dic[date] = [...]
result = pd.DataFrame(dic_result,index=['...']).T
#注意要转置

方便之处在于不用切片,书写时直接写变量即可

2.加布尔变量表示

for i,date in enumerate(close.index):
    change = False
    index = index*(1+index_change[date])
    if (hs_change_pct + zz_change_pct) > 0.2 and (pos >= 0.15):
        pos -= 0.05
        change = True
        signal = -1
    elif (hs_change_pct + zz_change_pct) < -0.2 and (pos <= 0.75):
        pos += 0.05
        change = True
        signal = 1
    if change:
        ...

方便之处在于加仓减仓公式是一致的(带正负号)不用书写两遍

3.变量

写策略时,cash和equity分开

  • 1.加减仓时
    equity(t) = total_return(t-1) * pos(t)
  • ** 因此需要在信号时就把pos给改变 **
    num_temp(t) = equity(t) * weight(t) / close(t)
  • 用temp来记录现在的num,来计算delta_num
    cash(t) = cash(t-1) +(num(t-1)-temp_num(t))*close(t)
  • 在赋值给现在的num
    num = num_temp
  • 2.条件外
    不管加仓不加仓都要进行的操作
    equity = num*close
    return = equity+cash

4.策略代码

dic_result = {}
index_change = pd.Series((df_close['沪深300']/df_close['沪深300'].shift(1)+df_close['中证500']/df_close['中证500'].shift(1)-2)/2)
index_change[0] = 0

for i,date in enumerate(df_close.index):
    change_rolling_time = False
    change = False
    index = index*(1+index_change[date])
    hs_change_pct = df_close.loc[date, '沪深300'] / hs_benchmark - 1
    zz_change_pct = df_close.loc[date, '中证500'] / zz_benchmark - 1
    [hs_pct, zz_pct] = calc_pct(date)
    if date == rolling_time[i]:     #rolling-time时刻的调仓
        pos = calc_total_pos(hs_pct, zz_pct)    #更新仓位       
        change_rolling_time = True
        signal_rolling = 1
    else:
        signal_rolling = 0
        if (hs_change_pct + zz_change_pct) > 0.2 and (pos >= 0.15):
            pos -= 0.05
            # pos = calc_total_pos(hs_pct, zz_pct)
            change = True
            signal = -1
        elif (hs_change_pct + zz_change_pct) < -0.2 and (pos <= 0.75):
            pos += 0.05
            # pos = calc_total_pos(hs_pct, zz_pct)
            change = True
            signal = 1
        else:
            signal = 0
            change = False
    if change or change_rolling_time:
        # [hs_pct, zz_pct] = calc_pct(date)
        hs_weight = calc_hs_weight(hs_pct, zz_pct)
        euqity = total_return * pos
        hs_num_temp = equity * hs_weight / df_close.loc[date, '沪深300']
        zz_num_temp = equity * (1 - hs_weight) / df_close.loc[date, '中证500']
        cash = cash + (hs_num - hs_num_temp) * df_close.loc[date, '沪深300'] + (zz_num - zz_num_temp) * df_close.loc[date, '中证500']
        hs_num = hs_num_temp
        zz_num = zz_num_temp
        hs_benchmark = df_close.loc[date, '沪深300']
        zz_benchmark = df_close.loc[date, '中证500']
    euqity = hs_num * df_close.loc[date, '沪深300'] + zz_num * df_close.loc[date, '中证500']
    total_return = euqity + cash
    dic_result[date] = [total_return,pos,signal_rolling,signal,index]

相关文章

  • python 积累4

    摘要 1.时间序列用字典存储 方便之处在于不用切片,书写时直接写变量即可 2.加布尔变量表示 方便之处在于加仓减仓...

  • Python积累

    从基因组注释中提取转录因子 字符串find使用+异常值处理 今天又写了一个小脚本,从基因组的注释文件中筛选所有可能...

  • python知识积累

  • python技巧积累

    数组逐行替换 生成多维数组,元素随机 dict按照key排序生成list、dict按照value排序生成list 链接

  • python 用法积累

    print 格式化输出 click to 格式化输出 json使用实例 re使用实例 one eval 和 jso...

  • python积累2

    摘要 nan和0之间转换 resample('M').pad() conn = cx_Oracle.connect...

  • python 积累3

    摘要 1. 线性回归简单归纳 2. 不提醒warning 3.获取当前工作路径 4.Dataframe 行列 5....

  • python积累1

    摘要 读特定sheet特定行的excel,设定时间轴 时间轴索引 更改非交易日的调仓时间 找离得最近的rollin...

  • python 积累5

    摘要 1. 模块同一个名字 可以放在不同文件夹下import 包名(文件夹名).模块名(py名).函数名 2.ma...

  • python 积累6

    摘要 1.对dataframe列的运算 转化成numpy计算但是要注意等长,可以考虑先concat成一个dataf...

网友评论

      本文标题:python 积累4

      本文链接:https://www.haomeiwen.com/subject/qypjkqtx.html