Beispiel #1
0
    def _debug_sim(self):
        self.running_mode = 'sim'

        #QA_SU_save_huobi_min(frequency= self.frequence, fetch_range= self.code)

        frequence_int = int(re.findall("\d+", self.frequence)[0])
        self.frequenceInt = frequence_int
        start = (datetime.datetime.now() + datetime.timedelta(
            minutes=-500 * frequence_int)).strftime("%Y-%m-%d %H:%M:%S")
        end = (datetime.datetime.now() + datetime.timedelta(
            minutes=-frequence_int)).strftime("%Y-%m-%d %H:%M:%S")
        code_list = [huobi_SYMBOL.format(x) for x in self.code]
        self._old_data = QA.QA_fetch_cryptocurrency_min(
            code_list, start, end, format='pd',
            frequence=self.frequence).set_index(['datetime', 'code'])

        #print(self._old_data)
        self._old_data = self._old_data.loc[:, [
            'open', 'high', 'low', 'close', 'volume'
        ]]

        self.database = pymongo.MongoClient(mongo_ip).QAREALTIME

        self.client = self.database.account
        self.subscriber_client = self.database.subscribe
        self.acc = QIFI_Account(username=self.strategy_id,
                                password=self.strategy_id,
                                trade_host=mongo_ip)
        self.acc.initial()
        self.pub = publisher_routing(exchange='QAORDER_ROUTER',
                                     host=self.trade_host,
                                     port=self.trade_port,
                                     user=self.trade_user,
                                     password=self.trade_password)
        '''
        self.subscribe_data(self.code, self.frequence, self.data_host,
                            self.data_port, self.data_user, self.data_password)
        '''

        self.database.strategy_schedule.job_control.update(
            {'strategy_id': self.strategy_id}, {
                'strategy_id': self.strategy_id,
                'taskid': self.taskid,
                'filepath': os.path.abspath(__file__),
                'status': 200
            },
            upsert=True)

        # threading.Thread(target=, daemon=True).start()
        #print(self.subscribe_data)
        #self.sub.start()
        #self.subscriber.start()
        self.callback(1, 2, 3, 4)
Beispiel #2
0
    def callback(self, a, b, c, body):
        """在strategy的callback中,我们需要的是

        1. 更新数据
        2. 更新bar
        3. 更新策略状态
        4. 推送事件

        Arguments:
            a {[type]} -- [description]
            b {[type]} -- [description]
            c {[type]} -- [description]
            body {[type]} -- [description]
        """
        print(time.strftime("%H:%M:%S", time.localtime()))
        while True:
            time_now_min = int(time.strftime("%M", time.localtime()))
            time_now_s = int(time.strftime("%S", time.localtime()))
            frequence_int = self.frequenceInt
            if (time_now_min%frequence_int == 0 and (time_now_s%30 == 0 or time_now_s%59 == 0)) :
                time.sleep(1) # 延迟1s 防止上边的if重复执行
                QA.QA_util_log_info('{} while开始....'.format(
                    str(datetime.datetime.now())))

                code_list = [huobi_SYMBOL.format(x) for x in self.code]
               
                # new_data = json.loads(str(body, encoding='utf-8'))
                lastPreMinTime = (datetime.datetime.now()).strftime(
                    "%Y-%m-%d %H:%M:%S")

                preLastPreMinTime = (datetime.datetime.now() + datetime.timedelta(minutes=-4 * frequence_int)).strftime(
                    "%Y-%m-%d %H:%M:%S")

                print(preLastPreMinTime, lastPreMinTime, self.frequence)
                self._new_data = QA.QA_fetch_cryptocurrency_min(code_list, preLastPreMinTime, lastPreMinTime, format='pd',
                                                              frequence=self.frequence)

                if (self._new_data.empty):
                    continue

                self._new_data = self._new_data.loc[:, ['code','open', 'high', 'low', 'close', 'volume','datetime']]
                #self._market_data.iloc[-1:]

                print('NEW DATA:', self._new_data)


                #整理成一个code的一个frequence为一个bar
                for idx, row in  self._new_data.iterrows():
                    #print("当前row:",row)
                    code = row['code']
                    self.latest_price[row['code']] = code

                    self.running_time = row['datetime']
                    #if self.dt != str(row['datetime'])[0:16]:
                    if code not in self.dtcode or self.dtcode[code] < str(row['datetime'])[0:16]:
                        # [0:16]是分钟线位数
                        print('update!!!!!!!!!!!!')
                        self.dtcode[code] = str(row['datetime'])[0:16]
                        self.isupdate = True


                        self.acc.on_price_change(code, row['close'])
                        bar = pd.DataFrame([row]).set_index(['datetime', 'code']).loc[:, ['open', 'high', 'low', 'close', 'volume']]
                        #print(bar)
                        self.upcoming_data(bar)