Skip to content

Commit

Permalink
Merge pull request #1158 from yutiansut/master
Browse files Browse the repository at this point in the history
fix unable to import QA_Account bug/ update QAPosition
  • Loading branch information
yutiansut authored May 6, 2019
2 parents 3270c6d + bddae1a commit 00aff7e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
11 changes: 6 additions & 5 deletions QUANTAXIS/QAMarket/QAMarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import sched
import threading

from QUANTAXIS.QAARP.QAAccount import QA_Account
# from QUANTAXIS.QAARP.QAAccount import QA_Account
from QUANTAXIS.QAEngine.QAEvent import QA_Event
from QUANTAXIS.QAEngine.QATask import QA_Task
from QUANTAXIS.QAMarket.QABacktestBroker import QA_BacktestBroker
Expand Down Expand Up @@ -208,10 +208,11 @@ def login(self, broker_name, account_cookie, account=None):
res = False
if account is None:
if account_cookie not in self.session.keys():
self.session[account_cookie] = QA_Account(
account_cookie=account_cookie,
broker=broker_name
)

# self.session[account_cookie] = QA_Account(
# account_cookie=account_cookie,
# broker=broker_name
# )
if self.sync_account(broker_name, account_cookie):
res = True

Expand Down
60 changes: 42 additions & 18 deletions QUANTAXIS/QAMarket/QAPosition.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ def __init__(self,

self.code = code
self.account_cookie = account_cookie
self.time = ''
self.market_preset = MARKET_PRESET().get_code(self.code)
self.position_id = str(uuid.uuid4())
self.moneypreset = moneypreset
self.moneypresetLeft = self.moneypreset
"""{'name': '原油',
'unit_table': 1000,
'price_tick': 0.1,
Expand Down Expand Up @@ -258,26 +260,39 @@ def realtime_message(self):
}

def order_check(self, amount: float, price: float, towards: int) -> bool:
if towards == ORDER_DIRECTION.BUY_CLOSE and (self.volume_short - self.volume_short_frozen) > amount:
res = False
if towards == ORDER_DIRECTION.BUY_CLOSE:
print('buyclose')
print(self.volume_short - self.volume_short_frozen)
print(amount)
if (self.volume_short - self.volume_short_frozen) >= amount:
# check
self.volume_short_frozen_today += amount
return True
self.volume_short_frozen_today += amount
res = True

elif towards == ORDER_DIRECTION.BUY_CLOSETODAY and (self.volume_short_today - self.volume_short_frozen_today) > amount:
self.volume_short_frozen_today += amount
return True

elif towards == ORDER_DIRECTION.SELL_CLOSE and (self.volume_long - self.volume_long_frozen) > amount:
self.volume_long_frozen_today += amount
return True
elif towards == ORDER_DIRECTION.BUY_CLOSETODAY and (self.volume_short_today - self.volume_short_frozen_today) >= amount:
self.volume_short_frozen_today += amount
res = True

elif towards == ORDER_DIRECTION.SELL_CLOSETODAY and (self.volume_long_today - self.volume_short_frozen_today) > amount:
elif towards == ORDER_DIRECTION.SELL_CLOSE:
print('sellclose')
print(self.volume_long - self.volume_long_frozen)
print(amount)
if (self.volume_long - self.volume_long_frozen) >= amount:
self.volume_long_frozen_today += amount
res =True

elif towards == ORDER_DIRECTION.SELL_CLOSETODAY and (self.volume_long_today - self.volume_short_frozen_today) >= amount:
print('sellclosetoday')
print(self.volume_long_today - self.volume_long_frozen)
print(amount)
self.volume_long_frozen_today += amount
return True
elif towards in [ORDER_DIRECTION.BUY_OPEN, ORDER_DIRECTION.SELL_OPEN, ORDER_DIRECTION.BUY]:
return True
else:
return False
res = True

return res

def send_order(self, amount: float, price: float, towards: int):
if self.order_check(amount, price, towards):
Expand All @@ -289,7 +304,7 @@ def send_order(self, amount: float, price: float, towards: int):
'instrument_id': self.code,
'towards': int(towards),
'exchange_id': str(self.exchange_id),
'order_time': str(datetime.datetime.now()),
'order_time': str(self.time),
'volume': float(amount),
'price': float(price),
'order_id': str(uuid.uuid4())
Expand Down Expand Up @@ -323,7 +338,7 @@ def update_pos(self, price, amount, towards):

temp_cost = float(amount)*float(price) * \
float(self.market_preset.get('unit_table', 1))
# if towards == ORDER_DIRECTION.SELL_CLOSE:

if towards == ORDER_DIRECTION.BUY:
# 股票模式/ 期货买入开仓
self.volume_long_today += amount
Expand All @@ -336,8 +351,9 @@ def update_pos(self, price, amount, towards):
elif towards == ORDER_DIRECTION.BUY_OPEN:

# 增加保证金
self.margin_long_today += temp_cost * \
temp_margin = temp_cost * \
self.market_preset['buy_frozen_coeff']
self.margin_long_today += temp_margin
# 重算开仓均价
self.open_price_long = (
self.open_price_long * self.volume_long + amount*price) / (amount + self.volume_long)
Expand All @@ -348,6 +364,9 @@ def update_pos(self, price, amount, towards):
self.volume_long_today += amount
#
self.open_cost_long += temp_cost
self.position_cost_long += temp_cost
self.moneypresetLeft -= temp_margin


elif towards == ORDER_DIRECTION.SELL_OPEN:
# 增加保证金
Expand All @@ -360,15 +379,18 @@ def update_pos(self, price, amount, towards):
self.position_price_short = (
self.position_price_short * self.volume_short + amount * price) / (amount + self.volume_short)
self.open_cost_short += temp_cost
self.position_cost_short += temp_cost
self.volume_short_today += amount


elif towards == ORDER_DIRECTION.BUY_CLOSETODAY:
if self.volume_short_today > amount:
self.position_cost_short = self.position_cost_short * \
(self.volume_short-amount)/self.volume_short
self.open_cost_short = self.open_cost_short * \
(self.volume_short-amount)/self.volume_short
self.volume_short_today -= amount
self.volume_short_frozen_today += amount
# close_profit = (self.position_price_short - price) * volume * position->ins->volume_multiple;

#self.volume_short_frozen_today += amount
Expand All @@ -384,6 +406,7 @@ def update_pos(self, price, amount, towards):
self.open_cost_long = self.open_cost_long * \
(self.volume_long-amount)/self.volume_long
self.volume_long_today -= amount
self.volume_long_frozen_today += amount

elif towards == ORDER_DIRECTION.BUY_CLOSE:
# 有昨仓先平昨仓
Expand All @@ -396,6 +419,7 @@ def update_pos(self, price, amount, towards):
else:
self.volume_short_today -= (amount - self.volume_short_his)
self.volume_short_his = 0
self.volume_short_frozen_today -= amount
elif towards == ORDER_DIRECTION.SELL_CLOSE:
# 有昨仓先平昨仓
self.position_cost_long = self.position_cost_long * \
Expand All @@ -406,8 +430,8 @@ def update_pos(self, price, amount, towards):
self.volume_long_his -= amount
else:
self.volume_long_today -= (amount - self.volume_long_his)
self.volume_long_his -= amount

self.volume_long_his = 0
self.volume_long_frozen_today -= amount
# 计算收益/成本

def settle(self):
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.0'
__version__ = '1.5.1'
__author__ = 'yutiansut'

import argparse
Expand Down

0 comments on commit 00aff7e

Please sign in to comment.