Skip to content

Commit

Permalink
Merge pull request #1396 from yutiansut/master
Browse files Browse the repository at this point in the history
update to 1.7.7
  • Loading branch information
yutiansut authored Jan 14, 2020
2 parents 59304c2 + a0cd9d2 commit 98521b3
Show file tree
Hide file tree
Showing 4 changed files with 1,347 additions and 16 deletions.
2 changes: 0 additions & 2 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

请使用如下代码对要PR的代码进行格式化:

qa的 yapf格式也是从vnpy那拿来的 参见 https://github.com/vnpy/vnpy/blob/v2.0-DEV/.style.yapf

使用以下代码来格式化
```
pip install https://github.com/google/yapf/archive/master.zip
Expand Down
264 changes: 251 additions & 13 deletions QUANTAXIS/QAARP/QAAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import copy
import datetime
import warnings

import random
import numpy as np
import pandas as pd
from pymongo import DESCENDING, ASCENDING
Expand All @@ -50,10 +50,12 @@
MARKET_TYPE,
ORDER_DIRECTION,
ORDER_MODEL,
ORDER_STATUS,
RUNNING_ENVIRONMENT,
TRADE_STATUS,
EXCHANGE_ID
)
from QUANTAXIS.QAFetch.Fetcher import QA_quotation
from QUANTAXIS.QAUtil.QARandom import QA_util_random_with_topic

# 2017/6/4修改: 去除总资产的动态权益计算
Expand Down Expand Up @@ -301,8 +303,6 @@ def __init__(
"""
super().__init__()

# warnings.warn('QUANTAXIS 1.0.46 has changed the init_assets ==> init_cash, please pay attention to this change if you using init_cash to initial an account class,\
# ', DeprecationWarning, stacklevel=2)
self._history_headers = [
'datetime', # 日期/时间
'code', # 品种
Expand Down Expand Up @@ -993,14 +993,24 @@ def weights(x):
).sort_index().loc[:datetime].groupby('code').apply(weights
).dropna()

def reset_assets(self, init_cash=None):
'reset_history/cash/'
def reset_assets(self, init_cash=1000000):
"""重置账户
Keyword Arguments:
init_cash {int} -- [description] (default: {1000000})
"""
self.sell_available = copy.deepcopy(self.init_hold)
self.history = []
self.init_cash = init_cash
self.cash = [self.init_cash]
self.cash_available = self.cash[-1] # 在途资金

self.time_index_max = []

self.frozen = {} # 冻结资金(保证金)
self.finishedOrderid = []


def receive_simpledeal(
self,
code,
Expand Down Expand Up @@ -1261,11 +1271,15 @@ def receive_simpledeal(
self.cash.append(
self.cash[-1] - trade_money - tax_fee - commission_fee
)
if self.allow_t0 or trade_towards == ORDER_DIRECTION.SELL:
if trade_towards in [ORDER_DIRECTION.BUY, ORDER_DIRECTION.BUY_OPEN, ORDER_DIRECTION.SELL_OPEN]:
"""平仓部分的sell_available已经被提前扣减了 在sendorder中
"""

self.sell_available[code] = self.sell_available.get(
code,
0
) + trade_amount * market_towards
if self.allow_t0:
self.buy_available = self.sell_available

self.cash_available = self.cash[-1]
Expand Down Expand Up @@ -1344,7 +1358,7 @@ def receive_deal(
[type] -- [description]
"""

print('QAACCOUNT ==> receive deal')
print('QAACCOUNT ==> receive deal {} {} {} {}'.format(trade_time, code, trade_price, trade_towards, ))

trade_time = str(trade_time)
code = str(code)
Expand Down Expand Up @@ -1502,6 +1516,7 @@ def send_order(
assert (int(towards) != 0)
if int(towards) in [1, 2, 3]:
# 是买入的情况(包括买入.买开.买平)

if self.cash_available >= money:
if self.market_type == MARKET_TYPE.STOCK_CN: # 如果是股票 买入的时候有100股的最小限制
amount = int(amount / 100) * 100
Expand Down Expand Up @@ -1530,10 +1545,11 @@ def send_order(
float(amount * price * (1 + self.commission_coeff))
)

print(_hold)
#print(_hold)
if self.cash_available >= _money:
if _hold < 0:
if _hold < 0 and abs(_hold) >= amount:
self.cash_available -= _money
self.sell_available[code] += amount

flag = True
else:
Expand All @@ -1558,10 +1574,11 @@ def send_order(

# 如果你的hold> amount>0
# 持仓数量>卖出数量
if _hold >= amount:
self.sell_available[code] -= amount
# towards = ORDER_DIRECTION.SELL
flag = True
if int(towards) in [-1, -3]:
if _hold >= amount:
self.sell_available[code] -= amount
# towards = ORDER_DIRECTION.SELL
flag = True
# 如果持仓数量<卖出数量
else:

Expand Down Expand Up @@ -2025,6 +2042,227 @@ def get_history(self, start, end):
pd.Timestamp(end))]



def generate_randomtrade(self, code, start, end, frequence):
"""快速生成一坨交易
Arguments:
code {[type]} -- [description]
frequence {[type]} -- [description]
start {[type]} -- [description]
end {[type]} -- [description]
Keyword Arguments:
num {int} -- [description] (default: {100})
"""

# 先生成交易日
day = start[0:9]
for idx, item in QA_quotation(code, start, end, frequence, self.market_type, 'mongo').iterrows():

code = idx[1]
time = idx[0]

if self.market_type == MARKET_TYPE.STOCK_CN:
if time != day:
self.settle()
day = time
if self.market_type == MARKET_TYPE:
self.settle()
day = time


price = item['close']
holdamount = self.sell_available.get(code, 0)

if random.random() < 0.5:
# open

if holdamount == 0:
if random.random() < 0.5:
self.buy(code,price, amount=1, time = time, if_selfdeal=True)
else:
self.sell(code,price, amount=1, time = time,if_selfdeal=True)
else:
pass
else:

if holdamount>0:
self.sell_close(code, price, amount=holdamount, time = time, if_selfdeal=True)
elif holdamount<0:
holdamount = abs(holdamount)
self.buy_close(code, price, amount=holdamount, time = time, if_selfdeal=True)





def buy(self, code:str, price:float, amount:int, time:str, order_model:str = ORDER_MODEL.LIMIT, amount_model:str = AMOUNT_MODEL.BY_AMOUNT, if_selfdeal:bool =False):
"""self.buy == self.buy_open 自动识别股票/期货
一个方便使用的sendorder的二次封装 方便直接调用
Arguments:
code {str} -- [description]
price {float} -- [description]
amount {int} -- [description]
time {str} -- [description]
Keyword Arguments:
order_model {str} -- [description] (default: {ORDER_MODEL.LIMIT})
amount_model {str} -- [description] (default: {AMOUNT_MODEL.BY_AMOUNT})
if_selfdeal {bool} -- [description] (default: {False})
"""

if self.market_type== MARKET_TYPE.FUTURE_CN:
towards=ORDER_DIRECTION.BUY_OPEN
else:
towards=QA.ORDER_DIRECTION.BUY



order = self.send_order(
code=code, time=time, amount=amount,
towards=towards, price=price,
order_model=order_model,
amount_model=amount_model
)

if if_selfdeal:
if order:

order.trade('buy', order.price,
order.amount, order.datetime)
self.orders.set_status(order.order_id, ORDER_STATUS.SUCCESS_ALL)

else:
return order

def sell(self, code:str, price:float, amount:int, time:str, order_model:str = ORDER_MODEL.LIMIT, amount_model:str = AMOUNT_MODEL.BY_AMOUNT, if_selfdeal:bool =False):
"""self.sell == self.sell_open 自动识别股票/期货
一个方便使用的sendorder的二次封装 方便直接调用
Arguments:
code {str} -- [description]
price {float} -- [description]
amount {int} -- [description]
time {str} -- [description]
Keyword Arguments:
order_model {str} -- [description] (default: {ORDER_MODEL.LIMIT})
amount_model {str} -- [description] (default: {AMOUNT_MODEL.BY_AMOUNT})
if_selfdeal {bool} -- [description] (default: {False})
"""

if self.market_type== MARKET_TYPE.FUTURE_CN:
towards=ORDER_DIRECTION.SELL_OPEN
else:
towards=QA.ORDER_DIRECTION.SELL



order = self.send_order(
code=code, time=time, amount=amount,
towards=towards, price=price,
order_model=order_model,
amount_model=amount_model
)

if if_selfdeal:
if order:
order.trade('sell', order.price,
order.amount, order.datetime)
self.orders.set_status(order.order_id, ORDER_STATUS.SUCCESS_ALL)
else:
return order


def buy_close(self, code:str, price:float, amount:int, time:str, order_model:str = ORDER_MODEL.LIMIT, amount_model:str = AMOUNT_MODEL.BY_AMOUNT, if_selfdeal:bool =False):
"""自动识别股票/期货
一个方便使用的sendorder的二次封装 方便直接调用
Arguments:
code {str} -- [description]
price {float} -- [description]
amount {int} -- [description]
time {str} -- [description]
Keyword Arguments:
order_model {str} -- [description] (default: {ORDER_MODEL.LIMIT})
amount_model {str} -- [description] (default: {AMOUNT_MODEL.BY_AMOUNT})
if_selfdeal {bool} -- [description] (default: {False})
"""

if self.market_type== MARKET_TYPE.FUTURE_CN:
towards=ORDER_DIRECTION.BUY_CLOSE
else:
print("WARING: 当前账户是股票账户, 不应该使用此接口")
towards=QA.ORDER_DIRECTION.BUY



order = self.send_order(
code=code, time=time, amount=amount,
towards=towards, price=price,
order_model=order_model,
amount_model=amount_model
)

if if_selfdeal:
if order:
order.trade('buyclose', order.price,
order.amount, order.datetime)
self.orders.set_status(order.order_id, ORDER_STATUS.SUCCESS_ALL)
else:
return order

def sell_close(self, code:str, price:float, amount:int, time:str, order_model:str = ORDER_MODEL.LIMIT, amount_model:str = AMOUNT_MODEL.BY_AMOUNT, if_selfdeal:bool =False):
"""self.buy == self.buy_open 自动识别股票/期货
一个方便使用的sendorder的二次封装 方便直接调用
Arguments:
code {str} -- [description]
price {float} -- [description]
amount {int} -- [description]
time {str} -- [description]
Keyword Arguments:
order_model {str} -- [description] (default: {ORDER_MODEL.LIMIT})
amount_model {str} -- [description] (default: {AMOUNT_MODEL.BY_AMOUNT})
if_selfdeal {bool} -- [description] (default: {False})
"""

if self.market_type== MARKET_TYPE.FUTURE_CN:
towards=ORDER_DIRECTION.SELL_CLOSE
else:
print("WARING: 当前账户是股票账户, 不应该使用此接口")
towards=QA.ORDER_DIRECTION.SELL



order = self.send_order(
code=code, time=time, amount=amount,
towards=towards, price=price,
order_model=order_model,
amount_model=amount_model
)

if if_selfdeal:
if order:
order.trade('sellclose', order.price,
order.amount, order.datetime)
self.orders.set_status(order.order_id, ORDER_STATUS.SUCCESS_ALL)
else:
return order


buy_open = buy
sell_open = sell


class Account_handler():

def __init__(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.7.6'
__version__ = '1.7.7'
__author__ = 'yutiansut'

import argparse
Expand Down
Loading

0 comments on commit 98521b3

Please sign in to comment.