From 030910cc00f06b59bdd21e5cdc8df94b8ebf8f35 Mon Sep 17 00:00:00 2001 From: Maria Carmina <50501033+mariacarmina@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:46:40 +0300 Subject: [PATCH] Replaced Binance API with Kraken example (#1382) --- READMEs/publish-flow-restapi.md | 41 ++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/READMEs/publish-flow-restapi.md b/READMEs/publish-flow-restapi.md index fd2f81e2c..7aa09377d 100644 --- a/READMEs/publish-flow-restapi.md +++ b/READMEs/publish-flow-restapi.md @@ -5,7 +5,7 @@ SPDX-License-Identifier: Apache-2.0 # Quickstart: Publish & Consume Flow for REST API-style URIs -This quickstart describes a flow to publish Binance REST API of ETH price feed, to make it available as free data asset on Ocean, and to consume it. +This quickstart describes a flow to publish Kraken REST API of OCEAN-USD pair price feed, to make it available as free data asset on Ocean, and to consume it. Here are the steps: @@ -24,13 +24,16 @@ Ensure that you've already (a) [installed Ocean](install.md), and (b) [set up lo In the same Python console: ```python -#data info -name = "Binance API v3 klines" +# Data info +name = "Kraken API OCEAN-USD price feed" +pair = 'OCEANUSD' # Choose the trading pair +interval = '1440' # Choose the time interval in minutes (1440 for daily) from datetime import datetime, timedelta end_datetime = datetime.now() -start_datetime = end_datetime - timedelta(days=7) #the previous week -url = f"https://api.binance.com/api/v3/klines?symbol=ETHUSDT&interval=1d&startTime={int(start_datetime.timestamp())*1000}&endTime={int(end_datetime.timestamp())*1000}" +start_datetime = end_datetime - timedelta(days=7) # The previous week +since = int(start_datetime.timestamp() * 1000) # Choose the start time in Unix timestamp +url = f'https://api.kraken.com/0/public/OHLC?pair={pair}&interval={interval}&since={since}' #create asset (data_nft, datatoken, ddo) = ocean.assets.create_url_asset(name, url, {"from": alice}) @@ -64,24 +67,24 @@ file_name = os.path.join(asset_dir, 'file0') Now, load the file and use its data. -The data follows the Binance docs specs for Kline/Candlestick Data, [here](https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-data). +The data follows the Kraken docs specs for Data, [here](https://docs.kraken.com/rest/#tag/Market-Data/operation/getOHLCData). In the same Python console: ```python -#load from file into memory +# Load from file into memory with open(file_name, "r") as file: - #data_str is a string holding a list of lists '[[1663113600000,"1574.40000000", ..]]' - data_str = file.read().rstrip().replace('"', '') - - -data = eval(data_str) - -#data is a list of lists -# -Outer list has one 7 entries; one entry per day. -# -Inner lists have 12 entries each: Kline open time, Open price, High price, Low price, close Price, Vol, .. - -#get close prices -close_prices = [float(data_at_day[4]) for data_at_day in data] + # Data is a string with the result inside. + data_str = file.read().rstrip().replace("'", '"') + +import json +data = json.loads(data_str) + +# Data is a list of lists +# -Outer dictionary contains 2 keys, one for errors and one for the result with the pair. +# -Inner dictionary have 9 entries each: Kline open time, Open price, High price, Low price, close Price, Vol, .. +# Get close price +close_prices = [float(data_at_day[4]) for data_at_day in data['result'][pair]] +print(f"close prices: {close_prices}") ```