|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import logging |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from shipane_sdk.market_utils import MarketUtils |
| 7 | +from shipane_sdk.ricequant.client import RiceQuantClient |
| 8 | +from shipane_sdk.ricequant.transaction import RiceQuantTransaction |
| 9 | + |
| 10 | + |
| 11 | +class RiceQuantFollowingJob(object): |
| 12 | + def __init__(self, config, client): |
| 13 | + self._log = logging.getLogger() |
| 14 | + self._config = config |
| 15 | + self._client = client |
| 16 | + self._rq_client = RiceQuantClient(username=self._config.get('RiceQuant', 'username'), |
| 17 | + password=self._config.get('RiceQuant', 'password'), |
| 18 | + run_id=self._config.get('RiceQuant', 'run_id')) |
| 19 | + self._rq_client.login() |
| 20 | + self._start_datatime = datetime.now() |
| 21 | + self._processed_transactions = [] |
| 22 | + |
| 23 | + def __call__(self): |
| 24 | + if MarketUtils.is_closed(): |
| 25 | + self._log.warning("********** 休市期间不跟单 **********") |
| 26 | + if self._processed_transactions: |
| 27 | + del self._processed_transactions[:] |
| 28 | + return |
| 29 | + |
| 30 | + self._log.info("********** 开始跟单 **********") |
| 31 | + try: |
| 32 | + transaction_detail = self._rq_client.query() |
| 33 | + raw_transactions = transaction_detail['resp']['trades'] |
| 34 | + self._log.info("获取到 {} 条委托".format(len(raw_transactions))) |
| 35 | + |
| 36 | + transactions = [] |
| 37 | + for raw_transaction in raw_transactions: |
| 38 | + transaction = RiceQuantTransaction(raw_transaction).normalize() |
| 39 | + if self._is_expired(transaction): |
| 40 | + continue |
| 41 | + |
| 42 | + transactions.append(transaction) |
| 43 | + self._log.info("获取到 {} 条有效委托".format(len(transactions))) |
| 44 | + |
| 45 | + for tx in transactions: |
| 46 | + self._processed_transactions.append(tx) |
| 47 | + self._log.info("开始以 {}元 {} {}股 {}".format(tx.price, tx.get_cn_type(), tx.amount, tx.symbol)) |
| 48 | + response = self._shipane_client.execute(None, |
| 49 | + action=tx.type, |
| 50 | + symbol=tx.symbol, |
| 51 | + type='LIMIT', |
| 52 | + price=tx.price, |
| 53 | + amount=tx.amount) |
| 54 | + if response is not None: |
| 55 | + self._log.info(u'实盘易回复:\nstatus_code: %d\ntext: %s', response.status_code, response.text) |
| 56 | + else: |
| 57 | + self._log.error('实盘易未回复') |
| 58 | + except Exception as e: |
| 59 | + self._log.exception("跟单异常") |
| 60 | + self._log.info("********** 结束跟单 **********\n") |
| 61 | + |
| 62 | + def _is_expired(self, transaction): |
| 63 | + if transaction.completed_at < self._start_datatime: |
| 64 | + return True |
| 65 | + if transaction in self._processed_transactions: |
| 66 | + return True |
| 67 | + return False |
0 commit comments