返回介绍

第一部分 新手入门

第二部分 股票量化相关

第三部分 基金、利率互换、固定收益类

第四部分 衍生品相关

市值最小300指数

发布于 2022-02-20 22:26:17 字数 17439 浏览 786 评论 0 收藏 0

刷爆沪深300

策略名称: 市值最小300指数

回测时间:2013-01-01 到 2015-09-24

调仓期 :20交易日

策略思想:找A股市场市值最小的300只股票,等权重构建最小300指数

注意 :

  • 内存不够请自行缩短回测时间或universe
  • 此贴有4个!!!
import pandas as pd 
import numpy as np
from pandas import Series, DataFrame
start = '2013-01-01'                       # 回测起始时间
end = '2015-09-24'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准

universe0 = set_universe('A')               # 证券池,支持股票和基金
universe1 = set_universe('HS300')
universe2 = set_universe('ZZ500')
universe = list(set(universe0).difference(set(universe1+universe2)))   #最小市值股一定不在中证500和沪深300 pass

capital_base = 100000000                   # 起始资金
freq = 'd'                                 # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测

refresh_rate = 20                          # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟

def initialize(account):                   # 初始化虚拟账户状态
    pass

def handle_data(account):                  # 每个交易日的买入卖出指令

    total_money = account.referencePortfolioValue     

    prices = account.referencePrice

    buylist = []

    marketValue = DataFrame()

    today = account.current_date.strftime('%Y%m%d')

    for s in range(len(account.universe)/40 + 1):       

        if s == len(account.universe)/40:
            temp_list = account.universe[s*40:]
        else :
            temp_list = account.universe[s*40:(s+1)*40]    

#MktEqudGet接口一次最多选50个

        try:  #排除最后一次temp_list为零的可能
            marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")            
        except : 
            pass    
        marketValue = pd.concat([marketValue,marketValue_temp])

    marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')

    marketValue.set_index('secID',inplace=True)

    marketValue = marketValue.dropna()

    #排除新股发行日
    for s in list(marketValue.index) : 

        if not (np.isnan(prices[s]) or prices[s] == 0) : 

            buylist.append(s)

        if len(buylist) >= 300 :

            break

    sell_list = [x for x in account.valid_secpos if x not in buylist]

    for stk in sell_list:
        order_to(stk, 0)



    for stk in buylist:     

        order_to(stk, int(total_money/300/prices[stk]/100)*100)

更改权重比例 :加权流通市值倒数(越小越买) 买买买!

import pandas as pd 
import numpy as np
from pandas import Series, DataFrame
start = '2013-01-01'                       # 回测起始时间
end = '2015-09-24'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准

universe0 = set_universe('A')               # 证券池,支持股票和基金
universe1 = set_universe('HS300')
universe2 = set_universe('ZZ500')
universe = list(set(universe0).difference(set(universe1+universe2)))

capital_base = 100000000                   # 起始资金
freq = 'd'                                 # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测

refresh_rate = 20                          # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟

def initialize(account):                   # 初始化虚拟账户状态
    pass

def handle_data(account):                  # 每个交易日的买入卖出指令

    total_money = account.referencePortfolioValue     

    prices = account.referencePrice

    buylist = []

    marketValue = DataFrame()

    today = account.current_date.strftime('%Y%m%d')

    for s in range(len(account.universe)/40 + 1):       

        if s == len(account.universe)/40:
            temp_list = account.universe[s*40:]
        else :
            temp_list = account.universe[s*40:(s+1)*40]    

#MktEqudGet接口一次最多选50个

        try:  #排除最后一次temp_list为零的可能
            marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue,negMarketValue",pandas="1")            
        except : 
            pass    
        marketValue = pd.concat([marketValue,marketValue_temp])

    marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')

    marketValue.set_index('secID',inplace=True)

    marketValue = marketValue.dropna()

    #排除新股发行日
    for s in list(marketValue.index) : 

        if not (np.isnan(prices[s]) or prices[s] == 0) : 

            buylist.append(s)

        if len(buylist) >= 300 :

            break

    sell_list = [x for x in account.valid_secpos if x not in buylist]

    for stk in sell_list:
        order_to(stk, 0)

    #加权流通市值倒数购买  
    weight_list = []

    for stk in buylist:               
        weight_list.append(1.0/marketValue['negMarketValue'][stk])

    temp_sum = 0   
    for temp in weight_list:
            temp_sum += temp

    weight_list = [x/temp_sum for x in weight_list]   

    i = 0
    for stk in buylist:     
        order_to(stk, int(total_money*weight_list[i]/prices[stk]/100)*100)     
        i += 1

是不是在想賺钱分分钟了? 您有买300只个股的毛爷爷吗,啊! 〇_〇- . .

木有?我会告诉你买十只也很叼吗???

import pandas as pd 
import numpy as np
from pandas import Series, DataFrame
start = '2013-01-01'                       # 回测起始时间
end = '2015-09-24'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准

universe0 = set_universe('A')               # 证券池,支持股票和基金
universe1 = set_universe('HS300')
universe2 = set_universe('ZZ500')
universe = list(set(universe0).difference(set(universe1+universe2)))

capital_base = 100000000                   # 起始资金
freq = 'd'                                 # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测

refresh_rate = 20                          # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟

def initialize(account):                   # 初始化虚拟账户状态
    pass

def handle_data(account):                  # 每个交易日的买入卖出指令

    total_money = account.referencePortfolioValue     

    prices = account.referencePrice

    buylist = []

    marketValue = DataFrame()

    today = account.current_date.strftime('%Y%m%d')

    for s in range(len(account.universe)/40 + 1):       

        if s == len(account.universe)/40:
            temp_list = account.universe[s*40:]
        else :
            temp_list = account.universe[s*40:(s+1)*40]    

#MktEqudGet接口一次最多选50个

        try:  #排除最后一次temp_list为零的可能
            marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")            
        except : 
            pass    
        marketValue = pd.concat([marketValue,marketValue_temp])

    marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')

    marketValue.set_index('secID',inplace=True)

    marketValue = marketValue.dropna()

    #排除新股发行日
    for s in list(marketValue.index) : 

        if not (np.isnan(prices[s]) or prices[s] == 0) : 

            buylist.append(s)

        if len(buylist) >= 10 :

            break

    sell_list = [x for x in account.valid_secpos if x not in buylist]

    for stk in sell_list:
        order_to(stk, 0)

    #只买最优十只

    for stk in buylist:     

        order_to(stk, int(total_money/10/prices[stk]/100)*100)

我会告诉你有庄家(机构持股)的股票更容易飞 ???   小注 :此策略中DataAPI.JY.EquInstShJYGet(恒生聚源接口)暂不开放!!!活跃用户自行申请

import pandas as pd 
import numpy as np
from pandas import Series, DataFrame
start = '2013-01-01'                       # 回测起始时间
end = '2015-09-24'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准

universe0 = set_universe('A')               # 证券池,支持股票和基金
universe1 = set_universe('HS300')
universe2 = set_universe('ZZ500')
universe = list(set(universe0).difference(set(universe1+universe2)))

capital_base = 100000000                   # 起始资金
freq = 'd'                                 # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测

refresh_rate = 20                           # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟

def initialize(account):                   # 初始化虚拟账户状态
    pass

def handle_data(account):                  # 每个交易日的买入卖出指令

    total_money = account.referencePortfolioValue     

    prices = account.referencePrice

    buylist = []

    marketValue = DataFrame()

    today = account.current_date.strftime('%Y%m%d')

    for s in range(len(account.universe)/40 + 1):       

        if s == len(account.universe)/40:
            temp_list = account.universe[s*40:]
        else :
            temp_list = account.universe[s*40:(s+1)*40]    

#MktEqudGet接口一次最多选50个

        try:  #排除最后一次temp_list为零的可能
            marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")            
        except : 
            pass    
        marketValue = pd.concat([marketValue,marketValue_temp])

    marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')

    marketValue.set_index('secID',inplace=True)

    marketValue = marketValue.dropna()

    # 机构持股  非第一天上市新股
    for s in list(marketValue.index) : 

        try :   
            # 处理巨源的数据接口没有此股
            temp =  DataAPI.JY.EquInstShJYGet ( secID = s , field = u"instNrfaPct" , pandas = "1" )

        except :                 
            print account.current_date.strftime('%Y-%m-%d'),' ',s,' ','DataAPI.JY.EquInstShJYGet get wrong'
            continue

            #有机构持股 > 30%
        if temp['instNrfaPct'][0] > 30 and not (np.isnan(prices[s]) or prices[s] == 0) : 

            buylist.append(s)

        if len(buylist) >= 10 :

            break

    sell_list = [x for x in account.valid_secpos if x not in buylist]

    for stk in sell_list:
        order_to(stk, 0)

    for stk in buylist:     

        order_to(stk, int(total_money/10/prices[stk]/100)*100)

呵呵,居然看完了,还不赶紧点赞克隆去賺钱!!!内存不够的还不赶紧签到 !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文