Esempio n. 1
0
def initialize(context):
    set_commission(commission.PerDollar(cost=0.0055))
    # context.strategy_name = "FOF_{mdd}"
    context.mdd = context.namespace["mdd"]
    context.fof_code = context.namespace["fof_code"]
    context.save2mysql = context.namespace.get("save_method", 0)
    context.i = 0
    schedule_function(scheduled_rebalance,
                      date_rules.month_start(days_offset=0),
                      time_rules.market_open())
    context.init_value = pd.Series()  # 记录自最近一次调仓以来,组合内各基金所达到的最大净值
    context.adjust_date = 0  # 记录距离最近一次季调的时间
    context.stop_date = 0  # 记录距离最近一次止损的时间
    # 基金池
    stock = [
        '000478.OF', '050002.OF', '110006.OF', '161120.OF', '000217.OF',
        "501018.OF", '000071.OF', '070031.OF', '000342.OF', 'B00.IPE',
        "HSCI.HI", '037.CS', "em_bond", "reit", "SPTAUUSDOZ.IDC", '096001.OF',
        'SPX.GI', '000905.SH'
    ]
    context.stocks = []
    for sym in stock:
        context.stocks.append(symbol(sym))
    #  指数与基金的对应关系 fixed
    index = [
        "HSCI.HI", '037.CS', "EM_BOND", "REIT", "SPTAUUSDOZ.IDC", "000905.SH",
        "B00.IPE", "NDX.GI", 'SPX.GI'
    ]
    index_etf = ["000071.OF","161120.OF","000342.OF",\
                 "070031.OF","000217.OF","000478.OF",\
                 "501018.OF","160213.OF",'096001.OF']
    context.df_index = pd.DataFrame(
        data=index_etf,
        index=index,
    )
Esempio n. 2
0
def initialize(context):
    # add_history(LOOKBACK_PERIOD, '1d', 'price')
    #set_max_leverage(1.25)
    context.i = 0
    set_commission(commission.PerDollar(cost=0.006))
    context.fof_code = context.namespace["fof_code"]
    context.save2mysql = context.namespace.get("save_method", 0)
    schedule_function(scheduled_rebalance,
                      date_rules.month_start(days_offset=0),
                      time_rules.market_open())
    context.weights = pd.Series()
    stock = [house,oil,Emerging_market_bonds,America_stock,Hongkong_stock,\
    gold,"HSCI.HI", "SPX.GI",'B00.IPE',"em_bond","reit",
              "SPTAUUSDOZ.IDC"]
    context.stocks = []
    for i in stock:
        context.stocks.append(symbol(i))
Esempio n. 3
0
def initialize(context):

    context.record_rank = {}
    context.save2mysql = 0
    # 模型训练参数
    context.horizon = 6
    context.percent = 5
    context.model_name = 'adaBoost'
    context.rolling = 1  # 是否为滚动,0为不滚动
    context.ishistory = 0  #是否用历史分类。0为不使用
    context.train_period = 12 * 1
    context.i = 0
    set_slippage(slippage.FixedSlippage(spread=0.00))
    set_commission(commission.PerDollar(cost=0.00325))

    month = 21
    week = 5
    rf = 0.025
    c = 252

    if context.namespace["fund_type"] == 'stock':
        benchmark = '000300.SH'
        fund_type = 1
    elif context.namespace["fund_type"] == 'hybrid':
        benchmark = '000300.SH'
        fund_type = 2
    elif context.namespace["fund_type"] == 'bond':
        benchmark = '037.CS'
        fund_type = 3
    # 选择基金池和benchmark
    ben_sid = symbol(benchmark).sid
    df_stock = df.query("update_time == update_time.max() and type_code=={"\
                        "type}".format(type = fund_type))
    # 基金900 开头的不需要,专门去掉
    df_stock = df_stock[df_stock["fund_code"] < '900000'].fund_code + ".OF"
    sfilt = SpecificAssets(symbols(
        benchmark, *tuple(df_stock)))  # 使用到的数据,包括基金和对应benchmark指数
    sample_filt = SpecificAssets(symbols(
        *tuple(df_stock)))  # 只包含基金,因为评级不应该包含benchmark,故在事后screen去掉benchmark

    # 只包含基金,因为评级不应该包含benchmark,故在事后screen去掉benchmark
    ## 16个因子指标
    down_sharpe = Down_sharpe(window_length=9 * month, rf=rf, c=c, mask=sfilt)
    drawdown = Drawdown(window_length=9 * month, mask=sfilt)
    dvar = Down_variation(window_length=9 * month, rf=rf, c=c, mask=sfilt)
    mean_r = Average_return(window_length=9 * month, mask=sfilt)
    lasting = Lasting(window_length=9 * month,
                      mask=sfilt,
                      time=6 * month,
                      percent=20)
    emr = Emr(window_length=9 * month, mask=sfilt, time=6 * month)
    rlt_var = Negative_variation(window_length=9 * month, c=c, mask=sfilt)
    mcv_in = Mcv_in(window_length=9 * month,
                    rf=rf,
                    c=c,
                    mask=sfilt,
                    time=week,
                    index_sid=ben_sid)
    rank_stable = Rank_stable(window_length=9 * month,
                              mask=sfilt,
                              time=6 * month)
    select_time, select_stock = CL(window_length=9 * month,
                                   rf=rf,
                                   mask=sfilt,
                                   time=week,
                                   index_sid=ben_sid)
    hit_rate = Hit_rate(window_length=10 * month,
                        mask=sfilt,
                        time=6 * month,
                        time_window=9 * month,
                        index_sid=ben_sid)
    value_at_risk = Value_at_risk(window_length=9 * month,
                                  mask=sfilt,
                                  q_value=5,
                                  time=6 * month)
    beta_in = Beta_in(window_length=9 * month,
                      mask=sfilt,
                      rf=rf,
                      time=week,
                      index_sid=ben_sid)
    bias_in = Bias_in(window_length=9 * month, mask=sfilt, time=126)
    ir = Information_ratio(window_length=9 * month,
                           mask=sfilt,
                           time=1,
                           index_sid=ben_sid)
    # 预测因变量Y
    _ry = Lag_Return(window_length=context.horizon * month, mask=sfilt)
    _sp = Lag_Sharpe(window_length=context.horizon * month, mask=sfilt)

    pipe = Pipeline(columns={
        "dhsp": down_sharpe,
        "drwd": drawdown,
        "dvar": dvar,
        "mean_r": mean_r,
        "lasting": lasting,
        "emr": emr,
        "rlt_var": rlt_var,
        "mcv_in": mcv_in,
        "rank_stable": rank_stable,
        "select_time": select_time,
        "select_stock": select_stock,
        "hit_rate": hit_rate,
        "value_at_risk": value_at_risk,
        "beta_in": beta_in,
        "bias_in": bias_in,
        "ir": ir,
        "_ry": _sp
    },
                    screen=sample_filt)

    attach_pipeline(pipe, 'my_pipeline')
    set_max_leverage(1.1)
    schedule_function(rebalance,
                      date_rule=date_rules.month_start(),
                      time_rule=time_rules.market_open())

    # 初始化记录变量
    context.rank_score = pd.Series()
    context.f = pd.Panel()
    context.f_dict = {}
    context.reb_flag = False
    context.B = {}
Esempio n. 4
0
def initialize(context):
    context.i = 0
    context.sym = symbol('GS')
    set_commission(commission.PerDollar(cost=0.00165))
Esempio n. 5
0
def initialize(context):
    context.i = 0
    context.sym = symbol('GS')
    set_commission(commission.PerDollar(cost=0.00165))  # 매수/매도 시 수수료 0.165% 발생 -> 총 0.33%의 비용 발생
Esempio n. 6
0
def initialize(context):
    # PerDollar는 원래는 달러를 기준으로 수수료를 설정하는 것이지만 우리는 원 단위로 생각하면 되기 때문에
    set_commission(commission.PerDollar(cost=0.00165))
    context.i = 0
    context.sym = symbol('GS')
    context.hold = False
def initialize(context):  #initial asset and trading pay
    context.sym = symbol('SKHynix')
    set_commission(commission.PerDollar(cost=0.00165))
Esempio n. 8
0
def initialize(context):
    set_commission(commission.PerDollar(cost=0.00165))
Esempio n. 9
0
def initialize(context):
    set_commission(commission.PerDollar(cost=0.00165))
    add_history(5, '1d', 'price')
    add_history(20, '1d', 'price')
    context.i = 0
    context.investment = False