def cal_simple_return(): stock = pd.read_csv(r"./data/stockszA.csv", index_col="Trddt") Vanke = stock[stock.Stkcd == 2] close = Vanke.Clsprc close.index = pd.to_datetime(close.index) close.index.name = "Date" print(close.head()) lagclose = close.shift(1) print(lagclose.head()) Calclose = pd.DataFrame({'close': close, 'lagclose': lagclose}) print(Calclose.head()) simpleret = (close - lagclose) / lagclose simpleret.name = 'simpleret' print(simpleret.head()) calret = pd.merge(Calclose, pd.DataFrame(simpleret), left_index=True, right_index=True) simpleret2 = (close - close.shift(2)) / close.shift(2) simpleret2.name = 'simpleret2' calret['simpleret2'] = simpleret2 print(calret.head()) print(calret.iloc[5, :]) ffnSimpleret = ffn.to_returns(close) ffnSimpleret.name = 'ffnSimpleret' print(ffnSimpleret.head())
def value_at_risk(self): # 半方差公式 只算下降的 def cal_half_dev(returns): # 均值 mu = returns.mean() tmp = returns["returnss" < mu] half_deviation = (sum((mu - tmp)**2) / len(returns))**0.5 return half_deviation close = pd.DataFrame()["close"] simple_return = ffn.to_returns(close) # 半方差的大小 res = cal_half_dev(simple_return) print(res) # 历史模拟 simple_return.quantile(0.05) # 风险值最差0.05的位置 norm.ppf(0.05, simple_return.mean(), simple_return.std()) # 最差0.05的均值期望 worst_return = simple_return[ "returnss" < simple_return.quantile(0.05)].mean() # 最大回撤 展开 price = (1 + simple_return).cumprod() # 返回的依然是数组 simple_return.cummax() - price # 最大回撤 ffn.calc_max_drawdown(price) ffn.calc_max_drawdown((1 + simple_return).cumprod())
def __call__(self, target): if target.now is None: return False if target.positions.shape == (0, 0): return True positions = target.positions.loc[target.now] if positions is None: return True prices = target.universe.loc[target.now, positions.index] if prices is None: return True current_weights = positions * prices / target.value target_weights = self.target_weights.loc[target.now, :] cols = list(current_weights.index.copy()) for c in target_weights.keys(): if not c in cols: cols.append(c) weights = pd.Series(np.zeros(len(cols)), index=cols) for c in cols: if c in current_weights: weights[c] = current_weights[c] if c in target_weights: weights[c] -= target_weights[c] t0 = target.now - self.lag prc = target.universe.loc[t0 - self.lookback:t0, cols] returns = ffn.to_returns(prc) # calc covariance matrix if self.covar_method == 'ledoit-wolf': covar = sklearn.covariance.ledoit_wolf(returns) elif self.covar_method == 'standard': covar = returns.cov() else: raise NotImplementedError('covar_method not implemented') PTE_vol = np.sqrt( np.matmul(weights.values.T, np.matmul(covar, weights.values)) * self.annualization_factor) if pd.isnull(PTE_vol): return False # vol is too high if PTE_vol > self.PTE_volatility_cap: return True else: return False return True
def meanRet(self, fracs): """ 给定各资产的比例,计算收益率均值 :param fracs: :return: """ from ffn import to_returns meanRisky = to_returns(self.returns).mean() assert len(meanRisky) == len(fracs) # Length of fractions must be equal to naumber of assets return np.sum(np.multiply(meanRisky, np.array(fracs)))
def trade(obv, price): signal = (2 * (obv.diff() > 0) - 1)[1:] ret = ffn.to_returns(price)[1:] ret.name = 'ret' tradeRet = ret * signal.shift(1) tradeRet.name = 'tradeRet' Returns=pd.merge(pd.DataFrame(ret),\ pd.DataFrame(tradeRet),\ left_index=True,right_index=True).dropna() return (Returns)
def meanRet(self, fracs): """ 给定各资产比例,计算收益率均值 :param fracs: 资产配置比 :return: """ meanRisky = ffn.to_returns(self.returns).mean() assert len(meanRisky) == len( fracs), 'Length of fractions must be equal to number of assets' return np.sum(np.multiply(meanRisky, np.array(fracs)))
def conponent_profit(self): close = pd.DataFrame()["close"] returns = ffn.get('aapl,msft,c,gs,ge', start='2010-01-01').to_returns(close).dropna() returns.calc_mean_var_weights().as_format('.2%') # 1. 方法 Tesla['Return'] = (Tesla['Close'] - Tesla['Close'].shift(1)) / Tesla['Close'].shift(1) Tesla = Tesla.dropna() # 2. 方法 GM['Return'] = ffn.to_returns(GM['Close']) # 3. 方法 Ford['Return'] = Ford['Close'].pct_change(1) Ford = Ford.dropna() # 年化率 simpleret = ffn.to_returns(close) # 复利化 simpleret = ffn.to_log_returns(close) annue = (1 + simpleret).cumprod()[-1]**(245 / 311) - 1 # 方差 simpleret.std()
def __call__(self, target): current_weights = target.temp['weights'] selected = current_weights.keys() # if there were no weights already set then skip if len(selected) == 0: return True t0 = target.now - self.lag prc = target.universe.loc[t0 - self.lookback:t0, selected] returns = ffn.to_returns(prc) # calc covariance matrix if self.covar_method == 'ledoit-wolf': covar = sklearn.covariance.ledoit_wolf(returns) elif self.covar_method == 'standard': covar = returns.cov() else: raise NotImplementedError('covar_method not implemented') weights = pd.Series([current_weights[x] for x in covar.columns], index=covar.columns) vol = np.sqrt( np.matmul(weights.values.T, np.matmul(covar, weights.values)) * self.annualization_factor) #vol is too high if vol > self.target_volatility: mult = self.target_volatility / vol #vol is too low elif vol < self.target_volatility: mult = self.target_volatility / vol else: mult = 1 for k in target.temp['weights'].keys(): target.temp['weights'][k] = target.temp['weights'][k] * mult return True
close = stock.Close close.index = pd.to_datetime(close.index) lagclose = close.shift(1) lagclose.head() simpleret = (close - lagclose) / lagclose simpleret.name = 'simpleret' simpleret.head() simpleret2 = (close - close.shift(2)) / close.shift(2) simpleret2.name = 'simpleret2' simpleret2.head() import ffn ffnSimpleret = ffn.to_returns(close) ffnSimpleret.name = 'ffnSimpleret' ffnSimpleret.head() annualize = (1 + simpleret).cumprod()[-1]**(245 / 311) - 1 annualize def annualize(returns, period): if period == 'day': return ((1 + returns).cumprod()[-1]**(245 / len(returns)) - 1) elif period == 'month': return ((1 + returns).cumprod()[-1]**(12 / len(returns)) - 1) elif period == 'quarter': return ((1 + returns).cumprod()[-1]**(4 / len(returns)) - 1) elif period == 'year':
simpleret.name = 'simpleret' simpleret.head() calret = pd.merge(Calclose, pd.DataFrame(simpleret), left_index=True, right_index=True) calret.head() simpleret2 = (close - close.shift(2)) / close.shift(2) simpleret2.name = 'simpleret2' calret['simpleret2'] = simpleret2 calret.head() calret.iloc[5, :] import ffn ffnSimpleret = ffn.to_returns(close) ffnSimpleret.name = 'ffnSimpleret' ffnSimpleret.head() #假设一年有245个交易日 annualize = (1 + simpleret).cumprod()[-1]**(245 / 311) - 1 annualize def annualize(returns, period): if period == 'day': return ((1 + returns).cumprod()[-1]**(245 / len(returns)) - 1) elif period == 'month': return ((1 + returns).cumprod()[-1]**(12 / len(returns)) - 1) elif period == 'quarter': return ((1 + returns).cumprod()[-1]**(4 / len(returns)) - 1)
dailyreturn_xianbank = (close_xianbank['close'] - close_xianbank['lag_close'] ) / close_xianbank['lag_close'] # 用pct_change()更简单 # dailyreturn_xianbank = close_xianbank['close'].pct_change() # 多期简单收益率 以2期为例 dailyreturn_xianbank_2 = ( close_xianbank['close'] - close_xianbank['close'].shift(2)) / close_xianbank['close'].shift(2) # 同样用pct_change()更简单 # dailyreturn_xianbank_2 = close_xianbank['close'].pct_change(periods = 2) # 利用ffn模块计算单期简单收益率 # pip install ffn import ffn ffn_dailyreturn_xianbank = ffn.to_returns(close_xianbank['close']) # 计算对数收益 / 连续复利收益率 logreturn_xianbank = np.log(close_xianbank['close'] / close_xianbank['close'].shift(1)) logreturn_xianbank_2 = np.log(close_xianbank['close'] / close_xianbank['close'].shift(2)) # 或者 ffn_logreturn_xianbank = ffn.to_log_returns(close_xianbank['close']) # 计算年化简单收益率,假设一年有245个交易日 annualize_dailyreturn_xianbank = (1 + dailyreturn_xianbank).cumprod()[-1]**( 245 / 311) - 1 # 收益率曲线绘图 dailyreturn_xianbank.plot()
#按照交易量由高至低排列,得到排序后的active_asset ##排序规则:在整个样本期内,20天日均成交量为基准 active_asset = find_active_asset(asset_list) list_Ind = param['Ind_func'] #技术指标列表 for i in range(len(active_asset)): print(' '.join([active_asset[i][0], active_asset[i][1]])) df = hdf.hdfRead(path, active_asset[i][0], active_asset[i][1], 'Stitch', '00', '1d', startdate='20120101', enddate='20171231') df[EXT_Bar_Close] = df[EXT_Bar_Close] * df[EXT_AdjFactor] df['ret'] = ffn.to_returns(df[EXT_Bar_Close]) mode = 'prod' All_Ind = pd.DataFrame([]) for Ind_func in list_Ind: Ind_temp = globals().get(Ind_func)(df.copy()) if All_Ind.size == 0: All_Ind = Ind_temp else: All_Ind = All_Ind.merge(Ind_temp, left_index=True, right_index=True, how='outer') df = df[['ret']].merge(All_Ind, left_index=True, right_index=True, how='outer')
def meanRet(self, fracs): meanRisky = ffn.to_returns(self.returns).mean() assert len(meanRisky) == len( fracs), 'Length of fractions must be equal to number of assets'
# -*- coding: utf-8 -*- """ Created on Tue Sep 25 10:55:37 2018 @author: DELL """ import pandas as pd import os import ffn price = [ 1.0, 1.01, 1.05, 1.1, 1.11, 1.07, 1.03, 1.03, 1.01, 1.02, 1.04, 1.05, 1.07, 1.06, 1.05, 1.06, 1.07, 1.09, 1.12, 1.18, 1.15, 1.15, 1.18, 1.16, 1.19, 1.17, 1.17, 1.18, 1.19, 1.23 ] data = pd.DataFrame(price) returnS = ffn.to_returns(data).dropna() max_dropdown = ffn.calc_max_drawdown((1 + returnS).cumprod()) print("最大回撤是: %.4f" % max_dropdown)
def meanRet(self,fracs): meanRisky=ffn.to_returns(self.returns).mean() assert len(meanRisky)==len(fracs), 'Length of fractions must be equal to number of assets' return(np.sum(np.multiply(meanRisky,np.array(fracs))))
simpleret2 = (close-close.shift(2))/close.shift(2) simpleret2.name = 'simpleret2' calret['simpleret2']=simpleret2 ffnSimpleret = ffn.to_returns(close) ffnSimpleret.name = 'ffnSimpleret' print (ffnSimpleret.head()) ''' SAPower = pd.read_csv('data/018/SAPower.csv', index_col='Date') SAPower.index = pd.to_datetime(SAPower.index) DalianRP = pd.read_csv('data/018/DalianRP.csv', index_col='Date') DalianRP.index = pd.to_datetime(DalianRP.index) returnS = ffn.to_returns(SAPower.Close) returnD = ffn.to_returns(DalianRP.Close) def cal_half_dev(returns): mu = returns.mean() temp = returns[returns < mu] half_deviation = (sum((mu - temp)**2) / len(returns))**0.5 return (half_deviation) returnS.quantile(0.05) returnD.quantile(0.05) #print (norm.ppf(0.05,returnS.mean(),returnS.std()))
print(close.head()) lagclose = close.shift(1) print(lagclose.head()) Calclose = pd.DataFrame({'close': close, 'lagclose': lagclose}) print(Calclose.head()) simpleret = (close - lagclose) / lagclose simpleret.name = 'simpleret' print(simpleret.head()) calret = pd.merge(Calclose, pd.DataFrame(simpleret), left_index=True, right_index=True) print(calret.head()) print(calret.iloc[5, :]) ffnSimpleret = ffn.to_returns(close) ffnSimpleret.name = 'ffnSimpleret' print(ffnSimpleret.head()) annualize = (1 + simpleret).cumprod()[-1]**(245 / 311) - 1 print(annualize) def annualize(returns, period): if period == 'day': return ((1 + returns).cumprod()[-1]**(245 / len(returns)) - 1) elif period == 'month': return ((1 + returns).cumprod()[-1]**(12 / len(returns)) - 1) elif period == 'quarter': return ((1 + returns).cumprod()[-1]**(4 / len(returns)) - 1) elif period == 'year':
df2,k=ztq.tq_usrDatXed(qx,df_usr) zt.prDF('df2',df2) # print('\n#3.3 ret') print('ret:',k,'%') print('\n#3.4 tq_usrDatXedFill') df=ztq.tq_usrDatXedFill(qx,df2) zt.prDF('df',df) #============== #4 ret xed ret=ffn.to_log_returns(df[xlst]).dropna() zt.prDF('\n#4.1,ret#1',ret) # ret=ffn.to_returns(df[xlst]).dropna() zt.prDF('\n#4.2,ret#2',ret) # ret[xlst]=ret[xlst].astype('float') zt.prDF('\n#4.3,ret#3',ret) #5 ret.hist print('\n#5 ret.hist') ax = ret.hist(figsize=(16,8)) #6 print('\n# ret.corr()') ret=ret.corr().as_format('.2f') zt.prDF('\n#6 ret.corr',ret) #7
def runstrat(excode, asset, args=None): hdf = HdfUtility() args = parse_args(args) cerebro = bt.Cerebro() dkwargs = dict() if args.fromdate: fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d') dkwargs['fromdate'] = fromdate start = ''.join(str(fromdate)[0:10].split('-')) if args.todate: todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d') dkwargs['todate'] = todate end = ''.join(str(todate)[0:10].split('-')) data0 = hdf.hdfRead(EXT_Hdf_Path, excode, asset, 'Stitch', '00', '1d', startdate=start, enddate=end) data0 = hdf2bt(data0) Indicator_All = pd.DataFrame([]) Indicator_All[EXT_Bar_Close] = data0[EXT_Bar_Close] Indicator_All = Indicator_All.reset_index().copy() # Feed data data0 = bt.feeds.PandasData(dataname=data0.copy(), fromdate=datetime.datetime(2012, 1, 1), todate=datetime.datetime(2017, 12, 31)) cerebro.adddata(data0) cerebro.addstrategy(TALibStrategy, ind=args.ind, doji=not args.no_doji, plot=args.plot) cerebro.run(runcone=not args.use_next, stdstats=False) stat = cerebro.runstrats[0][0] #print(dir(stat)) #print(stat.INDS) indicator_byvt = stat.ind_data #print(dir(indicator_byvt)) #params = setting['data_setting'] for indi, indicator in indicator_byvt.items(): Indicator_All[indi] = pd.DataFrame({indi: indicator.array}) #args.plot = True if args.plot: pkwargs = dict(style='candle') if args.plot is not True: # evals to True but is not True npkwargs = eval('dict(' + args.plot + ')') # args were passed pkwargs.update(npkwargs) cerebro.plot(**pkwargs) Indicator_All['ret'] = ffn.to_returns(Indicator_All[EXT_Bar_Close]) # set 'Date' as only index to test its Stability Indicator_All.set_index(Indicator_All[EXT_Bar_Date]) Ind_Stability(Indicator_All, excode, asset) # curret indicator corresponding to next period of return Indicator_All['ret'] = Indicator_All['ret'].shift(-1) Ind_Eff(Indicator_All, excode, asset) return