QAARP模块——账户、组合、策略

QAARP模块——账户、组合、策略

这部分看起来已经非常让我感到晕眩了。。。

账户/组合/策略的关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import QUANTAXIS as QA

# 创建用户
userA = QA.QA_User()

# 创建两个组合
PortfolioA1 = userA.new_portfolio()
PortfolioA2 = userA.new_portfolio()

# 在A1里面新建两个策略
strategy1 = PortfolioA1.new_account()
strategy2 = PortfolioA1.new_account()

# 打印user的组合
userA.portfolio_list

# 打印组合A1
PortfolioA1

# 打印策略strategy1 本质是Account类
strategy1

# 创建一个策略,自定义on_bar事件
class Strategy3(QA.QA_Strategy):
def __init__(self):
super().__init__()
def on_bar(self, event):
print(event)

# 实例化该策略到strategy3
strategy3 = Strategy3()

# 打印strategy3
strategy3

# 把该策略加载到A2组合中
PortfolioA2.add_account(strategy3)

# 打印A2
PortfolioA2

# 打印组合列表
userA.portfolio_list

创建自定义的策略

这个策略是这样的,初始化函数是设置交易频率为每天,交易市场为中国股市。on_bar()函数定义了一个sell_available的股票代码集合,如果当前账户中没有股票就立即买100股,如果股票集合中某只股票的sellavailable.get(item, 0)>0,根据get函数的含义,这个表达式大概是说item的第一个(索引为0)键值大于0就卖出sellavailable[item](大概是清空的意思)。否则,再买入100股。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from QUANTAXIS.QAARP.QAStrategy import QA_Strategy
from QUANTAXIS.QAUtil.QAParameter import (AMOUNT_MODEL, MARKET_TYPE, FREQUENCE, ORDER_DIRECTION, ORDER_MODEL)
from QUANTAXIS.QAUtil.QALogs import QA_util_log_info

class MAStrategy(QA_Strategy):
def __init__(self):
super().__init__()
self.frequence = FREQUENCE.DAY
self.market_type = MARKET_TYPE.STOCK_CN

def on_bar(self, event):
sellavailable = self.sell_available
try:
for item in event.market_data.code:
if sellavailable is None:
event.send_order(account_id = self.account_cookie,
amount = 100,
amount_model = AMOUNT_MODEL.BY_AMOUNT,
time = self.current_time,
code = item,
price = 0,
order_model = ORDER_MODEL.MARKET,
towards = ORDER_DIRECTION.BUY,
market_type = self.market_type,
frequence = self.frequence,
broker_name = self.broker)
else:
if sellavailable.get(item, 0) > 0:
event.send_order(account_id = self.account_cookie,
amount = sellavailable[item],
amount_model = AMOUNT_MODEL.BY_AMOUNT,
time = self.current_time,
code = item,
price = 0,
order_model = ORDER_MODEL.MARKET,
towards = ORDER_DIRECTION.SELL,
market_type = self.market_type,
frequency = self.frequence,
broker_name = self.broker)
else:
event.send_order(account_id = self.account_cookie,
amount = 100,
amount_model = AMOUNT_MODEL.BY_AMOUNT,
time = self.current_time,
code = item,
price = 0,
order_model = ORDER_MODEL.MARKET,
towards = ORDER_DIRECTION.BUY,
market_type = self.market_type,
frequence = self.frequence,
broker_name = self.broker)
except:
pass

深入了解策略的组成

QA_Strategy类完全继承了QA_Account,因此,策略可以完全调用account类中的属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
self.history # dict形式,账户的历史交易
self.history_table # pd.DataFrame形式,账户的交易表
self.cash # list格式,账户的现金表
self.cash_table # pd.DataFrame形式,账户的现金流表
self.hold # 账户的最新持仓
self.latest_cash # 账户最近的一次成功交易后的现金
self.trade # 账户的每日/分钟交易表
self.daily_cash # 账户的每日结算时的现金
self.daily_hold # 账户每日结算的持仓
self.current_time # 账户的当前时间

# 账户的on_bar事件
self.on_bar(self, event)
event事件封装类数据和方法*(包含所需的行情数据/下单借口)

风险分析模块

QA_Risk是一个风险计算模块。

1
2
3
4
ACCOUNT = QA.QA_Account()
ACCOUNT.reset_assets(200000)
ACCOUNT.account_cookie = 'JCSC_EXAMPLE'
R = QA.QA_Risk(ACCOUNT, benchmark_code = '000300', benchmark_type = MARKET_TYPE.INDEX_CN)

由于账户设置的不完整,所以最后会提示
QAACCOUNT: THIS ACCOUNT DOESNOT HAVE ANY TRADE

组合视角

QA_PortfolioView是一个组合视角,只需要输入account列表,就可以生成一个视角。

1
2
3
4
5
6
7
8
9
# 如果从数据库获取:
accounts = [QA.QA_Account().from_message(x) for x in QA.QA_fetch_account()]
# 中间可以对时间等进行筛选以后再放进来,或者对于持仓股票进行筛选
accounts = [QA_Account1, QA_Account2, ...]
PV = QA.QA_PortfolioView(accounts)
# PV可以直接被加载到QA_Risk模块中:
risk_pv = QA.QA_Risk(PV)
# 分析结果:
risk_pv.message

# Python

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×