0% found this document useful (0 votes)
307 views8 pages

Cryptocurrency Optimization: 1 Import The Libraries

This document describes a Python program that optimizes cryptocurrency portfolios by analyzing historical price data and calculating the Sharpe ratio of randomly generated portfolios. It pulls price data for several cryptocurrencies, builds a dataset merging this data, calculates returns, generates 50,000 random portfolios, and identifies the portfolio with the highest Sharpe ratio. Functions are defined to retrieve data, merge datasets, calculate returns, generate random portfolios, and identify the optimal portfolio. The results are plotted and the recommended portfolio is printed and saved.

Uploaded by

Samuel Peoples
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views8 pages

Cryptocurrency Optimization: 1 Import The Libraries

This document describes a Python program that optimizes cryptocurrency portfolios by analyzing historical price data and calculating the Sharpe ratio of randomly generated portfolios. It pulls price data for several cryptocurrencies, builds a dataset merging this data, calculates returns, generates 50,000 random portfolios, and identifies the portfolio with the highest Sharpe ratio. Functions are defined to retrieve data, merge datasets, calculate returns, generate random portfolios, and identify the optimal portfolio. The results are plotted and the recommended portfolio is printed and saved.

Uploaded by

Samuel Peoples
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cryptocurrency Optimization

January 10, 2018

1 Import the libraries


Determines best portfolio allocation between BTC ETH ETC LTC DASH NEO ZEC
XMR
Uses sharpe ratio, nds low volatility with high returns. Designed to be ran Monthly
Author: Samuel L. Peoples
Version date: 20171210
Latest run date: 20180110
In [12]: from [Link] import urlopen
import [Link] as plt
% matplotlib inline
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import datetime
import re
import os

2 Build Crypto List Function


This function builds the individual lists for the three months,
For BTC ETH ETC LTC DASH NEO ZEC, XMR the function will pull Time, High price
(USD), Low price (USD), Open Price (USD) Close price (USD), Volume From, Volume
To, and the Percent Returns
It will then save the data to [name][Link]
In [3]: def buildCrpytoList(html, name):
list = []
htmlOpen = urlopen(html)
htmlSoup = BeautifulSoup(htmlOpen, '[Link]')

1
htmlRows = str(htmlSoup).split('[')
del(htmlRows[0])
del(htmlRows[0])
del(htmlRows[0])
for i in range(htmlRows.__len__()-1):
htmlRows[i] = str(htmlRows[i]).split(']')[0]
htmlRows[htmlRows.__len__()-1] = str(
htmlRows[htmlRows.__len__()-1]).split(']')[0]
for row in htmlRows:
if row == '':
break
items = [Link](",")

returns = returnVal(float(items[1]),float(items[4]))
entry = {
"Time":timeFix(items[0]),
name+"Close":items[4],
name+"High":items[2],
name+"Low":items[3],
name+"Open":items[1],
name+"Volume":items[5],
name+"Returns":str(returns)}
[Link](entry)
df = [Link](list)
[Link](columns = ["Time",name+"High", name+"Low",
name+"Open",name+"Close", name+"Volume",
name+"Returns"]).to_csv(name+"[Link]",
header ={"Time","High",
"Low", "Open",
"Close", "Volume"})
return df

3 Helper Functions
This function converts timestamps to place legible timestamps in the dataset
In [4]: def timeFix(timestamp):
return([Link](
int(timestamp)).strftime('%Y-%m-%d %H:%M:%S'))

This function bypasses divZero errors.


In [5]: def returnVal(num, den):
if den <=0:
return 0
else:
diff = den-num
return diff/num

2
4 Build Master List Function
This fucntion builds the master list by merging all currency lists by date.
It will then save the data to [Link]
In [7]: def buildMaster(coins, coinsNames, backTesting):
masterList = []
for i in range(8):
[Link](buildCrpytoList(coins[i], coinsNames[i]))
masterDF = masterList[0].merge(masterList[1], left_on = "Time",
right_on = 'Time', how= 'outer').merge(
masterList[2], left_on = "Time", right_on = 'Time', how= 'outer').merge(
masterList[3], left_on = "Time", right_on = 'Time', how= 'outer').merge(
masterList[4], left_on = "Time", right_on = 'Time', how= 'outer').merge(
masterList[5], left_on = "Time", right_on = 'Time', how= 'outer').merge(
masterList[6], left_on = "Time", right_on = 'Time', how= 'outer').merge(
masterList[7], left_on = "Time", right_on = 'Time', how= 'outer')
[Link](columns = [
"Time",
coinsNames[0]+"High",coinsNames[0]+"Low",coinsNames[0]
+"Open",coinsNames[0]+"Close",coinsNames[0]+"Volume",coinsNames[0]+"Returns",
coinsNames[1]+"High",coinsNames[1]+"Low",coinsNames[1]
+"Open",coinsNames[1]+"Close",coinsNames[1]+"Volume",coinsNames[1]+"Returns",
coinsNames[2]+"High",coinsNames[2]+"Low",coinsNames[2]
+"Open",coinsNames[2]+"Close",coinsNames[2]+"Volume",coinsNames[2]+"Returns",
coinsNames[3]+"High",coinsNames[3]+"Low",coinsNames[3]
+"Open",coinsNames[3]+"Close",coinsNames[3]+"Volume",coinsNames[3]+"Returns",
coinsNames[4]+"High",coinsNames[4]+"Low",coinsNames[4]
+"Open",coinsNames[4]+"Close",coinsNames[4]+"Volume",coinsNames[4]+"Returns",
coinsNames[5]+"High",coinsNames[5]+"Low",coinsNames[5]
+"Open",coinsNames[5]+"Close",coinsNames[5]+"Volume",coinsNames[5]+"Returns",
coinsNames[6]+"High",coinsNames[6]+"Low",coinsNames[6]
+"Open",coinsNames[6]+"Close",coinsNames[6]+"Volume",coinsNames[6]+"Returns",
coinsNames[7]+"High",coinsNames[7]+"Low",coinsNames[7]
+"Open",coinsNames[7]+"Close",coinsNames[7]+"Volume",coinsNames[7]+"Returns",]
).to_csv(
"[Link]", header ={"Time","High",
"Low", "Open", "Close","Volume"})

return masterList

5 Calculate and populate returns


This function builds the list of percent returns, prints the percent returns for today and
average percent returns for the three months.
It will then save the data to [Link]

3
In [8]: def buildReturns():
data = pd.read_csv('[Link]')
data.set_index('Time',inplace=True)
returns = [Link]([Link][[0,1,2,3,4,5,7,8,9,10,
11,13,14,15,16,17,19,
20,21,22,23,25,26,27,
28,29,31,32,33,34,35,
37,38,39,40,41,43,44,
45,46,47]],axis =1)
returns.to_csv("[Link]")
print ("Percent Returns on Closure:")
print([Link][-1])
print()
print("Average Percent Returns on Closure:")
print([Link]())
return returns

6 Optimization
This function will develop 50,000 randomized portfolios, and nd the portfolio with
the highest sharpe ratio. ([Link]
It then saves the results to [Link] and passes them to be plotted.
In [9]: def optimize(masterList, returns):
mean_returns = [Link]()
matCov = [Link]()
coins = ["btc", "eth", "etc", "ltc", "dash", "neo", "zec", "xmr"]
port = 50000
results = [Link]((4 + len(coins)-1, port))
for i in range(port):
weights = [Link]([Link](8))
weights /= [Link](weights)
port_return = [Link](mean_returns*weights)*1140
port_stdv = [Link]([Link](weights.T,[Link](matCov, weights)))*[Link](1140)
#Returns based on this portfolio
results[0,i] = port_return
#standard deviation based on this portfolio
results[1,i] = port_stdv
#Variance of this portfolio
results[2,i] = results[0,i]/ (results[1,i])

#3: BTC
#4: ETH
#5: ETC
#6: LTC
#7: DASH
#8: NEO

4
#9: ZEC
#10: XMR
for j in range(len(weights)):
results[j+3,i]= weights[j]
[Link](results).to_csv("[Link]")
plotResults(results)

7 Plot the results


This function will develop the scatterplot of 50,000 portfolios, x = Volatility, y = Returns
It then saves the gure to [Link].
The function will then print the suggested portfolio allocation, and save the contents
to [Link]
In [10]: def plotResults(results):
heat = results[0]/(results[2])
scatter = []
for i in range(50000):
[Link]([str(results[2][i]),
str(results[0][i]),
"BTC: "+str(results[3][i]),
"ETH: "+str(results[4][i]),
"ETC: "+str(results[5][i]),
"LTC: "+str(results[6][i]),
"DASH: "+str(results[7][i]),
"NEO: "+str(results[8][i]),
"ZEC: "+str(results[9][i]),
"XMR: "+str(results[10][i])])
if results[0][i]/results[2][i] == max(heat):
maxX = results[2][i]
maxY = (results[0][i])
recommendedPortfolio = [
"Recommended Portfolio Distribution:",
"BTC: "+str(results[3][i]),
"ETH: "+str(results[4][i]),
"ETC: "+str(results[5][i]),
"LTC: "+str(results[6][i]),
"DASH: "+str(results[7][i]),
"NEO: "+str(results[8][i]),
"ZEC: "+str(results[9][i]),
"XMR: "+str(results[10][i])]
[Link](x = results[2], y = results[0], c=[str(point/255) for point in heat])
[Link](x = maxX, y = maxY, c = 200, s = 75, alpha = .2)
[Link]('50,000 Portfolios, Sharpe Ratio')
[Link]('Volatility')
[Link]('Returns')

5
[Link]("[Link]")
print()
for line in recommendedPortfolio:
print(line)
[Link](scatter).to_csv("[Link]")
[Link](recommendedPortfolio).to_csv("[Link]")
[Link]()

8 Go Wild
Dene the api locations and let her rip
In [11]: backTesting = True
path = [Link](" ",str([Link]()))[0]
if not [Link](path):
[Link](path)
[Link](path)
btc = "[Link]
eth = "[Link]
etc = "[Link]
ltc = "[Link]
dash ="[Link]
neo = "[Link]
zec = "[Link]
xmr = "[Link]
coins = [btc, eth, etc, ltc, dash, neo, zec, xmr]
coinsNames = ["btc", "eth", "etc", "ltc", "dash", "neo", "zec", "xmr"]
masterList = buildMaster(coins, coinsNames, backTesting)
optimize(masterList, buildReturns())

Percent Returns on Closure:


btcReturns -0.013504
ethReturns -0.030195
etcReturns -0.049800
ltcReturns -0.031777
dashReturns -0.035785
neoReturns -0.046518
zecReturns -0.069189
xmrReturns -0.036899
Name: 2018-01-11 [Link], dtype: float64

Average Percent Returns on Closure:


btcReturns 0.002251
ethReturns 0.002921
etcReturns 0.002924
ltcReturns 0.003391
dashReturns 0.002838
neoReturns 0.003489

6
zecReturns 0.002657
xmrReturns 0.003389
dtype: float64

C:\Users\Ripti\Anaconda3\lib\site-packages\ipykernel_launcher.py:12: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:


[Link]
if [Link][0] == '':

Recommended Portfolio Distribution:


BTC: 0.0021076267919
ETH: 0.0893510945295
ETC: 0.287048272754
LTC: 0.0406171784081
DASH: 0.000358525253673
NEO: 0.450344662471
ZEC: 0.129367477968
XMR: 0.000805161823629

7
8

You might also like