예제 #1
0
    def market_order_wait_till_execution3(cls, side, total_size):
        size = []
        price = []
        max_wait = 10
        num = 0
        bid = TickData.get_bid_price()
        ask = TickData.get_ask_price()
        try:
            cls.num_private_access += 1
            order_id = cls.bf.create_order(
                symbol='BTC/JPY',
                type='market',
                side=side,
                amount=total_size,
                params={'product_code': 'FX_BTC_JPY'})
        except Exception as e:
            print('market order failed! ' + str(e))
            LogMaster.add_log('market order failed! ' + str(e), None)
            LineNotification.send_error('market order failed! ' + str(e))
            cls.check_exception(e)
            return -1, 0, 0, ''

        order_id = order_id['info']['child_order_acceptance_id']
        print('waiting for market order execution...')
        start = time.time()
        while sum(size) < total_size:
            exe_data = TickData.get_exe_data()[-100:]
            if time.time() - start > max_wait:
                exe_data = cls.get_executions()
            exe_data = TickData.get_exe_data()[-100:]
            for exec in exe_data:
                if exec[side + "_child_order_acceptance_id"] == order_id:
                    size.append(exec["size"])
                    price.append(exec["price"])
            if time.time() - start > max_wait:
                print(
                    'can not complete Trade - check_and_wait_till_all_execution!'
                )
                LogMaster.add_log(
                    'can not complete Trade - check_and_wait_till_all_execution!',
                    None)
                LineNotification.send_error(
                    'can not complete Trade - check_and_wait_till_all_execution!'
                )
                return -1, sum(size), round(
                    sum(price[i] * size[i]
                        for i in range(len(price))) / sum(size)), order_id
            num += 1
        ave_p = round(
            sum(price[i] * size[i] for i in range(len(price))) / sum(size))
        print('market order has been successfully executed.' + 'side=' + side +
              ', ave price=' + str(ave_p) + ', size=' +
              str(round(sum(size), 2)))
        sp = ask - ave_p if side == 'buy' else ave_p - bid
        print('market order spread loss/profit = ' + str(sp))
        cls.spread_loss_profit += sp
        cls.spread_loss_profit_log.append(sp)
        return 0, round(sum(size), 2), ave_p, order_id
예제 #2
0
 def __check_execution_ws(order_id):
     exec_size = []
     exec_price = []
     exe_data = TickData.get_exe_data()[-30:]
     for exec in exe_data:
         if exec[side +
                 '_child_order_acceptance_id'] == order_id and exec[
                     'id'] not in checked_exec_id:
             exec_size.append(exec['size'])
             exec_price.append(exec['price'])
             checked_exec_id.append(exec['id'])
     return exec_size, exec_price
예제 #3
0
 def check_pt_order_exeution(self):
     executions = None
     if datetime.now().second >=57:
         executions = Trade.get_executions()
     else:
         executions = TickData.get_exe_data()[-30:]
     if self.pt_order_id != '':
         for i in range(len(executions)):
             if executions[i]['child_order_acceptance_id'] == self.pt_order_id and executions[i]['id'] not in self.pt_checked_exec_id:
                 side = executions[i]['side'].ilower()
                 price = executions[i]['price']
                 size = executions[i]['size']
                 self.pt_checked_exec_id.append(executions[i]['id'])
                 self.executions_hist_log.append(executions[i])
                 self.calc_pl(executions[i])
                 self.update_holding(side, price, size)
                 self.pt_outstanding_size -= size
                 if self.pt_outstanding_size <= 0:
                     print('pt order has been fully executed!')
                     self.initialize_pt_order()
예제 #4
0
 def __calc_price_move():
     executions = TickData.get_exe_data()[-3:]
     change = executions[-1]['price'] - executions[0]['price']
     return change