Ejemplo n.º 1
0
def markowitz_r(funddfr, bounds):

    rf = const.rf

    final_risk = 0
    final_return = 0
    final_ws = []
    final_sharp = -10000000000000000000000000.0
    final_codes = []

    codes = funddfr.columns

    return_rate = []

    for code in codes:
        return_rate.append(funddfr[code].values)

    risks, returns, ws = fin.efficient_frontier(return_rate, bounds)

    for j in range(0, len(risks)):
        sharp = (returns[j] - rf) / risks[j]
        if sharp > final_sharp:
            final_risk = risks[j]
            final_return = returns[j]
            final_ws = ws[j]
            final_sharp = sharp

    return final_risk, final_return, final_ws, final_sharp
Ejemplo n.º 2
0
def markowitz(funddf, bounds, d):

    rf = const.rf
    funddfr = funddf.pct_change()
    funddfr = funddfr.fillna(0.0)

    final_risk = 0
    final_return = 0
    final_ws = []
    final_sharp = -10000000000000000000000000.0
    final_codes = []

    codes = funddfr.columns

    return_rate = []

    for code in codes:
        return_rate.append(funddfr[code].values)

    risks, returns, ws = fin.efficient_frontier(return_rate, bounds)

    for j in range(0, len(risks)):
        sharp = (returns[j] - rf) / risks[j]
        if sharp > final_sharp:
            final_risk = risks[j]
            final_return = returns[j]
            final_ws = ws[j]
            final_sharp = sharp

    f_str = '%s, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n'
    f = open('./tmp/ef_' + d + '.csv', 'w')
    f.write(
        'date, risk, return, largecap, smallcap, rise, oscillation, decline ,growth ,value, ratebond, creditbond, convertiblebond, money1, money2, SP500.SPI, SPGSGCTR.SPI, HSCI.HI\n'
    )
    #for j in range(0, len(risks)):
    #    f.write(f_str % (d,risks[j], returns[j], ws[j][0], ws[j][1], ws[j][2], ws[j][3], ws[j][4], ws[j][5], ws[j][6], ws[j][7], ws[j][8], ws[j][9], ws[j][10], ws[j][11], ws[j][12], ws[j][13], ws[j][14] ))

    f.flush()
    f.close()

    return final_risk, final_return, final_ws, final_sharp
Ejemplo n.º 3
0
return_rate = []
dapan = df['dapan'].values
xiaopan = df['xiaopan'].values

dapanrs = []
xiaopanrs = []
for i in range(1, len(dapan)):
    dapanrs.append(dapan[i] / dapan[i - 1] - 1)
for i in range(1, len(xiaopan)):
    xiaopanrs.append(xiaopan[i] / xiaopan[i - 1] - 1)

return_rate.append(dapanrs)
return_rate.append(xiaopanrs)

risks, returns, portfolios = fin.efficient_frontier(return_rate)

rf = 0.025 / 52
n = 0
sharp = (returns[0] - rf) / risks[0]
for i in range(0, len(returns)):
    s = (returns[i] - rf) / risks[i]
    if s > sharp:
        sharp = s
        n = i

###########################################################################

#print 'sharp : ', sharp, 'return : ', returns[n], 'risk : ', risks[n], 'portfolio : ', portfolios[n]
#print 'annual return : ', returns[n] * 52