Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. def optimize( 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. #3: BTC
  19. #4: ETH
  20. #5: ETC
  21. #6: LTC
  22. #7: DASH
  23. #8: NEO
  24. #9: ZEC
  25. #10: XMR
  26. for j in range(len(weights)):
  27. results[j+3,i]= weights[j]
  28. pd.DataFrame(results).to_csv("sharpeRatios50kResults.csv")
  29. plotResults(results)
  30.  
  31. def plotResults(results):
  32. heat = results[0]/(results[2])
  33. scatter = []
  34. for i in range(50000):
  35. scatter.append([str(results[2][i]),
  36. str(results[0][i]),
  37. "BTC: "+str(results[3][i]),
  38. "ETH: "+str(results[4][i]),
  39. "ETC: "+str(results[5][i]),
  40. "LTC: "+str(results[6][i]),
  41. "DASH: "+str(results[7][i]),
  42. "NEO: "+str(results[8][i]),
  43. "ZEC: "+str(results[9][i]),
  44. "XMR: "+str(results[10][i])])
  45. if results[0][i]/results[2][i] == max(heat):
  46. maxX = results[2][i]
  47. maxY = (results[0][i])
  48. recommendedPortfolio = [
  49. "Recommended Portfolio Distribution:",
  50. "BTC: "+str(results[3][i]),
  51. "ETH: "+str(results[4][i]),
  52. "ETC: "+str(results[5][i]),
  53. "LTC: "+str(results[6][i]),
  54. "DASH: "+str(results[7][i]),
  55. "NEO: "+str(results[8][i]),
  56. "ZEC: "+str(results[9][i]),
  57. "XMR: "+str(results[10][i])]
  58. plt.scatter(x = results[2], y = results[0], c=[str(point/255) for point in heat])
  59. plt.scatter(x = maxX, y = maxY, c = 200, s = 75, alpha = .2)
  60. plt.title('50,000 Portfolios, Sharpe Ratio')
  61. plt.xlabel('Volatility')
  62. plt.ylabel('Returns')
  63. plt.savefig("sharpeRatios50k.png")
  64. print()
  65. for line in recommendedPortfolio:
  66. print(line)
  67. pd.DataFrame(scatter).to_csv("scatterPoints.csv")
  68. pd.DataFrame(recommendedPortfolio).to_csv("recommendedPortfolio.csv")
  69. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement