中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
量化交易策略——小市值策略(二)
2016-11-28 

本篇是持倉(cāng)1只股票與持倉(cāng)10只股票的小市值策略

一、持倉(cāng)1只股票的小市值策略


    策略基本思路:持有市值最小的1只股票,按照調(diào)倉(cāng)頻率定期更新。

收益風(fēng)險(xiǎn):

源碼:

import pandas as pd

import numpy as np

def initialize(context):

    g.choicenum = 1 #預(yù)選小市值股票數(shù)

    g.days = 0 # 計(jì)時(shí)器

    g.runned_years = set([])

    g.runned_seasons = set([])

    # 現(xiàn)在應(yīng)該持有的倉(cāng)位, 因?yàn)榭赡芡E茖?dǎo)致賣出不成功, 跟實(shí)際持倉(cāng)可能不一樣

    g.stocks_to_hold = set()

    # 調(diào)倉(cāng)頻率

    g.period = 'year' # week, month, season,year,day

    # 是否止損

    g.should_stop_loss = False #True:開(kāi)啟止損,False:關(guān)閉止損

    # 如果是按周調(diào)倉(cāng),

    if g.period == 'week':

        run_weekly(rebalance, 1)

    elif g.period in ('month', 'year', 'season'):

        run_monthly(monthly, 1)

    else:

        run_daily(daily)

    # 防止賣出不成功, 每日嘗試賣出

    # run_daily(clean_stocks_to_sell)

    if g.should_stop_loss:

        run_daily(stop_loss)

def daily(context):

    if g.days % g.period == 0:

        rebalance(context)

    g.days += 1

def monthly(context):

    if g.period == 'month':

        rebalance(context)

    elif g.period == 'year':

        year = context.current_dt.year

        if year not in g.runned_years:

            # 這一年沒(méi)運(yùn)行過(guò), 運(yùn)行一次

            g.runned_years.add(year)

            rebalance(context)

    elif g.period == 'season':

        year = context.current_dt.year

        # 取得月份對(duì)應(yīng)的季度, 1-3月->1季度, 4-6月->2季度, ...

        season = (context.current_dt.month - 1) / 3 + 1

        year_season = (year, season)

        if year_season not in g.runned_seasons:

            # 這一年的這一季度沒(méi)運(yùn)行過(guò), 運(yùn)行一次

            g.runned_seasons.add(year_season)

            rebalance(context)

    pass

# 調(diào)整

def rebalance(context):

    print 'rebalance at %s' % context.current_dt

    # 設(shè)置滬深兩市所有股票為股票池

    scu0 = get_index_stocks('000001.XSHG')

    scu3 = get_index_stocks('399106.XSHE')

    scu = scu0+scu3

    # scu = scu[:10]

    set_universe(scu)

    date=context.current_dt.strftime('%Y-%m-%d')

    # 選出低市值的股票,buylist

    df = get_fundamentals(query(

            valuation.code,valuation.market_cap

        ).filter(

            valuation.code.in_(context.universe)

        ).order_by(

            valuation.market_cap.asc()

        ), date=date

        ).dropna()

    buylist =unpaused(list(df['code']))

    g.stocks_to_hold = buylist=buylist[:g.choicenum]

    clean_stocks_to_sell(context)

    # 等權(quán)重買入buylist中的股票

    position_per_stk = context.portfolio.cash/g.choicenum

    closes = history(1, '1d', 'price', df=False)

    for stock in buylist:

        close = closes[stock][-1]

        if not isnan(close):

            amount = int(position_per_stk/close)

            order(stock, +amount)

    set_universe(g.stocks_to_hold)

# 清空應(yīng)該賣出的股票

def clean_stocks_to_sell(context):

    for stock in context.portfolio.positions:

        if stock not in g.stocks_to_hold:

            order_target(stock, 0)

# 止損

def stop_loss(context):

    for stock in context.portfolio.positions:

        p = context.portfolio.positions[stock]

        if p.price/p.avg_cost <>

            order_target(stock,0)

            if stock in g.stocks_to_hold:

                g.stocks_to_hold.remove(stock)

def unpaused(stockspool):

    current_data=get_current_data()

    return [s for s in stockspool if not current_data[s].paused]

二、持倉(cāng)10只股票的小市值策略


  策略思路:

    1.初始資金M1分十份,第1年的首個(gè)交易日,分別買入市值最小的十只股票(記做股票池S1);

    2.次年的第1個(gè)交易日,查詢得到的市值最小的十只股票(S2_target),賣出S1中不屬于S2_target的股票,在獲得資金M2后等分為十份,分別買入S2_target的十只股票;后續(xù)年份的處理依次類推。

    (如果股票因停牌、跌停等原因而未賣出,就繼續(xù)持有這些股票,等到下個(gè)周期第一個(gè)交易日再做處理)

收益風(fēng)險(xiǎn):

源碼:

import pandas as pd

import numpy as np

def initialize(context):

    g.choicenum = 10 #預(yù)選小市值股票數(shù)

    g.days = 0 # 計(jì)時(shí)器

    g.runned_years = set([])

    g.runned_seasons = set([])

    # 現(xiàn)在應(yīng)該持有的倉(cāng)位, 因?yàn)榭赡芡E茖?dǎo)致賣出不成功, 跟實(shí)際持倉(cāng)可能不一樣

    g.stocks_to_hold = set()

    # 調(diào)倉(cāng)頻率

    g.period = 'year' # week, month, season,year,day

    # 是否止損

    g.should_stop_loss = False #True:開(kāi)啟止損,False:關(guān)閉止損

    # 如果是按周調(diào)倉(cāng),

    if g.period == 'week':

        run_weekly(rebalance, 1)

    elif g.period in ('month', 'year', 'season'):

        run_monthly(monthly, 1)

    else:

        run_daily(daily)

    # 防止賣出不成功, 每日嘗試賣出

    # run_daily(clean_stocks_to_sell)

    if g.should_stop_loss:

        run_daily(stop_loss)

def daily(context):

    if g.days % g.period == 0:

        rebalance(context)

    g.days += 1

def monthly(context):

    if g.period == 'month':

        rebalance(context)

    elif g.period == 'year':

        year = context.current_dt.year

        if year not in g.runned_years:

            # 這一年沒(méi)運(yùn)行過(guò), 運(yùn)行一次

            g.runned_years.add(year)

            rebalance(context)

    elif g.period == 'season':

        year = context.current_dt.year

        # 取得月份對(duì)應(yīng)的季度, 1-3月->1季度, 4-6月->2季度, ...

        season = (context.current_dt.month - 1) / 3 + 1

        year_season = (year, season)

        if year_season not in g.runned_seasons:

            # 這一年的這一季度沒(méi)運(yùn)行過(guò), 運(yùn)行一次

            g.runned_seasons.add(year_season)

            rebalance(context)

    pass

# 調(diào)整

def rebalance(context):

    print 'rebalance at %s' % context.current_dt

    # 設(shè)置滬深兩市所有股票為股票池

    scu0 = get_index_stocks('000001.XSHG')

    scu3 = get_index_stocks('399106.XSHE')

    scu = scu0+scu3

    # scu = scu[:10]

    set_universe(scu)

    date=context.current_dt.strftime('%Y-%m-%d')

    # 選出低市值的股票,buylist

    df = get_fundamentals(query(

            valuation.code,valuation.market_cap

        ).filter(

            valuation.code.in_(context.universe)

        ).order_by(

            valuation.market_cap.asc()

        ), date=date

        ).dropna()

    buylist =unpaused(list(df['code']))

    g.stocks_to_hold = buylist=buylist[:g.choicenum]

    clean_stocks_to_sell(context)

    # 等權(quán)重買入buylist中的股票

    position_per_stk = context.portfolio.cash/g.choicenum

    closes = history(1, '1d', 'price', df=False)

    for stock in buylist:

        close = closes[stock][-1]

        if not isnan(close):

            amount = int(position_per_stk/close)

            order(stock, +amount)

    set_universe(g.stocks_to_hold)

# 清空應(yīng)該賣出的股票

def clean_stocks_to_sell(context):

    for stock in context.portfolio.positions:

        if stock not in g.stocks_to_hold:

            order_target(stock, 0)

# 止損

def stop_loss(context):

    for stock in context.portfolio.positions:

        p = context.portfolio.positions[stock]

        if p.price/p.avg_cost <>

            order_target(stock,0)

            if stock in g.stocks_to_hold:

                g.stocks_to_hold.remove(stock)

def unpaused(stockspool):

    current_data=get_current_data()

    return [s for s in stockspool if not current_data[s].paused]

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
量化交易
【止損】 止損/止盈 方案目錄 必讀 | RiceQuant米筐量化社區(qū) 交易策略論壇
MACD指標(biāo)對(duì)于指數(shù)的趨勢(shì)與擇時(shí)效應(yīng)
Python股票分析之股票數(shù)據(jù)采集循環(huán)完整代碼
使用dagster重構(gòu)可轉(zhuǎn)債與股票數(shù)據(jù)更新入庫(kù)
愛(ài)上記單詞【第3期】(故事法)
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服