Skip to content

Commit

Permalink
update 修复 双向开仓的时候的保证金结算问题
Browse files Browse the repository at this point in the history
update  修复 双向开仓的时候的保证金结算问题
  • Loading branch information
yutiansut authored Jul 25, 2019
2 parents 186bbd9 + c567f86 commit 1a2684d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
user = QA.QA_User(username='quantaxis', password='quantaxis')
portfolio = user.new_portfolio('qatestportfolio')
Account = portfolio.new_account(allow_sellopen=True, allow_margin=True, init_cash=5000, allow_t0=True,
account_cookie='future_test', market_type=QA.MARKET_TYPE.FUTURE_CN, frequence=QA.FREQUENCE.FIFTEEN_MIN)
account_cookie='future_testx', market_type=QA.MARKET_TYPE.FUTURE_CN, frequence=QA.FREQUENCE.FIFTEEN_MIN)


# %%
Expand All @@ -24,7 +24,7 @@

# %%
rb_ds = QA.QA_fetch_future_min_adv(
'RBL8', '2018-01-01', '2018-08-28', frequence='15min')
'RBL8', '2019-01-01', '2019-07-20', frequence='15min')


# %%
Expand Down Expand Up @@ -106,7 +106,6 @@ def MACD_JCSC(dataframe, SHORT=12, LONG=26, M=9):
res.trade_amount, res.trade_time)
Account.settle()


# %%
Risk = QA.QA_Risk(Account)

Expand Down
16 changes: 12 additions & 4 deletions QUANTAXIS/QAARP/QAAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def __init__(
'tax', # 税
'message', # 备注
'frozen', # 冻结资金.
'direction' # 方向
'direction', # 方向,
'total_frozen'
]
########################################################################
# 信息类:
Expand Down Expand Up @@ -873,7 +874,7 @@ def daily_frozen(self):
'每日交易结算时的持仓表'
res_ = self.history_table.assign(
date=pd.to_datetime(self.history_table.datetime)
).set_index('date').resample('D').frozen.last().fillna(method='pad')
).set_index('date').resample('D').total_frozen.last().fillna(method='pad')
res_ = res_[res_.index.isin(self.trade_range)]
return res_

Expand Down Expand Up @@ -1164,7 +1165,7 @@ def receive_simpledeal(
(
self.frozen[code][str(trade_towards)]['avg_price'] *
self.frozen[code][str(trade_towards)]['amount']
) + abs(raw_trade_money)
) + abs(trade_money)
) / (
self.frozen[code][str(trade_towards)]['amount'] +
trade_amount
Expand Down Expand Up @@ -1272,6 +1273,12 @@ def receive_simpledeal(
ORDER_DIRECTION.BUY_OPEN,
ORDER_DIRECTION.SELL_OPEN
] else 0

try:
total_frozen = sum([itex.get('avg_price',0)* itex.get('amount',0) for item in self.frozen.values() for itex in item.values()])
except Exception as e:
print(e)
total_frozen = 0
self.history.append(
[
str(trade_time),
Expand All @@ -1287,7 +1294,8 @@ def receive_simpledeal(
tax_fee,
message,
frozen_money,
trade_towards
trade_towards,
total_frozen
]
)
return 0
Expand Down
18 changes: 9 additions & 9 deletions QUANTAXIS/QAARP/QARisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def __init__(
if_fq选项是@尧提出的,关于回测的时候成交价格问题(如果按不复权撮合 应该按不复权价格计算assets)
"""
self.account = account
self.benchmark_code = benchmark_code # 默认沪深300
self.benchmark_code = benchmark_code # 默认沪深300
self.benchmark_type = benchmark_type
self.client = DATABASE.risk

Expand Down Expand Up @@ -175,14 +175,14 @@ def __init__(
self.market_data = market_data
self.if_fq = if_fq
if self.account.market_type == MARKET_TYPE.FUTURE_CN:
self.if_fq = False # 如果是期货, 默认设为FALSE
self.if_fq = False # 如果是期货, 默认设为FALSE

if self.market_value is not None:
if self.account.market_type == MARKET_TYPE.FUTURE_CN and self.account.allow_margin == True:
print('margin!')
self._assets = (
#self.account.daily_frozen +
self.market_value.sum(axis=1) +
self.account.daily_frozen +
# self.market_value.sum(axis=1) +
self.account.daily_cash.set_index('date').cash
).dropna()
else:
Expand All @@ -192,9 +192,9 @@ def __init__(
).fillna(method='pad')
else:
self._assets = self.account.daily_cash.set_index('date'
).cash.fillna(
method='pad'
)
).cash.fillna(
method='pad'
)

self.time_gap = QA_util_get_trade_gap(
self.account.start_date,
Expand Down Expand Up @@ -393,8 +393,8 @@ def message(self):
'totaltimeindex': self.total_timeindex,
'ir': self.ir,
'month_profit': self.month_assets_profit.to_dict()
# 'init_assets': round(float(self.init_assets), 2),
# 'last_assets': round(float(self.assets.iloc[-1]), 2)
# 'init_assets': round(float(self.init_assets), 2),
# 'last_assets': round(float(self.assets.iloc[-1]), 2)
}

@property
Expand Down
2 changes: 1 addition & 1 deletion QUANTAXIS/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
2017/4/8
"""

__version__ = '1.5.5'
__version__ = '1.5.6'
__author__ = 'yutiansut'

import argparse
Expand Down

0 comments on commit 1a2684d

Please sign in to comment.