Esempio n. 1
0
def calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, tradingDate,
                     stockHoldDailyList, initialMV):
    usableCach = capitalEntity.get_usable_cash()
    dailyMV = 0
    for innerCode in currHoldSet:
        dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(
            dailyQuote, tradingDate, innerCode)
        stockHoldEntity = currHoldSet[innerCode]
        vol = stockHoldEntity.vol
        # print('innerCode:' + str(innerCode))
        closePrice = dailyQuoteRow["ClosePrice"]
        mv = vol * closePrice
        dailyMV += mv
        #stockHoldEntity.setClosePrice(closePrice)
        #stockHoldEntity.setMV(mv)
        cost = stockHoldEntity.cost
        profit = (closePrice - cost) * vol

        holdDict = {
            'tradingDate': tradingDate,
            'innerCode': innerCode,
            'vol': vol,
            'buyPrice': stockHoldEntity.buyPrice,
            'cost': cost,
            'closePrice': closePrice,
            'mv': mv,
            'profit': profit,
            'profitPCT': NumUtil.get_round(profit / initialMV * 100, 5)
        }

        stockHoldDailyList.append(holdDict)
    #print('dailyMV:' + str(dailyMV))
    #print('usableCach:' + str(usableCach))
    dailyMV = dailyMV + usableCach
    return dailyMV
Esempio n. 2
0
def calculate_daily_mv(currHoldSet, capitalEntity, dailyQuote, tradingDay, stockHoldDailyList, initialCash):
    usableCash = capitalEntity.get_usable_cash()
    stockMV = 0
    for innerCode in currHoldSet:
        #try:
        dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode)
        #except:
            #缺少数据,取最近一个交易日的数据
            #dailyQuoteRow = SourceDataDao.selectByPrevTradingDay(dailyQuote, tradingDay, innerCode)
            #print('calculateDailyMV '+DateUtil.datetime2Str(tradingDay)+' '+str(innerCode))

        stockHoldEntity = currHoldSet[innerCode]
        #vol = stockHoldEntity.vol
        # print('innerCode:' + str(innerCode))
        closePrice = BackTestHelper.get_close_price(dailyQuoteRow)
        #mv = vol * closePrice
        #收盘市值
        closeMV = BackTestHelper.get_sell_mv(stockHoldEntity.lastTradePrice, stockHoldEntity.lastTradeMV, closePrice) #stockHoldEntity.buyPrice, stockHoldEntity.buyMV
        #
        stockHoldEntity.set_close_price(closePrice)
        stockHoldEntity.set_close_mv(closeMV)
        #每日市值
        stockMV += closeMV
        #该股收益(已扣佣金)(仅用于查看) TODO(部分卖出时的算法)
        profit = (closeMV - stockHoldEntity.lastTradeMV) * (1 - StockConst.BUY_COMMISSION / 100)

        holdDict = {'tradingDay': tradingDay,
                'innerCode': innerCode,
                'lastTradePrice': stockHoldEntity.lastTradePrice,
                'lastTradeMV': stockHoldEntity.lastTradeMV,
                'cost': stockHoldEntity.cost,
                'closePrice': closePrice,
                'closeMV': closeMV,
                'profit': profit,
                'openDate': stockHoldEntity.openDate,
                'profitPCT': NumUtil.get_round(profit / initialCash * 100, 5),
                'changeDate': stockHoldEntity.changeDate
        }

        stockHoldDailyList.append(holdDict)
    #print('dailyMV:' + str(dailyMV))
    #print('usableCash:' + str(usableCash))

    #总资产=股票市值+现金
    totalAsset = stockMV + usableCash
    capitalEntity.set_total_asset(totalAsset)
    #股票市值
    capitalEntity.set_stock_mv(stockMV)
Esempio n. 3
0
def calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, tradingDate,
                     stockHoldDailyList, initialMV):
    usableCach = capitalEntity.get_usable_cash()
    dailyMV = 0
    for innerCode in currHoldSet:
        #try:
        dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(
            dailyQuote, tradingDate, innerCode)
        #except:
        #缺少数据,取最近一个交易日的数据
        #dailyQuoteRow = SourceDataDao.selectByPrevTradingDay(dailyQuote, tradingDate, innerCode)
        #print('calculateDailyMV '+DateUtil.datetime2Str(tradingDate)+' '+str(innerCode))

        stockHoldEntity = currHoldSet[innerCode]
        #vol = stockHoldEntity.vol
        # print('innerCode:' + str(innerCode))
        closePrice = BackTestHelper.get_close_price(dailyQuoteRow)
        #mv = vol * closePrice
        #收盘市值
        closeMV = BackTestHelper.get_sell_mv(stockHoldEntity.buyPrice,
                                             stockHoldEntity.buyMV, closePrice)
        #每日市值
        dailyMV += closeMV
        #该股收益(已扣佣金)(仅用于查看)
        profit = (closeMV - stockHoldEntity.buyMV) * (
            1 - StockConst.BUY_COMMISSION / 100)

        holdDict = {
            'tradingDate': tradingDate,
            'innerCode': innerCode,
            'buyPrice': stockHoldEntity.buyPrice,
            'buyMV': stockHoldEntity.buyMV,
            'cost': stockHoldEntity.cost,
            'closePrice': closePrice,
            'closeMV': closeMV,
            'profit': profit,
            'openDate': stockHoldEntity.openDate,
            'profitPCT': NumUtil.get_round(profit / initialMV * 100, 5)
        }

        stockHoldDailyList.append(holdDict)
    #print('dailyMV:' + str(dailyMV))
    #print('usableCach:' + str(usableCach))
    dailyMV = dailyMV + usableCach
    return dailyMV
Esempio n. 4
0
def get_max_drop(net_value_list_param):
    net_value_list = net_value_list_param.copy()
    net_value_list.insert(1, 'hightestNetValue', Series())
    net_value_list.insert(2, 'drop', Series())
    hightest = 0
    #算出历史最高,和当日相对于历史最高的回撤
    for index, row in net_value_list.iterrows():
        #print(row['netValue'])
        if row['netValue'] > hightest:
            hightest = row['netValue']
        net_value_list.ix[index, ['hightestNetValue']] = hightest
        net_value_list.ix[index, ['drop']] = NumUtil.get_change_pct(
            hightest, row['netValue'], 2) * -1

    #最大回撤
    max_drop = net_value_list['drop'].max()

    # 最大回撤
    max_drop_rows = net_value_list.loc[(net_value_list['drop'] == max_drop)]
    # 最大回撤发生日
    max_drop_end_date = DateUtil.datetime2_str(
        max_drop_rows.index[0])  #& (netValueList.index > maxDropStartDate)
    hightest_net_value = max_drop_rows.ix[0, ['hightestNetValue']][
        0]  #['hightestNetValue']

    #print('maxDropEndDate:'+str(maxDropEndDate))
    #print('hightestNetValue:' + str(hightestNetValue))

    # 最大回撤开始日
    # 找到净值最高的记录 且 离开回撤发生日最近
    hightest_rows = net_value_list.loc[(
        net_value_list['netValue'] == hightest_net_value
    )]  # & (DateUtil.datetime2Str(netValueList.index) <= maxDropEndDate)
    hightest_rows = hightest_rows.sort_index(ascending=False)
    max_drop_start_date = DateUtil.datetime2_str(hightest_rows.index[0])
    #print('maxDropStartDate:' + str(maxDropStartDate))

    max_drop_dict = {
        'maxDrop': NumUtil.get_round(max_drop, 2),
        'maxDropStartDate': max_drop_start_date,
        'maxDropEndDate': max_drop_end_date
    }

    return max_drop_dict
Esempio n. 5
0
 def reduce_usable_cash(self, a_cach):
     self.usable_cach -= a_cach
     self.usable_cach = NumUtil.get_round(self.usable_cach,
                                          StockConst.USABLE_CASH_SCALE)
Esempio n. 6
0
 def increase_usable_cash(self, a_cach):
     self.usable_cach += a_cach
     self.usable_cach = NumUtil.get_round(self.usable_cach,
                                          StockConst.USABLE_CASH_SCALE)
Esempio n. 7
0
def handleBuyList(tradingDate, buyList, dailyQuote, usableVol, stockHoldDF,
                  stockTradeList, currHoldSet, actualBuyList, cannotBuyList,
                  capitalEntity):
    usableCach = capitalEntity.get_usable_cash()
    if usableCach == 0:
        return 0

    #if usableVol == 0:
    #return 0

    if len(buyList) == 0:
        return 0

    turnover = usableCach / len(buyList)
    #print('vol:'+vol)
    partChangePCT = 0
    for innerCode in buyList:
        dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(
            dailyQuote, tradingDate, innerCode)
        #print('innerCode:'+str(innerCode))
        #print(dailyQuoteRow)
        turnoverValue = dailyQuoteRow["TurnoverValue"]
        turnoverVolume = dailyQuoteRow["TurnoverVolume"]
        buyFlg = dailyQuoteRow[StockConst.BUY_FLG]
        #可买
        if buyFlg != -1:
            actualBuyList.append(innerCode)
            #平均买入价(没有算佣金)
            buyPrice = turnoverValue / turnoverVolume
            #成本价(算佣金)
            cost = buyPrice * (1 + StockConst.BUY_COMMISSION / 100)
            #仓位(股)
            vol = NumUtil.get_round(turnover / cost, StockConst.VOL_SCALE)
            #佣金(元)
            commission = BackTestHelper.get_buy_commission(
                turnover, StockConst.BUY_COMMISSION / 100)
            #更新可用金额(减少)
            capitalEntity.reduce_usable_cash(turnover)
            #changePCT = NumUtil.getChangePCT(cost, closePrice, 2)
            #realChangePCT = (changePCT - StockConst.buyCommission) * vol / 100.0
            #partChangePCT = partChangePCT + realChangePCT
            #
            #stockHold = StockHoldEntity.StockHoldEntity(tradingDate,innerCode,vol)
            #stockTrade = StockTradeEntity.StockTradeEntity(tradingDate, innerCode, 1, vol, cost, '', '')
            stockTradeDict = {
                'tradingDate': tradingDate,
                'innerCode': innerCode,
                'type': 1,
                'vol': vol,
                'price': buyPrice,
                'turnover': turnover,
                'commission': commission
            }
            stockTradeList.append(stockTradeDict)
            #插入表
            #stockHoldDF.setdefault(innerCode,stockHold)
            #当前持仓
            stockHoldEntity = StockHoldEntity.StockHoldEntity(
                innerCode, vol, buyPrice, cost,
                DateUtil.datetime2_str(tradingDate))  #tradingDate,
            currHoldSet.setdefault(innerCode, stockHoldEntity)  #vol
            """
            if (DateUtil.datetime2Str(tradingDate) == '2014-12-08') | (DateUtil.datetime2Str(tradingDate) == '2014-12-09'):
                print('-----handleBuyList-----')
                print(DateUtil.datetime2Str(tradingDate))
                print(innerCode)
                print(vol)
                print(buyPrice)
                print(cost)
                print(turnoverValue)
                print(dailyQuoteRow)
                print('----------')
            """
        else:
            cannotBuyList.append(innerCode)