Skip to content

Commit

Permalink
Replaced Binance API with Kraken example (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina authored Apr 6, 2023
1 parent 32fd0c1 commit 030910c
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions READMEs/publish-flow-restapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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})
Expand Down Expand Up @@ -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}")
```

0 comments on commit 030910c

Please sign in to comment.