xxxx18一60岁hd中国/日韩女同互慰一区二区/西西人体扒开双腿无遮挡/日韩欧美黄色一级片 - 色护士精品影院www

資源簡(jiǎn)介

量化交易策略之動(dòng)量與反轉(zhuǎn)交易python版,用戶可修改參數(shù)進(jìn)行自定義,可借助米匡、聚寬等網(wǎng)站平臺(tái)實(shí)現(xiàn)量化交易。動(dòng)量反轉(zhuǎn)策略被證實(shí)為長(zhǎng)久以來仍有明顯效果的交易策略。

資源截圖

代碼片段和文件信息

#?coding=utf-8
from?__future__?import?division
from?backtest?import?*
import?os
import?pandas?as?pd
from?math?import?floor


#?計(jì)算復(fù)權(quán)價(jià)格
def?cal_right_price(input_stock_data?type=‘前復(fù)權(quán)‘):
????“““
????:param?input_stock_data:?標(biāo)準(zhǔn)股票數(shù)據(jù),需要‘收盤價(jià)‘?‘漲跌幅‘
????:param?type:?確定是前復(fù)權(quán)還是后復(fù)權(quán),分別為‘后復(fù)權(quán)‘,‘前復(fù)權(quán)‘
????:return:?新增一列‘后復(fù)權(quán)價(jià)‘/‘前復(fù)權(quán)價(jià)‘的stock_data
????“““
????#?計(jì)算收盤復(fù)權(quán)價(jià)
????stock_data?=?input_stock_data.copy()
????num?=?{‘后復(fù)權(quán)‘:?0?‘前復(fù)權(quán)‘:?-1}
????price1?=?stock_data[‘close‘].iloc[num[type]]
????stock_data[‘復(fù)權(quán)價(jià)_temp‘]?=?(stock_data[‘change‘]?+?1.0).cumprod()
????price2?=?stock_data[‘復(fù)權(quán)價(jià)_temp‘].iloc[num[type]]
????stock_data[‘復(fù)權(quán)價(jià)‘]?=?stock_data[‘復(fù)權(quán)價(jià)_temp‘]?*?(price1?/?price2)
????stock_data.pop(‘復(fù)權(quán)價(jià)_temp‘)

????#?計(jì)算開盤復(fù)權(quán)價(jià)
????stock_data[‘復(fù)權(quán)價(jià)_開盤‘]?=?stock_data[‘復(fù)權(quán)價(jià)‘]?/?(stock_data[‘close‘]?/?stock_data[‘open‘])
????return?stock_data[[‘復(fù)權(quán)價(jià)_開盤‘?‘復(fù)權(quán)價(jià)‘]]


#?獲取股票數(shù)據(jù)
def?get_stock_data():
????#?遍歷數(shù)據(jù)文件夾中所有股票文件的文件名,得到股票代碼列表
????stock_code_list?=?[]
????#?此處為股票數(shù)據(jù)文件的本地路徑,請(qǐng)自行修改
????for?root?dirs?files?in?os.walk(‘e:/data/stock?data‘):
????????if?files:
????????????for?f?in?files:
????????????????if?‘.csv‘?in?f:
????????????????????stock_code_list.append(f.split(‘.csv‘)[0])

????all_stock?=?pd.Dataframe()

????for?code?in?stock_code_list:
????????#?此處為股票數(shù)據(jù)文件的本地路徑,請(qǐng)自行修改
????????stock_data?=?pd.read_csv(‘e:/data/stock?data/‘?+?code?+?‘.csv‘?parse_dates=[‘date‘])
????????stock_data?=?stock_data[[‘code‘?‘date‘?‘open‘?‘close‘?‘change‘]].sort_values(by=‘date‘)
????????stock_data.reset_index(drop=True?inplace=True)
????????#?計(jì)算復(fù)權(quán)價(jià)
????????stock_data[[‘open‘?‘close‘]]?=?cal_right_price(stock_data?type=‘后復(fù)權(quán)‘)
????????#?判斷每天開盤是否漲停
????????stock_data.ix[stock_data[‘open‘]?>?stock_data[‘close‘].shift(1)?*?1.097?‘limit_up‘]?=?1
????????stock_data[‘limit_up‘].fillna(0?inplace=True)

????????all_stock?=?all_stock.append(stock_data?ignore_index=True)

????return?all_stock[[‘code‘?‘date‘?‘change‘?‘limit_up‘]]


def?momentum_and_contrarian(all_stock?start_date?end_date?window=3):
????“““
????:param?all_stock:?所有股票的數(shù)據(jù)集
????:param?start_date:?起始日期(包含排名期)
????:param?end_date:?結(jié)束日期
????:param?window:?排名期的月份數(shù),默認(rèn)為3個(gè)月
????:return:?返回動(dòng)量策略和反轉(zhuǎn)策略的收益率和資金曲線
????“““
????#?取出指數(shù)數(shù)據(jù)作為交易天數(shù)的參考標(biāo)準(zhǔn)?此處為指數(shù)數(shù)據(jù)文件的本地路徑,請(qǐng)自行修改
????index_data?=?pd.read_csv(‘e:/data/index?data/sh000001.csv‘?parse_dates=True?index_col=‘date‘)
????index_data.sort_index(inplace=True)
????index_data?=?index_data[start_date:end_date]
????#?轉(zhuǎn)換成月度數(shù)據(jù)
????by_month?=?index_data[[‘close‘]].resample(‘M‘?how=‘last‘)
????by_month.reset_index(inplace=True)

????momentum_portfolio_all?=?pd.Dataframe()
????contrarian_portfolio_all?=?pd.Dataframe()

????for?i?in?range(window?len(by_month)?-?1):
????????start_month?=?by_month[‘date‘].iloc[i?-?window]??#?排名期第一個(gè)月
????????end_month?=?by_month[‘date‘].iloc[i]??#?排名期最后一個(gè)月

????????#?取出在排名期內(nèi)的數(shù)據(jù)
????????stock_temp?=?all_stock[(all_stock[‘date‘]?>?start_month)?&?(all_stock[‘date‘]?<=?end_month)]

????????#?將指數(shù)在這段時(shí)間的數(shù)據(jù)取出

評(píng)論

共有 條評(píng)論

相關(guān)資源