Beispiel #1
0
def heart_beat():
    last_processed_time = datetime.now()
    finish_flag = False
    while True:
        while datetime.now() - last_processed_time < timedelta(seconds=1):
            gevent.sleep()
        candidates = []
        while not candidate_queue.empty():
            candidates.append(candidate_queue.get())

        #print('candidate size', len(candidates), datetime.now())
        for c in candidates:
            if c['code'] == 'exit':
                logger.warning('SET FINISH FLAG')
                finish_flag = True

        if finish_flag:
            logger.warning('FINISH TODAY WORK')
            break
        else:
            for fw in followers:
                if fw.is_in_market():
                    fw.process_tick()

        last_processed_time = datetime.now()
Beispiel #2
0
    def ba_data_handler(self, code, data):
        if self.order_done:
            return

        balance = int(stock_api.get_balance(self.reader)['balance'] / BALANCE_DIVIDER)
        self.order_done = True

        bid_table = [data['fifth_bid_price'], data['fourth_bid_price'],
                        data['third_bid_price'], data['second_bid_price'],
                        data['first_bid_price']]
        price_table = [(data['first_ask_price'], data['first_ask_remain']),
                        (data['second_ask_price'], data['second_ask_remain']),
                        (data['third_ask_price'], data['third_ask_remain'])]
        price = self.find_target_price(price_table, balance)
        if (price == 0 or
                #self.is_abnormal_bid_table(bid_table) or
                price_info.get_price_unit_distance(data['first_bid_price'], data['first_ask_price'], self.code_info['is_kospi']) > 2):
            logger.warning('STOP, BA price is abnormal or cannot find target price %d\nPRICE TABLE: %s\nBID TABLE: %s\nFIRST BID PRICE %d\nFIRST ASK PRICE %d', price, str(price_table), str(bid_table), data['first_bid_price'], data['first_ask_price'])
            self.set_status(tradestatus.BUY_FAIL)
        else:
            qty = int(balance / price)
            if qty > 0:
                self.set_order_quantity(qty)
                logger.info('PROCESS BUY ORDER %s, price: %d, qty: %d', code, price, qty)
                result = stock_api.order_stock(self.reader, code, price, qty, True)
                logger.info('BUY ORDER RETURN %s', str(result))
                if result['status'] != 0:
                    self.set_status(tradestatus.BUY_FAIL)
                else:
                    self.set_status(tradestatus.BUY_ORDER_SEND_DONE)
            else:
                logger.warning('QUANTITY is 0, balance: %d, price: %d', balance, price)
                self.set_status(tradestatus.BUY_FAIL)
Beispiel #3
0
    def sell_immediately(self):
        super().sell_immediately()

        logger.info('ENTER SELL IMMEDIATELY FLAG: %s, iprice: %d',
                    str(self.sell_in_progress),
                    self.get_immediate_sell_price())
        if not self.sell_in_progress and self.get_immediate_sell_price() != 0:
            result = stock_api.order_stock(self.reader, self.get_code(), self.get_immediate_sell_price(), self.get_current_quantity(), False)
            self.sell_in_progress = True
        else:
            logger.warning('CANNOT DO SELL IMMEDIATELY')
Beispiel #4
0
    def start_trading(self, code_info):
        logger.warning('START TRADING %s', self.code)
        if client_info.TEST_MODE:
            stock_api.set_start_time(self.code)

        self.set_status(StockFollower.TRADING)
        self.code_info = code_info
        self.top_edges = list(price_info.get_peaks(self.avg_prices))
        self.bottom_edges = list(price_info.get_peaks(self.avg_prices, False))
        self.trader = trader.Trader(self.reader, self.code_info,
                                    self.market_status)
        self.trader.start()
Beispiel #5
0
def heart_beat():
    now = datetime.now()
    last_processed_time = datetime.now()
    close_time = now.replace(hour=15 + DELTA, minute=40, second=3)
    finish_flag = False
    while True:
        while datetime.now() - last_processed_time < timedelta(seconds=1):
            gevent.sleep()

        candidates = []
        while not candidate_queue.empty():
            candidates.append(candidate_queue.get())

        for c in candidates:
            if c['code'] == 'exit':
                logger.warning('SET FINISH FLAG')
                finish_flag = True

        if finish_flag:
            if datetime.now() > close_time:
                logger.warning('EXIT LOOP')
                break

            for fw in followers:
                if fw.get_status() != StockFollower.READY:
                    fw.finish_work()
        else:
            for fw in followers:
                fw.process_tick()

                if fw.is_in_market() and fw.get_status(
                ) == StockFollower.READY:
                    for c in candidates:
                        if c['code'] == fw.code:
                            fw.start_trading(c['info'])
            candidates.clear()

        last_processed_time = datetime.now()
Beispiel #6
0
 def start(self):
     logger.warning('START BUY STAGE %s', self.code_info['code'])
     self.stage = BuyStage(self.reader, self.code_info, self.market_status)
Beispiel #7
0
def receive_result(result):
    logger.warning('TRADE RESULT\n%s', str(result))
    for fw in followers:
        fw.receive_result(result)