Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. def optimize(masterList, returns):
  2. mean_returns = returns.mean()
  3. matCov = returns.cov()
  4. coins = ["btc", "eth", "etc", "ltc", "dash", "neo", "zec", "xmr"]
  5. port = 50000
  6. results = np.zeros((4 + len(coins)-1, port))
  7. for i in range(port):
  8. weights = np.array(np.random.random(8))
  9. weights /= np.sum(weights)
  10. port_return = np.sum(mean_returns*weights)*1140
  11. port_stdv = np.sqrt(np.dot(weights.T,np.dot(matCov, weights)))*np.sqrt(1140)
  12. #Returns based on this portfolio
  13. results[0,i] = port_return
  14. #standard deviation based on this portfolio
  15. results[1,i] = port_stdv
  16. #Variance of this portfolio
  17. results[2,i] = results[0,i]/ (results[1,i])
  18.  
  19. #3: BTC
  20. #4: ETH
  21. #5: ETC
  22. #6: LTC
  23. #7: DASH
  24. #8: NEO
  25. #9: ZEC
  26. #10: XMR
  27. for j in range(len(weights)):
  28. results[j+3,i]= weights[j]
  29. pd.DataFrame(results).to_csv("sharpeRatios50kResults.csv")
  30. plotResults(results)
  31.  
  32. def plotResults(results):
  33. heat = results[0]/(results[2])
  34. scatter = []
  35. for i in range(50000):
  36. scatter.append([str(results[2][i]),
  37. str(results[0][i]),
  38. "BTC: "+str(results[3][i]),
  39. "ETH: "+str(results[4][i]),
  40. "ETC: "+str(results[5][i]),
  41. "LTC: "+str(results[6][i]),
  42. "DASH: "+str(results[7][i]),
  43. "NEO: "+str(results[8][i]),
  44. "ZEC: "+str(results[9][i]),
  45. "XMR: "+str(results[10][i])])
  46. if results[0][i]/results[2][i] == max(heat):
  47. maxX = results[2][i]
  48. maxY = (results[0][i])
  49. recommendedPortfolio = [
  50. "Recommended Portfolio Distribution:",
  51. "BTC: "+str(results[3][i]),
  52. "ETH: "+str(results[4][i]),
  53. "ETC: "+str(results[5][i]),
  54. "LTC: "+str(results[6][i]),
  55. "DASH: "+str(results[7][i]),
  56. "NEO: "+str(results[8][i]),
  57. "ZEC: "+str(results[9][i]),
  58. "XMR: "+str(results[10][i])]
  59. plt.scatter(x = results[2], y = results[0], c=[str(point/255) for point in heat])
  60. plt.scatter(x = maxX, y = maxY, c = 200, s = 75, alpha = .2)
  61. plt.title('50,000 Portfolios, Sharpe Ratio')
  62. plt.xlabel('Volatility')
  63. plt.ylabel('Returns')
  64. plt.savefig("sharpeRatios50k.png")
  65. print()
  66. for line in recommendedPortfolio:
  67. print(line)
  68. pd.DataFrame(scatter).to_csv("scatterPoints.csv")
  69. pd.DataFrame(recommendedPortfolio).to_csv("recommendedPortfolio.csv")
  70. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement