Skip to content

Commit

Permalink
format test code
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricporter committed Apr 7, 2017
1 parent 1c73257 commit 3d75a2f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 38 deletions.
2 changes: 1 addition & 1 deletion rqalpha/model/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def name(self):
return self.__name

def __repr__(self):
return "{0}:{1}".format(self.__code,self.__name)
return "{0}:{1}".format(self.__code, self.__name)


class IndustryCode(object):
Expand Down
2 changes: 0 additions & 2 deletions rqalpha/model/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
from ..utils.logger import user_system_log




class Order(object):

order_id_gen = id_gen(int(time.time()))
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ def write_csv(path, fields):
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writerow({'date_time': end_time, 'time_spend': time_spend})
else:
if 0 < len(old_test_times) < 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times))/len(old_test_times) * 1.1:
if 0 < len(old_test_times) < 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times)) / len(old_test_times) * 1.1:
print('Average time of last 5 runs:', float(sum(float(i['time_spend']) for i in old_test_times))/len(old_test_times))
print('Now time spend:', time_spend)
raise RuntimeError('Performance regresses!')
elif len(old_test_times) >= 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times[-5:]))/5 * 1.1:
elif len(old_test_times) >= 5 and time_spend > float(sum(float(i['time_spend']) for i in old_test_times[-5:])) / 5 * 1.1:
print('Average time of last 5 runs:',
float(sum(float(i['time_spend']) for i in old_test_times[-5:])) / 5)
print('Now time spend:', time_spend)
Expand Down
33 changes: 15 additions & 18 deletions tests/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#!/usr/bin/env python
# encoding: utf-8


"""
@author: Gu Xi
@contact: guxi@ricequant.com
@site: http://www.ricequant.com
@file: __init__.py.py
@time: 2017/2/17 下午1:35
"""


def func():
pass


if __name__ == '__main__':
pass
# -*- coding: utf-8 -*-
#
# Copyright 2017 Ricequant, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
36 changes: 35 additions & 1 deletion tests/api/test_api_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
#
# Copyright 2017 Ricequant, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect


def test_get_order():
from rqalpha.api import order_shares, get_order

def init(context):
context.s1 = '000001.XSHE'
context.amount = 100
Expand All @@ -19,6 +36,7 @@ def handle_bar(context, bar_dict):

def test_get_open_order():
from rqalpha.api import order_shares, get_open_orders, get_order

def init(context):
context.s1 = '000001.XSHE'
context.limitprice = 8.9
Expand All @@ -45,6 +63,7 @@ def handle_bar(context, bar_dict):

def test_cancel_order():
from rqalpha.api import order_shares, cancel_order, get_order

def init(context):
context.s1 = '000001.XSHE'
context.limitprice = 8.59
Expand All @@ -63,6 +82,7 @@ def handle_bar(context, bar_dict):

def test_update_universe():
from rqalpha.api import update_universe, history_bars

def init(context):
context.s1 = '000001.XSHE'
context.s2 = '600340.XSHG'
Expand All @@ -82,6 +102,7 @@ def handle_bar(context, bar_dict):

def test_subscribe():
from rqalpha.api import subscribe

def init(context):
context.f1 = 'AU88'
context.amount = 1
Expand All @@ -94,6 +115,7 @@ def handle_bar(context, bar_dict):

def test_unsubscribe():
from rqalpha.api import subscribe, unsubscribe

def init(context):
context.f1 = 'AU88'
context.amount = 1
Expand All @@ -107,6 +129,7 @@ def handle_bar(context, bar_dict):

def test_get_yield_curve():
from rqalpha.api import get_yield_curve

def init(context):
pass

Expand All @@ -119,6 +142,7 @@ def handle_bar(context, bar_dict):

def test_history_bars():
from rqalpha.api import history_bars

def init(context):
context.s1 = '000001.XSHE'
pass
Expand All @@ -132,6 +156,7 @@ def handle_bar(context, bar_dict):

def test_all_instruments():
from rqalpha.api import all_instruments

def init(context):
pass

Expand All @@ -151,8 +176,10 @@ def handle_bar(context, bar_dict):
assert all_instruments('Future').shape >= (3500, 16)
test_all_instruments_code_new = "".join(inspect.getsourcelines(test_all_instruments)[0])


def test_instruments_code():
from rqalpha.api import instruments

def init(context):
context.s1 = '000001.XSHE'
pass
Expand All @@ -170,6 +197,7 @@ def handle_bar(context, bar_dict):

def test_sector():
from rqalpha.api import sector

def init(context):
pass

Expand All @@ -180,6 +208,7 @@ def handle_bar(context, bar_dict):

def test_industry():
from rqalpha.api import industry, instruments

def init(context):
context.s1 = '000001.XSHE'
context.s2 = '600340.XSHG'
Expand All @@ -196,6 +225,7 @@ def handle_bar(context, bar_dict):

def test_concept():
from rqalpha.api import concept, instruments

def init(context):
context.s1 = '000002.XSHE'

Expand All @@ -210,6 +240,7 @@ def handle_bar(context, bar_dict):
def test_get_trading_dates():
from rqalpha.api import get_trading_dates
import datetime

def init(context):
pass

Expand All @@ -228,6 +259,7 @@ def handle_bar(context, bar_dict):

def test_get_previous_trading_date():
from rqalpha.api import get_previous_trading_date

def init(context):
pass

Expand All @@ -244,6 +276,7 @@ def handle_bar(context, bar_dict):

def test_get_next_trading_date():
from rqalpha.api import get_next_trading_date

def init(context):
pass

Expand All @@ -256,6 +289,7 @@ def handle_bar(context, bar_dict):
def test_get_dividend():
from rqalpha.api import get_dividend
import pandas

def init(context):
pass

Expand Down
22 changes: 21 additions & 1 deletion tests/api/test_api_future.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
#
# Copyright 2017 Ricequant, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect


def test_buy_open():
from rqalpha.api import buy_open, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE

def init(context):
context.f1 = 'P88'
context.amount = 1
Expand All @@ -27,6 +44,7 @@ def handle_bar(context, bar_dict):

def test_sell_open():
from rqalpha.api import sell_open, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE

def init(context):
context.f1 = 'P88'
context.amount = 1
Expand All @@ -50,6 +68,7 @@ def handle_bar(context, bar_dict):

def test_buy_close():
from rqalpha.api import buy_close, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE

def init(context):
context.f1 = 'P88'
context.amount = 1
Expand All @@ -73,6 +92,7 @@ def handle_bar(context, bar_dict):

def test_sell_close():
from rqalpha.api import sell_close, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE

def init(context):
context.f1 = 'P88'
context.amount = 1
Expand Down
23 changes: 21 additions & 2 deletions tests/api/test_api_stock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
#
# Copyright 2017 Ricequant, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import datetime


def test_order_shares():
Expand All @@ -14,6 +28,7 @@ def handle_bar(context, bar_dict):

def test_order_shares():
from rqalpha.api import order_shares, get_order, SIDE, LimitOrder

def init(context):
context.order_count = 0
context.s1 = "000001.XSHE"
Expand All @@ -36,6 +51,7 @@ def handle_bar(context, bar_dict):

def test_order_lots():
from rqalpha.api import order_lots, get_order, SIDE, LimitOrder

def init(context):
context.order_count = 0
context.s1 = "000001.XSHE"
Expand All @@ -58,6 +74,7 @@ def handle_bar(context, bar_dict):

def test_order_value():
from rqalpha.api import order_value, get_order, SIDE, LimitOrder

def init(context):
context.order_count = 0
context.s1 = "000001.XSHE"
Expand All @@ -78,6 +95,7 @@ def handle_bar(context, bar_dict):

def test_order_percent():
from rqalpha.api import order_percent, get_order, SIDE, LimitOrder

def init(context):
context.order_count = 0
context.s1 = "000001.XSHE"
Expand All @@ -97,6 +115,7 @@ def handle_bar(context, bar_dict):

def test_order_target_value():
from rqalpha.api import order_target_percent, get_order, SIDE, LimitOrder

def init(context):
context.order_count = 0
context.s1 = "000001.XSHE"
Expand Down
3 changes: 1 addition & 2 deletions tests/test_s_dma.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def handle_bar(context, bar_dict):
if DDD < AMA and cur_position > 0:
order_target_percent(context.s1, 0)

if (HHV(MAX(O, C), 50) / LLV(MIN(O, C), 50) < 2
and CROSS(DDD, AMA) and cur_position == 0):
if (HHV(MAX(O, C), 50) / LLV(MIN(O, C), 50) < 2 and CROSS(DDD, AMA) and cur_position == 0):
order_target_percent(context.s1, 1)


Expand Down
1 change: 0 additions & 1 deletion tests/test_s_dual_thrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def handle_bar(context, bar_dict):
# 使用第n-1日的收盘价作为当前价
current_price = Close[2]


Range = max((HH - LC), (HC - LL))
K1 = 0.9
BuyLine = Openprice + K1 * Range
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def init(context):
scheduler.run_weekly(rebalance, 1, time_rule = market_open(0, 0))
scheduler.run_weekly(rebalance, 1, time_rule=market_open(0, 0))


def rebalance(context, bar_dict):
Expand Down
Loading

0 comments on commit 3d75a2f

Please sign in to comment.