1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 多因子策略_单因子选股轮动测试

多因子策略_单因子选股轮动测试

时间:2023-05-08 16:01:27

相关推荐

多因子策略_单因子选股轮动测试

多因子策略

因子

因子就是在选择一只股票时,能够帮助你做出决定的一个因素

选取因子

选取股票池

get_history_constituents(index, start_date=None, end_date=None)

函数返回的数据每月发布一次,故返回的数据是月频数据,trade_date为各月最后一天

from gm.api import *symbol_list = get_history_constituents(index="SHSE.000016", start_date='-07-10')

import pprintpprint.pprint(symbol_list[0])

{'constituents': {'SHSE.600000': 3.3299999237060547,'SHSE.600016': 4.510000228881836,'SHSE.600028': 1.4199999570846558,'SHSE.600029': 0.6299999952316284,'SHSE.600030': 3.0299999713897705,'SHSE.600036': 5.849999904632568,'SHSE.600048': 1.659999966621399,'SHSE.600050': 1.399999976158142,'SHSE.600100': 0.550000011920929,'SHSE.600104': 2.3399999141693115,'SHSE.600111': 0.8999999761581421,'SHSE.600340': 0.8100000023841858,'SHSE.600485': 0.5699999928474426,'SHSE.600518': 1.3700000047683716,'SHSE.600519': 5.360000133514404,'SHSE.600547': 0.5299999713897705,'SHSE.600606': 0.6399999856948853,'SHSE.600837': 2.6700000762939453,'SHSE.600887': 2.7799999713897705,'SHSE.600919': 0.25,'SHSE.600958': 1.1299999952316284,'SHSE.600999': 0.9100000262260437,'SHSE.601006': 1.149999976158142,'SHSE.601088': 0.8500000238418579,'SHSE.601166': 4.909999847412109,'SHSE.601169': 2.5299999713897705,'SHSE.601186': 1.3300000429153442,'SHSE.601198': 0.4399999976158142,'SHSE.601211': 2.0899999141693115,'SHSE.601229': 0.3799999952316284,'SHSE.601288': 3.1500000953674316,'SHSE.601318': 12.5,'SHSE.601328': 3.9000000953674316,'SHSE.601336': 1.059999942779541,'SHSE.601390': 1.4600000381469727,'SHSE.601398': 2.640000104904175,'SHSE.601601': 2.380000114440918,'SHSE.601628': 1.0199999809265137,'SHSE.601668': 3.4200000762939453,'SHSE.601688': 1.440000057220459,'SHSE.601766': 2.180000066757202,'SHSE.601788': 0.6600000262260437,'SHSE.601800': 0.5600000023841858,'SHSE.601818': 1.4900000095367432,'SHSE.601857': 0.8799999952316284,'SHSE.601881': 0.17000000178813934,'SHSE.601901': 0.8100000023841858,'SHSE.601985': 0.8100000023841858,'SHSE.601988': 1.8300000429153442,'SHSE.601989': 1.309999942779541},'trade_date': datetime.datetime(, 7, 31, 0, 0, tzinfo=tzfile('PRC'))}

从财务数据表提取因子

查询基本面数据最新n条

get_fundamentals_n(table, symbols, end_date, fields=None, filter=None, order_by=None, count=1, df=False)

参数

from gm.api import *set_token("61c4ec11af460c782950cd5ed41faacd5ac560f9")symbol_list = get_history_constituents(index="SHSE.000016", start_date='-07-10')[0].get("constituents").keys()df = get_fundamentals_n(table="deriv_finance_indicator", symbols=symbol_list, end_date='-07-10', fields="NPGRT", count=1, df=True)print(df)

deriv_finance_indicator衍生财务指标

NPGRT归属母公司净利润增长率

返回值

对因子进行排序

df = df.sort_values(["NPGRT"], ascending=False)print(df.head(5))

symbol pub_date end_dateNPGRT38 SHSE.601857 -08-25 00:00:00+08:00 -06-30 00:00:00+08:00 2300.378937 SHSE.600111 -08-18 00:00:00+08:00 -06-30 00:00:00+08:00 258.272222 SHSE.601088 -08-26 00:00:00+08:00 -06-30 00:00:00+08:00 147.405412 SHSE.601006 -08-30 00:00:00+08:00 -06-30 00:00:00+08:00 107.12038 SHSE.600050 -08-17 00:00:00+08:00 -06-30 00:00:00+08:00 74.3397

单因子选股轮动测试

股票池选用上证50的成分股选择股票池中净利润增长率排名前5的5家公司每个月进行调仓,调出排名跌出前5的公司,重新买入进入排名前5的公司

# coding=utf-8from __future__ import print_function, absolute_importfrom gm.api import *import os# 策略中必须有init方法def init(context):context.index = "SHSE.000016"context.num = 5schedule(schedule_func=algo, date_rule="1m", time_rule="09:31:00")def algo(context):now = context.noworder_close_all()symbol_list = get_history_constituents(index=context.index, start_date=now)[0].get("constituents").keys()df = get_fundamentals_n(table="deriv_finance_indicator", symbols=symbol_list, end_date=now, fields="NPGRT", count=1, df=True)df = df.sort_values(["NPGRT"], ascending=False)target_list = df["symbol"].valuestarget_list = target_list[:context.num]for symbol in target_list:order_target_percent(symbol=symbol, percent= 1./context.num, order_type=OrderType_Market, position_side=PositionSide_Long)def on_backtest_finished(context, indicator):print(indicator)if __name__ == '__main__':run(strategy_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',filename='main.py',mode=MODE_BACKTEST,token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',backtest_start_time = '-01-01 09:00:00',backtest_end_time='-06-25 15:00:00',backtest_initial_cash=10000000,backtest_adjust= ADJUST_PREV)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。