【QuantOS】jaqs实例代码(可以使用版本)
生活随笔
收集整理的這篇文章主要介紹了
【QuantOS】jaqs实例代码(可以使用版本)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡述
jaqs給的demo,其實一開始是沒辦法用的(在windows條件下)。
還有一些小細節(jié)。
- data_config和trade_config的username 和 password,寫的其實是手機號和命令。
- 命令是在官網(wǎng)上登錄之后,把鼠標移到右上角 就可以看到一個命令。然后點擊。可以看到這個了。
代碼
只要把代碼中的賬號和密碼都輸入之后,就可以運行了,運行結(jié)束之后,找到report.html用瀏覽器打開就好了。
""" A very first example of AlphaStrategy back-test:Market value weight among UNIVERSE.Benchmark is HS300."""from __future__ import print_function, unicode_literals, division, absolute_importimport osfrom jaqs.data import RemoteDataService, DataViewimport jaqs.util as jutilfrom jaqs.trade import model from jaqs.trade import (AlphaStrategy, AlphaBacktestInstance, AlphaTradeApi,PortfolioManager, AlphaLiveTradeInstance, RealTimeTradeApi) import jaqs.trade.analyze as anadata_config = {"remote.data.address": "tcp://data.quantos.org:8910","remote.data.username": "telephoneNumber","remote.data.password": "token" } trade_config = {"remote.trade.address": "tcp://gw.quantos.org:8901","remote.trade.username": "telephoneNumber","remote.trade.password": "token" }# # Data files are stored in this folder: # dataview_store_folder = '../../output/simplest/dataview' # # # Back-test and analysis results are stored here # backtest_result_folder = '../../output/simplest' ## change 1 # Data files are stored in this folder: dataview_store_folder = './output/simplest/dataview'# Back-test and analysis results are stored here backtest_result_folder = './output/simplest'def checkdir(path):if not os.path.exists(path):os.makedirs(path)checkdir(dataview_store_folder) checkdir(backtest_result_folder) # END change 1UNIVERSE = '000807.SH'def save_data():"""This function fetches data from remote server and stores them locally.Then we can use local data to do back-test."""dataview_props = {'start_date': 20170101, # Start and end date of back-test'end_date': 20171030,'universe': UNIVERSE, # Investment universe and performance benchmark'benchmark': '000300.SH','fields': 'total_mv,turnover', # Data fields that we need'freq': 1 # freq = 1 means we use daily data. Please do not change this.}# RemoteDataService communicates with a remote server to fetch datads = RemoteDataService()# Use username and password in data_config to loginds.init_from_config(data_config)# DataView utilizes RemoteDataService to get various data and store themdv = DataView()dv.init_from_config(dataview_props, ds)dv.prepare_data()dv.save_dataview(folder_path=dataview_store_folder)def do_backtest():# Load local data file that we just stored.dv = DataView()dv.load_dataview(folder_path=dataview_store_folder)backtest_props = {"start_date": dv.start_date, # start and end date of back-test"end_date": dv.end_date,"period": "month", # re-balance period length"benchmark": dv.benchmark, # benchmark and universe"universe": dv.universe,"init_balance": 1e8, # Amount of money at the start of back-test"position_ratio": 1.0, # Amount of money at the start of back-test}backtest_props.update(data_config)backtest_props.update(trade_config)# Create model context using AlphaTradeApi, AlphaStrategy, PortfolioManager and AlphaBacktestInstance.# We can store anything, e.g., public variables in context.trade_api = AlphaTradeApi()strategy = AlphaStrategy(pc_method='market_value_weight')pm = PortfolioManager()bt = AlphaBacktestInstance()context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm)bt.init_from_config(backtest_props)bt.run_alpha()# After finishing back-test, we save trade results into a folderbt.save_results(folder_path=backtest_result_folder)def do_livetrade():dv = DataView()dv.load_dataview(folder_path=dataview_store_folder)props = {"period": "day","strategy_no": 1044,"init_balance": 1e6}props.update(data_config)props.update(trade_config)strategy = AlphaStrategy(pc_method='market_value_weight')pm = PortfolioManager()bt = AlphaLiveTradeInstance()trade_api = RealTimeTradeApi(props)ds = RemoteDataService()context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm, data_api=ds)bt.init_from_config(props)bt.run_alpha()goal_positions = strategy.goal_positionsprint("Length of goal positions:", len(goal_positions))task_id, msg = trade_api.goal_portfolio(goal_positions)print(task_id, msg)def analyze_backtest_results():# Analyzer help us calculate various trade statistics according to trade results.# All the calculation results will be stored as its members.ta = ana.AlphaAnalyzer()dv = DataView()dv.load_dataview(folder_path=dataview_store_folder)ta.initialize(dataview=dv, file_folder=backtest_result_folder)ta.do_analyze(result_dir=backtest_result_folder,selected_sec=list(ta.universe)[:3])if __name__ == "__main__":is_backtest = Trueif is_backtest:save_data()do_backtest()analyze_backtest_results()else:save_data()do_livetrade()總結(jié)
以上是生活随笔為你收集整理的【QuantOS】jaqs实例代码(可以使用版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow安装【2018/12
- 下一篇: TensorFlow下载文件到当前目录