Skip to content

Commit

Permalink
Support Python2/3; add AlphaLiveTradeInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
brillliantz committed Nov 30, 2017
2 parents 616a873 + 42ff7b6 commit 4dfc076
Show file tree
Hide file tree
Showing 73 changed files with 1,767 additions and 1,399 deletions.
4 changes: 2 additions & 2 deletions config/data_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"remote.data.address": "tcp://data.tushare.org:8910",
"remote.data.username": "Your Username",
"remote.data.password": "Your Password"
"remote.data.username": "YourTelNumber",
"remote.data.password": "YourToken"
}
4 changes: 2 additions & 2 deletions config/trade_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"remote.trade.address": "tcp://gw.quantos.org:8901",
"remote.trade.username": "Your Username",
"remote.trade.password": "Your Password"
"remote.trade.username": "YourTelNumber",
"remote.trade.password": "YourToken"
}
4 changes: 2 additions & 2 deletions doc/realtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
使用**JAQS**进行回测与实盘运行的代码具有高一致性,回测满意后,只需以下几点改动,即可接入实盘/模拟撮合:

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

tapi.use_strategy(3)

Expand Down
8 changes: 0 additions & 8 deletions doc/source/jaqs.data.dataapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ jaqs\.data\.dataapi\.data\_api module
:undoc-members:
:show-inheritance:

jaqs\.data\.dataapi\.jrpc module
--------------------------------

.. automodule:: jaqs.data.dataapi.jrpc
:members:
:undoc-members:
:show-inheritance:

jaqs\.data\.dataapi\.jrpc\_py module
------------------------------------

Expand Down
16 changes: 8 additions & 8 deletions doc/source/jaqs.trade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ jaqs\.trade\.common module
:undoc-members:
:show-inheritance:

jaqs\.trade\.livetrade module
-----------------------------

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

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

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions doc/source/realtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
使用\ **JAQS**\ 进行回测与实盘运行的代码具有高一致性,回测满意后,只需以下几点改动,即可接入实盘/模拟撮合:

#. 使用实盘交易的交易接口:将\ ``BacktestTradeApi``\ 替换为\ ``RealTimeTradeApi``
#. 使用实盘交易主程序:将\ ``EventBacktestInstance``\ 替换为\ ``EventRealTimeInstance``
#. 使用实盘交易主程序:将\ ``EventBacktestInstance``\ 替换为\ ``EventLiveTradeInstance``
#. 在数据接口\ ``RemoteDataService``\ 中订阅所要交易品种的行情
#. 在主程序最后添加\ ``time.sleep(9999999)``.
保证在事件循环运行中,主程序不会提前终止
Expand All @@ -16,7 +16,7 @@
props = {'symbol': 'rb1801.SHF'}
tapi = RealTimeTradeApi()
ins = EventRealTimeInstance()
ins = EventLiveTradeInstance()
tapi.use_strategy(3)
Expand Down
4 changes: 3 additions & 1 deletion example/alpha/FamaFrench.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
start_date 20140101
end_date 20170301
"""
from __future__ import print_function
from __future__ import absolute_import
import time

import pandas as pd
Expand Down Expand Up @@ -126,4 +128,4 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))
4 changes: 3 additions & 1 deletion example/alpha/Graham.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
start_date 20140101
end_date 20170301
"""
from __future__ import print_function
from __future__ import absolute_import
import time

import numpy as np
Expand Down Expand Up @@ -158,4 +160,4 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))
4 changes: 3 additions & 1 deletion example/alpha/ICCombine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
2. do not care about them when construct portfolio
3. subtract market value and re-normalize weights (positions) after (daily) market open, before sending orders
"""
from __future__ import print_function
from __future__ import absolute_import
import time
import numpy as np
import numpy.linalg as nlg
Expand Down Expand Up @@ -294,5 +296,5 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))

1 change: 1 addition & 0 deletions example/alpha/config_path.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: UTF-8

from __future__ import print_function
import os


Expand Down
4 changes: 3 additions & 1 deletion example/alpha/select_stocks_industry_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
start_date 20170101
end_date 20171001
"""
from __future__ import print_function
from __future__ import absolute_import
import time

import pandas as pd
Expand Down Expand Up @@ -131,4 +133,4 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))
4 changes: 3 additions & 1 deletion example/alpha/select_stocks_pe_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
start_date 20170101
end_date 20171001
"""
from __future__ import print_function
from __future__ import absolute_import
import time

from jaqs.data import RemoteDataService
Expand Down Expand Up @@ -117,5 +119,5 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))

4 changes: 3 additions & 1 deletion example/alpha/single_factor_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
end_date 20171001
"""
from __future__ import print_function
from __future__ import absolute_import
import time

from jaqs.data import RemoteDataService
Expand Down Expand Up @@ -114,4 +116,4 @@ def test_backtest_analyze():
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))
46 changes: 31 additions & 15 deletions example/alpha/wine_industry_momentum.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# -*- encoding: utf-8 -*-
"""
Both backtest and live trading are included.
"""

from __future__ import print_function
from __future__ import absolute_import
import time

from jaqs.data import RemoteDataService
from jaqs.trade import AlphaBacktestInstance
from jaqs.trade import AlphaBacktestInstance, AlphaLiveTradeInstance

import jaqs.util as jutil
from jaqs.trade import PortfolioManager
import jaqs.trade.analyze as ana
from jaqs.trade import AlphaStrategy
from jaqs.trade import AlphaTradeApi
from jaqs.trade import AlphaTradeApi, RealTimeTradeApi
from jaqs.trade import model
from jaqs.data import DataView

Expand All @@ -21,14 +26,15 @@
backtest_result_dir_path = '../../output/wine_industry_momentum'

BENCHMARK = '399997.SZ'
is_backtest = False


def test_save_dataview():
ds = RemoteDataService()
ds.init_from_config(data_config)
dv = DataView()

props = {'start_date': 20170101, 'end_date': 20171001, 'universe': BENCHMARK,
props = {'start_date': 20170901, 'end_date': 20171129, 'universe': BENCHMARK,
'fields': 'close,volume,sw1',
'freq': 1}

Expand Down Expand Up @@ -63,27 +69,37 @@ def test_alpha_strategy_dataview():

"init_balance": 1e8,
"position_ratio": 1.0,
"strategy_no": 44
}
props.update(data_config)
props.update(trade_config)

trade_api = AlphaTradeApi()

stock_selector = model.StockSelector()
stock_selector.add_filter(name='rank_ret_top10', func=my_selector)

strategy = AlphaStrategy(stock_selector=stock_selector, pc_method='equal_weight')
pm = PortfolioManager()

bt = AlphaBacktestInstance()
if is_backtest:
bt = AlphaBacktestInstance()
trade_api = AlphaTradeApi()
ds = None
else:
bt = AlphaLiveTradeInstance()
trade_api = RealTimeTradeApi(props)
ds = RemoteDataService()

context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm)
context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm, data_api=ds)
stock_selector.register_context(context)

bt.init_from_config(props)
bt.run_alpha()

bt.save_results(folder_path=backtest_result_dir_path)

if is_backtest:
bt.save_results(folder_path=backtest_result_dir_path)
else:
goal_positions = strategy.goal_positions
print(goal_positions)


def test_backtest_analyze():
Expand All @@ -93,16 +109,16 @@ def test_backtest_analyze():

ta.initialize(dataview=dv, file_folder=backtest_result_dir_path)

ta.do_analyze(result_dir=backtest_result_dir_path, selected_sec=list(ta.universe)[:3],
brinson_group='sw1')
ta.do_analyze(result_dir=backtest_result_dir_path, selected_sec=list(ta.universe)[:3], brinson_group='sw1')


if __name__ == "__main__":
t_start = time.time()

# test_save_dataview()
# test_alpha_strategy_dataview()
test_backtest_analyze()
test_save_dataview()
test_alpha_strategy_dataview()
if is_backtest:
test_backtest_analyze()

t3 = time.time() - t_start
print "\n\n\nTime lapsed in total: {:.1f}".format(t3)
print("\n\n\nTime lapsed in total: {:.1f}".format(t3))
12 changes: 7 additions & 5 deletions example/eventdriven/CalendarSpread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# encoding: utf-8

from __future__ import print_function
from __future__ import absolute_import
import numpy as np
import statsmodels.api as sm

Expand Down Expand Up @@ -97,29 +99,29 @@ def on_quote(self, quote):

def on_trade(self, ind):
if self.output:
print "\nStrategy on trade: "
print("\nStrategy on trade: ")
print(ind)
print(self.ctx.pm.get_trade_stat(ind.symbol))
self.pos = self.ctx.pm.get_pos(ind.symbol)

def on_order_status(self, ind):
if self.output:
print "\nStrategy on order status: "
print("\nStrategy on order status: ")
print(ind)

def on_task_rsp(self, rsp):
if self.output:
print "\nStrategy on task rsp: "
print("\nStrategy on task rsp: ")
print(rsp)

def on_order_rsp(self, rsp):
if self.output:
print "\nStrategy on order rsp: "
print("\nStrategy on order rsp: ")
print(rsp)

def on_task_status(self, ind):
if self.output:
print "\nStrategy on task ind: "
print("\nStrategy on task ind: ")
print(ind)


Expand Down
12 changes: 7 additions & 5 deletions example/eventdriven/DoubleMA.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# encoding: utf-8

from __future__ import print_function
from __future__ import absolute_import
import time

import numpy as np
Expand All @@ -9,7 +11,7 @@
from jaqs.data import RemoteDataService
from jaqs.data.basic import Bar, Quote
from jaqs.trade import model
from jaqs.trade import EventRealTimeInstance
from jaqs.trade import EventLiveTradeInstance
from jaqs.trade import EventBacktestInstance
from jaqs.trade import RealTimeTradeApi, BacktestTradeApi
from jaqs.trade import PortfolioManager
Expand Down Expand Up @@ -118,18 +120,18 @@ def on_quote(self, quote_dic):
self.on_tick(quote)

def on_trade(self, ind):
print "\nStrategy on trade: "
print("\nStrategy on trade: ")
print(ind)
self.pos = self.ctx.pm.get_pos(self.symbol)

def on_order_status(self, ind):
if self.output:
print "\nStrategy on order status: "
print("\nStrategy on order status: ")
print(ind)

def on_task_status(self, ind):
if self.output:
print "\nStrategy on task ind: "
print("\nStrategy on task ind: ")
print(ind)


Expand All @@ -148,7 +150,7 @@ def run_strategy():
props = {'symbol': 'rb1801.SHF',
'strategy.no': 46}
tapi = RealTimeTradeApi(trade_config)
ins = EventRealTimeInstance()
ins = EventLiveTradeInstance()

props.update(data_config)
props.update(trade_config)
Expand Down
Loading

0 comments on commit 4dfc076

Please sign in to comment.