def get_pending_order(self): def _type(td): if '限' in td[2].text and '卖' in td[3].text: return const.ORDER_TYPE.LIMIT_SELL if '限' in td[2].text and '买' in td[3].text: return const.ORDER_TYPE.LIMIT_BUY wait = WebDriverWait(driver, 10) order_table_xpath = '//div[@class="bottom-current-delegation"]/table/div/tbody/tr' order_ele = wait.until(EC.presence_of_all_elements_located((By.XPATH, order_table_xpath))) pending_orders = [] for e in order_ele: td_ele = e.find_elements_by_tag_name('td') pending_orders.append({ 'pair': td_ele[1].text, 'type': _type(td_ele), 'price': util.safe_decimal(td_ele[4].text), 'amount': util.safe_decimal(td_ele[5].text), 'filled_amount': util.safe_decimal(td_ele[6].text), 'unsettled_amount': util.safe_decimal(td_ele[7].text), 'cancel_btn': td_ele[9] }) return pending_orders
def get_fee(self): def _stat_msc(df, settlement_currency): """stat_main_settlement_currency""" df_ = df.loc[df['交易对'].str.endswith(settlement_currency)] fee = df_['手续费'].sum() volume = df_['金额'].sum() return fee, volume def _pair_price(pair): price = req.get(current_usdt_url.format(pair), **self.req_kw).json() return price[0][4] content = req.get(csv_url, **self.req_kw).content df = pd.read_csv(io.StringIO(content.decode('utf-8'))) fee = 0 volume = 0 try: df[['手续费', '金额']] = df[['手续费', '金额']].astype('float64') usdt_fee, usdt_volume = _stat_msc(df, 'usdt') btc_fee, btc_volume = _stat_msc(df, 'btc') btc_usdt_price = _pair_price('btc') fee = usdt_fee + btc_fee * btc_usdt_price volume = usdt_volume + btc_volume * btc_usdt_price except KeyError: df[['Fee', 'Amount']] = df[['Fee', 'Amount']].astype('float64') fee = df['Fee'].sum() volume = df['Amount'].sum() return util.safe_decimal(fee), util.safe_decimal(volume), df.shape[0]
def get_price(self): """ 收矿日收盘价 9-10 挖的矿 9-11收到 获取9-11收盘价 """ kline = req.get(kline_url, **self.req_kw).json() return util.safe_decimal(kline[0][4])
def get_ticker(self): """盘口""" try: wait = WebDriverWait(driver, 30) ask_ele = wait.until(EC.presence_of_element_located(( By.XPATH, '//tbody[@class="scrollStyle ask-table"]/tr[last()]/td[1]') )) ask_price = util.safe_decimal(ask_ele.text) bid_ele = wait.until(EC.presence_of_element_located(( By.XPATH, '//tbody[@class="scrollStyle bid-table"]/tr[1]/td[1]') )) bid_price = util.safe_decimal(bid_ele.text) return { const.SIDE.ASK: ask_price, const.SIDE.BID: bid_price, } except: log.error(util.error_msg()) raise Exception('Error ticker')
def _stat(self, account_info, is_exit=True): amount = 0 is_ok = self._login(account_info) if is_ok: self._get_request_kw() driver.get(mine_profit_url_json) rep = req.get(mine_profit_url_json, **self.req_kw) stat_data = rep.json() for d in stat_data['minings']: real_create_time = json.loads(d['extra_info'])['created_at'] if NOW_DATE in real_create_time: amount += util.safe_decimal(d['amount']) if is_exit: driver.delete_all_cookies() return amount
def _dry_try_volume(self, side): # TODO:fix limit_buy or limit_sell xpath pattern = r'.*(\d+\.\d*).*' ask_form_xpath = '//div[@class="order-submit order-form"]/div[2]' price_ele = driver.find_element_by_xpath( ask_form_xpath + '/div[@class="input-label input-item input-price"]/input') price_ele.clear() price_ele.send_keys(str(self.ticker[const.SIDE.BID])) amount_ele = driver.find_element_by_xpath( ask_form_xpath + '/div[@class="input-label input-item input-amout"]/input') ask_amount = util.safe_decimal(re.search(pattern, amount_ele.get_attribute('placeholder')).groups()[0]) ask_volume = self.ticker[const.SIDE.BID] * ask_amount bid_volume = self.balance[const.SIDE.BID] volume = (ask_volume + bid_volume) / 2 if side == const.SIDE.ASK: return bid_volume - volume return ask_volume - volume
def _strategy(): """ 瞄准买一卖一缝隙刷单 目标是尽量成交自己的单,持续时间尽量久 先卖后买 先买后卖 两种操作 交替执行 sell_buy buy_sell 单被吃 反向操作进行两次 Example: 进行buy_sell 导致买单被吃 之后做两次sell_buy 再buy_sell sell_buy 循环 保存操作状态 :return: """ spread = self.ticker[const.SIDE.ASK] - self.ticker[const.SIDE.BID] order_price = util.safe_decimal(round( float(self.ticker[const.SIDE.BID]) + random.uniform(float(self.price_point), float(spread - self.price_point)), util.get_round(self.price_point))) order_amount = util.safe_decimal(round( random.uniform(float(self.amount_point), float(self._get_amount_balance(const.SIDE.ASK))), util.get_round(self.amount_point))) with Stash(strategy_id) as stash: self._balance_asset(order_price, stash) if spread > self.price_point: # 有空隙 if self._judge_mode(stash) == STRATEGY_FLAG.FLAG_SB: is_ok = self.limit_sell(order_price, order_amount) if is_ok is None: stash[MODE_KEY] = MODE.FLAG_BS stash[BALANCE_KEY] = const.SIDE.ASK log.info('no suitable sell order') return if is_ok: pending_order = self.get_pending_order() self.limit_buy(pending_order[0]['price'], pending_order[0]['unsettled_amount']) if stash.get(MODE_KEY) == MODE.FILL_B: stash[MODE_KEY] = MODE.FLAG_SB else: stash[MODE_KEY] = MODE.FLAG_BS else: log.warn('sell order has filled') stash[MODE_KEY] = MODE.FILL_S else: is_ok = self.limit_buy(order_price, order_amount) if is_ok is None: stash[MODE_KEY] = MODE.FLAG_SB stash[BALANCE_KEY] = const.SIDE.BID log.info('no suitable buy order') return if is_ok: pending_order = self.get_pending_order() self.limit_sell(pending_order[0]['price'], pending_order[0]['unsettled_amount']) if stash.get(MODE_KEY) == MODE.FILL_S: stash[MODE_KEY] = MODE.FLAG_BS else: stash[MODE_KEY] = MODE.FLAG_SB else: log.warn('buy order has filled') stash[MODE_KEY] = MODE.FILL_B else: wait_spread = random.uniform(1, 10) msg = 'BID ASk price too close sleep {} sec'.format(wait_spread) log.info(msg) print(msg) sleep(wait_spread)
mine_profit_url_json = 'https://abcc.com/history/minings.json?page=1&per_page=50' csv_url = 'https://abcc.com/history/trades.csv?per_page=1000&from={date} 00:00:00&to={date} 23:59:00'.format( date=NOW_DATE) kline_url = 'https://abcc.com/api/v2/k.json?market=atusdt&limit={}&period=1440'.format( day_limit) current_usdt_url = 'https://abcc.com/api/v2/k.json?market={}usdt&limit=1&period=1440' driver = webdriver.Chrome() logger.AutoLog.log_path = 'logs' strategy_id = 'abcc_stat' log = logger.AutoLog.file_log(strategy_id) conf = config.ABCC_STAT USDT_CNY = util.safe_decimal(conf.usdt_cny) class AT_STAT(object): amount = 0 def _dialog_close(self): wait = WebDriverWait(driver, 5) try: wait.until( EC.presence_of_element_located( (By.CLASS_NAME, 'dialog-content'))) except: return driver.find_element_by_css_selector('.iconfont.icon-guanbi').click()