Beispiel #1
0
def trade(tradedate):
    '''
    调仓周期
    '''
    conn = cbt.getConnection()
    # 如果当前没有到调仓周期,跳过
    if cc.currentPeriod != cc.changePeriod:
        cc.currentPeriod = cc.currentPeriod + 1
        return
    cc.currentPeriod = 1
    # 获取票池
    stocklist = getStockList()
    if len(stocklist) == 0:
        return
    price = 0
    positionList = cc.positionList
    '''
    通过买入条件获取买入股票,取并集或者交集
    '''
    techFactor = TechFactorService.TechFactorService()
    # 传入股票池、收盘价标识、交易日期、5日、10日、金叉参数
    stocklist1 = techFactor.ma(stocklist, 'close', tradedate, 5, 10, 1)
    t = TushareBasedata.TushareBasedata()
    trade_date_str = datetime.strftime(tradedate, '%Y-%m-%d')
    # 穿入股票,查询股票的市值因子列表,mv
    df = t.get_factor_data_by_stocklist(trade_date_str, stocklist, 'roe', 0)
    # 按因子排序,计算得到前10%的股票代码
    df = df.sort_values(by='fv')
    stocklist2 = df.code.tolist()
    # 取并集
    stocklist = stocklist1 + stocklist2
    # 订单列表
    orderlist = []
    '''
    调整仓位:卖出不在票池的股票
    '''
    # 卖出
    for position in positionList:
        code = position.code
        if code in stocklist:
            stocklist.remove(code)
            continue
        # cst.order(tradedate, code, price, 0)
        o = StockOrder.StockOrder(cc.strategyId, code, tradedate, price, 0)
        orderlist.append(o)
    '''
    调整仓位:买入符合条件的股票
    '''
    # 买入
    for code in stocklist:
        o = StockOrder.StockOrder(cc.strategyId, code, tradedate, price, 100)
        orderlist.append(o)
    # 订单入库,并更新仓位
    cst.order(orderlist, conn)

    if len(orderlist) == 0:
        return 0
    return 1
Beispiel #2
0
def updatePositionAccount(tradedate):
    conn = cbt.getConnection()
    print cc.initMoney
    print cc.tradeMoney
    # 记录资金账户信息
    money = cc.initMoney + cc.tradeMoney
    cst.saveAccount(cc.strategyId, tradedate, money, conn)
    cc.tradeMoney = 0
    cc.initMoney = money
Beispiel #3
0
def execute(strategyId, periodType, changePeriod, startDateStr, endDateStr,
            initMoney, stocktype_list, stocklist, stockExpression,
            baseVariableExpression, conditionVariableExpression,
            buyConditionlist, sellConditionlist):
    benchCode = '000300.SH'
    stocklistStr = ''
    for index in range(len(stocklist)):
        i = index + 1
        stocktype = stocktype_list[index]
        stocklistStr += "    s" + str(
            i
        ) + " = str(tbd.get_stocklist_by_type(tradedate, '" + stocktype + "'))\n"
    stocklistStr += "    s = \"" + stockExpression + "\"\n"

    template = env.get_template('mytemplate.html')
    codestr = template.render(stockExpression='', stocklistStr=stocklistStr, baseVariableExpression=baseVariableExpression, \
                              conditionVariableExpression=conditionVariableExpression ,buyConditionlist=buyConditionlist, sellConditionlist=sellConditionlist)
    # print codestr

    #filename = str(int(time.time()))+'.py'
    filename = "my_" + strategyId + ".py"
    #os.mknod("test.txt")
    # print filename
    f = file(filename, 'w')
    f.write(codestr)
    f.close()
    load = loader()
    m = load.load(filename)
    conn = bt.getConnection()
    st.deleteByStrategyId(strategyId, conn)
    df = bt.getTradeDay(conn,
                        start_date_str=startDateStr,
                        end_date_str=endDateStr,
                        type=1)
    # 按日期执行策略
    ip = InitParam.InitParam(strategyId, periodType, changePeriod,
                             startDateStr, endDateStr, initMoney)

    datelist = df['tradedate'].tolist()
    func = m["handle_data"]
    for d in datelist:
        # print d
        # print ip
        func(d, ip)

    summary = getSummary(strategyId, benchCode, startDateStr, endDateStr)
    # print summary
    os.remove(filename)
    return summary
Beispiel #4
0
def updatePosition(tradedate, stockPriceDf, conn):
    lastAccountDateStr = cst.getLastAccountDate(cc.strategyId, conn)
    # 如果仓位交易日不为空,查询仓位信息,复制仓位信息到新的交易日
    if lastAccountDateStr != None:
        lastPositionDf = cst.getPositionList(cc.strategyId, lastAccountDateStr, conn)
        if len(lastPositionDf) > 0:
            positionDf = lastPositionDf.copy()
            # 更新仓位日期
            positionDf['TRADEDATE'] = tradedate
            positionList = [tuple(x) for x in positionDf.values]
            print positionList
            positionListNew = []
            # 更新当前实时价格
            for p in positionList:
                pNew = (p[0], p[1], tradedate, stockPriceDf.loc[p[1], 'FACTOR_VALUE'], p[4], p[5])
                positionListNew.append(pNew)
            cst.savePosition(positionListNew, conn)
Beispiel #5
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
Beispiel #6
0
def endTrade(d, flag):
    conn = cbt.getConnection()
    # 查询最新的交易日
    lastAccountDateStr = cst.getLastAccountDate(cc.strategyId, conn)
    # 记录资金账户信息
    money = cc.initMoney + cc.tradeMoney
    cst.saveAccount(cc.strategyId, d, money, conn)
    cc.tradeMoney = 0
    # 如果没有订单,查询最新的仓位交易日
    if flag == 0:
        # 如果仓位交易日不为空,查询仓位信息,复制仓位信息到新的交易日
        if lastAccountDateStr != None:
            lastPositionDf = cst.getPositionList(cc.strategyId,
                                                 lastAccountDateStr, conn)
            if len(lastPositionDf) > 0:
                positionDf = lastPositionDf.copy()
                # 更新仓位日期
                positionDf['TRADEDATE'] = d
                positionList = [tuple(x) for x in positionDf.values]
                cst.savePosition(positionList, conn)
Beispiel #7
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)
Beispiel #8
0
def quant_post():
    print "start:", datetime.now()
    start = datetime.now()

    summaryList = []
    orderList = []
    positionList = []
    # accountList = []
    strategyId = None
    try:
        # 策略参数
        startDateStr = request.form['startDate']
        endDateStr = request.form['endDate']
        changePeriod = int(request.form['interval'])
        period = request.form['period']
        initMoney = float(request.form['initMoney'])
        fee = request.form['fee']
        print startDateStr
        print endDateStr
        print changePeriod
        print period
        print initMoney
        print fee
        # 股票筛选条件
        constituentStockStr = request.form['constituentStockStr']
        print constituentStockStr
        areaStr = request.form['areaStr']
        conceptStr = request.form['conceptStr']
        industryStr = request.form['industryStr']
        print constituentStockStr
        print areaStr
        print conceptStr
        print industryStr
        # 票池表达式、变量表达式
        stockExpression = request.form['stockExpression']
        baseVariableExpression = request.form['baseVariableExpression']
        conditionVariableExpression = request.form[
            'conditionVariableExpression']
        print stockExpression
        print baseVariableExpression
        print conditionVariableExpression
        formulastr = baseVariableExpression.split("\r\n")
        print formulastr
        factorlist = []
        baseVariableExpressionStr = ''
        for f in formulastr:
            baseVariableExpressionStr += f + r"\n"
        baseVariableExpression = baseVariableExpressionStr[:-2]

        conditionVariableExpressionStr = ''
        formulastr = conditionVariableExpression.split("\r\n")
        print formulastr
        for f in formulastr:
            conditionVariableExpressionStr += f + r"\n"
        conditionVariableExpression = conditionVariableExpressionStr[:-2]
        print baseVariableExpression
        print conditionVariableExpression

        # 买入条件、卖出条件
        buyConditionStr = request.form['buyConditionStr']
        buyFundPercentStr = request.form['buyFundPercentStr']
        buyWeightTypeStr = request.form['buyWeightTypeStr']
        sellConditionStr = request.form['sellConditionStr']
        sellFundPercentStr = request.form['sellFundPercentStr']
        print buyConditionStr
        print buyFundPercentStr
        print buyWeightTypeStr
        print sellConditionStr
        print sellFundPercentStr
        buyConditionlist = buyConditionStr.split(",")
        sellConditionlist = sellConditionStr.split(",")

        # todo 待完成
        periodType = 'D'  # period
        #changePeriod = 20
        #startDateStr = '2011-1-1'
        #endDateStr = '2013-1-1'
        #initMoney = 1000000
        stocktype_list = constituentStockStr.split(",")
        stocklist = constituentStockStr.split(
            ","
        )  #[('300', '2010-1-1', '2012-1-1'), ('500', '2017-1-1', '2017-8-1'), ('50', '2017-1-1', '2017-8-1')]
        #stockExpression = "(s1Ns2)Us3"
        #baseVariableExpression = r'MA5 = MA(trade_closeprice,5)\nMA10 = MA(trade_closeprice, 10)'
        #conditionVariableExpression = r'gx = CROSS(MA5, MA10)\nasc5 = SORT(MA5, desc, 5)'
        #buyConditionlist = ['asc5']
        #sellConditionlist = ['gx', 'asc5']
        conn = bt.getConnection()
        # todo 获取最大的策略id,加1
        strategyId = str(st.getMaxStrategyId(conn) + 1)
        print "strategyId start:" + strategyId

        # todo 返回策略收益率结果,,策略曲线
        result = frameM.execute(strategyId, periodType, changePeriod,
                                startDateStr, endDateStr, initMoney,
                                stocktype_list, stocklist, stockExpression,
                                baseVariableExpression,
                                conditionVariableExpression, buyConditionlist,
                                sellConditionlist)

        summaryDf = result[1].T
        asserts = result[0].df_assets
        asserts['date_new'] = asserts['date'].map(
            lambda x: time.mktime(x.timetuple()) * 1000)
        assetsDf = asserts[['date_new', 'asset']]
        assetsDf.to_csv('asserts_' + strategyId + '.csv', index=False, sep=',')

        # 基准指数
        indexDf = st.getIndexListByTradedate('000300.SH', startDateStr,
                                             endDateStr, conn)
        indexDf['date_new'] = indexDf['TRADEDATE'].map(
            lambda x: time.mktime(x.timetuple()) * 1000)
        indexDf['CLOSEPRICE'] = indexDf['CLOSEPRICE'].map(
            lambda x: 3000 / x * initMoney)
        indexDf = indexDf[['date_new', 'CLOSEPRICE']]
        indexDf.to_csv('baseline_' + strategyId + '.csv', index=False, sep=',')
        print indexDf

        conn = bt.getConnection()
        # 查询订单信息
        orderDf = st.getOrderList(strategyId, conn)
        # 查询仓位信息
        positionDf = st.getPositionListByStrategyId(strategyId, conn)
        # 查询资金信息
        # accountDf = st.getAccountListByStrategyId(strategyId, conn)
        summaryList = [tuple(x) for x in summaryDf.values]
        orderList = [tuple(x) for x in orderDf.values]
        positionList = [tuple(x) for x in positionDf.values]

        # accountList = [tuple(x) for x in accountDf.values]
    except Exception, e:
        traceback.print_exc()
Beispiel #9
0
def quant_index():
    conn = bt.getConnection()
    factorDf = st.getFactorList(conn)
    factorList = [tuple(x) for x in factorDf.values]
    print factorList
    return render_template('quant_form.html', factorList=factorList)
Beispiel #10
0
def execute(strategyId, periodType, changePeriod, startDateStr, endDateStr,
            initMoney, stocktype_list, stocklist, stockExpression,
            baseVariableExpression, conditionVariableExpression,
            buyConditionlist, sellConditionlist):
    benchCode = '000300.SH'
    stocklistStr = ''
    keywordList = []
    for index in range(len(stocklist)):
        i = index + 1
        stocktype = stocktype_list[index]
        stocklistStr += "    s" + str(
            i
        ) + " = tbd.get_stocklist_by_type(tradedate, '" + stocktype + "')\n"
        stocklistStr += "    str" + str(
            i) + " = \"\'\"+'\\',\\''.join(s" + str(i) + ")+\"\'\"\n"
        # stocklistStr += "    str"+str(i) + " = str"+str(i) + ".replace('\'', '')\n"
        keywordList.append("str" + str(i))
    stocklistStr += "    stockExpression = \"" + stockExpression + "\".replace('s', 'str')\n"
    stocklistStr += "    for str in " + str(keywordList) + ":\n"
    stocklistStr += "        stockExpression = stockExpression.replace(str, '\"+'+str+'+\"\')\n"
    stocklistStr += "        print stockExpression\n"
    # stocklistStr += "    stockExpression = stockExpression.replace(str, '"+'\"+str+'+"'\")\n"
    # print stocklistStr
    '''
    str1 = "'"+'\',\''.join(s1)+"'"
    str2 = "'"+'\',\''.join(s2)+"'"
    str3 = "'"+'\',\''.join(s3)+"'"
    s = "(str1Nstr2)Ustr3"
    for str in ['str1', 'str2', 'str3']:
        s = s.replace(str, '"+'+str+'+"')
        print s
    stockExpression = stockExpression.replace('s', 'str') #"(str11Nstr22)Ustr33"
    for str in ['str11', 'str22', 'str33']:
        s = s.replace(str, '"+' + str + '+"')
        print s
    # stocklistStr += "    s = \""+stockExpression+"\"\n"
    '''

    template = env.get_template('mytemplate.html')
    codestr = template.render(stockExpression='', stocklistStr=stocklistStr, baseVariableExpression=baseVariableExpression, \
                              conditionVariableExpression=conditionVariableExpression ,buyConditionlist=buyConditionlist, sellConditionlist=sellConditionlist)
    # print codestr

    #filename = str(int(time.time()))+'.py'
    filename = "my_" + strategyId + ".py"
    #os.mknod("test.txt")
    # print filename
    f = file(filename, 'w')
    f.write(codestr)
    f.close()

    load = loader()
    m = load.load(filename)
    conn = bt.getConnection()
    st.deleteByStrategyId(strategyId, conn)
    df = bt.getTradeDay(conn,
                        start_date_str=startDateStr,
                        end_date_str=endDateStr,
                        type=1)
    # 按日期执行策略
    ip = InitParam.InitParam(strategyId, periodType, changePeriod,
                             startDateStr, endDateStr, initMoney)

    datelist = df['tradedate'].tolist()
    func = m["handle_data"]
    for d in datelist:
        # print d
        # print ip
        func(d, ip)

    summary = getSummary(strategyId, benchCode, startDateStr, endDateStr)
    # print summary
    os.remove(filename)
    return summary