Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
  • Loading branch information
yutiansut committed May 17, 2019
1 parent 3818eb3 commit 3246758
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
64 changes: 53 additions & 11 deletions QUANTAXIS/QAARP/QAAccountPro.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,6 @@ def __init__(
self.today_trade = {'last': [], 'current': []}
self.today_orders = {'last': [], 'current': []}
self.pms = {}
########################################################################
# 规则类
# 1.是否允许t+0 及买入及结算
# 2.是否允许卖空开仓
# 3.是否允许保证金交易/ 如果不是false 就需要制定保证金比例(dict形式)

# 期货: allow_t0 True allow_sellopen True
#

self.allow_t0 = allow_t0
self.allow_sellopen = allow_sellopen
Expand Down Expand Up @@ -1665,9 +1657,9 @@ def from_message(self, message):
self.frozen = message.get('frozen', {})
self.finishedOrderid = message.get('finished_id', [])
pos_id = message.get('position_id', [])
self.pms = dict(zip(pos_id, [QA_Position(position_id=item,
account_cookie=self.account_cookie, portfolio_cookie=self.portfolio_cookie,
user_cookie=self.user_cookie) for item in pos_id]))
self.pms = dict(zip(pos_id, [QA_Position(position_id=item,
account_cookie=self.account_cookie, portfolio_cookie=self.portfolio_cookie,
user_cookie=self.user_cookie) for item in pos_id]))

self.settle()
return self
Expand Down Expand Up @@ -1787,6 +1779,56 @@ def run(self, event):
if event.callback:
event.callback(event)

@property
def positions_with_pos(self):
return pd.DataFrame([item.curpos for item in self.pms.values()],
index=pd.MultiIndex.from_tuples([(item.code, item.position_id) for item in self.pms.values()], names=['code', 'pos_id']))
@property
def positions(self):
return self.positions_with_pos.groupby('code').sum()

@property
def hold_detail_with_pos(self):
return pd.DataFrame([item.hold_detail for item in self.pms.values()],
index=pd.MultiIndex.from_tuples([(item.code, item.position_id) for item in self.pms.values()], names=['code', 'pos_id']))
@property
def hold_detail(self):
return self.hold_detail_with_pos.groupby('code').sum()

def get_position(self, code):
"""基于QAPosition的联合查询
Arguments:
code {[type]} -- [description]
Returns:
[type] -- [description]
"""
try:
return self.positions.loc[code].to_dict()
except KeyError:
return {'volume_long': 0, 'volume_short': 0}

def get_position_with_pos(self, code, pos_id):
"""基于QAPosition的联合查询
Arguments:
code {[type]} -- [description]
Returns:
[type] -- [description]
"""
try:
return self.positions_with_pos.loc[(code, pos_id)].to_dict()
except KeyError:
return {'volume_long': 0, 'volume_short': 0}

def get_holddetail(self, code):
try:
return self.hold_detail.loc[(code, slice(None))].sum().to_dict()
except KeyError:
return {'volume_long': 0, 'volume_short': 0}

def save(self):
"""
存储账户信息
Expand Down
32 changes: 22 additions & 10 deletions QUANTAXIS/QAMarket/QAPosition.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self,
portfolio_cookie='portfolio',
user_cookie='quantaxis',
moneypreset=100000, # 初始分配资金
frozen = None,
frozen=None,
moneypresetLeft=None,
volume_long_today=0,
volume_long_his=0,
Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(self,
self.orders = {} if orders is None else orders
self.frozen = {} if frozen is None else frozen
if auto_reload:
self.save()
self.reload()
self.allow_exceed = allow_exceed

def __repr__(self):
Expand Down Expand Up @@ -354,6 +354,18 @@ def static_message(self):
'orders': self.orders
}

@property
def hold_detail(self):
return {
# 持仓量
'volume_long_today': self.volume_long_today,
'volume_long_his': self.volume_long_his,
'volume_long': self.volume_long,
'volume_short_today': self.volume_short_today,
'volume_short_his': self.volume_short_his,
'volume_short': self.volume_short
}

@property
def realtime_message(self):
return {
Expand Down Expand Up @@ -420,7 +432,7 @@ def order_check(self, amount: float, price: float, towards: int, order_id: str)
1)
) * float(self.market_preset.get('buy_frozen_coeff',
1))

if (self.moneypresetLeft > moneyneed) or self.allow_exceed:
self.moneypresetLeft -= moneyneed
self.frozen[order_id] = moneyneed
Expand Down Expand Up @@ -658,7 +670,7 @@ def save(self):
def reload(self):
res = DATABASE.positions.find_one({
'account_cookie': self.account_cookie,
'portfolio_coookie': self.portfolio_cookie,
'portfolio_cookie': self.portfolio_cookie,
'user_cookie': self.user_cookie,
'position_id': self.position_id
})
Expand All @@ -671,7 +683,7 @@ def loadfrommessage(self, message):
self.__init__(
code=message['code'],
account_cookie=message['account_cookie'],
frozen = message['frozen'],
frozen=message['frozen'],
portfolio_cookie=message['portfolio_cookie'],
user_cookie=message['user_cookie'],
moneypreset=message['moneypreset'], # 初始分配资金
Expand Down Expand Up @@ -715,18 +727,18 @@ def on_order(self, order: QA_Order):
- 交易过程的外部手动交易
- 风控状态下的监控外部交易
order_id 是外部的
trade_id 不一定存在
"""

if order['order_id'] not in self.frozen.keys():
print('OUTSIDE ORDER')
#self.frozen[order['order_id']] = order[]
# self.frozen[order['order_id']] = order[]
# 回放订单/注册进订单系统
order = self.send_order(
order.get('amount', order.get('volume')),
order['price'],
order['price'],
eval('ORDER_DIRECTION.{}_{}'.format(
order.get('direction'),
order.get('offset')
Expand Down Expand Up @@ -754,9 +766,9 @@ def on_transaction(self, transaction: dict):
transaction.get('volume')),
towards
)
self.moneypresetLeft+= self.frozen.get(transaction['order_id'],0)
self.moneypresetLeft += self.frozen.get(transaction['order_id'], 0)
# 当出现外部交易的时候, 直接在frozen中注册订单
self.frozen[transaction['order_id']] =0
self.frozen[transaction['order_id']] = 0
self.orders[transaction['order_id']] = ORDER_STATUS.SUCCESS_ALL
self.trades.append(transaction)
except Exception as e:
Expand Down

0 comments on commit 3246758

Please sign in to comment.