coins = ['BTC', 'ETH', 'LTC', 'XMR', 'XRP', 'DASH', 'MAID', 'XEM', 'DOGE']
returns = [2.2638834003579813, 8.116901792476218, 1.3165650562863767, 28.082132882509775, 1.085332332146777, 3.133009784746374, 7.253804471524264, 21.965787725641633, 1.4764456923421703]
cov = {'DASH': {'DASH': 0.7644961854506808, 'XEM': 0.12731810810740216, 'DOGE': 0.024594538249552256, 'LTC': -0.017885008134766338, 'MAID': 0.15851962585719734, 'BTC': 0.04633192428471714, 'ETH': 0.06459627506298854, 'XRP': 0.0485013905397046, 'XMR': 0.07136735462548086}, 'XEM': {'DASH': 0.12731810810740216, 'XEM': 2.696964866397394, 'DOGE': 0.2573684776370606, 'LTC': 0.2451590145706355, 'MAID': 0.24638558526778676, 'BTC': 0.3162291738177758, 'ETH': 0.16795613694062173, 'XRP': 0.20794892099014484, 'XMR': 0.24474433942952412}, 'DOGE': {'DASH': 0.024594538249552256, 'XEM': 0.2573684776370606, 'DOGE': 0.9048010148463588, 'LTC': 0.21028685267231811, 'MAID': 0.1969666908833168, 'BTC': 0.18023154281182274, 'ETH': 0.04381871552036941, 'XRP': 0.22505760423798415, 'XMR': 0.10013128092001486}, 'LTC': {'DASH': -0.017885008134766338, 'XEM': 0.2451590145706355, 'DOGE': 0.21028685267231811, 'LTC': 0.3396675230101496, 'MAID': -0.023217502484851917, 'BTC': 0.2188873903143038, 'ETH': 0.0070895267193696165, 'XRP': 0.04038308119562005, 'XMR': 0.10439499530126177}, 'MAID': {'DASH': 0.15851962585719734, 'XEM': 0.24638558526778676, 'DOGE': 0.1969666908833168, 'LTC': -0.023217502484851917, 'MAID': 1.544265885009231, 'BTC': 0.05656467031525303, 'ETH': 0.43029061022937815, 'XRP': 0.060012979312135256, 'XMR': 0.25174166270626736}, 'BTC': {'DASH': 0.04633192428471714, 'XEM': 0.3162291738177758, 'DOGE': 0.18023154281182274, 'LTC': 0.2188873903143038, 'MAID': 0.05656467031525303, 'BTC': 0.25654027047139827, 'ETH': 0.06821288200609678, 'XRP': 0.046123059468558665, 'XMR': 0.1305858044189298}, 'ETH': {'DASH': 0.06459627506298854, 'XEM': 0.16795613694062173, 'DOGE': 0.04381871552036941, 'LTC': 0.0070895267193696165, 'MAID': 0.43029061022937815, 'BTC': 0.06821288200609678, 'ETH': 1.804694928609396, 'XRP': 0.07518882101080702, 'XMR': 0.303144422512906}, 'XRP': {'DASH': 0.0485013905397046, 'XEM': 0.20794892099014484, 'DOGE': 0.22505760423798415, 'LTC': 0.04038308119562005, 'MAID': 0.060012979312135256, 'BTC': 0.046123059468558665, 'ETH': 0.07518882101080702, 'XRP': 0.6588605216541893, 'XMR': 0.14127690730888484}, 'XMR': {'DASH': 0.07136735462548086, 'XEM': 0.24474433942952412, 'DOGE': 0.10013128092001486, 'LTC': 0.10439499530126177, 'MAID': 0.25174166270626736, 'BTC': 0.1305858044189298, 'ETH': 0.303144422512906, 'XRP': 0.14127690730888484, 'XMR': 2.1356359326142704}}

portfolio = [1.0 / len(coins)] * len(coins)

def score(dist):
    s = 0
    for i1, c1 in enumerate(coins):
        # s += dist[i1] * returns[i1] / 20
        if dist[i1] < 0:
            s -= 999999
        for i2, c2 in enumerate(coins):
            s -= dist[i1] * dist[i2] * cov[c1][c2]
    return s

p = portfolio
orig_score = score(p)

for i in range(1000):
    for j in range(len(coins)):
        pup = [(p[k] if k != j else p[k] + 0.01) - 0.01 / len(coins) for k in range(len(p))]
        scoreup = score(pup)
        pdown = [(p[k] if k != j else p[k] - 0.01) + 0.01 / len(coins) for k in range(len(p))]
        scoredown = score(pdown)
        if scoreup > orig_score > scoredown:
            p = pup
            orig_score = score(p)
        elif scoredown > orig_score > scoreup:
            p = pdown
            orig_score = score(p)
    print p, orig_score
