Practical data science and optimization: web scraping of crypto prices


In our time, data is a new commodity (like unrefined gold) from which we can derive ideas, insights, knowledge, etc. Basic skills in acquiring and analyzing data are essential skills for modern skilled workers. In this post, we consider a problem of web scraping crypto prices and determine if conditions for triangle arbitrage exist, based on these prices.

There are many tools to scrap crypto prices (see [1-6]). We use python package yfinance to scrap prices of crypto and fiat currencies.

First of all we load the required packages with the following commands:

import pandas as pd

import yfinance as yf

Now we can scrap crypto prices with the following command:

df=yf.download(tickers=’BTC-USD’,period='48h',interval='1m')

This command downloads BTC-USD prices over the last 48 hours with interval of 1 minute into df.

We can do the same for other crypto prices and calculate ROIs on triangle arbitrage.

The script below accomplishes this task. ROI is return on investments in triangle arbitrage without trading fees and ROI1 is return on investments in triangle arbitrage with trading fees of 0.1%.

import pandas as pd

import yfinance as yf

def download_data(t1,t2,t3):

   p1=t2+"-"+t1

   p2=t3+"-"+t2

   p3=t3+"-"+t1

   p1a=t1+"-"+t2

   p2a=t2+"-"+t3

   p3a=t1+"-"+t3

   df1=yf.download(tickers=p1,period='48h',interval='1m')

   df2=yf.download(tickers=p2,period='48h',interval='1m')

   df3=yf.download(tickers=p3,period='48h',interval='1m')

   return [df1,df2,df3]

def process_data(t1,t2,t3,df1,df2,df3):

   p1=t2+"-"+t1

   p2=t3+"-"+t2

   p3=t3+"-"+t1

   df1['Datetime']=df1.index

   df1n=df1[['Open']]

   df1n.rename(columns={'Open':p1},inplace=True)

   df2['Datetime']=df2.index

   df2n=df2[['Open']]

   df2n.rename(columns={'Open':p2},inplace=True)

   df3['Datetime']=df3.index

   df3n=df3[['Open']]

   df3n.rename(columns={'Open':p3},inplace=True)

   print(df3.head())

   df=pd.merge(df2n,df1n,on='Datetime')

   print(df.head())

   df=pd.merge(df,df3n,on='Datetime')

   df['roi']=abs(1/(df[p1]*df[p2])-1/df[p3])*100

   df['roi1']=df['roi']-0.1

   print(df.head(20))

   print("averages ",df['roi'].mean(),df['roi1'].mean())

   return df

#main

t1="USD";t2="BTC";t3="ETH"

[df1,df2,df3]=download_data(t1,t2,t3)

print(df1.head())

process_data(t1,t2,t3,df1,df2,df3)

 

The results are shown below.

 

p1

As we can see, for those traders who have opportunities to trade cryptos without trading fees, there are opportunities to get small positive ROI in triangle arbitrage USD_BTC_ETH. These opportunities disappear when trading fees are applied.

With other base currencies (EUR, JPY, GBP, CNY) we get similar results, which are shown below.

p2

 

p3

 

p4

 

p5

 

References:

[1] https://beautiful-soup-4.readthedocs.io/en/latest/

[2] https://scrapfly.io/

[3] https://zzhu17.medium.com/web-scraping-yahoo-finance-news-a18f9b20ee8a

[4] https://medium.com/@vikasthamke/web-scraping-historical-stock-market-data-from-yahoo-finance-using-selenium-and-beautifulsoup-in-691c625a79f1

[5] https://www.scrapehero.com/scrape-yahoo-finance-stock-market-data/

[6] https://levelup.gitconnected.com/yahoo-finance-web-scraping-with-r-5584d226c3a6

 

 

 

 

How do you rate this article?

21


I_g_o_r
I_g_o_r

I am curious about science, technologies and their applications to solving real problems.


Practical data science & optimization in examples
Practical data science & optimization in examples

This blog gives readers practical examples of data science and optimization problems and their solutions using python scripts. These scripts can be used to solve real problems in business and life.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.