Esempio n. 1
0
    def reverse_singleCode(self, code, startDate, endDate):
        days = list(TradedayDataProcess().getTradedays(startDate, endDate))
        #factors=['closeStd','index','marketValue','industry']
        dailyRepo = dailyFactorsProcess()
        dailyFactor = dailyRepo.getSingleStockDailyFactors(
            code, startDate, endDate)
        dailyKLine = KLineDataProcess('daily')
        dailyData = dailyKLine.getDataByDate(code, startDate, endDate)
        if dailyData.empty == True:
            logger.error(
                f'there is no data of {code} from {startDate} to {endDate}')
            return pd.DataFrame()
        tick = TickDataProcess()
        tickfactor = tickFactorsProcess()
        mydata = []
        position = 0
        profit = 0
        myStatusList = []
        myTradeList = []
        myStatus = {}
        myTrade = {}
        positionYesterday = 0
        select1 = []
        select2 = []
        selectall = []
        for today in days:
            #logger.info(f'{code} in {today} start!')
            todayInfo = dailyFactor[dailyFactor['date'] == today]
            if todayInfo.empty == True:
                logger.error(
                    f'there is no factor data of {code} in date {today}')
                continue
                pass
            todayKLine = dailyData[dailyData['date'] == today]
            if todayKLine.empty == True:
                logger.error(
                    f'there is no KLine data of {code} in date {today}')
                continue
                pass
            myStatus['date'] = today
            myStatus['closeStd20'] = todayInfo['closeStd20'].iloc[0]
            myStatus['weight50'] = todayInfo['weight50'].iloc[0]
            myStatus['weight300'] = todayInfo['weight300'].iloc[0]
            myStatus['weight500'] = todayInfo['weight500'].iloc[0]
            myStatus['ts_closeStd20'] = todayInfo['closeStd20'].iloc[0]
            myStatus['adjFactor'] = todayKLine['adjFactor'].iloc[0]
            myStatus['preClose'] = todayKLine['preClose'].iloc[0]
            positionNow = positionYesterday
            if (todayInfo.empty
                    == False) & (todayKLine['status'].iloc[0] != '停牌'):
                #tickData=tick.getDataByDateFromLocalFile(code,today)
                tickData = tick.getTickShotDataFromInfluxdbServer(code, today)
                #factors=tickfactor.getDataByDateFromLocalFile(code,today,'buySellForce')
                factors = tickfactor.getFactorsUsedByDateFromLocalFile(
                    code, today)
                all = pd.merge(tickData,
                               factors,
                               how='left',
                               left_index=True,
                               right_index=True)
                all['closeStd20'] = todayInfo['closeStd20'].iloc[0]
                all['ts_closeStd20'] = todayInfo['ts_closeStd20'].iloc[0]
                all['adjFactor'] = todayKLine['adjFactor'].iloc[0]
                all['preClose'] = todayKLine['preClose'].iloc[0]
                all['increaseToday'] = all['midPrice'] / all['preClose'] - 1
                all['midIncreasePrevious5m'] = all['midPrice'] / all[
                    'midPrice'].shift(60) - 1
                #select0=all[all['increaseToday']>2*all['closeStd20']]
                selectall.append(all)
                select0 = all[all['midIncreasePrevious5m'] > 0.5 *
                              all['closeStd20']]
                select1.append(select0)
                select0 = all[all['midIncreasePrevious5m'] < -0.5 *
                              all['closeStd20']]
                select2.append(select0)
                pass
            else:
                logger.warning(f'There is no data of {code} in {today}')
        mycolumns = [
            'midIncreaseNext1m', 'midIncreaseNext5m', 'ts_buyForceIncrease',
            'ts_sellForceIncrease', 'ts_buySellVolumeRatio5',
            'midIncreasePrevious3m', 'differenceHighLow',
            'ts_differenceHighLow', 'differenceMidVwap',
            'ts_differenceMidVwap', 'midStd60', 'ts_midStd60'
        ]
        select1 = pd.concat(select1)
        select2 = pd.concat(select2)
        selectall = pd.concat(selectall)
        selectall = selectall[mycolumns]
        s = select1[(select1['ts_sellForceIncrease'] > 0.6)
                    & (select1['ts_buySellVolumeRatio5'] > 0.6) &
                    (select1['increaseToday'] > select1['closeStd20'])]
        print(s['midIncreaseNext5m'].mean())
        ss = select2[(select2['ts_buySellVolumeRatio5'] > 0.6)
                     & (select2['increaseToday'] < -select2['closeStd20'])]
        print(ss['midIncreaseNext5m'].mean())
        return mydata


########################################################################
Esempio n. 2
0
 def dataPrepared(self, stockCodes, startDate, endDate):
     warnings.filterwarnings('ignore')
     recordFilePath = self.__localFileStrResult
     mytradedays = TradedayDataProcess.getTradedays(startDate, endDate)
     mylist = stockCodes
     myMinute = dkl.KLineDataProcess('minute')
     num = 0
     dailyFactor = dailyFactorsProcess()
     #factors=['closeStd','index','marketValue','industry']
     allDailyData = dailyFactor.getMultipleStockDailyFactors(
         mylist, startDate, endDate)
     #store = pd.HDFStore(self.__factorsAddress,'a')
     #allDailyData=store.select(self.__key,where=['date>="%s" and date<="%s"'%(startDate,endDate)])
     #store.close()
     store = pd.HDFStore(recordFilePath, 'a')
     oldKeys = store.keys()
     for code in mylist:
         num = num + 1
         print("{0}({1} of {2}) start!".format(str(code), num, len(mylist)))
         #print(datetime.datetime.now())
         if ('/' + code) in oldKeys:
             continue
             pass
         m = myMinute.getDataByDate(code, startDate, endDate)
         if len(m) == 0:
             continue
         m = m[m['date'].isin(mytradedays)]
         m['vwap'] = m['amount'] / m['volume']
         m.loc[(m['date'] == m['date'].shift(5)),
               'increase5m'] = (m['open'] / m['open'].shift(5) - 1)
         m.loc[(m['date'] == m['date'].shift(1)),
               'increase1m'] = (m['open'] / m['open'].shift(1) - 1)
         d = allDailyData.xs(code, level='code')
         dailyInfo = d.loc[m['date']]
         deleteColumns = [
             'code', 'date', 'open', 'high', 'low', 'close', 'volume',
             'amount', 'change', 'pctChange', 'vwap'
         ]
         mycolumns = [
             col for col in dailyInfo.columns if col not in deleteColumns
         ]
         dailyInfo = dailyInfo[mycolumns]
         dailyInfo.rename(columns={'preClose': 'yesterdayClose'},
                          inplace=True)
         dailyInfo.index = m.index
         m = pd.concat([m, dailyInfo], axis=1)
         m['increaseInDay'] = (m['open'] / m['yesterdayClose'] - 1)
         m = m[m['status'] != 'N']
         m['canBuy'] = 0
         m['canSell'] = 0
         m['canBuyPrice'] = None
         m['canSellPrice'] = None
         m.loc[((m['open'] < round(1.097 * m['yesterdayClose'], 2)) &
                (m['open'] > 0)), 'canBuy'] = 1
         m.loc[((m['open'] > round(0.903 * m['yesterdayClose'], 2)) &
                (m['open'] > 0)), 'canSell'] = 1
         m.loc[m['canBuy'] == 1, 'canBuyPrice'] = m.loc[m['canBuy'] == 1,
                                                        'open']
         m.loc[m['canBuy'] == 1, 'canBuyPriceAdj'] = m.loc[m['canBuy'] == 1,
                                                           'adjFactor']
         m['canBuyPrice'] = m['canBuyPrice'].fillna(method='bfill')
         m['canBuyPriceAdj'] = m['canBuyPriceAdj'].fillna(method='bfill')
         m.loc[m['canSell'] == 1, 'canSellPrice'] = m.loc[m['canSell'] == 1,
                                                          'open']
         m.loc[m['canSell'] == 1,
               'canSellPriceAdj'] = m.loc[m['canSell'] == 1, 'adjFactor']
         m['canSellPrice'] = m['canSellPrice'].fillna(method='bfill')
         m['canSellPriceAdj'] = m['canSellPriceAdj'].fillna(method='bfill')
         m['timeStamp'] = m['date'] + m['tick']
         mselect = m.set_index(['timeStamp', 'code'])
         store.append(code, mselect, append=False, format="table")
         pass
     store.close()
     pass
Esempio n. 3
0
    def reverse_singleCode(self,
                           code,
                           startDate,
                           endDate,
                           parameters=[300, 100000000, 1.5]):
        myindex = parameters[0]
        totalCash = parameters[1]
        std1 = parameters[2]
        days = list(TradedayDataProcess().getTradedays(startDate, endDate))
        #factors=['closeStd','index','marketValue','industry']
        dailyRepo = dailyFactorsProcess()
        dailyFactor = dailyRepo.getSingleStockDailyFactors(
            code, startDate, endDate)
        dailyKLine = KLineDataProcess('daily')
        dailyData = dailyKLine.getDataByDate(code, startDate, endDate)
        if dailyData.empty == True:
            logger.error(
                f'there is no data of {code} from {startDate} to {endDate}')
            return pd.DataFrame()
        tick = TickDataProcess()
        mydata = []
        position = 0
        profit = 0
        myStatusList = []
        myTradeList = []
        myStatus = {}
        myTrade = {}
        positionYesterday = 0
        for today in days:
            #logger.info(f'{code} in {today} start!')
            todayInfo = dailyFactor[dailyFactor['date'] == today]
            if todayInfo.empty == True:
                logger.error(
                    f'there is no factor data of {code} in date {today}')
                continue
                pass

            todayKLine = dailyData[dailyData['date'] == today]
            if todayKLine.empty == True:
                logger.error(
                    f'there is no KLine data of {code} in date {today}')
                continue
                pass
            myStatus['date'] = today
            myStatus['closeStd20'] = todayInfo['closeStd20'].iloc[0]
            myStatus['weight50'] = todayInfo['weight50'].iloc[0]
            myStatus['weight300'] = todayInfo['weight300'].iloc[0]
            myStatus['weight500'] = todayInfo['weight500'].iloc[0]
            myStatus['ts_closeStd20'] = todayInfo['closeStd20'].iloc[0]
            myStatus['adjFactor'] = todayKLine['adjFactor'].iloc[0]
            myStatus['preClose'] = todayKLine['preClose'].iloc[0]
            positionNow = positionYesterday
            if (todayInfo.empty
                    == False) & (todayKLine['status'].iloc[0] != '停牌'):
                if myindex == 300:
                    maxPosition = myStatus[
                        'weight300'] * totalCash * 0.01 / myStatus['preClose']
                elif myindex == 500:
                    maxPosition = myStatus[
                        'weight300'] * totalCash * 0.01 / myStatus['preClose']
                elif myindex == 50:
                    maxPosition = myStatus[
                        'weight50'] * totalCash * 0.01 / myStatus['preClose']
                else:
                    maxPosition = myStatus['totalCash'] * 0.001 / myStatus[
                        'preClose']
                maxPosition = round(maxPosition, -2)
                if maxPosition == 0:
                    continue
                tickData = tick.getDataByDateFromLocalFile(code, today)
                #['code' ,'date','tick' ,'lastPrice','S1','S2','S3','S4','S5','S6','S7','S8','S9','S10','B1','B2','B3','B4','B5','B6','B7','B8','B9','B10','SV1','SV2','SV3','SV4','SV5','SV6','SV7','SV8','SV9','SV10','BV1','BV2','BV3','BV4','BV5','BV6','BV7','BV8','BV9','BV10','volume' ,'amount','volumeIncrease','amountIncrease']
                tickList = tickData.as_matrix()
                for i in range(0, len(tickList) - 60):
                    now = tickList[i]
                    midPrice = (now[4] + now[14]) / 2
                    lastPrice = now[3]
                    tickShot = now[4:43]
                    upCeiling = False
                    downCeiling = False
                    if now[24] == 0:
                        upCeiling = True
                        pass
                    if now[34] == 0:
                        downCeiling = True
                        pass
                    mytime = datetime.datetime.strptime(
                        now[1] + now[2], '%Y%m%d%H%M%S%f')
                    increaseToday = tickList[i][3] / myStatus['preClose'] - 1
                    if (i >= 100) & (tickList[i - 100][3] > 0):
                        increase5m = tickList[i][3] / tickList[i - 100][3] - 1
                    else:
                        increase5m = np.nan
                    if ((positionNow == 0) & (positionYesterday == 0) &
                        (i <= 4500) & (i >= 100) &
                        (increaseToday > std1 * myStatus['closeStd20']) &
                        (maxPosition > 0) & (downCeiling == False)):
                        #开空头
                        [price, deltaPosition,
                         amount] = TradeUtility.sellByTickShotData(
                             tickShot, maxPosition, 0.001)
                        positionNow = -deltaPosition
                        myTrade['date'] = today
                        myTrade['opentime'] = mytime
                        myTrade['position'] = positionNow
                        myTrade['open'] = price
                        myTrade['openAdj'] = myStatus['adjFactor']
                        myTrade['increase5m'] = increase5m
                        myTrade['increaseToday'] = increaseToday
                        openIndex = i
                        maxPosition = maxPosition - deltaPosition
                        pass
                    elif ((positionNow == 0) & (positionYesterday == 0) &
                          (i <= 4500) & (i >= 100) &
                          (increaseToday < -std1 * myStatus['closeStd20']) &
                          (maxPosition > 0) & (upCeiling == False)):
                        #开多头
                        [price, deltaPosition,
                         amount] = TradeUtility.buyByTickShotData(
                             tickShot, maxPosition, 0.001)
                        positionNow = deltaPosition
                        myTrade['date'] = today
                        myTrade['opentime'] = mytime
                        myTrade['position'] = positionNow
                        myTrade['open'] = price
                        myTrade['openAdj'] = myStatus['adjFactor']
                        myTrade['increase5m'] = increase5m
                        myTrade['increaseToday'] = increaseToday
                        maxPosition = maxPosition - deltaPosition
                        openIndex = i
                        pass
                    elif (positionNow > 0):
                        profit = (lastPrice * myStatus['adjFactor'] -
                                  myTrade['open'] * myTrade['openAdj']) / (
                                      myTrade['open'] * myTrade['openAdj'])
                        if (((profit < 0) & (i >= openIndex + 400)) |
                            (i >= 4700) | (i >= openIndex + 1200) |
                            (positionYesterday != 0)):
                            if downCeiling == False:  #未跌停
                                #平多头,记录一笔交易
                                [price, deltaPosition,
                                 amount] = TradeUtility.sellByTickShotData(
                                     tickShot, positionNow, 0.1)
                                myTrade['closetime'] = mytime
                                myTrade['close'] = price
                                myTrade['closeAdj'] = myStatus['adjFactor']
                                positionNow = 0
                                positionYesterday = 0
                                openIndex = 0
                                tradeCopy = copy.deepcopy(myTrade)
                                myTradeList.append(tradeCopy)
                                myTrade = {}
                            pass
                    elif (positionNow < 0):
                        profit = (-lastPrice * myStatus['adjFactor'] +
                                  myTrade['open'] * myTrade['openAdj']) / (
                                      myTrade['open'] * myTrade['openAdj'])
                        if (((profit < 0) & (i >= openIndex + 400)) |
                            (i >= 4700) | (i >= openIndex + 1200) |
                            (positionYesterday != 0)):
                            if upCeiling == False:
                                #平空头,记录一笔交易
                                [price, deltaPosition,
                                 amount] = TradeUtility.buyByTickShotData(
                                     tickShot, -positionNow, 0.1)
                                myTrade['closetime'] = mytime
                                myTrade['close'] = price
                                myTrade['closeAdj'] = myStatus['adjFactor']
                                positionNow = 0
                                positionYesterday = 0
                                openIndex = 0
                                tradeCopy = copy.deepcopy(myTrade)
                                myTradeList.append(tradeCopy)
                                myTrade = {}

                            pass
                if positionNow != 0:
                    logger.info(
                        f'{code} of {today} can not close the position!')
                positionYesterday = positionNow
                pass
            else:
                logger.warning(f'There is no data of {code} in {today}')
        m = pd.DataFrame(data=myTradeList,
                         columns=[
                             'date', 'opentime', 'position', 'open', 'openAdj',
                             'increase5m', 'increaseToday', 'closetime',
                             'close', 'closeAdj'
                         ])

        m['profit'] = m['position'] * (m['close'] * m['closeAdj'] -
                                       m['open'] * m['openAdj']) / m['openAdj']
        m['fee'] = m['position'].abs() * m[['open', 'close'
                                            ]].max(axis=1) * 0.0012
        m['netProfit'] = m['profit'] - m['fee']
        m['code'] = code
        mycolumns = [
            'date', 'closeStd20', 'ts_closeStd20', 'industry', 'industryName'
        ]
        dailyFactor = dailyFactor[mycolumns]
        mydata = pd.merge(m,
                          dailyFactor,
                          how='left',
                          left_on='date',
                          right_on='date')
        #mydata=mydata[((mydata['increase5m']>mydata['closeStd20']) & (mydata['position']<0)) |((mydata['increase5m']<-mydata['closeStd20']) & (mydata['position']>0))]
        '''
        print(mydata)
        print(mydata['profit'].sum())
        print(mydata['fee'].sum())
        print(mydata['netProfit'])
        '''
        return mydata


########################################################################
Esempio n. 4
0
 def updateDailyFactors(self, codeList, factors=DAILYFACTORSNEEDTOUPDATE):
     dailyFactor = dailyFactorsProcess()
     dailyFactor.parallelizationUpdateFactorsVersion2(codeList, factors)
     pass