Материал: Модели портфельного инвестирования

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

import correlation as cor #custom module for correlation calculationdatetime import datetimedateutil.rrule import *numpy as np

sum2one(corCoefList):= [v if (v > 0 and v < 0.9) else 0 for v in corCoefList]= corCoefList[:].sort(reverse = True)

= 0 #total correlation= []= []k, v in enumerate(corCoefList):sumOfElements + v < 1 and v != 0:

sumOfElements += v

selectedCorrelation.append(v)

useStocks.append(saveKeysCorCoefList.index(v))

useStocks, selectedCorrelation, sumOfElements

getPrices(sliceDate, dataFrame):= []index, row in dataFrame.ix[sliceDate.strftime('%Y%m%d'):sliceDate.strftime('%Y%m%d')].iteritems():= 0val in row:

if np.isnan(val) == False and val != 0:

securityPrice = val.append(securityPrice)prices

= cor.allDF('Data/Stocks/')= cor.allDF('Data/Futures/')

= datetime(2010,2,8).date()= rrule(DAILY, dtstart = dateStart, byweekday=(MO,TU,WE,TH,FR))[60].date()

# [[stock, stock, stock ..], [stock, stock, stock, ..] ]= []= 0= []= []= 0; deltaFuturePrice = 0;= []= [0 for n in stocks.columns.values]; futuresPortfolio = [0 for n in futures.columns.values]# lists with full amount of securities

# where key = number of security in initial list, value = amount and direction= [] #type=0= 1= open('logs.txt', 'w')= open('stat.csv', 'w')True:= cor.calcCorrel(stocks, futures, dateStart, dateEnd)= []

#portfolio = [n, n2, n3] where n, n2, n3 ... are the keys

#of stocks in the initial list, hedgeCorel, sumCorel = sum2one(corList[m[0]])

= getPrices(dateStart, stocks) # prices for all stocks in a list [10, 20, 0, 0, 14. ...]= getPrices(dateStart, futures)

= [stockPrices[stock] for stock in portfolio] #stock prices that we will operate with= futurePrices[m[0]]= [a*futPrice for a in hedgeCorel]= [a/b for a,b in zip(stockValueCalculation, portfolioPrices)] # amount of stocks we required to buy= np.floor(stockValueCalculation) # amount of stocks exactly to buy

sum([1 if np.isnan(n) == False and np.isinf(n) == False and n != 0 else -500 for n in stockValueCalculation]) <= 0: #The portfolio for some reasons is not constructed= rrule(DAILY, dtstart = dateStart, byweekday=(MO,TU,WE,TH,FR))[1].date()= rrule(DAILY, dtstart = dateEnd, byweekday=(MO,TU,WE,TH,FR))[1].date()

#close all open positions+= sum([amount*price for amount, price in zip(stockPortfolio, stockPrices)])+= sum([amount*price for amount, price in zip(futuresPortfolio, futurePrices)])= [0 for n in stocks.columns.values]= [0 for n in futures.columns.values]

#futures are less expensive than shares

#buy futures!futPrice < sum(stockValueCalculation*portfolioPrices):.write(dateStart.strftime('%Y%m%d')+' Buy '+futures.columns.values[m[0]]+' for '+("%.2f"%futurePrices[m[0]])+' total '+(str(stockValueCalculation[key]*portfolioPrices[key]))+'\r\n')[m[0]] = 1-= futurePrices[m[0]]

key, stock in enumerate(portfolio):

logs.write('{} Sell {} {} for {} {}'.format(dateStart.strftime('%Y%m%d'),

stockValueCalculation[key],

stocks.columns.values[stock],

("%.2f"%stockPrices[stock]),

("%.2f"%stockValueCalculation[key]*portfolioPrices[key])

))

stockPortfolio[stock] = (-1)*stockValueCalculation[key] #sell some stocks

profitDelta += stockValueCalculation[key]*portfolioPrices[key]:.write(dateStart.strftime('%Y%m%d')+' Sell '+futures.columns.values[m[0]]+' for '+("%.2f"%futurePrices[m[0]])+'\r\n')[m[0]] = -1+= futurePrices[m[0]]

key, stock in enumerate(portfolio):

logs.write('{} Buy {} {} for {} {}'.format(dateStart.strftime('%Y%m%d'),

stockValueCalculation[key],

stocks.columns.values[stock],

("%.2f"%stockPrices[stock]),

stockValueCalculation[key]*portfolioPrices[key]

))

stockPortfolio[stock] = stockValueCalculation[key] #sell some stocks

profitDelta -= stockValueCalculation[key]*portfolioPrices[key]

logs.write('\r\n').write('%.2f'%profitDelta).write(dateStart.strftime('%Y%m%d')+";"+'%.2f'%profitDelta+"\r\n").write('\r\n')(dateStart.strftime('%Y%m%d'), profitDelta)

= futPricedateEnd >= datetime(2015,2,6).date():;= rrule(DAILY, dtstart = dateStart, byweekday=(MO,TU,WE,TH,FR))[1].date()= rrule(DAILY, dtstart = dateEnd, byweekday=(MO,TU,WE,TH,FR))[1].date()+= 1i == 600:();

Вычисление оптимального инвестиционного портфеля


from os import listdir

import math, numpy as npos.path import isfile, join

= [ f for f in listdir("Data\\Stocks_reduced") if isfile(join("Data\\Stocks_reduced",f)) ]=[]; testData=[]=[]

#place all values in one list 0 value is the farthesti in range(len(onlyfiles)):.append([]); testData.append([]);open("Data\\Stocks_reduced\\"+onlyfiles[i]) as f:=0line in f.readlines():

line=line.split(',')

if n > 0:

testData[i].append(float(line[5].replace(',','.')))

n+=1[i].reverse()[i].reverse()

markowitz(data, reqReturn):i in range(len(data)):.append(sum(data[i])/len(data[i]))

#calculate day returns=[]=[]i in range(len(data)):.append([])n in range(1,len(data[i])):

dataReturn[i].append(data[i][n]/data[i][n-1]-1).append(sum(dataReturn[i])/len(dataReturn[i]))

#count covariance=[]i in range(len(dataReturn)):.append([])n in range(len(dataReturn)):

covar[i].append(sum([(dataReturn[i][f]-expectedReturn[i])*(dataReturn[n][f]-expectedReturn[n]) for f in range(len(dataReturn[n]))])/len(dataReturn[i]))

#inverse the matrix=np.linalg.inv(np.array(covar))

#create matrix with one column of ones=np.array([[1] for i in range(len(covarInverse))])

#create the matrix (one row) with ones=np.ones(len(covarInverse))

#multiply two prev. matrices=np.dot(uT,covarInverse)

#calculate value A=np.dot(uTxCovInv,np.array(expectedReturn))

#calculate value B=np.dot(covarInverse,np.array(expectedReturn))=np.dot(eTxCovInv,np.array(expectedReturn))

#calculate value C=np.dot(uTxCovInv,u)

#calculate value D=B*C-A*A

#intermediate coefficients=np.dot(covarInverse,u)=np.dot(covarInverse,np.array([[x] for x in expectedReturn]))

#calculating the portfolio coordinates (g and h are points)=(B*m-A*l)/D

#calculating the return of the portfolio g=sum(g*np.array([[x] for x in expectedReturn]))

#calculating portfolio h=(C*l-A*m)/D

#calculating the return of the portfolio h=sum(h*np.array([[x] for x in expectedReturn]))

#calculate the efficient portfolio depending on the predetermined return=g+h*reqReturn

=np.dot(np.dot(np.array([x[0] for x in weights]),np.array(covar)),weights)

#convert numpy array to simple list=[x[0] for x in weights][portfolioRisk[0],weights]

positions(balance, weights, data):=[]w in range(len(weights)):

#value in money equivalent; weeight*balance/last (newest) price, 2.1=3(len(data[w]), w).append(math.ceil(weights[w]*balance/data[w][len(data[w])-1]))position

sliseData(step,period,data):=[]i in range(math.trunc((len(data[0])-period)/step)):

slised.append([])

for n in range(len(data)):

slised[i].append(data[n][i*step:period+i*step])slised

maxreturn(data):= 0= 0i in range(1,100):

port = markowitz(data, i/10000)

if (i/10000)/port[0] > mCoef:

mCoef = (i/10000)/port[0]

mReturn = i/10000mReturn

= open('risk.txt','a+')

=1; period=200; depo=0=sliseData(step, period, testData);

# i-len(testData[0])-1 step to the future :)= 10000i in range(len(slicedTest)-1, 50, -1):= maxreturn(slicedTest[i])(ret)= markowitz(slicedTest[i],ret)n in range(len(port[1])):

#if Current price/previous price <= 0.97 => through out the stocktestData[n][i-len(testData[n])/testData[n][i-len(testData[n])+1 <= 0.97:

port[1][n] = 0= math.fabs(port[1][n]*depo)= port[1][n]/math.fabs(port[1][n])= math.floor(allocateForStock / slicedTest[i][n][0])+= directionOfTrade*( amountToBuy*testData[n][i-len(testData[n])-1] - amountToBuy*slicedTest[i][n][0]).write(str(depo) + ";" + str(ret) + "\n")(i, depo)

()

i in range(1,100):= markowitz(slicedTest[len(slicedTest)-1], i/10000).write(str(i/10000)+";"+str(port[0])+"\n")()

= []; ris =[]i in range(len(slicedTest)):= markowitz(slicedTest[i], 0.005).append(port[1]).append(port[0])i == 15:;= 0

i,v in enumerate(st[0]):i2, v2 in enumerate(st):.write(str(round(st[i2][i],3)) + ';').write('\r\n')

'''=positions(10000,mark[1],slicedTest[i])n in range(len(pos)):+=(testData[n][period+(i+1)*step-1]-testData[n][period+i*step-1])*pos[n](depo)

'''

=[]step in range(10,20): #prediction.append([])period in range(100,200): #change the history period=sliseData(step,period,data)=0i in range(len(slc)):

mark=markowitz(slc[i],0.0022)

pos=positions(10000,mark[1],slc[i])

for n in range(len(pos)):

depo+=(data[n][period+(i+1)*step-1]-data[n][period+i*step-1])*pos[n](str(step)+" "+str(period)+" "+str(depo))[step-2].append(depo)       

Приложение 3. Расчет доходности ценной бумаги

os import listdir

from os.path import isfile, join

Return(timeSer):(timeSer[0]-timeSer[-1])/timeSer[-1]

= open('return.txt','a+')= [ f for f in listdir("Data\\dailyDataDeutsche") if isfile(join("Data\\dailyDataDeutsche",f)) ]= []i in range(len(onlyfiles)):.append([]);open("Data\\dailyDataDeutsche\\"+onlyfiles[i]) as f:=0line in f.readlines():

line=line.split(';')

data[i].append(float(line[4].replace(',','.')))

n += 1

i in range(len(data)):.write(str(Return(data[i]))+";")