def __init__(self, ctySize=1): self.exchange = ccxt.gdax(self.getConfig()) self.market = self.exchange.market('BTC/EUR') self.account_id = '37a4a4f2-7188-44d5-857e-575ea110f9a5' self.reporter = None self.positionManager = None self.ctySize = ctySize
def _createExchangeObject(self, exchange): ''' Depricated ... now instantiating class using "eval" in __init__''' if exchange == "Binance": exchange = ccxt.binance() elif exchange == "Bitfinex": exchange = ccxt.bitfinex2() elif exchange == "Bitmex": exchange = ccxt.bitmex() elif exchange == "BitStamp": exchange = ccxt.binance() elif exchange == "Bittrex": exchange = ccxt.binance() elif exchange == "Bithumb": exchange = ccxt.bithumb() elif exchange == "CCTX": exchange = ccxt.cctx() elif exchange == "GDAX": exchange = ccxt.gdax() elif exchange == "HitBC": exchange = ccxt.hitbc() elif exchange == "Huobipro": exchange = ccxt.huobipro() elif exchange == "Kraken": exchange = ccxt.kraken() elif exchange == "OKEx": exchange = ccxt.okex() else: raise "Exception ExchangeCCXT::_createExchangeObject - ccxt Exchange not valid" return exchange
def __init__(self): self.api = gdax({ 'apiKey': getenv('gdax_api_key'), 'secret': getenv('gdax_api_secret'), 'password': getenv('gdax_api_passphrase') }) self.accounts = pd.DataFrame(self.api.privateGetAccounts())
def job(): ##symbols = ['BCH/USD', 'ETH/USD', 'LTC/USD', 'DASH/USD'] symbols = ['BCH/USD', 'LTC/USD', 'ETH/USD'] pairs = [(r, s) for s in symbols for r in symbols if r is not s] gdax = ccxt.gdax({'password': '', 'apiKey': '', 'secret': ''}) gdax.load_markets() bitfinex = ccxt.bitfinex({'apiKey': '', 'secret': ''}) bitfinex.load_markets() print("Connected to exchanges") exchanges = [gdax, bitfinex] for r, s in pairs: print("Fetching values") asks, bids = {}, {} for x in exchanges: # TODO check if exchange supports symbol rPrice = x.fetch_ticker(r) sPrice = x.fetch_ticker(s) print(x.name, rPrice['ask'], rPrice['bid']) print(x.name, sPrice['bid'], sPrice['ask']) asks[x.name] = rPrice['ask'] / rPrice['bid'] bids[x.name] = sPrice['bid'] / sPrice['ask'] lowX = min(asks, key=asks.get) highX = max(bids, key=bids.get) print(r, s, bids[highX] / asks[lowX]) if (bids[highX] / asks[lowX]) > 1.005: print("CONGRATS you found an opportunity") print("Short %s on %s, Long %s on %s" % (s, highX, r, lowX)) time.sleep(1)
def results(request): output = "" if request.method == 'POST': form = SimulationForm(request.POST) if form.is_valid(): amount = form.cleaned_data['amount'] base_currency = form.cleaned_data['base_currency'] quote_currency = form.cleaned_data['quote_currency'] duration = form.cleaned_data['duration'] use_fees = form.cleaned_data['include_fees'] sim = simulation.ArbitrageSimulation( ccxt.exmo(), ccxt.gdax(), amount, base_currency + "/" + quote_currency) old_stdout = sys.stdout result = StringIO() sys.stdout = result sim.start_simulation(duration, include_fees=use_fees) sim.create_trade_visuals() sys.stdout = old_stdout result_list = result.getvalue().split("\n") result_string = [] for s in result_list: if s is not "": result_string.append(s) output = result_string context = { 'form': form, 'duration': duration, 'amount': amount, 'output': output } return render(request, "crypto2/results.html", context) else: form = SimulationForm() return render(request, "crypto2/index.html", {'form': form})
def __init__(self): self.gdax = ccxt.gdax({'nonce': ccxt.gdax.milliseconds}) if (settings['gdax_exporter'].get('api_key') and (settings['gdax_exporter'].get('api_secret'))): self.gdax.apiKey = settings['gdax_exporter'].get('api_key') self.gdax.secret = settings['gdax_exporter'].get('api_secret') self.hasApiCredentials = True
def get_dataframe(): exchange = ccxt.gdax() ohlcv = exchange.fetch_ohlcv('BTC/USD', '1d') assert len(ohlcv) == 300 df = pd.DataFrame(ohlcv, columns=DOHLCV_COLUMNS) df['date'] = pd.to_datetime(df['date'], unit='ms') df = df.set_index('date') return df
def getEtherUSD(): symbol = 'ETH/USD' exchange = ccxt.gdax() markets = exchange.load_markets() ticker = exchange.fetch_ticker(symbol.upper()) print(ticker) avgPrice = (ticker['bid'] + ticker['ask']) / 2 print(avgPrice) return avgPrice
def __init__(self, keypath): with open(keypath, "r") as f: passphrase = f.readline().strip() key = f.readline().strip() secret = f.readline().strip() Exchange.__init__( self, 'Gdax', gdax({ 'apiKey': key, 'secret': secret, 'password': passphrase }))
def __init__(self, key=None, secret=None, currency_from="BTC", currency_to="ZAR"): super().__init__(currency_from, currency_to) self.exchange = ccxt.gdax() self.exchange.apiKey = key self.exchange.secret = secret self.set_sell_fees(variable=2.5 / 100) self.set_buy_fees(variable=0) self.set_send_fees(variable=0.1 / 100) self.set_receive_fees(fixed=0.0002) self.set_withdrawl_fees(fixed=0.15) self.set_deposit_fees(fixed=0.15)
def get(cls): return { 'binance': ccxt.binance({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'bitstamp': ccxt.bitstamp({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'bittrex': ccxt.bittrex({ 'apiKey': os.environ.get("BITTREX_KEY"), 'secret': os.environ.get("BITTREX_SECRET"), }), 'gdax': ccxt.gdax({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'hitbtc': ccxt.hitbtc({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'kraken': ccxt.kraken({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'liqui': ccxt.liqui({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), 'poloniex': ccxt.poloniex({ 'apiKey': 'YOUR_PUBLIC_API_KEY', 'secret': 'YOUR_SECRET_PRIVATE_KEY', }), # add additional supported exchanges }
def __init__(self): self.url = "wss://ws-feed.gdax.com" self.public_client = ccxt.gdax() self.product_ids = GDAX_PRODUCT_IDS self.order_books = {x: {} for x in self.product_ids} self.inside_order_books = { x: {"bids": {}, "asks": {}} for x in self.product_ids } self.last_trade_ids = {x: None for x in self.product_ids} self.db = Database(DATABASE['GDAX'], migrate=True) self.ws = websocket.WebSocketApp( self.url, on_message=self.on_message, on_error=self.on_error, on_open=self.on_open, )
def ETH_Kraken_Ticker(): i=0 last=[1] period = 60 start_Time = time.time() exchange = ccxt.gdax() markets = exchange.load_markets() developing_candle_sticks = [] candle_sticks = [None] j = 0 print('Hello, I am Crypto Bot! \n Starting Trading..... ') opens = None high = None low = None close = None while True: coin_name = exchange.fetchTicker('ETH/USD')['symbol'] current = float(exchange.fetchTicker('ETH/USD')['last']) change = ((current - last[i])/last[i])*100 last.append( current ) print('%s $%.2f %.2f%%' %(coin_name,current,change)) if candles(current,start_Time,period,candle_sticks,opens, high, low, close) is True: start_Time = time.time() j+=1 opens = None high = None low = None close = None print('Open: $%.2f, High: $%.2f, Low: $%.2f, Close: $%.2f, Average: $%.2f'%(candle_sticks[j][0],candle_sticks[j][1],candle_sticks[j][2],candle_sticks[j][3],candle_sticks[j][4])) else: opens,high,low,close = (candles(current,start_Time,period,candle_sticks,opens, high, low, close)) i+=1 time.sleep(1)
def get_exchange(exchange): config = CONFIG[exchange] if exchange == "gdax": return ccxt.gdax({ "api_key": config['api_key'], "secret": config['secret'], "password": config['password'] }) if exchange == "binance": return ccxt.binance({ "api_key": config['api_key'], "secret": config['secret'] }) if exchange == "hitbtc": return ccxt.hitbtc({ "api_key": config['api_key'], "secret": config['secret'] }) if exchange == "cryptopia": return ccxt.cryptopia({ "api_key": config['api_key'], "secret": config['secret'] })
def preloadData(self, iteration=100, interval=1): print("Starting preload") data = self.makeFetchDF() exchange = ccxt.gdax() trade_id = 0 i = 0 j = 0 while i < iteration: if i % 20 == 0 and i != j: j = i print("Currently at iteration ", i) startTime = timer() trade_data, new_trade_id = self.fetchExchangeData(exchange) print('fetch gdax data time: ', timer() - startTime) if new_trade_id != trade_id: trade_id = new_trade_id data = pd.concat([data, trade_data]) i += 1 #data.tail(1)[trade_id] time.sleep(interval) self.last_trade_id = trade_id return data.sort_values(by='trades_date_time'), trade_id
# ----------------------------------------------------------------------------- import ccxt # noqa: E402 # ----------------------------------------------------------------------------- # common constants msec = 1000 minute = 60 * msec hold = 30 # ----------------------------------------------------------------------------- exchange = ccxt.gdax({ 'rateLimit': 3000, 'enableRateLimit': True, # 'verbose': True, }) # ----------------------------------------------------------------------------- from_datetime = '2017-09-01 00:00:00' from_timestamp = exchange.parse8601(from_datetime) # ----------------------------------------------------------------------------- now = exchange.milliseconds() # ----------------------------------------------------------------------------- data = []
profit_cumulative = [] profit_cumulative.append(self.profit[0]) for i in range(1, len(self.profit)): profit_cumulative.append(self.profit[i]\ + profit_cumulative[i - 1]) plt.plot(range(len(profit_cumulative)), profit_cumulative) plt.suptitle("Cumulative Profit over Time") plt.ylabel("Cumulative Profit (" + self.quote_currency + ")") plt.xlabel("Time (number of trades since start)") plt.savefig("crypto/static/graphs/output_profit.png") plt.clf() if __name__ == "__main__": CHOICES = [ ArbitrageSimulation(ccxt.bittrex(), ccxt.hitbtc(), "BTC/USDT"), ArbitrageSimulation(ccxt.exmo(), ccxt.gdax(), "BTC/USD"), ArbitrageSimulation(ccxt.exmo(), ccxt.kraken(), "BTC/EUR"), ArbitrageSimulation(ccxt.bitfinex(), ccxt.exmo(), "ETH/USD"), ] if len(sys.argv) > 1: # can provide commandline args to run simulation PARSER = argparse.ArgumentParser(description="Produces visual output"\ + " from simulator with the given parameters.") PARSER.add_argument("simulation_type", help="whether to simulate a"\ + " duration (0) or for to time a profit (1).", type=int,\ choices=[0, 1]) PARSER.add_argument("limit_value",\ help="If duration, the time in minutes if timing a profit,"\ + " the amount of profit.", type=float) PARSER.add_argument("simulation_choice",\ help="Which default simulation to use.", type=int,\ choices=range(len(CHOICES)))
root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(root + '/python') import ccxt # noqa: E402 # ----------------------------------------------------------------------------- # common constants msec = 1000 minute = 60 * msec hold = 30 # ----------------------------------------------------------------------------- exchange = ccxt.gdax() # ----------------------------------------------------------------------------- from_datetime = '2017-09-01 00:00:00' from_timestamp = exchange.parse8601(from_datetime) # ----------------------------------------------------------------------------- now = exchange.milliseconds() # ----------------------------------------------------------------------------- data = [] while from_timestamp < now:
import pandas from statistics import stdev import numpy as np import matplotlib.pyplot as plt import ccxt import time exchange = ccxt.gdax({ 'rateLimit': 10000, 'enableRateLimit': True, 'verbose': False, }) buying = False collection_size = 50 # decides the size of the samples taken to calculate the avg and stdev curr_accept_price = None percent_gain = 1.002 curr_buy_order_price = None last_points = [] while True: if len(last_points) < collection_size: print(len(last_points)) ticker = exchange.fetch_ticker('BTC/USD') current_price = ticker['last'] last_points.append(current_price) if len(last_points) == collection_size: buying = True
root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(root) import ccxt def dump(*args): print(' '.join([str(arg) for arg in args])) # instantiate exchanges gdax = ccxt.gdax({ 'apiKey': '92560ffae9b8a01d012726c698bcb2f1', # standard 'secret': '9aHjPmW+EtRRKN/OiZGjXh8OxyThnDL4mMDre4Ghvn8wjMniAr5jdEZJLN/knW6FHeQyiz3dPIL5ytnF0Y6Xwg==', 'password': '******', # GDAX requires a password! }) gdax.urls[ 'api'] = 'https://api-public.sandbox.gdax.com' # use the testnet for GDAX hitbtc = ccxt.hitbtc({ 'apiKey': '18339694544745d9357f9e7c0f7c41bb', 'secret': '8340a60fb4e9fc73a169c26c7a7926f5', }) quadrigacx = ccxt.quadrigacx({ 'apiKey': 'jKvWkMqrOj', 'secret': 'f65a2e3bf3c73171ee14e389314b2f78', 'uid': '395037', # QuadrigaCX requires uid!
# -*- coding: utf-8 -*- import os import sys root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(root + '/python') import ccxt # noqa: E402 gdax = ccxt.gdax({ 'apiKey': "a43edfe629bc5991acc83a536ac6358e", 'secret': "xOvq+iH8NT07TheFB/fmY3GcnMZMwP7Xct9zwWtAZxsCbJh8rxeEe/0BGxfbV2em7P9iqQD7/TJGqmsDO8B/kw==", 'password': '******', 'verbose': True, # switch it to False if you don't want the HTTP log }) # move gdax to sandbox gdax.urls['api'] = 'https://api-public.sandbox.gdax.com' print(gdax.fetch_balance())
bl3p_db = dataset.connect('mysql://*****:*****@139.59.147.221/bl3p') btcmarkets_db = dataset.connect('mysql://*****:*****@139.59.147.221/btcmarkets') coinfloor_db = dataset.connect('mysql://*****:*****@139.59.147.221/coinfloor') gemini_db = dataset.connect('mysql://*****:*****@139.59.147.221/gemini') tidex_db = dataset.connect('mysql://*****:*****@139.59.147.221/tidex') cex_db = dataset.connect('mysql://*****:*****@139.59.147.221/cex') koineks_db = dataset.connect('mysql://*****:*****@139.59.147.221/koineks') # DEFINE DATABASE CONNECTION FOR TECHNICAL INDICATORS indicator_db = dataset.connect('mysql://*****:*****@139.59.147.221/indicators') historical_db = dataset.connect('mysql://*****:*****@139.59.147.221/historical') gdax = ccxt.gdax({ 'rateLimit': 3000, 'enableRateLimit': True, # 'verbose': True, 'exchangeName': "gdax", 'database': gdax_db }) bitfinex = ccxt.bitfinex({ 'rateLimit': 3000, 'enableRateLimit': True, # 'verbose': True, 'exchangeName': "bitfinex", 'database': bitfinex_db }) huobipro = ccxt.huobipro({ 'rateLimit': 3000, 'enableRateLimit': True,
ccxt.coinone(), ccxt.coinsecure(), ccxt.coinspot(), ccxt.coolcoin(), ccxt.cryptopia(), ccxt.dsx(), ccxt.ethfinex(), ccxt.exmo(), ccxt.exx(), ccxt.flowbtc(), ccxt.foxbit(), ccxt.fybse(), ccxt.fybsg(), ccxt.gatecoin(), ccxt.gateio(), ccxt.gdax(), ccxt.gemini(), ccxt.getbtc(), ccxt.hadax(), ccxt.hitbtc(), ccxt.hitbtc2(), ccxt.huobi(), ccxt.huobicny(), ccxt.huobipro(), ccxt.ice3x(), ccxt.independentreserve(), ccxt.indodax(), ccxt.itbit(), ccxt.jubi(), ccxt.kraken(), ccxt.kucoin(),
oversolds = [await check_rsi(symbol, t, exchange) for t in timeframe] print(exchange.name) list(map(print_values, oversolds)) if sum(o.get('oversold') == True for o in oversolds) > 3: alert(exchange.name + ": " + symbol + " oversold alert") print("") async def check(exchange, timeframe): while True: try: [ await check_oversold(symbol, exchange, timeframe) for symbol in exchange.load_markets() ] except Exception as e: print(e) exchanges = [(ccxt.gdax(), ['5m', '15m', '1h', '6h']), (ccxt.binance(), ['5m', '15m', '1h', '4h'])] # too low volume # (ccxt.bittrex(), ['5m', '30m', '1h', '1d']), # (ccxt.poloniex(), ['5m', '15m', '2h', '4h']), tasks = [check(e, t) for e, t in exchanges] alert("RSI Alert Bot started") asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))
def initialize_exchange(self): self.exchange = ccxt.gdax() self.exchange.fetch_markets() self.initialize_api() return self.exchange
from ccxt_sandbox_api import sandbox_api_key, sandbox_secret_key, sandbox_passphrase from ccxt_real_api import real_api_key, real_secret_key # Import the RNN packages import tensorflow as tf from tensorflow.python.ops import rnn, rnn_cell import numpy as np import pandas as pd import matplotlib.pyplot as plt # Instantiate sandbox gdax api to execute practice trades sandbox = ccxt.gdax({'apiKey': sandbox_api_key, 'secret': sandbox_secret_key, 'password': sandbox_passphrase, 'nonce': ccxt.gdax.seconds, 'verbose': False}) # If verbose is True, log HTTP requests sandbox.urls['api'] = 'https://api-public.sandbox.gdax.com' # Instantiate real gdax api to get live data feed gdax = ccxt.gdax({'apiKey': real_api_key, 'secret': real_secret_key, 'password': real_passphrase, 'nonce': ccxt.gdax.seconds, 'verbose': False}) # If verbose is True, log HTTP requests gdax.urls['api'] = 'https://api.gdax.com' delay = 2 # delay in seconds
# Code inputs: N_pairs_with_tether = 10 # Number of pairs to print in exchanges possibly with tether N_pairs_without_tether = 10 # Number of pairs to print in exchanges without tether verbose = False # Do not print market fetching status # Exchanges with tether (you can transfer USDT between exchanges) bittrex = ccxt.bittrex() # 266 markets binance = ccxt.binance() poloniex = ccxt.poloniex() # 99 markets kraken = ccxt.kraken() # 59 markets # Exchanges without tether (cannot transfer USD between exchanges) cex = ccxt.cex() # 27 markets bitstamp = ccxt.bitstamp() # 15 markets gdax = ccxt.gdax() # 10 markets exchanges = [bittrex, kraken, poloniex, gdax, cex, bitstamp, binance] # Load markets and check if exchanges are working working_exchanges = [] for exchange in exchanges: # Try loading the markets, if you can, add it to the working exchanges list try: exchange.load_markets() working_exchanges.append(exchange) # If you cannot, say so, and dont add it to the list except: print('%s is down! Excluding %s'%(exchange.name, exchange.name)) # Padding for printing out names
sys.path.append(root + '/python') import ccxt # noqa: E402 # ----------------------------------------------------------------------------- # common constants msec = 1000 minute = 60 * msec hold = 30 # ----------------------------------------------------------------------------- exchange = ccxt.gdax({ 'rateLimit': 3000, 'enableRateLimit': True, # 'verbose': True, }) # ----------------------------------------------------------------------------- from_datetime = '2017-09-01 00:00:00' from_timestamp = exchange.parse8601(from_datetime) # ----------------------------------------------------------------------------- now = exchange.milliseconds() # ----------------------------------------------------------------------------- data = []
# ----------------------------------------------------------------------------- this_folder = os.path.dirname(os.path.abspath(__file__)) root_folder = os.path.dirname(os.path.dirname(this_folder)) sys.path.append(root_folder + '/python') sys.path.append(this_folder) # ----------------------------------------------------------------------------- import ccxt # noqa: E402 # ----------------------------------------------------------------------------- kraken = ccxt.kraken() gdax = ccxt.gdax() symbol = 'BTC/USD' # each ohlcv candle is a list of [ timestamp, open, high, low, close, volume ] index = 4 # use close price from each ohlcv candle def print_chart(exchange, symbol, timeframe): print("\n" + exchange.name + ' ' + symbol + ' ' + timeframe + ' chart:') # get a list of ohlcv candles ohlcv = exchange.fetch_ohlcv(symbol, timeframe) # get the ohlCv (closing price, index == 4)
# -*- coding: utf-8 -*- import os import sys root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(root + '/python') import ccxt # noqa: E402 gdax = ccxt.gdax({ 'apiKey': "a43edfe629bc5991acc83a536ac6358e", 'secret': "xOvq+iH8NT07TheFB/fmY3GcnMZMwP7Xct9zwWtAZxsCbJh8rxeEe/0BGxfbV2em7P9iqQD7/TJGqmsDO8B/kw==", 'password': '******', 'verbose': True, # switch it to False if you don't want the HTTP log }) # move gdax to sandbox gdax.urls['api'] = 'https://api-public.sandbox.gdax.com' print(gdax.fetch_balance())
return style(s, '\033[1m') def underline(s): return style(s, '\033[4m') def dump(*args): print(' '.join([str(arg) for arg in args])) # instantiate exchanges gdax = ccxt.gdax({ 'apiKey': '92560ffae9b8a01d012726c698bcb2f1', # standard 'secret': '9aHjPmW+EtRRKN/OiZGjXh8OxyThnDL4mMDre4Ghvn8wjMniAr5jdEZJLN/knW6FHeQyiz3dPIL5ytnF0Y6Xwg==', 'password': '******', # GDAX requires a password! }) gdax.urls['api'] = 'https://api-public.sandbox.gdax.com' # use the testnet for GDAX hitbtc = ccxt.hitbtc({ 'apiKey': '18339694544745d9357f9e7c0f7c41bb', 'secret': '8340a60fb4e9fc73a169c26c7a7926f5', }) quadrigacx = ccxt.quadrigacx({ 'apiKey': 'jKvWkMqrOj', 'secret': 'f65a2e3bf3c73171ee14e389314b2f78', 'uid': '395037', # QuadrigaCX requires uid! })
def exchangeObject(exchange_in): exchanges = [ ccxt.acx(), ccxt.bitbay(), ccxt.bitfinex(), ccxt.bitflyer(), ccxt.bithumb(), ccxt.bitlish(), ccxt.bitmarket(), ccxt.bitmex(), ccxt.bitso(), ccxt.bitstamp(), ccxt.bitstamp1(), ccxt.bittrex(), ccxt.bl3p(), ccxt.bleutrade(), ccxt.btcbox(), ccxt.btcchina(), ccxt.btcexchange(), ccxt.btcmarkets(), ccxt.btctradeua(), ccxt.btcturk(), ccxt.bxinth(), ccxt.ccex(), ccxt.cex(), ccxt.chbtc(), ccxt.chilebit(), ccxt.coincheck(), ccxt.coinfloor(), ccxt.coingi(), ccxt.coinmarketcap(), ccxt.coinmate(), ccxt.coinspot(), ccxt.cryptopia(), ccxt.dsx(), ccxt.exmo(), ccxt.flowbtc(), ccxt.foxbit(), ccxt.fybse(), ccxt.fybsg(), ccxt.gatecoin(), ccxt.gateio(), ccxt.gdax(), ccxt.gemini(), ccxt.getbtc(), ccxt.hitbtc(), ccxt.independentreserve(), ccxt.itbit(), ccxt.jubi(), ccxt.kraken(), ccxt.kucoin(), ccxt.kuna(), ccxt.lakebtc(), ccxt.liqui(), ccxt.livecoin(), ccxt.luno(), ccxt.mercado(), ccxt.mixcoins(), ccxt.nova(), ccxt.okcoincny(), ccxt.okcoinusd(), ccxt.okex(), ccxt.paymium(), ccxt.poloniex(), ccxt.qryptos(), ccxt.quadrigacx(), ccxt.southxchange(), ccxt.surbitcoin(), ccxt.therock(), ccxt.tidex(), ccxt.urdubit(), ccxt.vaultoro(), ccxt.vbtc(), ccxt.virwox(), ccxt.wex(), ccxt.xbtce(), ccxt.yobit(), ccxt.yunbi(), ccxt.zaif(), ccxt.zb() ] for count, exchange in enumerate([str(x) for x in exchanges]): if exchange_in.lower() in exchange: return exchanges[count] break
# coding=utf-8 import ccxt import time import pprint delay = 2 # seconds pp = pprint.PrettyPrinter(indent=2) binance = ccxt.binance() binance_markets = binance.load_markets() gdax = ccxt.gdax() gdax_markets = gdax.load_markets() # pp.pprint(gdax.fetch_order_book("ETH/BTC")) # pp.pprint(binance.fetch_order_book("ETH/BTC")) while True: gdax_order_book = gdax.fetch_order_book("ETH/BTC") binance_order_book = binance.fetch_order_book("ETH/BTC") gdax_sell_price = gdax_order_book['bids'][0][0] gdax_buy_price = gdax_order_book['asks'][0][0] binance_sell_price = binance_order_book['bids'][0][0] binance_buy_price = binance_order_book['asks'][0][0] print(binance_sell_price) print(binance_buy_price)
def train_and_predict(self, restore=False, data_rnn=None, data_rnn_ckpt=None): #tf.reset_default_graph() print('Restore model? %s' % restore) # RNN Hyperparams # num_epochs is already defined as part of the class batch_size = 1 total_series_length = len(data_rnn.index) truncated_backprop_length = 5 # The size of the sequence state_size = 10 # The number of neurons num_features = 2 + self.future_price_window + self.order_book_window * 6 # The number of columns to be used for xTrain analysis in RNN num_classes = 1 # The number of targets to be predicted num_batches = int(total_series_length / batch_size / truncated_backprop_length) min_test_size = 1000 PriceRange, PriceMean, data_rnn_norm, data_rnn_processed = self.process_data( data_rnn, restore) rnn_column_list = self.get_rnn_column_list() # RNN Placeholders batchX_placeholder = tf.placeholder( dtype=tf.float32, shape=[None, truncated_backprop_length, num_features], name='data_ph') batchY_placeholder = tf.placeholder( dtype=tf.float32, shape=[None, truncated_backprop_length, num_classes], name='target_ph') # RNN Train-Test Split test_first_idx = 0 # needed for train if restore: data_rnn_test = data_rnn_norm #print('nrows of testing = %s' % len(data_rnn_test.index)) else: for i in range(min_test_size, len(data_rnn_processed.index)): if (i % truncated_backprop_length * batch_size == 0): test_first_idx = len(data_rnn_processed.index) - i break # Purposefully uses data_rnn['row_nums'] because data_rnn['row_nums'] also becomes normalized print(data_rnn_processed.columns) print(test_first_idx) print(len(data_rnn_norm)) data_rnn_train = data_rnn_norm[ data_rnn_processed['row_num'] < test_first_idx] #data_rnn_train = data_rnn_norm[data_rnn['row_num'] < (test_first_idx-2400)] print('nrows of training = %s' % len(data_rnn_train.index)) data_rnn_test = data_rnn_norm[ data_rnn_processed['row_num'] >= test_first_idx] print('nrows of testing = %s' % len(data_rnn_test.index)) xTrain = data_rnn_train[rnn_column_list].as_matrix() yTrain = data_rnn_train[['future_ma_%s' % self.future_ma_window ]].as_matrix() xTest = data_rnn_test[rnn_column_list].as_matrix() yTest = data_rnn_test[['future_ma_%s' % self.future_ma_window ]].as_matrix() if restore: # Weights and Biases In weight = tf.get_variable(name='weight', shape=[state_size, num_classes]) bias = tf.get_variable(name='bias', shape=[num_classes]) labels_series = tf.unstack(batchY_placeholder, axis=1) # Unpacking else: # Weights and Biases In weight = tf.Variable(tf.truncated_normal([state_size, num_classes]), name='weight') bias = tf.Variable(tf.constant(0.1, shape=[num_classes]), name='bias') labels_series = tf.unstack(batchY_placeholder, axis=1) # Unpacking # Forward Pass: Unrolling the cell (input to hidden recurrent layer) cell = tf.contrib.rnn.BasicRNNCell( num_units=state_size) # this takes forever! states_series, current_state = tf.nn.dynamic_rnn( cell=cell, inputs=batchX_placeholder, dtype=tf.float32) states_series = tf.transpose(states_series, [1, 0, 2]) # Backward Pass: Output last_state = tf.gather(params=states_series, indices=states_series.get_shape()[0] - 1) last_label = tf.gather(params=labels_series, indices=len(labels_series) - 1) # Prediction, Loss, and Optimizer prediction = tf.matmul(last_state, weight) + bias loss = tf.reduce_mean(tf.squared_difference(last_label, prediction)) train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) loss_list = [] test_pred_list = [] # Add saver variable to save and restore all variables from trained model saver = tf.train.Saver() # Initialize and run session with tf.Session() as sess: if restore: saver.restore(sess, data_rnn_ckpt) exchange = ccxt.gdax() predicted_price = 0 while True: #input(data_rnn['trades_date_time']) start_time = timer() updated, updated_data_rnn = self.updateData( exchange, data_rnn) if updated: data_rnn = updated_data_rnn #input(data_rnn['trades_date_time']) test_pred_list = [] PriceRange, PriceMean, data_rnn_norm, data_rnn_processed = self.process_data( data_rnn, restore) xTest = data_rnn_norm[ self.get_rnn_column_list()].as_matrix() yTest = data_rnn_norm[[ 'future_ma_%s' % self.future_ma_window ]].as_matrix() for test_idx in range( len(xTest) - truncated_backprop_length): testBatchX = xTest[ test_idx:test_idx + truncated_backprop_length, :].reshape( (1, truncated_backprop_length, num_features)) testBatchY = yTest[ test_idx:test_idx + truncated_backprop_length].reshape( (1, truncated_backprop_length, 1)) # _current_state = np.zeros((batch_size,state_size)) feed = { batchX_placeholder: testBatchX, batchY_placeholder: testBatchY } # Test_pred contains 'window_size' predictions, we want the last one _last_state, _last_label, test_pred = sess.run( [last_state, last_label, prediction], feed_dict=feed) test_pred_list.append( test_pred[-1][0]) # The last one actual_price = data_rnn.tail(1)['trade_px'] print("actual price - predicted price: ", actual_price.item() - predicted_price) predicted_price = test_pred_list[ -1] * PriceRange + PriceMean print("time taken for iteration: ", timer() - start_time) print("") #self.plot_predictions(test_pred_list, yTest, PriceRange, PriceMean) time.sleep(0.75) else: tf.global_variables_initializer().run() for epoch_idx in range(self.num_epochs): print('Epoch %d' % int(epoch_idx + 1)) try: for batch_idx in range(num_batches): start_idx = batch_idx * truncated_backprop_length end_idx = start_idx + truncated_backprop_length * batch_size batchX = xTrain[start_idx:end_idx, :].reshape( batch_size, truncated_backprop_length, num_features) batchY = yTrain[start_idx:end_idx].reshape( batch_size, truncated_backprop_length, 1) feed = { batchX_placeholder: batchX, batchY_placeholder: batchY } # TRAIN! _loss, _train_step, _pred, _last_label, _prediction = sess.run( fetches=[ loss, train_step, prediction, last_label, prediction ], feed_dict=feed) loss_list.append(_loss) if (batch_idx % 1000 == 0): print('Step %d - Loss: %.10f' % (batch_idx, _loss)) except ValueError: print('You have reached the end of Training Epoch %d' % int(epoch_idx + 1)) pass # Before going on into testing, save variables from trained model to disk save_path = saver.save(sess, data_rnn_ckpt) print("Model saved in file: %s" % save_path) # TEST for test_idx in range(len(xTest) - truncated_backprop_length): testBatchX = xTest[test_idx:test_idx + truncated_backprop_length, :].reshape( (1, truncated_backprop_length, num_features)) testBatchY = yTest[test_idx:test_idx + truncated_backprop_length].reshape( (1, truncated_backprop_length, 1)) # _current_state = np.zeros((batch_size,state_size)) feed = { batchX_placeholder: testBatchX, batchY_placeholder: testBatchY } # Test_pred contains 'window_size' predictions, we want the last one _last_state, _last_label, test_pred = sess.run( [last_state, last_label, prediction], feed_dict=feed) test_pred_list.append(test_pred[-1][0]) # The last one self.plot_predictions(test_pred_list, yTest, PriceRange, PriceMean)