Last active
May 2, 2020 00:38
-
-
Save the023/67cb228724a63c27a205ef108f13eec3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Chart arbitrage of a currency between different exchanges | |
| """ | |
| import pandas as pd | |
| import sys | |
| import matplotlib.pyplot as plt | |
| from time import sleep | |
| import datetime | |
| import ccxt | |
| class ccxt_spread(): | |
| def __init__(self, exchanges, pair, timeframe): | |
| self.exchanges = exchanges | |
| self.pair = pair | |
| self.timeframe = timeframe | |
| self.apisleeptime = 2 | |
| self.apiretry = 5 | |
| def run(self): | |
| df = self.fetch_ohlcv_to_df() | |
| self.dfindexdate(df) | |
| self.graph(df.dropna()) | |
| def graph(self,df): | |
| print(df[-10:]) | |
| # graph relatives prices | |
| ax1 = plt.subplot2grid((2, 1), (0, 0)) | |
| df.plot(ax=ax1,title='Close prices') | |
| # graph arbitrage, compare with first exchange | |
| ax2 = plt.subplot2grid((2, 1), (1, 0)) | |
| base = self.exchanges[0].id | |
| rest = self.exchanges | |
| if len(df[base].values) == 0: | |
| print('need '+base+' datas, try later when API is working') | |
| sys.exit(1) | |
| exchangesok = [] | |
| for ex in rest: | |
| if ex.id in df: | |
| name = 'arbi_' + ex.id | |
| df[name] = ((df[ex.id] / df[base]) - 1) * 100 | |
| exchangesok.append(name) | |
| df[exchangesok].plot(ax=ax2,title='Arbitrage '+self.pair) | |
| plt.show() | |
| def fetch_ohlcv_to_df(self): | |
| responses = {} | |
| for ex in self.exchanges: | |
| for i in range(self.apiretry): | |
| sleep(self.apisleeptime) | |
| print ('get ' + ex.id + ' try ' + str(i)) | |
| try: | |
| responses[ex.id] = ex.fetch_ohlcv(symbol=self.pair, timeframe=self.timeframe) | |
| break | |
| except Exception as e: | |
| print(' Error: Exception' + repr(e)) | |
| responses[ex.id] = [] | |
| return self.ohlcv2dataframe(responses) | |
| def ohlcv2dataframe(self, responses): | |
| df = None | |
| for ex in self.exchanges: | |
| if responses[ex.id] == []: continue | |
| # build df, reverse exchange df, extract close, rename column | |
| dfex = pd.DataFrame(responses[ex.id], columns=['mts', 'open', 'high', 'low', 'close', 'volume']).set_index('mts')[::-1] | |
| dfex = dfex[['close']] | |
| dfex.columns = [ex.id] | |
| # join dfexs | |
| df = dfex[[ex.id]] if df is None else df.join(dfex[[ex.id]]) | |
| return df | |
| def dfindexdate(self, df, dateformat='%Y-%m-%d %H:%M:%S'): | |
| df.index = map(lambda x: datetime.datetime.fromtimestamp(int(x / 1000)).strftime(dateformat), df.index) | |
| ccxt_spread( | |
| [ccxt.bitfinex(), ccxt.gdax(), ccxt.kraken()], # first is used as comparaison for arbitrage | |
| 'BTC/EUR', | |
| '1h' # 1d, 1h 30m 15m | |
| ).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment