コード例 #1
0
ファイル: NewAnalyze2.py プロジェクト: mikkie/stocking
class NewAnalyze2(object):
    def __init__(self):
        self.__config = Config()
        try:
            if self.__config.get_mongo_config()['load_from_mongo']:
                self.__mongo_client = pymongo.MongoClient(
                    self.__config.get_mongo_config()['mongo_url'])
                self.__mongo_client.admin.command('ismaster')
                self.__config.update_proxy(self.get_t1_from_mongo())
                self.__config.get_mongo_config(
                )['load_time'] = dt.datetime.now()
        except Exception as e:
            MyLog.error('can not connect to mongo, %s' % e)
        self.__buyedCount = 0
        self.__balance = self.__config.get_t1()['trade']['balance']
        if self.__config.get_t1()['trade']['enable']:
            self.__trade = Trade()
        if self.__config.get_t1()['trade']['enableMock']:
            self.__mockTrade = MockTrade()
        self.__engine = create_engine(self.__config.get_DBurl())

    def get_t1_from_mongo(self):
        for doc in self.__mongo_client['stocking']['config'].find(
            {'t1': {
                '$exists': True
            }}):
            return doc
        return None

    def update_config_if_needed(self):
        try:
            if self.__config.get_mongo_config(
            )['load_from_mongo'] and self.__config.get_mongo_config(
            )['load_time'] is not None and (
                    dt.datetime.now() -
                    self.__config.get_mongo_config()['load_time']
            ).seconds > self.__config.get_mongo_config()['load_interval']:
                self.__config.update_proxy(self.get_t1_from_mongo())
                self.__config.get_mongo_config(
                )['load_time'] = dt.datetime.now()
        except Exception as e:
            MyLog.error('failed to update config %s ' % e)

    def printPerformace(self, timestamp):
        deltaSeconds = (dt.datetime.now() - timestamp).seconds
        if deltaSeconds > 3:
            print('calc more than %s seconds' % deltaSeconds)

    def calcMain(self, zs, dh, timestamp, balance, lock):
        self.update_config_if_needed()
        data = dh.get_data()
        finalCode = ''
        result = []
        codes = []
        for code in data:
            if not self.hasNewData(data[code]):
                continue
            if code in dh.get_ignore():
                continue
        #   self.updateStock(data[code],dh)
            if code in dh.get_buyed():
                #self.cancelBuyIfNeed(data[code],dh,timestamp,lock,balance)
                #continue
                pass
            try:
                if self.calc(zs, data[code], dh):
                    result.append(data[code])
            #  self.printPerformace(timestamp)
            except Exception as e:
                last_line = data[code].get_Lastline()
                MyLog.error(last_line['time'] + ' :calc ' + code + ' error')
                MyLog.error(str(e))
        if len(result) != 0:
            for stock in result:
                try:
                    last_line = stock.get_Lastline()
                    return self.outputRes(stock, last_line, timestamp, dh,
                                          balance, lock)
                except Exception as e:
                    MyLog.error('outputRes error %s' % stock.get_code())
                    MyLog.error(str(e))

    def updateStock(self, stock, dh):
        self.updateBreak10(stock)
        max_amount = stock.get_cache('max_b1_amount')
        now_line = stock.get_Lastline()
        stop_price = round(float(now_line['pre_close']) * 1.1, 2)
        if float(now_line['b1_p']) == stop_price:
            now_amount = float(now_line['b1_p']) * self.convertToFloat(
                now_line['b1_v']) * 100
            if max_amount is None or now_amount > max_amount:
                stock.set_cache('max_b1_amount', now_amount)
        if stock.get_code() in dh.get_buyed():
            last_second_line = stock.get_LastSecondline()
            if last_second_line is None or float(
                    last_second_line['b1_p']) != stop_price:
                return
            deal_amount = self.convertToFloat(
                now_line['amount']) - self.convertToFloat(
                    last_second_line['amount'])
            old_deal_amount = stock.get_cache('deal_amount')
            if old_deal_amount is None:
                stock.set_cache('deal_amount', [deal_amount])
            else:
                if len(old_deal_amount) >= 15:
                    old_deal_amount.pop(0)
                old_deal_amount.append(deal_amount)
                stock.set_cache('deal_amount', old_deal_amount)

    def cancelBuyIfNeed(self, stock, dh, timestamp, lock, balance):
        trade = self.__config.get_t1()['trade']
        last_second_line = stock.get_LastSecondline()
        now_line = stock.get_Lastline()
        last_second_buy1_v = self.convertToFloat(last_second_line['b1_v'])
        last_second_buy1_amount = float(
            last_second_line['b1_p']) * last_second_buy1_v * 100
        now_buy1_v = self.convertToFloat(now_line['b1_v'])
        now_buy1_amount = float(now_line['b1_p']) * now_buy1_v * 100
        stop_price = round(float(now_line['pre_close']) * 1.1, 2)
        max_b1_amount = stock.get_cache('max_b1_amount')
        deal_amount = self.convertToFloat(
            now_line['amount']) - self.convertToFloat(
                last_second_line['amount'])
        cancel_b1_amount = self.__config.get_t1()['hit10']['cancel_b1_amount']
        strTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
        if strTime > '13:30:00':
            cancel_b1_amount = self.__config.get_t1(
            )['hit10']['cancel_b1_amount_1']
        if strTime > '14:30:00':
            cancel_b1_amount = self.__config.get_t1(
            )['hit10']['cancel_b1_amount_2']
        if max_b1_amount is None:
            max_b1_amount = -1
        sum_last_10_deal_amount = 0
        last_10_deal_amount = stock.get_cache('deal_amount')
        if last_10_deal_amount is not None:
            sum_last_10_deal_amount = sum(last_10_deal_amount)
        if float(now_line['price']) == stop_price:
            if float(now_line['b1_p']) == stop_price and self.convertToFloat(
                    now_line['a1_v']
            ) == 0 and (
                    now_buy1_amount < cancel_b1_amount
                    or now_buy1_v < last_second_buy1_v *
                    self.__config.get_t1()['hit10']['cancel_ratio']
                    or now_buy1_amount < max_b1_amount *
                    self.__config.get_t1()['hit10']['cancel_ratio_max_amount']
                    or deal_amount >= now_buy1_amount *
                    self.__config.get_t1()['hit10']['cancel_deal_amount_ratio']
                    or sum_last_10_deal_amount > now_buy1_amount *
                    self.__config.get_t1()['hit10']['max_deal_amount']):
                info = '[%s] 在 [%s] 撤单 [%s],b1_v=%s' % (
                    Utils.getCurrentTime(), str(now_line['date']) + ' ' +
                    str(now_line['time']), stock.get_code(), now_buy1_v)
                MyLog.info(info)
                if trade['enable'] or trade['enableMock']:
                    self.cancelBuy(trade, stock, dh, lock, balance)
                # self.printPerformace(timestamp)

    @Utils. async
    def save_to_excel(self, stock):
        try:
            now_line = stock.get_Lastline()
            df = pd.DataFrame(stock.get_data())
            writer = pd.ExcelWriter(
                'D:/aqua/stock/stocking/data/result/%s-%s.xlsx' %
                (stock.get_code(), now_line['time'].replace(':', '-')))
            df.to_excel(writer, 'Sheet1')
            writer.save()
        except Exception as e:
            MyLog.error('save %s error, e = %s' % (stock.get_code(), e))

    def cancelBuy(self, trade, stock, dh, lock, balance):
        status = -1
        if trade['enable']:
            try:
                lock.acquire()
                status = self.__trade.cancel(stock.get_code(), True)
                buyMoney = stock.get_cache('buyMoney')
                if buyMoney is not None:
                    balance.value = balance.value + buyMoney
            except Exception as e:
                pass
            finally:
                lock.release()
        if trade['enableMock']:
            buyMoney = stock.get_cache('buyMoney')
            if buyMoney is not None:
                balance.value = balance.value + buyMoney
            status = self.__mockTrade.cancelBuy(stock.get_code())
        #cancel success
        if status == 0:
            MyLog.info('[%s] 撤单成功' % stock.get_code())
            now_line = stock.get_Lastline()
            now_buy1_v = self.convertToFloat(now_line['b1_v'])
            now_buy1_amount = float(now_line['b1_p']) * now_buy1_v * 100
            stock.set_cache('cancel_b1_amount', now_buy1_amount)
            dh.get_buyed().remove(stock.get_code())
        #cancel failed, already buyed
        else:
            MyLog.info('[%s] 撤单失败' % stock.get_code())
            stock.add_cancelTimes()
            if stock.get_cancelTimes() >= 2:
                dh.add_ignore(stock.get_code())

    def outputRes(self, stock, df_final, timestamp, dh, balance, lock):
        p = (float(df_final['price']) - float(df_final['pre_close'])) / float(
            df_final['pre_close'])
        d_price = round(float(df_final['pre_close']) * (1 + p + 0.005), 2)
        #   d_price = round(float(df_final['price']) + 0.02, 2)
        trade = self.__config.get_t1()['trade']
        buyVolume = trade['volume']
        if trade['dynamicVolume']:
            buyVolume = int(trade['amount'] / d_price / 100) * 100
        buyMoney = d_price * buyVolume
        price = str('%.2f' % d_price)
        try:
            if lock is not None:
                lock.acquire()
            if balance is not None and buyMoney > balance.value or buyVolume == 0:
                return None
            if df_final['code'] in dh.get_buyed():
                dh.add_ignore(df_final['code'])
            info = '在 %s 以 %s 买入 [%s]%s %s 股' % (
                str(df_final['date']) + ' ' + str(df_final['time']), price,
                df_final['code'], df_final['name'], str(buyVolume))
            MyLog.info(info)
            now = dt.datetime.now()
            deltaSeconds = (now - timestamp).seconds
            if deltaSeconds > trade['timestampLimit']:
                MyLog.info('[%s] 行情超时 %s秒 放弃买入' %
                           (df_final['code'], deltaSeconds))
                return None
            if trade['enable']:
                res = str(
                    self.__trade.buy(df_final['code'], buyVolume,
                                     float(price)))
                if 'entrust_no' in res or 'success' in res:
                    if balance is not None:
                        balance.value = balance.value - buyMoney
                    stock.set_cache('buyMoney', buyMoney)
                    stock.set_cache('buy_price', d_price)
                    dh.add_buyed(df_final['code'])
                    return df_final['code'], str(d_price)
                return None
            if trade['enableMock']:
                res = self.__mockTrade.mockTrade(df_final['code'],
                                                 float(price), buyVolume)
                if res == 0:
                    if balance is not None:
                        balance.value = balance.value - buyMoney
                    stock.set_cache('buyMoney', buyMoney)
                    stock.set_cache('buy_price', d_price)
                    dh.add_buyed(df_final['code'])
                    return df_final['code'], str(d_price)
                return None
            stock.set_cache('buy_price', d_price)
            dh.add_buyed(df_final['code'])
            return df_final['code'], str(d_price)
        except Exception as e:
            MyLog.error(e)
            return None
        finally:
            if lock is not None:
                lock.release()
            self.save_to_excel(stock)
        #   self.printPerformace(timestamp)

    def calc(self, zs, stock, dh):
        if not self.canCalc(stock, dh):
            return False
        if stock.get_code() in dh.get_buyed():
            try:
                return self.is_bc_point(stock, dh)
            except Exception as e:
                MyLog.error('bc point error %s' % e)
                return False
        return self.isStockMatch(zs, stock, dh)

    def isOpenMatch(self, row):
        if float(row['pre_close']) == 0 or float(row['open']) == 0:
            return False
        return (float(row['open']) - float(row['pre_close'])) / float(
            row['pre_close']) * 100 >= -1.5

    def hasNewData(self, stock):
        lastLine = stock.get_Lastline()
        if stock.get_time() == lastLine['time']:
            return False
        stock.set_time(lastLine['time'])
        return True

    def canCalc(self, stock, dh):
        if stock.len() < 2:
            return False
        lastLine = stock.get_Lastline()
        lastSecondLine = stock.get_LastSecondline()
        if float(lastLine['open']) == 0.0 or float(
                lastSecondLine['open']) == 0.0:
            return False
        return True

    def convertToFloat(self, str):
        if str == '':
            return 0
        try:
            return float(str)
        except Exception as e:
            MyLog.error('convertToFloat error: ' + str + '\n')
            return 0

    def isStockMatch(self, zs, stock, dh):
        if self.isZSMatch(zs, stock):
            if stock.get_cache('ignore_ydls') is None and self.isYDLS(
                    stock, dh):
                return True
        try:
            return self.is_tail_match(stock, dh)
        except Exception as e:
            MyLog.error('buy tailed error %s' % e)
            return False

    def is_tail_match(self, stock, dh):
        now_line = stock.get_Lastline()
        if now_line['time'] < '14:30:00':
            return False
        cur_p = self.getCurrentPercent(stock)
        if cur_p > self.__config.get_t1()['buy_tail']['p_range'][
                1] or cur_p < self.__config.get_t1()['buy_tail']['p_range'][0]:
            return False
        now_price = self.convertToFloat(now_line['price'])
        lowest_price = self.convertToFloat(now_line['low'])
        highest_price = self.convertToFloat(now_line['high'])
        if now_price == lowest_price or (
                now_price - lowest_price) / self.convertToFloat(
                    now_line['pre_close']) * 100 > self.__config.get_t1(
                    )['buy_tail']['p_limit_lowest']:
            return False
        if (highest_price - lowest_price) / self.convertToFloat(
                now_line['pre_close']) * 100 < self.__config.get_t1(
                )['buy_tail']['high_low_diff']:
            return False
        datas = stock.get_data()
        start = -15
        if len(datas) < start * -1:
            start = len(datas) * -1
        amount = self.convertToFloat(
            datas[-1]['buy_amount']) - self.convertToFloat(
                datas[start]['buy_amount'])
        sell_amount = self.convertToFloat(
            datas[-1]['sell_amount']) - self.convertToFloat(
                datas[start]['sell_amount'])
        amount_ratio = 100
        if sell_amount != 0.0:
            amount_ratio = amount / sell_amount
        tag = amount > self.__config.get_t1(
        )['buy_tail']['min_amount'] and amount_ratio > self.__config.get_t1(
        )['buy_tail']['amount_ratio']
        if tag:
            MyLog.info(
                'buy tail match : time = %s, lowest_price = %s, buy_amount = %s, sell_amount = %s, amount_ratio = %s'
                % (now_line['date'] + ' ' + now_line['time'], lowest_price,
                   amount, sell_amount, amount_ratio))
        return tag

    def get_data_time(self, data):
        return dt.datetime.strptime(data['date'] + ' ' + data['time'],
                                    '%Y-%m-%d %H:%M:%S')

    def is_bc_point(self, stock, dh):
        now_line = stock.get_Lastline()
        now_second_line = stock.get_LastSecondline()
        buyed_price = stock.get_cache('buy_price')
        now_price = self.convertToFloat(now_line['price'])
        if buyed_price is not None and (
                buyed_price - now_price) / self.convertToFloat(
                    now_line['pre_close']) * 100 < self.__config.get_t1(
                    )['bc_point']['p_drop']:
            return False
        lowest_price = stock.get_cache('lowest_price')
        if lowest_price is None or lowest_price > now_price:
            lowest_price = now_price
            stock.set_cache('lowest_price', lowest_price)
        if now_price == lowest_price or (
                now_price - lowest_price) / self.convertToFloat(
                    now_line['pre_close']) * 100 > self.__config.get_t1(
                    )['bc_point']['p_limit_lowest']:
            return False
        datas = stock.get_data()
        start = -15
        if len(datas) < start * -1:
            start = len(datas) * -1
        amount = self.convertToFloat(
            datas[-1]['buy_amount']) - self.convertToFloat(
                datas[start]['buy_amount'])
        sell_amount = self.convertToFloat(
            datas[-1]['sell_amount']) - self.convertToFloat(
                datas[start]['sell_amount'])
        amount_ratio = 100
        if sell_amount != 0.0:
            amount_ratio = amount / sell_amount
        tag = amount > self.__config.get_t1(
        )['bc_point']['min_amount'] and amount_ratio > self.__config.get_t1(
        )['bc_point']['amount_ratio']
        if tag:
            MyLog.info(
                'bc buy match: time = %s, lowest_price = %s, buy_amount = %s, sell_amount = %s, amount_ratio = %s'
                % (now_line['date'] + ' ' + now_line['time'], lowest_price,
                   amount, sell_amount, amount_ratio))
        return tag

    def isYDLS(self, stock, dh):
        now_line = stock.get_Lastline()
        high_limit = round(
            decimal.Decimal(self.convertToFloat(now_line['pre_close']) * 1.1),
            2)
        if self.convertToFloat(now_line['price']) >= float(high_limit):
            stock.set_cache('ignore_ydls', True)
            self.save_to_excel(stock)
            return False
        if self.getOpenPercent(stock) - self.getPercent(
                now_line['price'],
                stock) > self.__config.get_t1()['ydls']['open-low']:
            stock.set_cache('ignore_ydls', True)
            return False
        if self.getPercent(now_line['high'], stock) - self.getPercent(
                now_line['price'],
                stock) > self.__config.get_t1()['ydls']['open-low']:
            stock.set_cache('ignore_ydls', True)
            return False
        if float(now_line['high']) > float(now_line['price']):
            return False
        stop_p = self.__config.get_t1()['ydls']['stop_p']
        current_p = self.getCurrentPercent(stock)
        if current_p < stop_p[0] or current_p > stop_p[1]:
            return False
        datas = stock.get_data_in_len(self.__config.get_t1()['ydls']['in_len'])
        length = len(datas)
        last_datetime = self.get_data_time(datas[-1])
        fist_datetime = self.get_data_time(datas[-1 * length])
        p1 = self.getPercent(datas[-1 * length]['price'], stock)
        p2 = self.getPercent(datas[-1]['price'], stock)
        p = p2 - p1
        amount = self.convertToFloat(
            datas[-1]['buy_amount']) - self.convertToFloat(
                datas[-1 * length]['buy_amount'])
        sell_amount = self.convertToFloat(
            datas[-1]['sell_amount']) - self.convertToFloat(
                datas[-1 * length]['sell_amount'])
        delta = (last_datetime - fist_datetime).seconds
        if delta < 45:
            if delta < 15:
                return False
            if p < self.__config.get_t1()['ydls']['yd_p']:
                return False
            if amount < self.__config.get_t1()['ydls']['min_amount'] or (
                    sell_amount > 0.0 and amount / sell_amount <
                    self.__config.get_t1()['ydls']['amount_ratio']):
                return False
            MyLog.info(
                '%s is match ydls, t1 = %s, t2 = %s, p1 = %s, p2 = %s, p = %s, amount = %s, sell_amount = %s, amount_ratio = %s'
                % (stock.get_code(), datas[-1 * length]['date'] + ' ' +
                   datas[-1 * length]['time'], datas[-1]['date'] + ' ' +
                   datas[-1]['time'], p1, p2, p, amount, sell_amount,
                   100 if sell_amount == 0.0 else amount / sell_amount))
            return True
        else:
            if length > 15:
                step = 2
                i = -15
                while i >= length * -1:
                    p1 = self.getPercent(datas[i]['price'], stock)
                    p2 = self.getPercent(datas[-1]['price'], stock)
                    p = p2 - p1
                    amount = self.convertToFloat(
                        datas[-1]['buy_amount']) - self.convertToFloat(
                            datas[i]['buy_amount'])
                    sell_amount = self.convertToFloat(
                        datas[-1]['sell_amount']) - self.convertToFloat(
                            datas[i]['sell_amount'])
                    fist_datetime_temp = self.get_data_time(datas[i])
                    i = i - step
                    if (last_datetime - fist_datetime_temp).seconds < 45:
                        continue
                    if p >= pow(
                        ((last_datetime - fist_datetime_temp).seconds - 45),
                            self.__config.get_t1()['ydls']
                        ['yd_ratio']) + self.__config.get_t1()['ydls']['yd_p']:
                        if amount >= self.__config.get_t1(
                        )['ydls']['min_amount'] and (
                                sell_amount == 0.0 or amount / sell_amount >=
                                self.__config.get_t1()['ydls']['amount_ratio']
                        ):
                            MyLog.info(
                                '%s is match ydls, t1 = %s , t2 = %s, p1 = %s, p2 = %s, p = %s, amount = %s, sell_amount = %s, amount_ratio = %s'
                                %
                                (stock.get_code(), datas[i + step]['date'] +
                                 ' ' + datas[i + step]['time'],
                                 datas[-1]['date'] + ' ' + datas[-1]['time'],
                                 p1, p2, p, amount, sell_amount, 100 if
                                 sell_amount == 0.0 else amount / sell_amount))
                            return True
            if p >= pow(((last_datetime - fist_datetime).seconds - 45),
                        self.__config.get_t1()['ydls']
                        ['yd_ratio']) + self.__config.get_t1()['ydls']['yd_p']:
                if amount >= self.__config.get_t1()['ydls']['min_amount'] and (
                        sell_amount == 0.0 or amount / sell_amount >=
                        self.__config.get_t1()['ydls']['amount_ratio']):
                    MyLog.info(
                        '%s is match ydls, t1 = %s , t2 = %s, p1 = %s, p2 = %s, p = %s, amount = %s, sell_amount = %s, amount_ratio = %s'
                        %
                        (stock.get_code(), datas[-1 * length]['date'] + ' ' +
                         datas[-1 * length]['time'], datas[-1]['date'] + ' ' +
                         datas[-1]['time'], p1, p2, p, amount, sell_amount,
                         100 if sell_amount == 0.0 else amount / sell_amount))
                    return True
        return False

    def updateBreak10(self, stock):
        if stock.get_cache('status') == 1:
            if stock.get_cache('min_break') is None:
                stock.set_cache('min_break', self.getCurrentPercent(stock))
            elif self.getCurrentPercent(stock) < stock.get_cache('min_break'):
                stock.set_cache('min_break', self.getCurrentPercent(stock))

    def isReach10Again(self, stock):
        now_line = stock.get_Lastline()
        stop_price = round(float(now_line['pre_close']) * 1.1, 2)
        if float(now_line['b1_p']) == stop_price and self.convertToFloat(
                now_line['a1_v']) == 0:
            stock.set_cache('min_break', None)
            stock.set_cache('status', None)
            return False
        else:
            if self.convertToFloat(now_line['a5_v']) != 0:
                return False
            if stock.get_cache('min_break') is None or stock.get_cache(
                    'min_break') > self.__config.get_t1(
                    )['hit10']['min_break']:
                return False
            if stock.get_cache('break_time') is None or (
                    dt.datetime.now() - stock.get_cache('break_time')
            ).seconds > self.__config.get_t1()['hit10']['break_time']:
                return False
            if stock.get_cache('hit_top_time') is None or (
                    stock.get_cache('break_time') -
                    stock.get_cache('hit_top_time')
            ).seconds < self.__config.get_t1()['hit10']['hit_break_inter']:
                return False
            total_sell = 0
            for i in range(1, 6):
                total_sell += self.convertToFloat(
                    now_line['a' + str(i) + '_v']) * self.convertToFloat(
                        now_line['a' + str(i) + '_p']) * 100
            if total_sell > stock.get_cache(
                    'max_b1_amount') * self.__config.get_t1(
                    )['hit10']['max_b1_amount_ratio']:
                return False
            info = '[%s]在[%s][%s] match 10,b1_v=%s' % (
                Utils.getCurrentTime(), str(now_line['date']) + ' ' +
                str(now_line['time']), stock.get_code(), now_line['b1_v'])
            MyLog.info(info)
            return True

    def isbreak10(self, stock, dh):
        now_line = stock.get_Lastline()
        stop_price = round(float(now_line['pre_close']) * 1.1, 2)
        if float(now_line['b1_p']) != stop_price and self.convertToFloat(
                now_line['a1_v']) != 0:
            count = stock.get_cache('break_count')
            if count is None:
                count = 1
            else:
                count = count + 1
            stock.set_cache('break_count', count)
            if count >= self.__config.get_t1()['hit10']['break_count']:
                stock.set_cache('status', 0)
                dh.add_ignore(stock.get_code())
            else:
                stock.set_cache('break_time', dt.datetime.now())
                stock.set_cache('min_break', self.getCurrentPercent(stock))
                stock.set_cache('status', 1)
                MyLog.info('%s is break 10' % stock.get_code())
        return False

    def isReach10(self, stock):
        now_line = stock.get_Lastline()
        stop_price = round(float(now_line['pre_close']) * 1.1, 2)
        if float(now_line['b1_p']) == stop_price and self.convertToFloat(
                now_line['a1_v']) == 0:
            hit_top_time = stock.get_cache('hit_top_time')
            strTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
            if hit_top_time is None and strTime > self.__config.get_t1(
            )['hit10']['hit_top_time']:
                return False
            stock.set_cache('hit_top_time', dt.datetime.now())
            now_b1_amount = self.convertToFloat(now_line['b1_v']) * float(
                now_line['b1_p']) * 100
            if now_b1_amount >= self.__config.get_t1(
            )['hit10']['buy_b1_amount']:
                MyLog.info('%s is reach 10' % stock.get_code())
                stock.set_cache('status', 0)
        return False

    def isZSMatch(self, zs, stock):
        if zs is None:
            return True
        code = stock.get_code()
        i = 0
        if code.startswith('3'):
            i = 5
        line = zs.iloc[i]
        pre_close = line.get('pre_close')
        price = line.get('price')
        p = (float(price) - float(pre_close)) / float(pre_close) * 100
        return p > self.__config.get_t1()['ydls']['zs']

    def getPercent(self, price, stock):
        lastLine = stock.get_Lastline()
        pre_close = lastLine['pre_close']
        open_p = (float(price) - float(pre_close)) / float(pre_close) * 100
        return open_p

    def getCurrentPercent(self, stock):
        lastLine = stock.get_Lastline()
        price = lastLine['price']
        return self.getPercent(price, stock)

    def getOpenPercent(self, stock):
        ocp = stock.get_cache('ocp')
        if ocp is not None:
            return ocp
        lastLine = stock.get_Lastline()
        open = lastLine['open']
        ocp = self.getPercent(open, stock)
        stock.set_cache('ocp', ocp)
        return ocp
コード例 #2
0
ファイル: NewAnalyze.py プロジェクト: mikkie/stocking
class NewAnalyze(object):
    def __init__(self):
        self.__config = Config()
        self.__buyedCount = 0
        self.__balance = self.__config.get_t1()['trade']['balance']
        if self.__config.get_t1()['trade']['enable']:
            self.__trade = Trade()
        if self.__config.get_t1()['trade']['enableMock']:
            self.__mockTrade = MockTrade()
        self.__engine = create_engine(self.__config.get_DBurl())

    def printPerformace(self, timestamp):
        deltaSeconds = (dt.datetime.now() - timestamp).seconds
        if deltaSeconds > 3:
            print('calc more than %s seconds' % deltaSeconds)

    def calcMain(self, zs, dh, timestamp, buyCount):
        data = dh.get_data()
        finalCode = ''
        result = []
        codes = []
        for code in data:
            if code in dh.get_buyed() or code in dh.get_ignore():
                continue
            try:
                if self.calc(zs, data[code], dh):
                    result.append(data[code])
            #  self.printPerformace(timestamp)
            except Exception as e:
                last_line = data[code].get_Lastline()
                MyLog.error(last_line['time'] + ' :calc ' + code + ' error')
                MyLog.error(str(e))
        if len(result) != 0:
            for stock in result:
                try:
                    last_line = stock.get_Lastline()
                    res = self.outputRes(last_line, timestamp, buyCount)
                    if res is not None:
                        codes.append(stock.get_code())
                except Exception as e:
                    MyLog.error('outputRes error %s' % stock.get_code())
                    MyLog.error(str(e))
        return codes

    def outputRes(self, df_final, timestamp, buyCount):
        trade = self.__config.get_t1()['trade']
        if buyCount <= 0:
            return None
        buyMoney = (float(df_final['price']) +
                    trade['addPrice']) * trade['volume']
        if buyMoney > self.__balance:
            return None
        price = str('%.2f' % (float(df_final['price']) + trade['addPrice']))
        info = '[%s] 在 %s 以 %s 买入 [%s]%s %s 股' % (
            Utils.getCurrentTime(),
            str(df_final['date']) + ' ' + str(df_final['time']), price,
            df_final['code'], df_final['name'], str(trade['volume']))
        MyLog.info(info)
        now = dt.datetime.now()
        deltaSeconds = (now - timestamp).seconds
        if deltaSeconds > trade['timestampLimit']:
            MyLog.info('[%s] 行情超时 %s秒 放弃买入' % (df_final['code'], deltaSeconds))
            return None
        if trade['enable']:
            res = str(
                self.__trade.buy(df_final['code'], trade['volume'],
                                 float(price)))
            if 'entrust_no' in res:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
            return None
        if trade['enableMock']:
            res = self.__mockTrade.mockTrade(df_final['code'], float(price),
                                             trade['volume'])
            if res == 0:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
            return None
        return df_final['code']

    def calc(self, zs, stock, dh):
        if not self.canCalc(stock, dh):
            return False
        open_p = self.getOpenPercent(stock)
        conf = self.getConfig(open_p)
        if conf is None:
            dh.add_ignore(stock.get_code())
            return False
        if not stock.is_inited():
            self.initStockData(stock, open_p, conf)
        self.updateStock(stock, conf)
        return self.isStockMatch(zs, stock, conf, dh)

    def isOpenMatch(self, row):
        if float(row['pre_close']) == 0 or float(row['open']) == 0:
            return False
        return (float(row['open']) - float(row['pre_close'])) / float(
            row['pre_close']) * 100 >= -1.0

    def canCalc(self, stock, dh):
        if stock.len() < 0:
            return False
        lastLine = stock.get_Lastline()
        if float(lastLine['open']) == 0.0:
            return False
        lastSecondLine = stock.get_LastSecondline()
        if lastSecondLine is None:
            return False
        if float(lastSecondLine['open']) == 0.0:
            return False
        return True

    def initStockData(self, stock, open_p, conf):
        r_line = self.__config.get_t1()['R_line']
        for key in r_line:
            if stock.get_r_val(key) == -10:
                val = open_p + (10 - open_p) * r_line[key]
                stock.set_r_val(key, val)
        stock.set_inited()

    def updateStock(self, stock, conf):
        self.updateBreakRtimes(stock, conf)

    def convertToFloat(self, str):
        if str == '':
            return 0
        try:
            return float(str)
        except Exception as e:
            MyLog.error('convertToFloat error: ' + str + '\n')
            return 0

    def updateBreakRtimes(self, stock, conf):
        now_p = self.getCurrentPercent(stock)
        lastSecondLine = stock.get_LastSecondline()
        last_p = self.getPercent(lastSecondLine['price'], stock)
        for key in ['R1', 'R2', 'R3', 'R4']:
            val = stock.get_r_val(key)
            if last_p > val and now_p < val:
                stock.add_rBreakTimes(key)
        minR = stock.get_minR()
        if minR is None:
            minR = conf['min_R']
        if stock.get_rBreakTimes(minR) > 0:
            for i in [1, 2, 3, 4, 5]:
                if i > int(minR[-1]) and stock.get_rBreakTimes('R' +
                                                               str(i)) == 0:
                    stock.set_minR('R' + str(i))
                    break
        else:
            stock.set_minR(minR)

    def getConfig(self, open_p):
        t1 = self.__config.get_t1()
        keys = ['A', 'B', 'C']
        for key in keys:
            if t1[key]['open_p'][0] <= open_p and open_p < t1[key]['open_p'][1]:
                return t1[key]

    def isStockMatch(self, zs, stock, conf, dh):
        if 'time' in self.__config.get_t1(
        )['strategy'] and not self.isTimeMatch(stock, conf):
            return False
        if 'xspeed' in self.__config.get_t1(
        )['strategy'] and not self.isXSpeedMatch(dh, stock):
            return False
        if 'sellWindow' in self.__config.get_t1(
        )['strategy'] and not self.isSellWindowMatch(zs, stock):
            return False
        if 'minR' in self.__config.get_t1(
        )['strategy'] and not self.isReachMinR(stock):
            return False
        return self.isLastTwoMatch(stock)

    def isZSMatch(self, zs, stock):
        if zs is None:
            return True
        code = stock.get_code()
        i = 0
        if code.startswith('3'):
            i = 5
        line = zs.iloc[i]
        pre_close = line.get('pre_close')
        price = line.get('price')
        p = (float(price) - float(pre_close)) / float(pre_close) * 100
        if p < -0.3:
            return False
        if code in self.__config.get_conceptCodes():
            return True
        return p > 0

    def isSellWindowMatch(self, zs, stock):
        data = stock.get_data()
        size = len(data)
        if size > self.__config.get_t1()['big_money']['count']:
            size = self.__config.get_t1()['big_money']['count']
        temp = data[size * -1:]
        nowAmount = self.convertToFloat(temp[-1]['amount'])
        lastAmount = self.convertToFloat(temp[0]['amount'])
        avgAmount = (nowAmount - lastAmount) / size
        if avgAmount < self.__config.get_t1()['big_money']['amount']:
            return False
        if avgAmount < self.__config.get_t1()['big_money']['amount_2']:
            if 'zs' in self.__config.get_t1(
            )['strategy'] and not self.isZSMatch(zs, stock):
                return False
        MyLog.info('[%s] average amount = %s' % (stock.get_code(), avgAmount))
        now_line = stock.get_Lastline()
        totalAmountInSell = 0.0
        for i in range(1, 6):
            temp = self.convertToFloat(
                now_line['a' + str(i) + '_p']) * self.convertToFloat(
                    now_line['a' + str(i) + '_v']) * 100
            totalAmountInSell = totalAmountInSell + temp
        ratio = totalAmountInSell / (nowAmount - lastAmount)
        MyLog.info('[%s] sell window ratio = %s' % (stock.get_code(), ratio))
        return ratio <= self.__config.get_t1()['sellWindow']['threshold']

    def isXSpeedMatch(self, dh, stock):
        now_line = stock.get_Lastline()
        ccp = self.getCurrentPercent(stock)
        ocp = self.getOpenPercent(stock)
        #   if ocp - ccp >= self.__config.get_t1()['x_speed']['lowerThanBefore']:
        #      stock.add_lowerThanBeforeTimes()
        #      if stock.get_lowerThanBeforeTimes() > self.__config.get_t1()['x_speed']['lowerThanBeforeTimes']:
        #         dh.add_ignore(stock.get_code())
        #         return False
        ct = dt.datetime.strptime(now_line['date'] + ' ' + now_line['time'],
                                  '%Y-%m-%d %H:%M:%S')
        pcpArray = self.generatePCPArray(stock)
        i = 0
        while i < len(pcpArray):
            line = pcpArray[i]
            if line['time'] == now_line['time']:
                break
            price = float(line['price'])
            pcp = (float(pcpArray[i]['price']) -
                   float(pcpArray[i]['pre_close'])) / float(
                       pcpArray[i]['pre_close']) * 100
            # if pcp - ccp >= self.__config.get_t1()['x_speed']['lowerThanBefore']:
            #    stock.add_lowerThanBeforeTimes()
            #    if stock.get_lowerThanBeforeTimes() > self.__config.get_t1()['x_speed']['lowerThanBeforeTimes']:
            #       dh.add_ignore(stock.get_code())
            #       return False
            if ccp - pcp >= (10 -
                             pcp) * self.__config.get_t1()['x_speed']['a']:
                if ccp - pcp >= (ccp - ocp) * self.__config.get_t1(
                )['x_speed']['b'] and pcp > ocp:
                    pt = dt.datetime.strptime(
                        line['date'] + ' ' + line['time'], '%Y-%m-%d %H:%M:%S')
                    c = self.__config.get_t1()['x_speed']['c'][0]
                    if ocp >= 2 and ocp < 5:
                        c = self.__config.get_t1()['x_speed']['c'][1]
                    elif ocp >= -1 and ocp < 2:
                        c = self.__config.get_t1()['x_speed']['c'][2]
                    if (ct - pt).seconds / 60 < (ccp - pcp) * c:
                        MyLog.info('[%s] match cond a, ccp = %s, pcp = %s' %
                                   (stock.get_code(), ccp, pcp))
                        MyLog.info('[%s] match cond b, ccp = %s, ocp = %s' %
                                   (stock.get_code(), ccp, ocp))
                        MyLog.info('[%s] match cond c, ct = %s, pt = %s' %
                                   (stock.get_code(), ct, pt))
                        last_second_line = stock.get_LastSecondline()
                        stock.add_buySignal()
                        if stock.get_buySignal() >= self.__config.get_t1(
                        )['trade']['maxBuySignal']:
                            return True
                        return False
            i = i + 1

    #   stock.reset_buySignal()
        return False

    def generatePCPArray(self, stock):
        pcpArray = []
        len = stock.len()
        start = 0
        step = 1
        if len > 30:
            if len > 250:
                start = -250
                len = 250
            step = math.ceil(len / 30)
        data = stock.get_data()
        for val in data[start::step]:
            pcpArray.append(val)
        return pcpArray

    def isLastTwoMatch(self, stock):
        data = stock.get_data()
        if stock.len() < 3:
            return False
        price = float(data[-1]['price'])
        price2 = float(data[-2]['price'])
        price3 = float(data[-2]['price'])
        return price - price2 >= 0 and price2 - price3 >= 0

    def isTimeMatch(self, stock, conf):
        lastLine = stock.get_Lastline()
        timeStr = lastLine['time']
        return timeStr <= conf['time']

    def getPercent(self, price, stock):
        lastLine = stock.get_Lastline()
        pre_close = lastLine['pre_close']
        open_p = (float(price) - float(pre_close)) / float(pre_close) * 100
        return open_p

    def getCurrentPercent(self, stock):
        lastLine = stock.get_Lastline()
        price = lastLine['price']
        return self.getPercent(price, stock)

    def getOpenPercent(self, stock):
        ocp = stock.get_cache('ocp')
        if ocp is not None:
            return ocp
        lastLine = stock.get_Lastline()
        open = lastLine['open']
        ocp = self.getPercent(open, stock)
        stock.set_cache('ocp', ocp)
        return ocp

    def isReachMinR(self, stock):
        now_p = self.getCurrentPercent(stock)
        minR = stock.get_minR()
        return now_p > stock.get_r_val(minR) or now_p >= 9.0
コード例 #3
0
ファイル: hitTop.py プロジェクト: mikkie/stocking
       start = 0
       codeList = []
       length = len(df_todayAll)
       while start < length:
             end = start + step
             if end >= length:
                end = length 
             df_temp = df_todayAll.iloc[start:end]
             df = ts.get_realtime_quotes(df_temp['code'].tolist())
             df = df[df.apply(analyze.isOpenMatch, axis=1)]
             for code in df['code'].tolist():
                 codeList.append(code)
             start = end
       return codeList

   pool = mp.Pool(setting.get_t1()['process_num'])
   manager = mp.Manager()

   codeLists = init(False)
   MyLog.info('calc stocks %s' % codeLists)
   codeSplitMaps = {} 
   queueMaps = {}
   interDataHolder = {
      'currentTime' : dt.datetime.now()
   }
   if setting.get_t1()['trade']['enableMock']:
      mockTrade.relogin() 
   for code in setting.get_ignore():
       if code in codeLists:
          codeLists.remove(code)  
   length = len(codeLists)
コード例 #4
0
        start = 0
        codeList = []
        length = len(df_todayAll)
        while start < length:
            end = start + step
            if end >= length:
                end = length
            df_temp = df_todayAll.iloc[start:end]
            df = ts.get_realtime_quotes(df_temp['code'].tolist())
            df = df[df.apply(analyze.isOpenMatch, axis=1)]
            for code in df['code'].tolist():
                codeList.append(code)
            start = end
        return codeList

    pool = mp.Pool(setting.get_t1()['process_num'])
    manager = mp.Manager()

    codeLists = init(False)
    MyLog.info('calc stocks %s' % codeLists)
    codeSplitMaps = {}
    queueMaps = {}
    interDataHolder = {
        'currentTime': dt.datetime.now(),
        'hygn': None,
        'netMoney': None
    }
    if setting.get_t1()['trade']['enableMock']:
        mockTrade.relogin()
    for code in setting.get_ignore():
        if code in codeLists:
コード例 #5
0
from t1.datas.DataHolder import DataHolder
from t1.analyze.Analyze import Analyze

dh = DataHolder(codes)
analyze = Analyze()

for code in codes:
    try:
       src_datas[code] = pd.read_sql_table('live_' + code, con=engine)
    except Exception as e:
           MyLog.error('read mock data error \n')
           MyLog.error(str(e) +  '\n')   

def run(i):
    df = pd.DataFrame()
    for code in src_datas:
        if i < len(src_datas[code]):
           df = df.append(src_datas[code].iloc[i])
    dh.addData(df)
    analyze.calcMain(dh)
    i = i + 1    
    global timer 
    timer = threading.Timer(setting.get_t1()['get_data_inter'], run,[i])
    timer.start()

timer = threading.Timer(setting.get_t1()['get_data_inter'], run,[0])
timer.start()

while True:
      time.sleep(1)
pass
コード例 #6
0
            dh.addSellData(df)
            analyze.calcMain(zs, dh)
            MyLog.debug('process %s, calc data time = %d' %
                        (os.getpid(), (int(round(time.time() * 1000)) - s)))
            data = queue.get(True)
    except Exception as e:
        MyLog.error('error %s' % str(e))


if __name__ == '__main__':
    MyLog.info('main process %s.' % os.getpid())
    strTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
    while strTime < '09:30:01':
        time.sleep(0.1)
        strTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
    if setting.get_t1()['trade']['enableMock']:
        mockTrade.relogin()
    pool = mp.Pool(1)
    manager = mp.Manager()
    queue = manager.Queue()
    pool.apply_async(run, (queue, ))
    sched = BlockingScheduler()
    interDataHolder = {'currentTime': dt.datetime.now()}

    @sched.scheduled_job('interval',
                         seconds=setting.get_t1()['get_data_inter'])
    def getData():
        if setting.get_t1()['trade']['enableMock']:
            now = dt.datetime.now()
            if (now - interDataHolder['currentTime']).seconds > 60:
                interDataHolder['currentTime'] = now
コード例 #7
0
ファイル: DataHolder.py プロジェクト: mikkie/stocking
class DataHolder(object):
    def __init__(self, codes):
        self.__data = {}
        self.__buyed = []
        self.__selled = []
        self.__setting = Config()
        self.__tpe = ThreadPoolExecutor(5)
        self.__engine = create_engine(self.__setting.get_DBurl())
        if self.__setting.get_t1(
        )['need_recover_data'] and self.needRecoverData():
            self.recoverData(codes)

    def recoverData(self, codes):
        for code in codes:
            try:
                src_data = pd.read_sql_table('live_' + code, con=self.__engine)
                if src_data is not None and len(src_data) > 0:
                    last = src_data.iloc[-1]
                    now_date = time.strftime('%Y-%m-%d',
                                             time.localtime(time.time()))
                    if now_date == last['date']:
                        self.__data[code] = Stock(code, src_data)
            except Exception as e:
                MyLog.error('recover data error \n')
                MyLog.error(str(e) + '\n')

    def needRecoverData(self):
        now = dt.datetime.now()
        return now > dt.datetime(now.year, now.month, now.day, 9, 15)

    def get_buyed(self):
        return self.__buyed

    def get_selled(self):
        return self.__selled

    def add_selled(self, code, save=False):
        self.__selled.append(code)
        if save:
            self.__tpe.submit(self.saveData, self.__data[code].get_data())

    def add_buyed(self, code, save=False):
        self.__buyed.append(code)
        if save:
            self.__tpe.submit(self.saveData, self.__data[code].get_data())

    def get_data(self):
        return self.__data

    def addDataHandler(self, row):
        code = row['code']
        if code in self.get_buyed():
            return
        if code in self.__data and self.__data[code].len() > 0:
            self.__data[code].add_Line(row)
        else:
            self.__data[code] = Stock(code, row)
        if float(row['pre_close']) != 0 and (float(row['price']) - float(
                row['pre_close'])) / float(row['pre_close']) * 100 >= 9.9:
            if not (code in self.get_buyed()):
                MyLog.info('[%s] reach 10' % code)
                self.add_buyed(code, True)

    def addSellDataHandler(self, row):
        code = row['code']
        if code in self.get_selled():
            return
        if code in self.__data and self.__data[code].len() > 0:
            self.__data[code].add_Line(row)
        else:
            self.__data[code] = Stock(code, row)

    def addData(self, df):
        df.apply(self.addDataHandler, axis=1)

    def addSellData(self, df):
        df.apply(self.addSellDataHandler, axis=1)

    def saveData(self, data):
        try:
            line = data.iloc[0]
            code = line['code']
            data.to_sql('live_' + code,
                        con=self.__engine,
                        if_exists='replace',
                        index=False)
            MyLog.info('[%s] save data' % code)
        except Exception as e:
            MyLog.error('[%s %s] save [%s] data error \n' %
                        (line['date'], line['time'], code))
            MyLog.error(str(e) + '\n')
コード例 #8
0
ファイル: hitTop2.py プロジェクト: mikkie/stocking
def delete_log():
    path = os.path.join(os.path.dirname(__file__), '../log') 
    for file in os.listdir(path):
        try:
            os.remove(path + '/' + file)
        except Exception as e:
               pass           



if __name__ == '__main__':
   delete_log() 
   MyLog.info('main process %s.' % os.getpid()) 
   engine = create_engine(setting.get_DBurl()) 
   mockTrade = MockTrade()
   if setting.get_t1()['trade']['enable']:
      trade = Trade()

   def init(forceUpdate):
       def cb(**kw):
           df = ts.get_today_all()
           df['pick'] = 0
           return df
       df_todayAll = Utils.queryData('today_all','code',engine, cb, forceUpdate=forceUpdate, sql='select * from today_all where pick = 1', load_if_empty=False)
       strTime = time.strftime('%H:%M:%S',time.localtime(time.time()))
       while strTime < '09:30:01':
             time.sleep(0.1)
             strTime = time.strftime('%H:%M:%S',time.localtime(time.time()))
       step = 880
       start = 0
       codeList = []
コード例 #9
0
ファイル: HitBottomAnalyze.py プロジェクト: mikkie/stocking
class HitBottomAnalyze(object):
    
      def __init__(self):
          self.__config = Config()
          self.__buyedCount = 0
          self.__balance = self.__config.get_t1()['trade']['balance']
          if self.__config.get_t1()['trade']['enable']:
             self.__trade = Trade()
          if self.__config.get_t1()['trade']['enableMock']:
             self.__mockTrade = MockTrade()    
          self.__engine = create_engine(self.__config.get_DBurl())



      def printPerformace(self,timestamp):
          deltaSeconds = (dt.datetime.now() - timestamp).seconds 
          if deltaSeconds > 3: 
             print('calc more than %s seconds' % deltaSeconds)    

      def calcMain(self,dh,timestamp,buyCount):
          data = dh.get_data()
          finalCode = ''
          result = []
          codes = []
          for code in data:
              if code in dh.get_buyed() or code in dh.get_ignore():
                 continue 
              try:  
                 if self.calc(data[code],dh):
                    result.append(data[code])
                #  self.printPerformace(timestamp)  
              except Exception as e:
                     last_line = data[code].get_Lastline()
                     MyLog.error(last_line['time'] + ' :calc ' + code + ' error')
                     MyLog.error(str(e))      
          if len(result) != 0:
             for stock in result: 
                 try:
                     last_line = stock.get_Lastline()
                     res = self.outputRes(last_line,timestamp,buyCount)
                     if res is not None:
                        codes.append(stock.get_code())
                 except Exception as e:
                        MyLog.error('outputRes error %s' % stock.get_code())
                        MyLog.error(str(e))   
          return codes   


      def outputRes(self,df_final,timestamp,buyCount):
          trade = self.__config.get_t1()['trade']
          if buyCount <= 0:
             return None
          buyMoney = (float(df_final['price']) + trade['addPrice']) * trade['volume']  
          if buyMoney > self.__balance:
             return None   
          price = str('%.2f' % (float(df_final['price']) + trade['addPrice']))
          info = '[%s] 在 %s 以 %s 买入 [%s]%s %s 股' % (Utils.getCurrentTime(),str(df_final['date']) + ' ' + str(df_final['time']), price, df_final['code'], df_final['name'], str(trade['volume']))
          MyLog.info(info)
          now = dt.datetime.now()
          deltaSeconds = (now - timestamp).seconds
          if deltaSeconds > trade['timestampLimit']:
             MyLog.info('[%s] 行情超时 %s秒 放弃买入' % (df_final['code'],deltaSeconds)) 
             return None
          if trade['enable']:
             res = str(self.__trade.buy(df_final['code'],trade['volume'],float(price)))
             if 'entrust_no' in res:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
             return None  
          if trade['enableMock']:
             res = self.__mockTrade.mockTrade(df_final['code'],float(price),trade['volume'])
             if res == 0:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
             return None  
          return df_final['code']  


              
      def calc(self,stock,dh):
          ccp = self.getCurrentPercent(stock)
          if ccp >= self.__config.get_t1()['hitBottom']['max_p'] or ccp <= self.__config.get_t1()['hitBottom']['min_p']:
             return False
          lastLine = stock.get_Lastline()
          price = float(lastLine['price'])
          openPrice = float(lastLine['open'])
          high = float(lastLine['high'])
          low = float(lastLine['low'])
          if price >= openPrice or price <= low:
             return False
          return price - low <= (high - low) * self.__config.get_t1()['hitBottom']['margin']
                      

      def convertToFloat(self,str):
          if str == '':
             return 0 
          try:
              return float(str)
          except Exception as e:
                 MyLog.error('convertToFloat error: ' + str + '\n') 
                 return 0   


      def getPercent(self,price,stock):
          lastLine = stock.get_Lastline()
          pre_close = lastLine['pre_close']
          open_p = (float(price) - float(pre_close)) / float(pre_close) * 100
          return open_p
      
      def getCurrentPercent(self,stock):
          lastLine = stock.get_Lastline()
          price = lastLine['price']
          return self.getPercent(price,stock)
              

      def getOpenPercent(self,stock):
          ocp = stock.get_cache('ocp')
          if ocp is not None:
             return ocp 
          lastLine = stock.get_Lastline()
          open = lastLine['open']
          ocp = self.getPercent(open,stock)
          stock.set_cache('ocp',ocp)
          return ocp  
コード例 #10
0
class Analyze(object):
    def __init__(self, thshy, thsgn):
        self.__config = Config()
        self.__buyedCount = 0
        self.__balance = self.__config.get_t1()['trade']['balance']
        if self.__config.get_t1()['trade']['enable']:
            self.__trade = Trade()
        if self.__config.get_t1()['trade']['enableMock']:
            self.__mockTrade = MockTrade()
        self.__engine = create_engine(self.__config.get_DBurl())
        self.__hygnData = self.initHYGN(thshy, thsgn)

    def initHYGN(self, thshy, thsgn):
        if thshy is None or thsgn is None:
            return
        data = {}
        for index, row in thshy.iterrows():
            if row['code'] not in data:
                data[row['code']] = {'hy': [], 'gn': []}
            if row['hy'] not in data[row['code']]['hy']:
                data[row['code']]['hy'].append(row['hy'])
        for index, row in thsgn.iterrows():
            if row['code'] not in data:
                data[row['code']] = {'hy': [], 'gn': []}
            if row['cont'] not in data[row['code']]['gn']:
                data[row['code']]['gn'].append(row['cont'])
        return data

    def calcMain(self, zs, dh, hygn, netMoney, timestamp):
        data = dh.get_data()
        finalCode = ''
        result = []
        codes = []
        for code in data:
            if len(dh.get_buyed()) > 0:
                if code in dh.get_buyed():
                    continue
            try:
                if self.calc(zs, data[code], dh, hygn, netMoney):
                    result.append(data[code])
            except Exception as e:
                last_line = data[code].get_Lastline()
                MyLog.error(last_line['time'] + ' :calc ' + code + ' error')
                MyLog.error(str(e))
        if len(result) != 0:
            for stock in result:
                try:
                    last_line = stock.get_Lastline()
                    res = self.outputRes(last_line, timestamp)
                    if res is not None:
                        codes.append(stock.get_code())
                        self.saveData(stock)
                except Exception as e:
                    MyLog.error('outputRes error %s' % stock.get_code())
                    MyLog.error(str(e))
        return codes

    def save(self, data):
        try:
            code = data.iloc[0]['code']
            data.to_sql('live_' + code,
                        con=self.__engine,
                        if_exists='replace',
                        index=False)
        except Exception as e:
            MyLog.error('save [%s] data error \n' % code)
            MyLog.error(str(e) + '\n')

    def saveData(self, stock):
        data = stock.get_data()
        t = threading.Thread(target=self.save, args=(data, ))
        t.start()

    def outputRes(self, df_final, timestamp):
        trade = self.__config.get_t1()['trade']
        if self.__buyedCount >= trade['max_buyed']:
            return None
        buyMoney = (float(df_final['price']) +
                    trade['addPrice']) * trade['volume']
        if buyMoney > self.__balance:
            return None
        price = str('%.2f' % (float(df_final['price']) + trade['addPrice']))
        info = '[%s] 在 %s 以 %s 买入 [%s]%s %s 股' % (
            Utils.getCurrentTime(),
            str(df_final['date']) + ' ' + str(df_final['time']), price,
            df_final['code'], df_final['name'], str(trade['volume']))
        MyLog.info(info)
        now = dt.datetime.now()
        deltaSeconds = (now - timestamp).seconds
        if deltaSeconds > trade['timestampLimit']:
            MyLog.info('[%s] 行情超时 %s秒 放弃买入' % (df_final['code'], deltaSeconds))
            return None
        if trade['enable']:
            res = str(
                self.__trade.buy(df_final['code'], trade['volume'],
                                 float(price)))
            if 'entrust_no' in res:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
            return None
        if trade['enableMock']:
            res = self.__mockTrade.mockTrade(df_final['code'], float(price),
                                             trade['volume'])
            if res == 0:
                self.__buyedCount = self.__buyedCount + 1
                self.__balance = self.__balance - buyMoney
                return df_final['code']
            return None
        return df_final['code']

    def goTopsis(self, result):
        df = pd.DataFrame()
        for stock in result:
            df = self.buildData(df, stock)
        return self.topsisCalc(df)

    def topsisCalc(self, df):
        df = df.fillna(df.mean())
        self.commonCalc(df, 'net',
                        self.__config.get_t1()['topsis']['net'], True)
        self.commonCalc(df, 'v120',
                        self.__config.get_t1()['topsis']['v120'], True)
        self.commonCalc(df, 'v300',
                        self.__config.get_t1()['topsis']['v300'], True)
        self.commonCalc(df, 'v30',
                        self.__config.get_t1()['topsis']['v30'], True)
        self.commonCalc(df, 'r_break',
                        self.__config.get_t1()['topsis']['r_break'], False)
        for index, row in df.iterrows():
            self.calcDi(df, index, row,
                        ['net', 'v120', 'v300', 'v30', 'r_break'], '_best')
            self.calcDi(df, index, row,
                        ['net', 'v120', 'v300', 'v30', 'r_break'], '_worst')
            self.calcCi(df, index, row)
        df = df.sort_values('ci', ascending=False)
        return df

    def calcCi(self, df, index, row):
        if (row['Di_best'] + row['Di_worst']) == 0:
            row['ci'] = 0
            df.loc[index, 'ci'] = 0
        else:
            row['ci'] = row['Di_worst'] / (row['Di_best'] + row['Di_worst'])
            df.loc[index, 'ci'] = row['ci']

    def calcDi(self, df, index, row, columnList, best_worst):
        temp = 0
        for column in columnList:
            temp = temp + np.square(row[column + '_vi'] -
                                    row[column + best_worst])
        row['Di' + best_worst] = cmath.sqrt(temp)
        df.loc[index, 'Di' + best_worst] = row['Di' + best_worst]

    def commonCalc(self, df, columnName, weight, positive):
        temp = cmath.sqrt((df[columnName] * df[columnName]).sum())
        #wi*ri
        if temp == 0:
            df[columnName + '_vi'] = weight * 0
        else:
            df[columnName + '_vi'] = weight * df[columnName] / temp
        #vi+, vi-
        if positive == True:
            df[columnName + '_best'] = df[columnName + '_vi'].max()
            df[columnName + '_worst'] = df[columnName + '_vi'].min()
        else:
            df[columnName + '_best'] = df[columnName + '_vi'].min()
            df[columnName + '_worst'] = df[columnName + '_vi'].max()

    def buildData(self, df, stock):
        data = stock.get_data()
        last_line = stock.get_Lastline()
        data.loc[data['time'] == last_line['time'], 'net'] = stock.get_net()
        data.loc[data['time'] == last_line['time'],
                 'v300'] = stock.get_speed('v300')
        data.loc[data['time'] == last_line['time'],
                 'v120'] = stock.get_speed('v120')
        data.loc[data['time'] == last_line['time'],
                 'v30'] = stock.get_speed('v30')
        minR = stock.get_minR()
        times = 0
        for i in [1, 2, 3, 4]:
            if 'R' + str(i) != minR:
                times = times + stock.get_rBreakTimes('R' + str(i))
            else:
                break
        data.loc[data['time'] == last_line['time'], 'r_break'] = times
        last_line = stock.get_Lastline()
        return df.append([last_line])

    def calc(self, zs, stock, dh, hygn, netMoney):
        if not self.canCalc(stock, dh):
            return False
        open_p = self.getOpenPercent(stock)
        conf = self.getConfig(open_p)
        if conf is None:
            dh.add_buyed(stock.get_code(), False)
            return False
        self.initStockData(stock, open_p, conf)
        self.updateStock(stock, conf)
        return self.isStockMatch(zs, stock, conf, hygn, netMoney, dh)

#   def isJHJJMatch(self,stock,dh):
#       lastLine = stock.get_Lastline()
#       now_time = lastLine['time']
#       if now_time >= '09:30:00':
#          return True
#       if stock.len() >= 2:
#          lastSecondLine = stock.get_LastSecondline()
#          if now_time > '09:17:00' and now_time < '09:20:00':
#             b1_v = self.convertToFloat(lastLine.get('b1_v'))
#             b1_amount = float(lastLine.get('b1_p')) * b1_v * 100
#             lastB1_v = self.convertToFloat(lastSecondLine.get('b1_v'))
#             lastB1_amount = float(lastSecondLine.get('b1_p')) * lastB1_v * 100
#             if b1_v < lastB1_v * 0.5 or b1_amount < lastB1_amount * 0.5:
#                dh.add_buyed(stock.get_code(),True)
#                return False
#          if now_time > '09:24:30' and now_time < '09:25:03':
#             b1_v = self.convertToFloat(lastLine.get('b1_v'))
#             b1_amount = float(lastLine.get('b1_p')) *  b1_v * 100
#             if b1_v <= 1000 or b1_amount <= 3000000:
#                dh.add_buyed(stock.get_code(),True)
#                return False
#          return True
#       return False

    def isOpenMatch(self, row):
        if float(row['pre_close']) == 0 or float(row['open']) == 0:
            return False
        return (float(row['open']) - float(row['pre_close'])) / float(
            row['pre_close']) * 100 >= -1.0

    def isOpenMatch2(self, row):
        if float(row['settlement']) == 0 or float(row['open']) == 0:
            return False
        return (float(row['open']) - float(row['settlement'])) / float(
            row['settlement']) * 100 >= -1.0

    def canCalc(self, stock, dh):
        if stock.len() < 0:
            return False

    #   if not self.isJHJJMatch(stock,dh):
    #      return False
        lastLine = stock.get_Lastline()
        if float(lastLine.get('open')) == 0.0:
            return False
        lastSecondLine = stock.get_LastSecondline()
        if lastSecondLine is None:
            return False
        if float(lastSecondLine.get('open')) == 0.0:
            return False
        return True

    def initStockData(self, stock, open_p, conf):
        r_line = self.__config.get_t1()['R_line']
        for key in r_line:
            if stock.get_r_val(key) == -10:
                val = open_p + (10 - open_p) * r_line[key]
                stock.set_r_val(key, val)

    def updateStock(self, stock, conf):
        self.updateBreakRtimes(stock, conf)

    #   self.updateSpeed(stock)
    #   self.updatePriceVolumeMap(stock)
    #   try:
    #      self.updateBigMoney(stock,conf)
    #   except Exception as e:
    #          MyLog.error('updateBigMoney error \n')
    #          MyLog.error(str(e) +  '\n')

    def updatePriceVolumeMap(self, stock):
        try:
            last_line = stock.get_Lastline()
            time = last_line['time']
            if time > self.__config.get_t1(
            )['stop']['am_stop'] and time < self.__config.get_t1(
            )['stop']['pm_start']:
                return
            stock.addPriceVolumeMap(last_line['date'], last_line['time'],
                                    float(last_line['price']),
                                    float(last_line['volume']))
        except Exception as e:
            MyLog.error('updatePriceVolumeMap error \n')
            MyLog.error(str(e) + '\n')

    def updateSpeed(self, stock):
        data = stock.get_data()
        last_line = stock.get_Lastline()
        now_time = dt.datetime.strptime(
            last_line['date'] + ' ' + last_line['time'], '%Y-%m-%d %H:%M:%S')
        l = stock.len()
        for i in [30, 120, 300]:
            pos = l
            if int(i / 3) + 1 < l:
                pos = int(i / 3) + 1
            df_temp = data.tail(pos)
            for index, row in df_temp.iterrows():
                if float(row['open']) != 0.0:
                    row_time = dt.datetime.strptime(
                        row['date'] + ' ' + row['time'], '%Y-%m-%d %H:%M:%S')
                    deltaS = (now_time - row_time).seconds
                    if deltaS == 0:
                        stock.set_speed('v' + str(i), 0)
                        break
                    if deltaS <= i and deltaS >= i - 6:
                        p = (float(last_line.get('price')) - float(
                            row['price'])) / float(row['pre_close']) * 100
                        stock.set_speed('v' + str(i), p / deltaS)
                        # MyLog.info('speed %s = %f' % ('v' + str(i),p / deltaS))
                        break

    def convertToFloat(self, str):
        if str == '':
            return 0
        try:
            return float(str)
        except Exception as e:
            MyLog.error('convertToFloat error: ' + str + '\n')
            return 0

    def theLastIsSellOrBuy(self, stock):
        lastLine = stock.get_Lastline()
        lastSeconfLine = stock.get_LastSecondline()
        last_amount = float(lastLine.get('amount'))
        last_sec_amount = float(lastSeconfLine.get('amount'))
        if last_sec_amount == last_amount:
            return 'no_deal'
        last_price = float(lastLine.get('price'))
        last_b_p_1 = float(lastLine.get('b1_p'))
        last_b_v_1 = self.convertToFloat(lastLine.get('b1_v'))
        last_a_p_1 = float(lastLine.get('a1_p'))
        last_a_v_1 = self.convertToFloat(lastLine.get('a1_v'))
        last_sec_price = float(lastSeconfLine.get('price'))
        last_sec_b_p_1 = float(lastSeconfLine.get('b1_p'))
        last_sec_b_v_1 = self.convertToFloat(lastSeconfLine.get('b1_v'))
        last_sec_a_p_1 = float(lastSeconfLine.get('a1_p'))
        last_sec_a_v_1 = self.convertToFloat(lastSeconfLine.get('a1_v'))
        if (last_a_p_1 > last_sec_a_p_1 or
            (last_a_p_1 == last_sec_a_p_1 and
             last_a_v_1 < last_sec_a_v_1)) and (last_price >= last_sec_price):
            return 'drive_buy'
        elif (last_b_p_1 < last_sec_b_p_1 or
              (last_b_p_1 == last_sec_b_p_1 and last_b_v_1 < last_sec_b_v_1)
              ) and (last_price <= last_sec_price):
            return 'drive_sell'
        return 'unknown'

    def updateBigMoney(self, stock, conf):
        lastLine = stock.get_Lastline()
        now_time = dt.datetime.strptime(
            lastLine['date'] + ' ' + lastLine['time'], '%Y-%m-%d %H:%M:%S')
        lastSeconfLine = stock.get_LastSecondline()
        last_time = dt.datetime.strptime(
            lastSeconfLine['date'] + ' ' + lastSeconfLine['time'],
            '%Y-%m-%d %H:%M:%S')
        last_amount = float(lastLine.get('amount'))
        last_sec_amount = float(lastSeconfLine.get('amount'))
        last_volume = float(lastLine.get('volume'))
        last_sec_volume = float(lastSeconfLine.get('volume'))
        amount = last_amount - last_sec_amount
        volume = last_volume - last_sec_volume
        big_amount = self.__config.get_t1()['big_money']['amount']
        big_volume = self.__config.get_t1()['big_money']['volume']
        #   MyLog.info('price=%s,amount=%s' % (lastLine['price'],amount))
        #   if amount >= big_amount or volume >= big_volume:
        type = self.theLastIsSellOrBuy(stock)
        if type == 'drive_buy':
            stock.addBigMoneyIn(last_amount - last_sec_amount)
            #  MyLog.info('in = %f' % stock.getBigMoneyIn())
            stock.addNetBuy(last_amount - last_sec_amount)
        #  MyLog.info('net = %f' % stock.get_net())
        elif type == 'drive_sell':
            stock.addBigMoneyOut(last_amount - last_sec_amount)
            stock.addNetBuy(last_sec_amount - last_amount)
        #    MyLog.info('net = %f' % stock.get_net())

    def updateBreakRtimes(self, stock, conf):
        now_p = self.getCurrentPercent(stock)
        lastSecondLine = stock.get_LastSecondline()
        last_p = self.getPercent(lastSecondLine.get('price'), stock)
        for key in ['R1', 'R2', 'R3', 'R4']:
            val = stock.get_r_val(key)
            if last_p > val and now_p < val:
                stock.add_rBreakTimes(key)
        minR = stock.get_minR()
        if minR is None:
            minR = conf['min_R']
        if stock.get_rBreakTimes(minR) > 0:
            for i in [1, 2, 3, 4, 5]:
                if i > int(minR[-1]) and stock.get_rBreakTimes('R' +
                                                               str(i)) == 0:
                    stock.set_minR('R' + str(i))
                    break
        else:
            stock.set_minR(minR)

    def getConfig(self, open_p):
        t1 = self.__config.get_t1()
        keys = ['A', 'B', 'C']
        for key in keys:
            if t1[key]['open_p'][0] <= open_p and open_p < t1[key]['open_p'][1]:
                return t1[key]

    def isStockMatch(self, zs, stock, conf, hygn, netMoney, dh):
        #   if 'zs' in self.__config.get_t1()['strategy'] and not self.isZSMatch(zs,stock):
        #      return False
        if 'time' in self.__config.get_t1(
        )['strategy'] and not self.isTimeMatch(stock, conf):
            return False

    #   if 'hygn' in self.__config.get_t1()['strategy'] and not self.isHygnMatch(stock,hygn):
    #      return False
    #   if 'net' in self.__config.get_t1()['strategy'] and not self.isNetMatch(stock,conf):
    #      return False
    #   if 'speed' in self.__config.get_t1()['strategy'] and stock.get_minR() != 'R5':
    #      if not self.isSpeedMatch(stock,conf):
    #         return False
    #   if 'pvmap' in self.__config.get_t1()['strategy'] and not self.isPriceVolumeMapMatch(stock):
    #      return False
    #   if 'netRatio' in self.__config.get_t1()['strategy'] and not self.netMoneyRatioMatch(stock,netMoney):
    #      return False
        if 'xspeed' in self.__config.get_t1(
        )['strategy'] and not self.isXSpeedMatch(dh, stock):
            return False
        if 'minR' in self.__config.get_t1(
        )['strategy'] and not self.isReachMinR(stock):
            return False
        return self.isLastTwoMatch(stock)

    def isZSMatch(self, zs, stock):
        code = stock.get_code()
        i = 0
        if code.startswith('3'):
            i = 5
        line = zs.iloc[i]
        pre_close = line.get('pre_close')
        price = line.get('price')
        p = (float(price) - float(pre_close)) / float(pre_close) * 100
        return p > 0

    def isXSpeedMatch(self, dh, stock):
        now_line = stock.get_Lastline()
        ccp = self.getCurrentPercent(stock)
        ocp = self.getOpenPercent(stock)
        if ocp - ccp >= self.__config.get_t1()['x_speed']['lowerThanBefore']:
            stock.add_lowerThanBeforeTimes()
            if stock.get_lowerThanBeforeTimes() > self.__config.get_t1(
            )['x_speed']['lowerThanBeforeTimes']:
                dh.add_buyed(stock.get_code(), False)
                return False
        ct = dt.datetime.strptime(now_line['date'] + ' ' + now_line['time'],
                                  '%Y-%m-%d %H:%M:%S')
        len = stock.len()
        i = len - 2
        if i > self.__config.get_t1()['x_speed']['maxStickNum']:
            i = self.__config.get_t1()['x_speed']['maxStickNum']
        while i >= 0:
            line = stock.get_data().iloc[i]
            price = float(line['price'])
            pcp = self.getPercent(price, stock)
            if pcp - ccp >= self.__config.get_t1(
            )['x_speed']['lowerThanBefore']:
                stock.add_lowerThanBeforeTimes()
                if stock.get_lowerThanBeforeTimes() > self.__config.get_t1(
                )['x_speed']['lowerThanBeforeTimes']:
                    dh.add_buyed(stock.get_code(), False)
                    return False
            if ccp - pcp >= (10 -
                             pcp) * self.__config.get_t1()['x_speed']['a']:
                if ccp - pcp >= (ccp - ocp) * self.__config.get_t1(
                )['x_speed']['b'] and pcp > ocp:
                    pt = dt.datetime.strptime(
                        line['date'] + ' ' + line['time'], '%Y-%m-%d %H:%M:%S')
                    if (ct - pt).seconds / 60 <= (
                            ccp -
                            pcp) * self.__config.get_t1()['x_speed']['c']:
                        #   MyLog.info('[%s] match cond a, ccp = %s, pcp = %s' % (stock.get_code(),ccp,pcp))
                        #   MyLog.info('[%s] match cond b, ccp = %s, ocp = %s' % (stock.get_code(),ccp,ocp))
                        #   MyLog.info('[%s] match cond c, ct = %s, pt = %s' % (stock.get_code(),ct,pt))
                        last_second_line = stock.get_LastSecondline()
                        deltaVolume = float(now_line['volume']) - float(
                            last_second_line['volume'])
                        deltaAmount = float(now_line['amount']) - float(
                            last_second_line['amount'])
                        stock.add_buySignal()
                        if stock.get_buySignal() >= self.__config.get_t1(
                        )['trade']['maxBuySignal'] or deltaVolume >= self.__config.get_t1(
                        )['x_speed'][
                                'minimumVolume'] or deltaAmount >= self.__config.get_t1(
                                )['x_speed']['minimumAmount']:
                            return True
                        return False
            i = i - 1
        stock.reset_buySignal()
        #   perform the calc time test
        #   now = dt.datetime.now()
        #   if stock.get_time() is not None:
        #      deltaSeconds = (now - stock.get_time()).seconds
        #      if deltaSeconds > 3:
        #         print('[%s] calc more than %s s' % (stock.get_code(),deltaSeconds))
        #   stock.set_time(now)
        return False

    def netMoneyRatioMatch(self, stock, netMoney):
        if netMoney is None:
            return False
        return stock.get_code() in netMoney

    def isHygnMatch(self, stock, hygn):
        if hygn is None:
            return False
        code = stock.get_code()
        if code not in self.__hygnData:
            return False
        stockHYGN = self.__hygnData[code]
        if 'hy' in stockHYGN:
            shys = stockHYGN['hy']
            for shy in shys:
                for hy in hygn['hy']:
                    if shy == hy:
                        return True
        if 'gn' in stockHYGN:
            sgns = stockHYGN['gn']
            for sgn in sgns:
                for gn in hygn['gn']:
                    if sgn == gn:
                        return True
        return False

    def isPriceVolumeMapMatch(self, stock):
        pvMap = stock.getPriceVolumeMap()
        if len(pvMap) < 2:
            return True
        lastPrice = 0
        maxBuyVolume = 0
        for pv in pvMap:
            if lastPrice != 0:
                if pv['price'] < lastPrice and maxBuyVolume != 0 and pv[
                        'volume'] > maxBuyVolume * self.__config.get_t1(
                        )['pvRatio']:
                    return False
                if pv['price'] > lastPrice and pv['volume'] > maxBuyVolume:
                    maxBuyVolume = pv['volume']
            lastPrice = pv['price']
        return True

    def isLastTwoMatch(self, stock):
        data = stock.get_data()
        if stock.len() < 3:
            return False
        price = float(data.iloc[-1].get('price'))
        price2 = float(data.iloc[-2].get('price'))
        price3 = float(data.iloc[-2].get('price'))
        return price - price2 > 0 and price2 - price3 >= 0

    def isSpeedMatch(self, stock, conf):
        v30 = stock.get_speed('v30')
        v120 = stock.get_speed('v120')
        v300 = stock.get_speed('v300')
        p = self.getCurrentPercent(stock)
        v_list = [v30, v120, v300]
        flag = False
        if v_list[0] * 30 >= (
                10 - p) * self.__config.get_t1()['speed']['v30_ratio']:
            flag = True
        elif v_list[0] >= self.__config.get_t1()['speed']['v30'] and (
                v_list[1] >= self.__config.get_t1()['speed']['v120']
                or v_list[2] >= self.__config.get_t1()['speed']['v300']):
            flag = True
        if flag == True:
            last_line = stock.get_Lastline()
            info = '[%s] *** [%s] match speed at %s %s,v30=%s,v120=%s,v300=%s ***' % (
                Utils.getCurrentTime(), stock.get_code(), last_line['date'],
                last_line['time'], str(v30), str(v120), str(v300))
            MyLog.info(info)
        return flag

    def isBigMoneyMatch(self, stock, conf):
        p = stock.get_net() / stock.getBigMoneyIn()
        flag = p > self.__config.get_t1()['big_money']['threshold']
        if flag == True:
            last_line = stock.get_Lastline()
            info = '[%s] *** [%s] match big_money at %s %s,net=%s,in=%s,p=%s ***' % (
                Utils.getCurrentTime(), stock.get_code(), last_line['date'],
                last_line['time'], str(
                    stock.get_net()), str(stock.getBigMoneyIn()), str(p * 100))
            MyLog.info(info)
        return flag

    def isNetMatch(self, stock, conf):
        last_line = stock.get_Lastline()
        return stock.get_net() >= conf['big_money']['net'] * float(
            last_line['price'])

    def isTimeMatch(self, stock, conf):
        lastLine = stock.get_Lastline()
        timeStr = lastLine['time']
        return timeStr <= conf['time']

    def getPercent(self, price, stock):
        lastLine = stock.get_Lastline()
        pre_close = lastLine.get('pre_close')
        open_p = (float(price) - float(pre_close)) / float(pre_close) * 100
        return open_p

    def getCurrentPercent(self, stock):
        lastLine = stock.get_Lastline()
        price = lastLine.get('price')
        return self.getPercent(price, stock)

    def getOpenPercent(self, stock):
        ocp = stock.get_cache('ocp')
        if ocp is not None:
            return ocp
        lastLine = stock.get_Lastline()
        open = lastLine.get('open')
        ocp = self.getPercent(open, stock)
        stock.set_cache('ocp', ocp)
        return ocp

    def isReachMinR(self, stock):
        now_p = self.getCurrentPercent(stock)
        minR = stock.get_minR()
        return now_p > stock.get_r_val(minR)