def load_cryptopair(pair, from_dt, to_dt, period=Period.D1): """Load OHLC data on a cryptocurrency pair from Poloniex exchange""" polo = Poloniex() t = timer() if isinstance(period, Period): period = period.value chart_data = polo.returnChartData(pair, period=period, start=dt_to_ts(from_dt), end=dt_to_ts(to_dt)) print("done. %.2fs" % (timer() - t)) chart_df = pd.DataFrame(chart_data) chart_df.set_index('date', drop=True, inplace=True) chart_df.index = pd.to_datetime(chart_df.index, unit='s') chart_df.fillna(method='ffill', inplace=True) # fill gaps forwards chart_df.fillna(method='bfill', inplace=True) # fill gaps backwards chart_df = chart_df.astype(float) chart_df.rename(columns={ 'open': 'O', 'high': 'H', 'low': 'L', 'close': 'C', 'volume': 'V' }, inplace=True) return chart_df[['O', 'H', 'L', 'C', 'V']]
def poloniex_linear_regression(polo_model, lr_model): polo = Poloniex() data_points = polo_model.get_data_points() x = np.asarray([point['date'] for point in data_points]).astype(np.float) y = np.asarray([point['weightedAverage'] for point in data_points]).astype(np.float) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1) polo_class = hc.PoloniexData( polo.returnChartData('BTC_ETH', 300, time.time() - 86400, time.time() + 300)) polo_x_test = np.asarray([point['date'] for point in polo_class.get_data_points()]).astype(np.float)\ .reshape((-1, 1)) np.append(polo_x_test, np.add(time.time(), 1800)) polo_y_test = np.asarray([point['weightedAverage'] for point in polo_class.get_data_points()]).astype(np.float)\ .reshape((-1, 1)) x_train = x_train.reshape((-1, 1)) y_train = y_train.reshape((-1, 1)) lr_model.fit(x_train, y_train) predict = lr_model.predict(polo_x_test) return { 'model': lr_model, 'predict': predict, 'x_test': polo_x_test, 'y_test': polo_y_test }
def get_poloniex_data(cryptocurrency, period, start_time): polo = Poloniex() data = polo.returnChartData(cryptocurrency, period=period, start=start_time) dataframe = pd.DataFrame(data) return dataframe
class Ticker(Series): def __init__(self, name, period, pmult, start, smult): Series.__init__(self) self.api = Poloniex() self.name = name self.period = t[period] * pmult self.start = time() - t[start] * smult self.data = self.update() def plot(self, columns=['close']): for column in columns: plt.plot(self.data[column]) def update(self): name = self.name period = self.period start = self.start data = DF(self.api.returnChartData(name, period, start, time())) out = {} for col in data.keys(): out[col] = np.array(data[col]).astype('double') return out
class PoloniexApi: def __init__(self): self.api = Poloniex() def get_chart_data(self, pair): end = int(time.time()) start = end - 60 * 60 * 24 * 120 return self.api.returnChartData(pair, period=300, start=start, end=end)
def maVal(st, et, cur, dp): currency = cur period = 86400 startTime = time.mktime( datetime.datetime.strptime(st, "%Y/%m/%d").timetuple()) endTime = time.mktime( datetime.datetime.strptime(et, "%Y/%m/%d").timetuple()) sizeOfMA = dp connection = Poloniex( "343DL7SG-Z5FLTLRU-W72O0KVT-OH3GZSET", "a53e8a0defdd21398547fd4a33f64b80555ae4c3f6704c8b6f0602c05dfbcea459be7af7de9449a28c14c51a2e50b6bba70c7f90ab318293e16ab9241a374ace" ) historicalData = connection.returnChartData(currency, period, startTime, endTime) dataPoints = len(historicalData) countofMA = len(historicalData) - sizeOfMA + 1 cPricesPlot = [] for c in historicalData: cPricesPlot.append(c['close']) xDatesPlot = [] for x in historicalData: xDatesPlot.append(x['date']) datesConv = [] size = len(xDatesPlot) for a in range(size): dates = xDatesPlot[a] + period dates1 = datetime.datetime.fromtimestamp( int(dates)).strftime('%Y-%m-%d ') datesConv.append(dates1) movingAvgs = [] for a in range(countofMA): #First Loop i indicating moving average index s = 0.0 for j in range(sizeOfMA): #j is the calculated price in ath index index = a + j s = s + cPricesPlot[ index] #Adds up the values in cPrices according to sizeofMA movingAvgs.append(s / sizeOfMA) ret = [] for a in range(countofMA): index = a + sizeOfMA - 1 one = dict() one['date'] = str(datesConv[index]) one['cpp'] = cPricesPlot[index] one['sma'] = movingAvgs[a] ret.append(one) return ret
def getPriceMovement(): import time from poloniex import Poloniex coinNames = getCoinNames() config = getConfig() polo = Poloniex() coinPriceChanges = {} period = config["period"] for coin in coinNames: startTime = time.time() - period coinWtdAvg = float( polo.returnChartData(coinNames[coin], start=startTime)[0]["weightedAverage"]) lastCoinWtdAvg = float( polo.returnChartData(coinNames[coin], start=startTime - period, end=startTime)[0]["weightedAverage"]) changePercent = coinWtdAvg / lastCoinWtdAvg - 1 coinPriceChanges[coin] = changePercent return coinPriceChanges
def get_tick_interval(coin, last_date, interval): polo = Poloniex() h = polo.returnChartData(coin, interval, start=last_date) tick = np.mean((float(h['close']) + float(h['open']) + float(h['low']) + float(h['high']))) date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(h['date'])) try: tickers.insert_tickers(date, coin, tick, 0) except Exception: print("Error Inserting Tick") return None return h
def fetch_data(): end = int(time.time()) start = end - 100000 * 1800 start, end polo = Poloniex() df = pd.DataFrame( polo.returnChartData("USDT_BTC", period=PERIOD, start=start, end=end)) columns_to_convert_to_float = [CN_HIGH, CN_LOW, CN_WEIGHTED_AVERAGE] df[columns_to_convert_to_float] = df[columns_to_convert_to_float].astype( 'float32') return df
def _get_new_bar(self, symbol): polo = Poloniex() start = datetime.datetime.now() - datetime.timedelta(days=1) data = polo.returnChartData(symbol, period=self.period, start=start.timestamp())[-1] yield DataPoint({ 'symbol': symbol, 'datetime': datetime.datetime.fromtimestamp(data['date']), 'timestamp': data['date'], 'open': data['open'], 'high': data['high'], 'low': data['low'], 'close': data['close'], 'volume': data['volume'] })
def _request(currency: str, period: int, start: int, end: int) -> list: '''Get candles data from Poloniex exchange for a specified currency relative to BTC. Args: currency: Currency which we will request. period: Period for one candle in seconds. start: Start of the range (UNIX timestamp). end: End of the range (UNIX timestamp). Returns: A list of dictionaries, each of each represents one candle. ''' polo = Poloniex() pair = f"BTC_{currency}" return polo.returnChartData(pair, period, start=start, end=end)
def writeData2csv(filename, pair='BTC_ZEC', period=300, days=230): from poloniex import Poloniex import time, datetime csvfile = open(filename, "a") conn = Poloniex() startTime = time.time() - (86400 * days) #startTime = time.time() - (period * 50) endTime = time.time() #End at current time data = conn.returnChartData(pair, period, startTime, endTime) for tick in data: #print(str(datetime_from_timestamp(tick['date'])) + "," + tick['close'], file=csvfile) print(tick['close'], file=csvfile) return
def historical_prices_of(polo: _Poloniex, btc_price_history: Series, pair: str, period: int = 900, start: Optional[float] = None, end: Optional[float] = None) -> Iterable[Series]: ''' Returns a series of time-indexed prices. `pair` is of the form e.g. 'BTC_ETH', `period` is an integer number of seconds, either 300, 900, 1800, 7200, 14400, or 86400. We do some data marshalling in this method as well, to turn API results into stuff amenable for our Postgres DB. ''' def contemporary_usd_price(row: Series) -> float: contemporary_btc_price = btc_price_history['price_usd'].asof(row.name) return row['weightedAverage'] * contemporary_btc_price # Scraping now = time.time() start = start or now - YEAR_IN_SECS end = end or now ex_trades = polo.returnChartData(pair, period, start, end) # Data marshalling ts_df = DataFrame(ex_trades, dtype=float) ts_df['time'] = [datetime.fromtimestamp(t) for t in ts_df['date']] ts_df.index = ts_df['time'] ts_df['price_usd'] = ts_df.apply(contemporary_usd_price, axis=1) ts_df['currency_pair'] = ts_df.apply(lambda x: pair, axis=1) ts_df = ts_df.rename(index=str, columns={ 'quoteVolume': 'quote_volume', 'weightedAverage': 'weighted_average', }) for _, row in ts_df.iterrows(): # chart = scraped_chart(pair, row) # for some reason, when there's no chart data to report, # the API will give us some reading with all 0s. if row['volume'] == 0 and row['weighted_average'] == 0: # we will just ignore these pass else: yield row
def prepareLiveData(pair="DGB_BTC", start=string2ts("2017-06-01 00:00:00"), end=string2ts("2017-09-01 00:00:00"), period=3600 * 4): """ funcion que trae los datos y los devuleve en dataframe de pandas """ from poloniex import Poloniex #import json #import pylab as plt poloKeys = open("../.kp").readlines() polo = Poloniex(poloKeys[0].strip(), poloKeys[1].strip()) #print conn.returnBalances()['BTC'] #polo = Poloniex() # historyData es una lista de diccionarios python # candlestick period in seconds; valid values are 300, 900, 1800, 7200, 14400, and 86400 historyData = polo.returnChartData(currencyPair=pair, start=start, end=end, period=period) # convirtiendo datos en data frame df = pd.DataFrame(historyData) # convirtiendo string a float o int df["close"] = pd.to_numeric(df['close']) df["open"] = pd.to_numeric(df['open']) df["low"] = pd.to_numeric(df['low']) df["volume"] = pd.to_numeric(df['volume']) df["date"] = pd.to_datetime(df["date"].apply(ts2string)) df['weightedAverage'] = pd.to_numeric(df['weightedAverage']) df['high'] = pd.to_numeric(df['high']) df['open'] = pd.to_numeric(df['open']) # calculando volatilidad en función del tamaño de las velas df["volatility"] = makeVolatility(df) # seleccionando la columna de fecha como indice df = df.set_index("date") return df, polo
def get_historical_data(coin, start): polo = Poloniex() print('Fetching External Data') # historical = polo.returnChartData(coin, 300) historical = polo.returnChartData(coin, 300, start=start) print('Got External Data') inserted = 0 for h in historical: # tick = np.mean((float(h['close']) + float(h['open']) + float(h['low']) + float(h['high']))) tick = float(h['close']) volume = float(h['volume']) date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(h['date'])) try: tickers.insert_tickers(date, coin, tick, volume, 0) except Exception: continue features.update_indicators(date, coin, 0) inserted += 1 return inserted
class TestPoloniex(object): def setup(self): self.poloniex = Poloniex('', '') def test_ticker(self): res = self.poloniex.returnTicker() assert 'BTC_ETH' in res.keys() assert 'lowestAsk' in res['BTC_ETH'].keys() def test_volume(self): res = self.poloniex.return24hVolume() assert 'BTC_ETH' in res.keys() assert 'BTC' in res['BTC_ETH'].keys() def test_orderbook(self): res = self.poloniex.returnOrderBook('BTC_ETH') assert 'bids' in res.keys() assert 'asks' in res.keys() def test_tradehistory(self): start = datetime.now() - timedelta(hours=1) res = self.poloniex.returnTradeHistory('BTC_ETH', start) assert len(res) assert 'date' in res[0].keys() assert 'type' in res[0].keys() assert 'amount' in res[0].keys() assert 'rate' in res[0].keys() def test_chartdata(self): start = datetime.now() - timedelta(hours=1) res = self.poloniex.returnChartData('BTC_ETH', 300, start) assert len(res) assert 'date' in res[0].keys() assert 'volume' in res[0].keys() assert 'high' in res[0].keys() assert 'low' in res[0].keys() assert 'close' in res[0].keys()
class BotChart(object): def __init__(self, exchange, pair, period): self.conn = Poloniex() self.pair = pair self.period = period Days = 2 #TODO: Fix Naming of this variable self.startTime = 1499309550 - (86400 * Days) self.endTime = 1499309550 #self.startTime = time.time() - (86400 * Days) #self.endTime = time.time() #End at current time self.data = self.conn.returnChartData(self.pair, self.period, self.startTime, self.endTime) #self.data = self.conn.returnTicker()[self.pair] def getPoints(self): return self.data
class BotChart(object): def __init__(self, exchange, pair, period): self.polo = Poloniex( "GG7WHRPQ-J7HEGPKN-ITAJAEBQ-H73W8K9F", "4e1714667f572cf8f743b00f7b1d33390dab70929cb979f80ec3122e8fb75c742" "dfcc8b4e7bdada779505ad043905f710d3aed2ae0824558cb15da09fcbe4dee") self.pair = pair self.period = period self.start_time = 1491048000 self.end_time = 1491591200 # if start_time: # self.start_time = start_time # if end_time: # self.end_time = end_time self.data = self.polo.returnChartData(pair, period, self.start_time, self.end_time) def getPoints(self): return self.data
def prepareData(pair="DGB_BTC", start=string2ts("2017-06-01 00:00:00"), end=string2ts("2017-09-01 00:00:00"), period=3600*4): """ funcion que trae los datos y los devuleve en dataframe de pandas """ from poloniex import Poloniex import json #import pylab as plt polo = Poloniex() # historyData es una lista de diccionarios python # candlestick period in seconds; valid values are 300, 900, 1800, 7200, 14400, and 86400 historyData = polo.returnChartData(currencyPair=pair, start=start, end=end, period=period) # convirtiendo datos en data frame df = pd.DataFrame(historyData) # convirtiendo string a float o int df["close"] =pd.to_numeric(df['close']) df["open"] =pd.to_numeric(df['open']) df["low"] =pd.to_numeric(df['low']) df["volume"] =pd.to_numeric(df['volume']) df["date"] = pd.to_datetime(df["date"].apply(ts2string)) df['weightedAverage'] = pd.to_numeric(df['weightedAverage']) df['high'] = pd.to_numeric(df['high']) df['open'] = pd.to_numeric(df['open']) # seleccionando la columna de fecha como indice df = df.set_index("date") return df
def get_polo_data2csv(currency_pair, period=86400, date_from=(2015, 1, 1), name_fout='bitcoin_data.csv'): date_from_utc = datetime(year=date_from[0], month=date_from[1], day=date_from[2]).timestamp() polo = Poloniex() data = polo.returnChartData(currency_pair, period, start=date_from_utc, end=9999999999) with open(name_fout, 'w', newline='') as fout: fields = tuple(data[0].keys()) writer = csv.writer(fout) writer.writerow(fields) for row in data: values = list(row.values()) values[0] = datetime.utcfromtimestamp( values[0]).strftime('%Y-%m-%d') writer.writerow(values)
def get_btc_data(currency_pair, period=86400, date_from=(2015, 1, 1), name_table='btc_data'): logger = logging.getLogger('App.getpolo.get_btc_data') try: date_from_utc = datetime(year=date_from[0], month=date_from[1], day=date_from[2]).timestamp() polo = Poloniex() data = polo.returnChartData(currency_pair, period, start=date_from_utc, end=9999999999) except Exception as e: logger.error('Getting bitcoin data from Poloniex!') logger.info('Data from Poloniex has been received!') fields = tuple(data[0].keys()) rows = [] for row in data: values = [] for field in fields: if field == 'date': values.append( datetime.utcfromtimestamp(row[field]).strftime('%Y-%m-%d')) else: values.append(row[field]) rows.append(values) df_data = pd.DataFrame.from_records(rows, columns=fields) db = dbwork.DataBase(settings.NAME_DATABASE) df_data.to_sql(name_table, db.engine, index=False, if_exists='replace') logger.info('Data BTC has been loaded into DATABASE.')
def Pivot(self, pair, period, cdate): global Pivot_date, curPivot curDay = datetime.combine(datetime.fromtimestamp(cdate), time(0, 0, 0)) if curDay == Pivot_date: return curPivot else: polo = Poloniex() #dt = datetime.combine(date.today(), time(0, 0, 0)) # get start of today dt = datetime.combine(datetime.fromtimestamp(cdate), time(0, 0, 0)) # get start of today Pivot_date = dt startTime = int((dt - timedelta(days=1)).timestamp()) endTime = int((dt - timedelta(seconds=1)).timestamp()) data = polo.returnChartData(pair, period, startTime, endTime) highs = [] lows = [] closes = [] for i in data: highs.append(float(i['high'])) lows.append(float(i['low'])) closes.append(float(i['close'])) avHigh = sum(highs) / len(highs) avLow = sum(lows) / len(lows) avClose = sum(closes) / len(closes) PP = (float(avHigh) + float(avLow) + float(avClose)) / 3 curPivot = PP return PP
class Polo(Base): """ Poloniex interface """ arg_parser = configargparse.get_argument_parser() arg_parser.add('--polo_api_key', help='polo_api_key') arg_parser.add("--polo_secret", help='polo_secret') arg_parser.add("--polo_txn_fee", help='Poloniex txn. fee') arg_parser.add("--polo_buy_order", help='Poloniex buy order type') arg_parser.add("--polo_sell_order", help='Poloniex sell order type') valid_candle_intervals = [300, 900, 1800, 7200, 14400, 86400] def __init__(self): args = self.arg_parser.parse_known_args()[0] super(Polo, self).__init__() api_key = args.polo_api_key secret = args.polo_secret self.transaction_fee = float(args.polo_txn_fee) self.polo = Poloniex(api_key, secret) self.buy_order_type = args.polo_buy_order self.sell_order_type = args.polo_sell_order self.verbosity = args.verbosity self.pair_delimiter = '_' self.tickers_cache_refresh_interval = 50 # If the ticker request is within the interval, get data from cache self.last_tickers_fetch_epoch = 0 # self.last_tickers_cache = None # Cache for storing immediate tickers def get_balances(self): """ Return available account balances (function returns ONLY currencies > 0) """ try: balances = self.polo.returnBalances() only_non_zeros = { k: float(v) for k, v in balances.items() if float(v) > 0.0 } except PoloniexError as e: print( colored('!!! Got exception (polo.get_balances): ' + str(e), 'red')) only_non_zeros = dict() return only_non_zeros def get_symbol_ticker(self, symbol, candle_size=5): """ Returns real-time ticker Data-Frame for given symbol/pair Info: Currently Poloniex returns tickers for ALL pairs. To speed the queries and avoid unnecessary API calls, this method implements temporary cache """ epoch_now = int(time.time()) if epoch_now < (self.last_tickers_fetch_epoch + self.tickers_cache_refresh_interval): # If the ticker request is within cache_fetch_interval, try to get data from cache pair_ticker = self.last_tickers_cache[symbol].copy() else: # If cache is too old fetch data from Poloniex API try: ticker = self.polo.returnTicker() pair_ticker = ticker[symbol] self.last_tickers_fetch_epoch = int(time.time()) self.last_tickers_cache = ticker.copy() except (PoloniexError | JSONDecodeError) as e: print( colored( '!!! Got exception in get_symbol_ticker. Details: ' + str(e), 'red')) pair_ticker = self.last_tickers_cache[symbol].copy() pair_ticker = dict.fromkeys(pair_ticker, None) df = pd.DataFrame.from_dict(pair_ticker, orient="index") df = df.T # We will use 'last' price as closing one df = df.rename(columns={'last': 'close', 'baseVolume': 'volume'}) df['close'] = df['close'].astype(float) df['volume'] = df['volume'].astype(float) df['pair'] = symbol df['date'] = int(datetime.datetime.utcnow().timestamp()) return df def return_ticker(self): """ Returns ticker for all currencies """ return self.polo.returnTicker() def cancel_order(self, order_number): """ Cancels order for given order number """ return self.polo.cancelOrder(order_number) def return_open_orders(self, currency_pair='all'): """ Returns your open orders """ return self.polo.returnOpenOrders(currency_pair) def get_pairs(self): """ Returns ticker pairs for all currencies """ ticker = self.polo.returnTicker() return list(ticker) def get_candles_df(self, currency_pair, epoch_start, epoch_end, period=False): """ Returns candlestick chart data in pandas dataframe """ try: data = self.get_candles(currency_pair, epoch_start, epoch_end, period) df = pd.DataFrame(data) df = df.tail(1) df['close'] = df['close'].astype(float) df['volume'] = df['volume'].astype(float) df['pair'] = currency_pair return df except (PoloniexError, JSONDecodeError) as e: print() print( colored( '!!! Got exception while retrieving polo data:' + str(e) + ', pair: ' + currency_pair, 'red')) return pd.DataFrame() def get_candles(self, currency_pair, epoch_start, epoch_end, interval_in_sec=False): """ Returns candlestick chart data """ candle_interval = self.get_valid_candle_interval(interval_in_sec) data = [] try: data = self.polo.returnChartData(currency_pair, candle_interval, epoch_start, epoch_end) except (PoloniexError, JSONDecodeError) as e: print() print( colored( '!!! Got exception while retrieving polo data:' + str(e) + ', pair: ' + currency_pair, 'red')) return data def get_valid_candle_interval(self, period_in_sec): """ Returns closest value from valid candle intervals """ if not period_in_sec: return period_in_sec if period_in_sec in self.valid_candle_intervals: return period_in_sec # Find the closest valid interval return min(self.valid_candle_intervals, key=lambda x: abs(x - period_in_sec)) def trade(self, actions, wallet, trade_mode): if trade_mode == TradeMode.backtest: return Base.trade(actions, wallet, trade_mode) else: actions = self.life_trade(actions) return actions def life_trade(self, actions): """ Places orders and returns order number !!! For now we are NOT handling postOnly type of orders !!! """ for action in actions: if action.action == TradeState.none: actions.remove(action) continue # Handle buy_sell mode wallet = self.get_balances() if action.buy_sell_mode == BuySellMode.all: action.amount = self.get_buy_sell_all_amount(wallet, action) elif action.buy_sell_mode == BuySellMode.fixed: action.amount = self.get_fixed_trade_amount(wallet, action) if self.verbosity: print( 'Processing live-action: ' + str(action.action) + ', amount:', str(action.amount) + ', pair:', action.pair + ', rate:', str(action.rate) + ', buy_sell_mode:', action.buy_sell_mode) # If we don't have enough assets, just skip/remove the action if action.amount == 0.0: print( colored( 'No assets to buy/sell, ...skipping: ' + str(action.amount) + ' ' + action.pair, 'green')) actions.remove(action) continue # ** Buy Action ** if action.action == TradeState.buy: try: print( colored( 'Setting buy order: ' + str(action.amount) + '' + action.pair, 'green')) action.order_number = self.polo.buy( action.pair, action.rate, action.amount, self.buy_order_type) except PoloniexError as e: print( colored( 'Got exception: ' + str(e) + ' Txn: buy-' + action.pair, 'red')) continue amount_unfilled = action.order_number.get('amountUnfilled') if float(amount_unfilled) == 0.0: actions.remove(action) print( colored( 'Bought: ' + str(action.amount) + '' + action.pair, 'green')) else: action.amount = amount_unfilled print( colored( 'Not filled 100% buy txn. Unfilled amount: ' + str(amount_unfilled) + '' + action.pair, 'red')) # ** Sell Action ** elif action.action == TradeState.sell: try: print( colored( 'Setting sell order: ' + str(action.amount) + '' + action.pair, 'yellow')) action.order_number = self.polo.sell( action.pair, action.rate, action.amount, self.buy_order_type) except PoloniexError as e: print( colored( 'Got exception: ' + str(e) + ' Txn: sell-' + action.pair, 'red')) continue amount_unfilled = action.order_number.get('amountUnfilled') if float(amount_unfilled) == 0.0: actions.remove(action) print( colored( 'Sold: ' + str(action.amount) + '' + action.pair, 'yellow')) else: action.amount = amount_unfilled print( colored( 'Not filled 100% sell txn. Unfilled amount: ' + str(amount_unfilled) + '' + action.pair, 'red')) return actions
from keras.models import load_model from poloniex import Poloniex from pandas import read_csv import numpy conn = Poloniex() data = conn.returnChartData('BTC_ZEC', 300, 1498521600, 1498608000) model = load_model('BTC_ZEC_10Days_5min_29_6_17.h5') model.load_weights('BTC_ZEC_10Days_5min_29_6_17_weights.h5') def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return numpy.array(dataX), numpy.array(dataY) dataframe = read_csv('btc_zec_1_day_5min.csv', usecols=[1], engine='python', skipfooter=3) dataset = dataframe.values dataset = dataset.astype('float32') test = dataset#[train_size:len(dataset),:] look_back = 1 testX, testY = create_dataset(test, look_back) testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
class PoloData: def __init__(self, *args, **kwargs): self._polo = Poloniex(*args, **kwargs) self.ticker_update_freq = 1 self.ticker_updated = datetime(1970, 1, 1) self._ticker = {} self.ticker = None self.ticker_active = False self._ticker_thread = None self.charts_update_freq = 60 self.charts_updated = datetime(1970, 1, 1) self.charts = {} self.charts_active = False self._charts_thread = None self.chart_path = None self._new_chart = False self.balances_update_freq = 1 self.balances_updated = datetime(1970, 1, 1) self.balances = None self.balances_active = False self._balances_thread = None self.markets = self._get_markets() def start_ticker(self, update_freq): self.ticker_update_freq = update_freq self._ticker_thread = threading.Thread(target=self._get_ticker) self._ticker_thread.setDaemon(True) self.ticker_active = True self._ticker_thread.start() def _get_ticker(self): while self.ticker_active: # print(datetime.now(), "Ticker Update") self._ticker = self._polo.returnTicker() self._populate_ticker() self.ticker_updated = datetime.now() time.sleep(self.ticker_update_freq) print("Ticker thread stopped.") def stop_ticker(self): self.ticker_active = False def _get_markets(self): ticker = self._polo.returnTicker() markets = {} for k in ticker.keys(): pc, sc = k.split("_") if pc not in markets: markets[pc] = pd.DataFrame(columns=[ 'last', 'highestBid', 'baseVolume', 'lowestAsk', 'quoteVolume', 'low24hr', 'high24hr', 'percentChange', 'isFrozen', 'id', 'favourite' ], dtype=float) if sc not in markets[pc]: markets[pc].ix[sc, "favourite"] = 0 return markets def _populate_ticker(self): self.ticker = pd.DataFrame.from_dict(self._ticker, orient='index', dtype=float).fillna(0) def start_charts(self, update_freq, chart_path): self.charts_update_freq = update_freq self.chart_path = chart_path self._charts_thread = threading.Thread(target=self._get_charts) self._charts_thread.setDaemon(True) self.charts_active = True self._charts_thread.start() def _get_charts(self): while self.charts_active: # print(datetime.now(), "Charts Update") update_time = datetime.now() + timedelta( seconds=self.charts_update_freq) for chart in self.charts: market, currency = chart.split("_") if self.charts[chart] is None: self.charts[chart] = self._load_chart( market, currency, datetime.now() - timedelta(days=90), datetime.now()) self._new_chart = False else: self.charts[chart] = self._update_chart( market, currency, self.charts[chart]) self.charts_updated = datetime.now() while datetime.now() < update_time and not self._new_chart: time.sleep(1) print("Charts thread stopped.") def stop_charts(self): self.charts_active = False def add_chart(self, market): if market not in self.charts: self.charts[market] = None self._new_chart = True def remove_chart(self, market): if market is not None: del self.charts[market] def start_balances(self, update_freq): self.balances_update_freq = update_freq self._balances_thread = threading.Thread(target=self._get_balances) self._balances_thread.setDaemon(True) self.balances_active = True self._balances_thread.start() def _get_balances(self): while self.balances_active: # print(datetime.now(), "Balances Update") self.balances = self._polo.returnCompleteBalances() self.balances_updated = datetime.now() time.sleep(self.balances_update_freq) print("Balances thread stopped.") def stop_balances(self): self.balances_active = False def _retrieve_chart_data(self, market, currency, start_date, end_date, freq=300): start_date = start_date.timestamp() end_date = end_date.timestamp() raw_chart_data = self._polo.returnChartData(market + "_" + currency, freq, start_date, end_date) chart_data = pd.DataFrame(raw_chart_data, dtype=float) chart_data["Date"] = [ datetime.fromtimestamp(d) for d in chart_data["date"] ] chart_data.set_index(["Date"], inplace=True) chart_data.drop("date", axis=1, inplace=True) chart_data = chart_data.drop_duplicates() freq_str = str(int(freq / 60)) + "Min" chart_data = chart_data.asfreq(freq_str, method='pad') return chart_data def _load_chart(self, market, currency, start_date, end_date, freq=300, force_reload=False): path = self.chart_path + market + "_" + currency + ".csv" if isfile(path) and not force_reload: # print("Loading:", market + "_" + currency, end='', flush=True) chart_data = pd.read_csv(path, parse_dates=True, index_col="Date") # print(" OK.") else: # print("Downloading:", market + "_" + currency) chart_data = self._retrieve_chart_data(market, currency, start_date, end_date, freq) chart_data.to_csv(path) return chart_data def _update_chart(self, market, currency, chart_data, freq=300): path = self.chart_path + market + "_" + currency + ".csv" next_entry = chart_data.ix[-1].name.to_pydatetime() + timedelta( minutes=int(freq / 60)) update = self._retrieve_chart_data(market, currency, next_entry, datetime.now(), freq) # print("Updating:", market + "_" + currency) if update.shape[0] > 1: chart_data = pd.concat([chart_data, update]) chart_data = chart_data.drop_duplicates() freq_str = str(int(freq / 60)) + "Min" chart_data = chart_data.asfreq(freq_str, method='pad') chart_data.to_csv(path) return chart_data def calculate_macd(self, closes, ema_fast, ema_slow, ema_signal): ema_fast = closes.ewm(ema_fast).mean() ema_slow = closes.ewm(ema_slow).mean() macd_line = ema_fast - ema_slow signal_line = macd_line.ewm(ema_signal).mean() histogram = macd_line - signal_line return macd_line, signal_line, histogram def calculate_rsi(self, closes, periods): delta = closes.diff() up_periods, down_periods = delta.copy(), delta.copy() up_periods[up_periods <= 0] = 0 down_periods[down_periods > 0] = 0 up_ema = up_periods.ewm(periods).mean() down_ema = down_periods.ewm(periods).mean().abs() rs = up_ema / down_ema rsi = 100 - (100 / (1 + rs)) return rsi def buy(self, market, price, amount): current_price = float(self.ticker.ix[market, 'lowestAsk']) orderType = 'postOnly' if float( price) < current_price else 'immediateOrCancel' result = self._polo.buy(market, price, amount, orderType=orderType) return result def sell(self, market, price, amount): current_price = float(self.ticker.ix[market, 'highestBid']) orderType = 'postOnly' if float( price) > current_price else 'immediateOrCancel' result = self._polo.sell(market, price, amount, orderType=orderType) return result
import datetime import json import numpy as np from sklearn import preprocessing from poloniex import Poloniex polo = Poloniex() # specify time period beg_date = datetime.datetime(2018, 3, 10) end_date = datetime.datetime(2018, 3, 20) print("data from: {} to {}".format(beg_date, end_date)) chart_data = polo.returnChartData(currencyPair='USDT_BTC', period=300, start=beg_date.timestamp(), end=end_date.timestamp()) prices = [] dates = [] for chunk in chart_data: parsed_chunk = json.loads(str(chunk).replace("'", '"')) prices.append(parsed_chunk['weightedAverage']) dates.append(parsed_chunk['date']) #print(prices) dates = np.reshape(dates, (len(dates), 1)) scaler = preprocessing.StandardScaler() dates = scaler.fit_transform(dates)
api_key = getkeys()[0] api_secret = getkeys()[1] polo = Poloniex(api_key, api_secret) ######################### II - One trade at a time trading bot print( '\n---------------- \n---------------- \n We start to trade {} with nominal amount {} $, it is {}' .format(pair, amount_dollar, datetime.now())) try: while True: if int(time()) % period < 60: print("It is {} ... Let's trade {} ! ".format( datetime.now(), pair)) # I- Request the data raw = polo.returnChartData(pair, period=period, start=int(time()) - period * 10000) df = pd.DataFrame(raw).iloc[ 1:] # First row may contain useless data df['date'] = pd.to_datetime(df["date"], unit='s') while (datetime.utcnow() - df['date'].iloc[-1]).total_seconds( ) > period: #Check we've got the very recent candle : we stay here until Poloniex delivers it print('{} : Waiting for actualized data...'.format( datetime.now())) sleep(1) raw = polo.returnChartData(pair, period=period, start=int(time()) - period * 1000) df = pd.DataFrame(raw).iloc[ 1:] # First row may contain useless data df['date'] = pd.to_datetime(df["date"], unit='s')
class BotApi(object): def __init__(self,platform): self.platform=platform if(self.platform=='binance'): # from account [email protected] self.api_key="ScXClxYXs8AnGpB7X8LeDducznZVHOylFcDh5ETsAt4BQh9FXG4daC04xOdRjJ5w" self.api_secret="N8aLvhbx55aaEQbErj89qESFGdfcZGx1YkNeCbbSnV9KwgSUDxmhdtyqWKdJctzu" # for changing periods in seconds binance accepted strings self.period_dict={60:"1m",180:"3m",300:"5m",900:"15m",1800:"30m",3600:"1h",\ 7200:"2h",14400:"4h",21600:"6h",28800:"8h",43200:"12h",86400:"1d",\ 259200:"3d",1209600:"1w"} elif(self.platform=='poloniex'): # has to be filled in later for private commands self.conn = Poloniex('api-key', 'secret') def returnTicker(self): if(self.platform=='binance'): # returns a list of dictionaries with key symbol and price for each pair request=requests.get("https://api.binance.com/api/v3/ticker/price?") # translates request into python datatype ticker=request.json() # creates a dictionary with the pairs as keys ticker={each.pop('symbol'): each for each in ticker} # makes the dictionary to a string to replace price by last to be poloniex compatible and then converts it back to a dictionary ticker_str=str(ticker) ticker_str=ticker_str.replace("price","last") ticker=ast.literal_eval(ticker_str) if(self.platform=='poloniex'): ticker=self.conn.returnTicker() return ticker def returnCurrentPrice(self,pair): if(self.platform=='binance'): pair=pair.split("_") pair.reverse() pair="".join(pair) # gives dictionary with keys symbol (pair) and price request=requests.get("https://api.binance.com/api/v3/ticker/price?symbol={}".format(pair)) currentPrice=request.json()['price'] if(self.platform=='poloniex'): currentPrice=self.conn.returnTicker()[pair]['last'] return currentPrice def returnChartData(self,pair,start,end,period): #print("here") if(self.platform=='binance'): chart_lis=[] period_str=self.period_dict[period] # the pairs are written in poloniex convention with quote_base and therefore have to be reversed pair=pair.split("_") pair.reverse() pair="".join(pair) # binance works with timestamps in miliseconds so our timestamps have to be converted start=1000*start end=1000*end # split the request in chunks that have in maximum 1000 datapoints numParts=math.ceil((end-start)/(period*1e6)) print('pair:',pair,'numparts:',numParts) for i in range(numParts): subStart=start+i*(end-start)/numParts subEnd=start+(i+1)*(end-start)/numParts print('start:',start,'end:',end,'subStart:',subStart,'subEnd:',subEnd) url = 'https://api.binance.com/api/v1/klines?symbol={}&interval={}&startTime={}&endTime={}&limit=1000'.format(\ pair,period_str,int(subStart),int(subEnd)) request=requests.get(url) chart_lis+=request.json() # chart_lis is a list of lists, highest in hierarchy are the timestamps # chart becomes a list of dictionaries, a dictionary for each timestamp chart=[] for i in chart_lis: chart.append({}) chart[-1]['date']=int(i[0])/1000 chart[-1]['open']=float(i[1]) chart[-1]['high']=float(i[2]) chart[-1]['low']=float(i[3]) chart[-1]['close']=float(i[4]) chart[-1]['volume']=float(i[5]) chart[-1]['closeTime']=i[6] chart[-1]['quoteAssetVolume']=i[7] chart[-1]['numberOfTrades']=i[8] chart[-1]['takerBuyBaseAssetVolume']=i[9] chart[-1]['takerBuyQuoteAssetVolume']=i[10] if(self.platform=='poloniex'): chart=self.conn.returnChartData(currencyPair=pair, start=start, end=end, period=period) return chart def chartToCsv(self,pair,start,end,period): #print("here") if(self.platform=='binance'): chart_lis=[] period_str=self.period_dict[period] # the pairs are written in poloniex convention with quote_base and therefore have to be reversed pair=pair.split("_") pair.reverse() pair="".join(pair) # binance works with timestamps in miliseconds so our timestamps have to be converted start=1000*start end=1000*end # split the request in chunks that have in maximum 1000 datapoints numParts=math.ceil((end-start)/(period*1e6)) print('pair:',pair,'numparts:',numParts) for i in range(numParts): subStart=start+i*(end-start)/numParts subEnd=start+(i+1)*(end-start)/numParts print('start:',start,'end:',end,'subStart:',subStart,'subEnd:',subEnd) url = 'https://api.binance.com/api/v1/klines?symbol={}&interval={}&startTime={}&endTime={}&limit=1000'.format(\ pair,period_str,int(subStart),int(subEnd)) request=requests.get(url) chart_lis+=request.json() # chart_lis is a list of lists, highest in hierarchy are the timestamps # chart becomes a list of dictionaries, a dictionary for each timestamp chart=[] for i in chart_lis: chart.append({}) chart[-1]['date']=int(i[0])/1000 chart[-1]['open']=float(i[1]) chart[-1]['high']=float(i[2]) chart[-1]['low']=float(i[3]) chart[-1]['close']=float(i[4]) chart[-1]['volume']=float(i[5]) chart[-1]['closeTime']=i[6] chart[-1]['quoteAssetVolume']=i[7] chart[-1]['numberOfTrades']=i[8] chart[-1]['takerBuyBaseAssetVolume']=i[9] chart[-1]['takerBuyQuoteAssetVolume']=i[10] if(self.platform=='poloniex'): chart=self.conn.returnChartData(currencyPair=pair, start=start, end=end, period=period) return chart
def maTS(st, et, cur, dp): currency = cur period = 86400 periodDays = dp startTime = time.mktime( datetime.datetime.strptime(st, "%Y/%m/%d").timetuple()) endTime = time.mktime( datetime.datetime.strptime(et, "%Y/%m/%d").timetuple()) connection = Poloniex( "343DL7SG-Z5FLTLRU-W72O0KVT-OH3GZSET", "a53e8a0defdd21398547fd4a33f64b80555ae4c3f6704c8b6f0602c05dfbcea459be7af7de9449a28c14c51a2e50b6bba70c7f90ab318293e16ab9241a374ace" ) sellOrders = 0 buyOrders = 0 sellPrices = [] buyPrices = [] current_MA = 0 movingAvgs = [] historicalData = connection.returnChartData(currency, period, startTime, endTime) cPrices = [] for c in historicalData: cPrices.append(c['close']) #closing prices for all days in period datesConv = [] for d in historicalData: dates = d['date'] + period dates1 = datetime.datetime.fromtimestamp( int(dates)).strftime('%Y-%m-%d') datesConv.append(dates1) #calculate number of mas countofMA = len( historicalData ) - periodDays + 1 #How many moving averages will be generated according to values derived from API according to start time to end time if (countofMA < 1): return [], 0, 0, 0 orders = [] #from now calculate for i in range( countofMA): #First Loop i indicating moving average index, 0 start s = 0.0 xPrices = [] for j in range(periodDays): #j is the calculated price in index index = i + j xPrices.append(cPrices[index]) #prices added to xPrices list s = s + cPrices[ index] #Adds up the values in cPrices according to sizeofMA current_day = datesConv[i + periodDays - 1] movingAvgs.append(s / periodDays) current_MA = movingAvgs[-1] priceNow = xPrices[-1] lastPrice = xPrices[-2] order = dict() order['date'] = current_day order['price'] = priceNow order['ma'] = current_MA if ((priceNow > current_MA) and (priceNow < lastPrice)): # print("Sell Order at ", current_day, " [Current Price:" , priceNow, " MA:",current_MA,"]") order['action'] = 'Sell' elif ((priceNow < current_MA) and (priceNow > lastPrice)): # print("Buy Order at ", current_day, " [Current Price:" , priceNow, " MA:",current_MA,"]") order['action'] = 'Buy' else: order['action'] = 'Exit' orders.append(order) finalPrice = cPrices[-1] # print("Total sellOrders " + str(sellOrders)) # print("Total buyOrders " + str(buyOrders)) sellTotal = 0 buyTotal = 0 sellOrders = 0 buyOrders = 0 for order in orders: if order['action'] == 'Sell': sellTotal += order['price'] sellOrders += 1 if order['action'] == 'Buy': buyTotal += order['price'] buyOrders += 1 profit = 0.0 if sellOrders > buyOrders: diff = sellOrders - buyOrders buyBack = diff * finalPrice profit = sellTotal - buyTotal - buyBack else: diff = buyOrders - sellOrders sellBack = diff * finalPrice profit = sellTotal - buyTotal + sellBack return orders, profit
from poloniex import Poloniex from time import time import pandas as pd polo = Poloniex() # this should give you all the historical candle data for USDT_BTC market in 1hr candles candles = polo.returnChartData('USDT_BTC', period=1800, start=int(time()) - 60 * 60 * 24 * 30 * 3) df = pd.DataFrame.from_dict(candles) dates = pd.to_datetime(df['date'], unit='s') results = pd.DataFrame(dates.apply(lambda x: x.strftime('%Y-%m-%d %I-%p'))) results['close'] = df.close results['open'] = df.open results['low'] = df.low results['high'] = df.high results['volume'] = df.volume results.to_csv('data/test/BTC_USD_30min.csv')