Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
yutiansut authored Jun 4, 2018
2 parents 4ba63db + c334339 commit 050cda2
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 386 deletions.
94 changes: 48 additions & 46 deletions QUANTAXIS_Test/QAARP_Test/QAAccount_Test.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import unittest;
import random as rnd
import unittest

from QUANTAXIS.QAUtil import (QADate)
from QUANTAXIS.QAUtil import (QARandom)
import QUANTAXIS as QA
import random as rnd
from QUANTAXIS.QAUtil import QADate, QARandom

class Test_QAAccount(unittest.TestCase):

class Test_QAAccount(unittest.TestCase):

def test_BuyAndSell(self):
# 测试买卖事件
#测试流程,
#随机选中一组股票
#随机选中几个交易日 (只测试当前日期)
#随机卖出
#随机买入
#发送订单
#从队列中取回订单
#比较
# 测试流程,
# 随机选中一组股票
# 随机选中几个交易日 (只测试当前日期)
# 随机卖出
# 随机买入
# 发送订单
# 从队列中取回订单
# 比较
codeCount = 10
codeList = QARandom.QA_util_random_with_zh_stock_code(codeCount)
#print(codeList)
# print(codeList)
# { code: QA_DataStruct_Stock_day }
# 如果代码不存在 则 code:None
codeForDate = {}
for codeIndex in range(0,codeCount):
aStockDataStructDay = QA.QA_fetch_stock_day_adv(codeList[codeIndex])
for codeIndex in range(0, codeCount):
aStockDataStructDay = QA.QA_fetch_stock_day_adv(
codeList[codeIndex])
codeForDate[codeList[codeIndex]] = aStockDataStructDay
#print(codeForDate)
# print(codeForDate)

str = QADate.QA_util_today_str()
timestamp = QADate.QA_util_to_datetime(str)
Expand All @@ -37,34 +37,34 @@ def test_BuyAndSell(self):
orderList = []
for a_stock_code in codeForDate.keys():
anOrder = account.send_order(code=a_stock_code,
amount=100,
time = timestamp,
towards= 1,
price= 8.8,
order_model= QA.ORDER_MODEL.LIMIT,
amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)
amount=100,
time=timestamp,
towards=1,
price=8.8,
order_model=QA.ORDER_MODEL.LIMIT,
amount_model=QA.AMOUNT_MODEL.BY_AMOUNT)
orderList.append(anOrder)

orderQueue = account.get_orders()
print(orderQueue)

self.assertEqual(len(orderQueue.queue_df) , codeCount)
self.assertEqual(len(orderQueue.queue_df), codeCount)

orderList2 = []
print(orderQueue.queue_df)
for orderId in orderQueue.queue_df.index:
anOrder2 = orderQueue.query_order(orderId)
orderList2.append(anOrder2)

self.assertEqual( len(orderList),len(orderList2) )
self.assertEqual(len(orderList), len(orderList2))

orderCount = len(orderList)
for i in range(orderCount):

order_01 = orderList[i]
order_02 = orderList2[i]

#总是不正确
# 总是不正确
#b = order_01 is order_02
#b = order_01 == order_02
#b = (order_01.__dict__ == order_02.__dict__)
Expand All @@ -78,53 +78,54 @@ def test_BuyAndSell(self):
print(v1)
print(v2)
self.fail("订单数据不正确")
#todo 继续研究为何不正确
# todo 继续研究为何不正确
#self.assertEqual(order_01, order_02)
pass

def n0_test_QAAccount_class(self):

#测试流程 获取 美康生物 300439 的走势 从 2017年10月01日开始 到 2018年 4月30日
# 测试流程 获取 美康生物 300439 的走势 从 2017年10月01日开始 到 2018年 4月30日
#
test_stock_code = '300439'
stock_price_list = QA.QA_fetch_stock_day(code = test_stock_code, start = '2017-10-01', end = '2018-04-30')
stock_price_list = QA.QA_fetch_stock_day(
code=test_stock_code, start='2017-10-01', end='2018-04-30')
#buy_list = []

print(stock_price_list)
print("---------开始测试买入------------")
#price_list_size = stock_price_list.size

#生成随机数种子
# 生成随机数种子
rnd.seed(QA.QA_util_time_now().timestamp())

Account = QA.QA_Account()
B = QA.QA_BacktestBroker()

for aday_stock_price in stock_price_list:
stock_code = aday_stock_price[0]
price_open = aday_stock_price[1]
price_high = aday_stock_price[2]
price_low = aday_stock_price[3]
stock_code = aday_stock_price[0]
price_open = aday_stock_price[1]
price_high = aday_stock_price[2]
price_low = aday_stock_price[3]
price_close = aday_stock_price[4]
stock_volume = aday_stock_price[5]
stock_turn = aday_stock_price[6]
stock_turn = aday_stock_price[6]
stock_timestamp = aday_stock_price[7]

#print(type(stock_timestamp))
# print(type(stock_timestamp))

dt = QADate.QA_util_pands_timestamp_to_datetime(stock_timestamp);
date_time_to_buy = QADate.QA_util_datetime_to_strdatetime(dt);
dt = QADate.QA_util_pands_timestamp_to_datetime(stock_timestamp)
date_time_to_buy = QADate.QA_util_datetime_to_strdatetime(dt)

dt = QADate.QA_util_to_datetime(date_time_to_buy)

if price_low != price_high:
price_diff = (price_high - price_low);
price_diff = (price_high - price_low)
self.assertTrue(price_diff >= 0, "最高价一定是非负数")

rand_value = rnd.random()
buy_price = price_low + price_diff * rand_value
print("{} 申报买入的成交价格 {}".format(dt, buy_price))
#每天随机在开盘价格和收盘价格直接买入
# 每天随机在开盘价格和收盘价格直接买入
Order = Account.send_order(code=test_stock_code,
price=buy_price,
amount=100,
Expand All @@ -134,21 +135,22 @@ def n0_test_QAAccount_class(self):
order_model=QA.ORDER_MODEL.LIMIT,
amount_model=QA.AMOUNT_MODEL.BY_AMOUNT
)
print('ORDER的占用资金: {}'.format((Order.amount * Order.price) * (1 + Account.commission_coeff)))
print('账户剩余资金 :{}'.format(Account.cash_available)) ## ??
print('ORDER的占用资金: {}'.format(
(Order.amount * Order.price) * (1 + Account.commission_coeff)))
print('账户剩余资金 :{}'.format(Account.cash_available)) # ??
cash_available = Account.cash_available
print(cash_available)

rec_mes=B.receive_order(QA.QA_Event(order=Order))
rec_mes = B.receive_order(QA.QA_Event(order=Order))
print(rec_mes)
Account.receive_deal(rec_mes)

knock_down_price = rec_mes['body']['order']['price'];
bid_price = round(buy_price, 2);
knock_down_price = rec_mes['body']['order']['price']
bid_price = round(buy_price, 2)

print("{} 获取订单的价格 {}".format(dt, buy_price))

self.assertEqual(knock_down_price, bid_price)

print('账户的可用资金 {}'.format(Account.cash_available))
print('-----------------------------------------------')
print('-----------------------------------------------')
8 changes: 4 additions & 4 deletions QUANTAXIS_Test/QAARP_Test/QAUser_Test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import QUANTAXIS as QA
import unittest

import QUANTAXIS as QA


class Test_QAUser(unittest.TestCase):
def testQAUser(self):
Expand All @@ -21,9 +22,8 @@ def testQAUser(self):
p2 = user.get_portfolio(portfolio1)
ac2 = user.get_portfolio(portfolio1).get_account(ac1)

self.assertEqual(p2,p)
self.assertEqual(ac1,ac2)
self.assertEqual(p2, p)
self.assertEqual(ac1, ac2)

except:
print("Error")

8 changes: 5 additions & 3 deletions QUANTAXIS_Test/QAAnalysis_Test/QAAnalysis_dataframe_Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import QUANTAXIS as QA


class Test_QAAnalysis_dataframe(unittest.TestCase):
def test_QAAnalysis_stock(self):

data = QA.QA_fetch_stock_day_adv('600066', '2013-12-01', '2017-10-01') # [可选to_qfq(),to_hfq()]
data = QA.QA_fetch_stock_day_adv(
'600066', '2013-12-01', '2017-10-01') # [可选to_qfq(),to_hfq()]
s = QA.QAAnalysis_stock(data)
# s 的属性是( < QAAnalysis_Stock > )

Expand All @@ -23,7 +25,7 @@ def test_QAAnalysis_stock(self):
s.max # price的最大值
s.min # price的最小值
s.mad # price的平均绝对偏差
#s.mode # price的众数(没啥用)
# s.mode # price的众数(没啥用)
s.price_diff # price的一阶差分
s.variance # price的方差
s.pvariance # price的样本方差
Expand All @@ -35,4 +37,4 @@ def test_QAAnalysis_stock(self):
s.kurtosis # price的偏度 (3阶中心距)
s.pct_change # price的百分比变化序列

s.add_func(QA.QA_indicator_CCI) # 指标计算, 和DataStruct用法一致
s.add_func(QA.QA_indicator_CCI) # 指标计算, 和DataStruct用法一致
21 changes: 11 additions & 10 deletions QUANTAXIS_Test/QABacktest_Test/QABacktestSimple_Test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import unittest

import QUANTAXIS as QA

import numpy as np
import pandas as pd

import QUANTAXIS as QA


class QABacktestSimple_Test(unittest.TestCase):

# define the MACD strategy
def MACD_JCSC(self,dataframe, SHORT=12, LONG=26, M=9):
def MACD_JCSC(self, dataframe, SHORT=12, LONG=26, M=9):
"""
1.DIF向上突破DEA,买入信号参考。
2.DIF向下跌破DEA,卖出信号参考。
Expand All @@ -26,7 +26,7 @@ def MACD_JCSC(self,dataframe, SHORT=12, LONG=26, M=9):
{'DIFF': DIFF, 'DEA': DEA, 'MACD': MACD, 'CROSS_JC': CROSS_JC, 'CROSS_SC': CROSS_SC, 'ZERO': ZERO})

def setUp(self):
#准备数据
# 准备数据

# create account
self.Account = QA.QA_Account()
Expand All @@ -44,8 +44,8 @@ def setUp(self):
self.ind = self.data.add_func(self.MACD_JCSC)
# ind.xs('000001',level=1)['2018-01'].plot()

self.data_forbacktest = self.data.select_time('2018-01-01', '2018-05-20')

self.data_forbacktest = self.data.select_time(
'2018-01-01', '2018-05-20')

def tearDown(self):

Expand All @@ -54,7 +54,7 @@ def tearDown(self):
print(self.Account.daily_hold)

# create Risk analysis
Risk = QA.QA_Risk( self.Account)
Risk = QA.QA_Risk(self.Account)
print(Risk.message)
print(Risk.assets)
Risk.plot_assets_curve()
Expand All @@ -67,7 +67,8 @@ def tearDown(self):
self.Account.save()
Risk.save()

account_info = QA.QA_fetch_account({'account_cookie': 'user_admin_macd'})
account_info = QA.QA_fetch_account(
{'account_cookie': 'user_admin_macd'})
account = QA.QA_Account().from_message(account_info[0])
print(account)

Expand All @@ -93,7 +94,8 @@ def test_simpleQABacktest(self):
order = self.Account.send_order(
code=item.data.code[0],
time=item.data.date[0],
amount=self.Account.sell_available.get(item.code[0], 0),
amount=self.Account.sell_available.get(
item.code[0], 0),
towards=QA.ORDER_DIRECTION.SELL,
price=0,
order_model=QA.ORDER_MODEL.MARKET,
Expand All @@ -102,4 +104,3 @@ def test_simpleQABacktest(self):
self.Account.receive_deal(self.Broker.receive_order(
QA.QA_Event(order=order, market_data=item)))
self.Account.settle()

Loading

0 comments on commit 050cda2

Please sign in to comment.