Exemple #1
0
def get_basic_factor_val(factorname, date_list, stock_list):
    #t = TushareBasedata.TushareBasedata()
    #print factorname
    #print date_list
    #print stock_list
    t = MysqlBasedata.MysqlBasedata()
    if len(date_list) > 1:
        start_date_str = datetime.strftime(date_list[0], '%Y-%m-%d')
        end_date_str = datetime.strftime(date_list[1], '%Y-%m-%d')
        if factorname == 'trade_closeprice':
            factorDf = t.get_factor_data_by_datecode(stock_list,
                                                     start_date_str,
                                                     end_date_str, factorname,
                                                     0)
        else:
            factorDf = t.get_factor_data_by_datecode(stock_list,
                                                     start_date_str,
                                                     end_date_str, factorname,
                                                     -1)
    else:
        start_date_str = datetime.strftime(date_list[0], '%Y-%m-%d')
        factorDf = t.get_factor_data_by_datecode(stock_list, start_date_str,
                                                 start_date_str, factorname,
                                                 -1)

    factorDf['FACTOR_VALUE'] = factorDf['FACTOR_VALUE'].map(lambda x: float(x))
    return factorDf
Exemple #2
0
def getStockList(tradedate):
    tbd = MysqlBasedata.MysqlBasedata()
    s1 = str(tbd.get_stocklist_by_type(tradedate, '300'))
    s2 = str(tbd.get_stocklist_by_type(tradedate, '500'))
    s3 = str(tbd.get_stocklist_by_type(tradedate, '50'))
    s = "(s1Ns2)Us3"

    return tbd.get_stocklist_by_type(tradedate, '300')
Exemple #3
0
def getSummary(strategyId, benchCode, startDateStr, endDateStr):
    conn = cbt.getConnection()
    t = MysqlBasedata.MysqlBasedata()
    df_bench = t.get_history_index_data_by_date(benchCode, startDateStr,
                                                endDateStr, None)
    df_bench = df_bench.sort_values(by='tradedate', ascending=True)
    #sql = "SELECT * from r_position where strategy_id='"+stategyId+"' and tradedate ORDER BY tradedate"
    #df_position = pd.read_sql(sql, conn)
    df_position = st.getPositionListByTradedate(strategyId, startDateStr,
                                                endDateStr, conn)
    rsumm = returnSummary.ReturnSummary(df_position, df_bench)
    summ = rsumm.get_summary()
    print summ
    # total asset data
    print rsumm.df_assets
    # rsumm.df_assets = rsumm.df_assets[rsumm.df_assets]
    rsumm.df_assets.to_csv('asserts_.csv', index=False, sep=',')
    print pd.read_csv('asserts_.csv')
    return rsumm, summ
Exemple #4
0
def tradeNew(tradedate):
    # 数据库操作类初始化
    t = MysqlBasedata.MysqlBasedata()
    # 连接池初始化
    conn = cbt.getConnection()
    # 日期格式转换
    trade_date_str = datetime.strftime(tradedate, '%Y-%m-%d')
    # 查询股票行情接口
    stockPriceDf = t.get_history_data_by_stocklist(trade_date_str, None, 'D',
                                                   'B')
    # 验证调仓日期:验证是否到达调仓日期
    # 如果当前不是到调仓日期,新增资金账户信息
    # 查询前一天是否存在仓位,如果存在,拷贝前一天仓位信息
    if cc.currentPeriod % cc.changePeriod != 1:
        # 如果是第一天,记录账户资金信息
        # todo 否则,判断是否
        # 拷贝前一天的仓位信息,更新仓位中的股票价格
        updatePosition(tradedate, stockPriceDf, conn)
        #updatePositionAccount(tradedate)
        cc.currentPeriod = cc.currentPeriod + 1
        return
    cc.currentPeriod = cc.currentPeriod + 1
    # 如果是调仓日期,进入策略逻辑
    # 获取票池
    stocklist = getStockList(tradedate)
    print stocklist
    # 如果股票池为0
    if len(stocklist) == 0:
        # 新增资金账户信息,拷贝前一天仓位信息
        updatePosition(tradedate, stockPriceDf, conn)
        return
    # 定义买入股票列表、卖出股票列表
    buystocklist = stocklist
    # 计算得出中间条件、筛选股票的条件
    factor_txt = 'MA5 = MA(trade_closeprice,5)\nMA10 = MA(trade_closeprice, 10)'
    filter_txt = 'gx = CROSS(MA5, MA10)\nasc5 = SORT(MA5, desc, 5)'
    d1, d2 = intp.interp(factor_txt, filter_txt)
    # 通过买入条件得出买入股票列表
    for key in ['asc5']:
        buystocklist = d2[key].filter(buystocklist, tradedate)
        # print 'Stock list by applying filter', key, ':', buystocklist
    # 当前订单列表
    currentOrderlist = []

    lastAccountDateStr = cst.getLastAccountDate(cc.strategyId, conn)
    positionList = []
    # 如果仓位交易日不为空,查询仓位信息,复制仓位信息到新的交易日
    if lastAccountDateStr != None:
        positionDf = cst.getPositionList(cc.strategyId, lastAccountDateStr,
                                         conn)
        positionList = positionDf.values

    # 通过卖出条件得出卖出股票列表,并将卖出股票列表放入订单
    for position in positionList:
        code = position[1]
        if code in buystocklist:
            buystocklist.remove(code)
            continue
        print code
        print stockPriceDf.loc[code, 'FACTOR_VALUE']
        o = StockOrder.StockOrder(cc.strategyId, code, tradedate,
                                  stockPriceDf.loc[code, 'FACTOR_VALUE'], 0)
        print o
        currentOrderlist.append(o)
    # 买入,并将买入股票列表放入订单
    for code in buystocklist:
        o = StockOrder.StockOrder(cc.strategyId, code, tradedate,
                                  stockPriceDf.loc[code, 'FACTOR_VALUE'], 100)
        currentOrderlist.append(o)
    print currentOrderlist
    # 订单入库,并更新仓位
    cst.order(currentOrderlist, stockPriceDf, conn)
Exemple #5
0
            self.retrn, self.retrn_annual, self.retrn_bench,
            self.retrn_bench_annual, self.alpha, self.beta, self.IR, self.vol,
            self.vol_bench, self.win_daily, self.max_loss_daily
        ]
        df_summary['FieldName'] = names
        df_summary["FieldValue"] = values
        return df_summary


if __name__ == "__main__":
    # 连接池初始化
    conn = cbt.getConnection()
    sql = "SELECT * from r_position where strategy_id='2' ORDER BY tradedate"
    df = pd.read_sql(sql, conn)
    # print df
    t = MysqlBasedata.MysqlBasedata()
    df_bench_new = t.get_history_index_data_by_date('000300.SH', '2015-05-10',
                                                    '2017-05-10', None)

    df_bench = pd.read_pickle('test_bench_returnSummary.pkl')
    df_position = pd.read_pickle('test_data_returnSummary.pkl')
    rsumm = ReturnSummary(df_position, df_bench)
    summ = rsumm.get_summary()
    print summ.T
    print type(summ)
    # total asset data
    print rsumm.df_assets
    #df['tradedate'] = df['tradedate'].map(lambda x:time.mktime(x.timetuple()))
    #print df
    # print datetime.now().microsecond
    # print time.mktime(t.timetuple())