美股量化交易 - pipeline 使用 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
investguider
V2EX    程序员

美股量化交易 - pipeline 使用

  •  
  •   investguider 2018-10-22 14:15:24 +08:00 2227 次点击
    这是一个创建于 2550 天前的主题,其中的信息可能已经有所发展或是发生改变。

    摘要: 最近在开始学习美股量化交易, 本篇着重介绍一下 pipeline 的使用; 如果你对量化交易感兴趣, 可以点击 [原文] 查看更多内容。

    策略思路

    • 选择成交量最大的 200 只股票
    • 去除其中价格小于 5 的股票,取市值最小的 10 只进行交易
    • 每月调仓一次,使用个股的市值作为调仓的权重

    运行环境

    本代码运行 老虎证券 pc 端 量化交易环境, 运行前 可以注册老虎证券 使用模拟账号 进行运行量化代码。

    代码

    import pandas as pd from tquant.pipeline import Pipeline from tquant.pipeline.data import Fundamentals, USEquityPricing # 策略初始化方法, 只在开始执行回测时运行一次 def initialize(context): print('初始化策略') # 使用 标普 500 ETF 作为策略基准 set_benchmark(symbol('SPY')) # 设置策略的佣金规则:每股 0.01 美元, 最低 1 美元 set_commission(commission.PerShare(cost=0.01, min_trade_cost=1)) # 定义两个时区 context.remote_time_zOne= 'US/Eastern' context.local_time_zome = 'Asia/Chongqing' # 定时运行方法: # 设置『 month_start 』这个方法在每个月初的第一个交易日开盘时执行一次。 schedule_function(month_start, date_rule=date_rules.month_start(), time_rule=time_rules.market_open()) # 初始化 pipeline 对象 pipe = Pipeline() # 向 pipeline 对象中添加两列数据:收盘价和总股本 pipe.add(USEquityPricing.close.latest, 'close') pipe.add(Fundamentals.shares.latest, 'shares') # 添加一个筛选器:使用成交量最大的 200 只股票 screener = (USEquityPricing.volume.latest.top(200)) pipe.set_screen(screener) # 注册 pipeline 对象 attach_pipeline(pipe, 'universe') # 实现之前定义的 month_start 方法 def month_start(context, data): # 获取 pipeline 的数据, # universe 是一个 dataframe,index 是股票池,column 是数据列 # index 每次计算时,交易量最大的 200 只股票 universe = pipeline_output('universe') # 计算市值,市值=总股本*收盘价 universe['market_cap'] = universe['close'] * universe['shares'] # 去除其中收盘价小于 5 的股票, 并取市值最小的 10 只股票 target_stock = universe[universe['close']>5].sort_values('market_cap', ascending=True) ['market_cap'][:10] # 计算股票的目标调仓权重 target_weight = target_stock/target_stock.sum() # 打印结果 print('当前的日期是:', context.get_datetime(context.local_time_zome)) print('目标调仓权重为:', target_weight) # 调仓 reblance(context, target_weight) def reblance(context, target_weight): # 取当前持仓的所有股票 current_position = context.portfolio.positions.keys() # 计算当前持仓的股票与目标调仓股票的差集 sell = set(current_position) - set(target_weight.index) # 卖出不在目标调仓中的股票 for code in sell: order_target_percent(code, 0) # 按照目标权重, 使用市价单下单 for code in target_weight.keys(): order_target_percent(code, target_weight[code]) log.info('调仓完成') 

    运行结果如下:

    美股、港股量化交易

    仓位调动日志明细

    初始化策略 当前的日期是: 2018-01-03 05:00:00+08:00 目标调仓权重为: Equity(3407 [HMNY]) 0.000142 Equity(8742 [UNG]) 0.035254 Equity(10314 [RIOT]) 0.076770 Equity(5626 [PGNX]) 0.099965 Equity(7738 [WATT]) 0.103301 Equity(2948 [GCAP]) 0.107777 Equity(2848 [FTR]) 0.126808 Equity(8262 [ATRA]) 0.132405 Equity(4587 [MDR]) 0.148945 Equity(3778 [INSY]) 0.168632 Name: market_cap, dtype: float64 2018-01-02 16:00:00 reblance:72 INFO 调仓完成 当前的日期是: 2018-02-02 05:00:00+028:00 目标调仓权重为: Equity(8282 [BLCM]) 0.014110 Equity(8742 [UNG]) 0.042728 Equity(4587 [MDR]) 0.058059 Equity(3728 [IMGN]) 0.084797 Equity(235 [AKS]) 0.111286 Equity(2507 [EXTR]) 0.119066 Equity(3141 [GPOR]) 0.130049 Equity(1338 [CLF]) 0.141863 Equity(8743 [USO]) 0.146597 Equity(1761 [DBC]) 0.151445 Name: market_cap, dtype: float64 2018-02-01 16:00:00 reblance:72 INFO 调仓完成 当前的日期是: 2018-03-02 05:00:00+08:00 目标调仓权重为: Equity(5640 [PHH]) 0.029012 Equity(2100 [ECYT]) 0.033073 Equity(2848 [FTR]) 0.046434 Equity(1720 [CYH]) 0.048309 Equity(2266 [ENDP]) 0.118623 Equity(4763 [MNK]) 0.121333 Equity(3506 [HTZ]) 0.128298 Equity(235 [AKS]) 0.137047 Equity(8743 [USO]) 0.165894 Equity(5000 [NBR]) 0.171978 Name: market_cap, dtype: float64 2018-03-01 16:00:00 reblance:72 INFO 调仓完成 当前的日期是: 2018-04-03 04:00:00+08:00 目标调仓权重为: Equity(4587 [MDR]) 0.034007 Equity(788 [BKD]) 0.073548 Equity(3086 [GME]) 0.075315 Equity(10703 [DBX]) 0.098588 Equity(83 [ACXM]) 0.104958 Equity(2623 [FFBC]) 0.108441 Equity(1338 [CLF]) 0.121434 Equity(8743 [USO]) 0.122429 Equity(5000 [NBR]) 0.129578 Equity(7961 [XOP]) 0.131701 Name: market_cap, dtype: float64 2018-04-02 16:00:00 reblance:72 INFO 调仓完成 当前的日期是: 2018-05-02 04:00:00+08:00 目标调仓权重为: Equity(4587 [MDR]) 0.030825 Equity(8496 [FIT]) 0.057020 Equity(7852 [WPG]) 0.058974 Equity(5460 [P]) 0.070114 Equity(8743 [USO]) 0.107669 Equity(1338 [CLF]) 0.108268 Equity(2384 [ESV]) 0.121031 Equity(7961 [XOP]) 0.133455 Equity(6060 [RDN]) 0.151056 Equity(9671 [UAA]) 0.161588 Name: market_cap, dtype: float64 2018-05-01 16:00:00 reblance:72 INFO 调仓完成 当前的日期是: 2018-06-02 04:00:00+08:00 目标调仓权重为: Equity(8322 [VKTX]) 0.021032 Equity(8743 [USO]) 0.077514 Equity(6481 [SFUN]) 0.093043 Equity(1338 [CLF]) 0.104112 Equity(3062 [GLNG]) 0.108587 Equity(5000 [NBR]) 0.108815 Equity(2384 [ESV]) 0.117255 Equity(1886 [DKS]) 0.117884 Equity(1349 [CLNY]) 0.120782 Equity(7961 [XOP]) 0.130976 Name: market_cap, dtype: float64 2018-06-01 16:00:00 reblance:72 INFO 调仓完成 
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5899 人在线   最高记录 6679       Select anguage
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 02:20 PVG 10:20 LAX 19:20 JFK 22:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86