Skip to content

Commit

Permalink
pre-release 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brillliantz committed Nov 15, 2017
1 parent 6a4ff4c commit ee4c336
Show file tree
Hide file tree
Showing 43 changed files with 575 additions and 950 deletions.
2 changes: 1 addition & 1 deletion doc/backtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_alpha_strategy_dataview():
"position_ratio": 0.5,
}

gateway = DailyStockSimGateway()
gateway = AlphaTradeApi()
gateway.init_from_config(props)

context = model.Context(dataview=dv, gateway=gateway)
Expand Down
80 changes: 80 additions & 0 deletions doc/realtime.md
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
## Real Time Trading

使用**JAQS**进行回测与实盘运行的代码具有高一致性,回测满意后,只需以下几点改动,即可接入实盘/模拟撮合:

1. 使用实盘交易的交易接口:将`BacktestTradeApi`替换为`RealTimeTradeApi`
2. 使用实盘交易主程序:将`EventBacktestInstance`替换为`EventRealTimeInstance`
3. 在数据接口`RemoteDataService`中订阅所要交易品种的行情
4. 在主程序最后添加`time.sleep(9999999)`. 保证在事件循环运行中,主程序不会提前终止
5. 实时行情均为逐笔或Tick数据,即使订阅多个品种,行情数据仍会逐个到达`strategy.on_tick()`函数

这里我们以双均线策略为例,展示实盘运行的代码:

```python
props = {'symbol': 'rb1801.SHF'}
tapi = RealTimeTradeApi()
ins = EventRealTimeInstance()

tapi.use_strategy(3)

ds = RemoteDataService()
strat = DoubleMaStrategy()
pm = PortfolioManager()

context = model.Context(data_api=ds, trade_api=tapi,
instance=ins, strategy=strat, pm=pm)

ins.init_from_config(props)
ds.subscribe(props['symbol'])

ins.run()

time.sleep(999999)

```

程序运行后,行情、策略、交易会在不同的线程内运行,我们只需要在`on_tick`中进行下单,在`on_trade`, `on_order_status`中处理交易回报即可。

正常收到行情如下:
```
rb1801.SHF 20171116-211319500 (BID) 229@3889.00 | 3891.00@12 (ASK)
Fast MA = 3889.89 Slow MA = 3889.81
rb1801.SHF 20171116-211320000 (BID) 224@3889.00 | 3891.00@13 (ASK)
Fast MA = 3889.93 Slow MA = 3889.83
rb1801.SHF 20171116-211320500 (BID) 223@3889.00 | 3891.00@5 (ASK)
Fast MA = 3889.89 Slow MA = 3889.85
rb1801.SHF 20171116-211321000 (BID) 223@3889.00 | 3891.00@5 (ASK)
Fast MA = 3889.93 Slow MA = 3889.88
rb1801.SHF 20171116-211321500 (BID) 24@3890.00 | 3891.00@5 (ASK)
Fast MA = 3890.00 Slow MA = 3889.92
```

如果发生下单、成交,则会收到如下回报:

```
rb1801.SHF 20171116-211319000 (BID) 230@3889.00 | 3891.00@6 (ASK)
Fast MA = 3889.93 Slow MA = 3889.79
Strategy on order status:
New | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1
Strategy on order status:
Accepted | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1
Strategy on trade:
20171115( 211319) Buy rb1801.SHF@3.000 size = 1
Strategy on order status:
Filled | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1
Strategy on task ind:
task_id = 81116000001 | task_status = Stopped | task_algo =
task_msg =
DONE Execution Report (00'00"039):
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
|underlyer| ba_id| symbol|jzcode|side|is_filled|price|fill_price|size|fill_size|pending_size|cancels|rejects|entrusts| duration|
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
| 0|800000|rb1801.SHF| 27509|Long| Y| 3.0| 3.0000| 1| 1| 0| 0| 0| 1|00'00"033|
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
```
2 changes: 1 addition & 1 deletion doc/source/backtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ series)和信号(float series)权重
"position_ratio": 0.5,
}
gateway = DailyStockSimGateway()
gateway = AlphaTradeApi()
gateway.init_from_config(props)
context = model.Context(dataview=dv, gateway=gateway)
Expand Down
26 changes: 9 additions & 17 deletions doc/source/jaqs.trade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ jaqs\.trade\.common module
:undoc-members:
:show-inheritance:

jaqs\.trade\.gateway module
---------------------------

.. automodule:: jaqs.trade.gateway
:members:
:undoc-members:
:show-inheritance:

jaqs\.trade\.model module
-------------------------

Expand All @@ -53,26 +45,26 @@ jaqs\.trade\.portfoliomanager module
:undoc-members:
:show-inheritance:

jaqs\.trade\.pubsub module
--------------------------
jaqs\.trade\.realtime module
----------------------------

.. automodule:: jaqs.trade.pubsub
.. automodule:: jaqs.trade.realtime
:members:
:undoc-members:
:show-inheritance:

jaqs\.trade\.realinstance module
--------------------------------
jaqs\.trade\.strategy module
----------------------------

.. automodule:: jaqs.trade.realinstance
.. automodule:: jaqs.trade.strategy
:members:
:undoc-members:
:show-inheritance:

jaqs\.trade\.strategy module
----------------------------
jaqs\.trade\.tradegateway module
--------------------------------

.. automodule:: jaqs.trade.strategy
.. automodule:: jaqs.trade.tradegateway
:members:
:undoc-members:
:show-inheritance:
Expand Down
80 changes: 80 additions & 0 deletions doc/source/realtime.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
Real Time Trading
-----------------

使用\ **JAQS**\ 进行回测与实盘运行的代码具有高一致性,回测满意后,只需以下几点改动,即可接入实盘/模拟撮合:

#. 使用实盘交易的交易接口:将\ ``BacktestTradeApi``\ 替换为\ ``RealTimeTradeApi``
#. 使用实盘交易主程序:将\ ``EventBacktestInstance``\ 替换为\ ``EventRealTimeInstance``
#. 在数据接口\ ``RemoteDataService``\ 中订阅所要交易品种的行情
#. 在主程序最后添加\ ``time.sleep(9999999)``.
保证在事件循环运行中,主程序不会提前终止
#. 实时行情均为逐笔或Tick数据,即使订阅多个品种,行情数据仍会逐个到达\ ``strategy.on_tick()``\ 函数

这里我们以双均线策略为例,展示实盘运行的代码:

.. code:: python
props = {'symbol': 'rb1801.SHF'}
tapi = RealTimeTradeApi()
ins = EventRealTimeInstance()
tapi.use_strategy(3)
ds = RemoteDataService()
strat = DoubleMaStrategy()
pm = PortfolioManager()
context = model.Context(data_api=ds, trade_api=tapi,
instance=ins, strategy=strat, pm=pm)
ins.init_from_config(props)
ds.subscribe(props['symbol'])
ins.run()
time.sleep(999999)
程序运行后,行情、策略、交易会在不同的线程内运行,我们只需要在\ ``on_tick``\ 中进行下单,在\ ``on_trade``,
``on_order_status``\ 中处理交易回报即可。

正常收到行情如下:

::

rb1801.SHF 20171116-211319500 (BID) 229@3889.00 | 3891.00@12 (ASK)
Fast MA = 3889.89 Slow MA = 3889.81
rb1801.SHF 20171116-211320000 (BID) 224@3889.00 | 3891.00@13 (ASK)
Fast MA = 3889.93 Slow MA = 3889.83
rb1801.SHF 20171116-211320500 (BID) 223@3889.00 | 3891.00@5 (ASK)
Fast MA = 3889.89 Slow MA = 3889.85
rb1801.SHF 20171116-211321000 (BID) 223@3889.00 | 3891.00@5 (ASK)
Fast MA = 3889.93 Slow MA = 3889.88
rb1801.SHF 20171116-211321500 (BID) 24@3890.00 | 3891.00@5 (ASK)
Fast MA = 3890.00 Slow MA = 3889.92

如果发生下单、成交,则会收到如下回报:

::

rb1801.SHF 20171116-211319000 (BID) 230@3889.00 | 3891.00@6 (ASK)
Fast MA = 3889.93 Slow MA = 3889.79

Strategy on order status:
New | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1

Strategy on order status:
Accepted | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1

Strategy on trade:
20171115( 211319) Buy rb1801.SHF@3.000 size = 1

Strategy on order status:
Filled | 20171115( 211319) Buy rb1801.SHF@3.000 size = 1

Strategy on task ind:
task_id = 81116000001 | task_status = Stopped | task_algo =
task_msg =
DONE Execution Report (00'00"039):
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
|underlyer| ba_id| symbol|jzcode|side|is_filled|price|fill_price|size|fill_size|pending_size|cancels|rejects|entrusts| duration|
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
| 0|800000|rb1801.SHF| 27509|Long| Y| 3.0| 3.0000| 1| 1| 0| 0| 0| 1|00'00"033|
+---------+------+----------+------+----+---------+-----+----------+----+---------+------------+-------+-------+--------+---------+
2 changes: 1 addition & 1 deletion jaqs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

SOURCE_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

__version__ = '0.3.0'
__version__ = '0.4.0'
9 changes: 5 additions & 4 deletions jaqs/data/dataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import numpy as np
import pandas as pd

from jaqs.util import fileio
# from jaqs.trade.pubsub import Publisher
from jaqs.trade.event import EVENT_TYPE, Event
from jaqs.data.dataapi import DataApi
from jaqs.data import align
Expand Down Expand Up @@ -259,7 +257,10 @@ def init_from_config(self, props=None):
props = dict()

if self.data_api is not None:
self.data_api.close()
if len(props) == 0:
return
else:
self.data_api.close()

def get_from_list_of_dict(l, key, default=None):
res = None
Expand All @@ -271,7 +272,7 @@ def get_from_list_of_dict(l, key, default=None):
res = default
return res

props_default = fileio.read_json(fileio.join_relative_path('etc/data_config.json'))
props_default = jutil.read_json(jutil.join_relative_path('etc/data_config.json'))
dic_list = [props, props_default]

address = get_from_list_of_dict(dic_list, "remote.address", "")
Expand Down
13 changes: 6 additions & 7 deletions jaqs/data/dataview.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import numpy as np
import pandas as pd

import jaqs.util.fileio
from jaqs.util import dtutil
import jaqs.util as jutil
from jaqs.data.align import align
from jaqs.data.py_expression_eval import Parser

Expand Down Expand Up @@ -405,8 +404,8 @@ def init_from_config(self, props, data_api):

# initialize parameters
self.start_date = props['start_date']
self.extended_start_date_d = dtutil.shift(self.start_date, n_weeks=-8) # query more data
self.extended_start_date_q = dtutil.shift(self.start_date, n_weeks=-80)
self.extended_start_date_d = jutil.shift(self.start_date, n_weeks=-8) # query more data
self.extended_start_date_q = jutil.shift(self.start_date, n_weeks=-80)
self.end_date = props['end_date']
self.all_price = props.get('all_price', True)
self.freq = props.get('freq', 1)
Expand Down Expand Up @@ -1326,7 +1325,7 @@ def load_dataview(self, folder_path='.'):
Folder path to store hd5 file and meta data.
"""
meta_data = jaqs.util.fileio.read_json(os.path.join(folder_path, 'meta_data.json'))
meta_data = jutil.read_json(os.path.join(folder_path, 'meta_data.json'))
dic = self._load_h5(os.path.join(folder_path, 'data.hd5'))
self.data_d = dic.get('/data_d', None)
self.data_q = dic.get('/data_q', None)
Expand Down Expand Up @@ -1357,7 +1356,7 @@ def save_dataview(self, folder_path):
meta_data_to_store = {key: self.__dict__[key] for key in self.meta_data_list}

print "\nStore data..."
jaqs.util.fileio.save_json(meta_data_to_store, meta_path)
jutil.save_json(meta_data_to_store, meta_path)
self._save_h5(data_path, data_to_store)

print ("Dataview has been successfully saved to:\n"
Expand All @@ -1379,7 +1378,7 @@ def _save_h5(fp, dic):
import warnings
warnings.filterwarnings('ignore', category=pd.io.pytables.PerformanceWarning)

jaqs.util.fileio.create_dir(fp)
jutil.create_dir(fp)
h5 = pd.HDFStore(fp, complevel=9, complib='blosc')
for key, value in dic.viewitems():
h5[key] = value
Expand Down

0 comments on commit ee4c336

Please sign in to comment.