Bitcoin Forum
December 26, 2024, 09:33:55 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Need help getting FTX Historical Data using API  (Read 122 times)
adaseb (OP)
Legendary
*
Offline Offline

Activity: 3878
Merit: 1733


View Profile
March 25, 2022, 08:16:05 PM
Merited by hugeblack (6), GreatArkansas (1)
 #1

Complete noobie here when it comes to fetching API data. I want to get historical data from FTX using their API. Their docs are here.
https://docs.ftx.com/#get-historical-index

I got Python installed and whenever I try and execute any of those scripts I get errors. Is there any beginners guide or some python imports I can download somewhere to make the API easier to execute?


Basically I would like to run this code

Quote
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(x = historical.index,
                                    open = historical['open'],
                                    high = historical['high'],
                                    low = historical['low'],
                                    close = historical['close'],
                                    ),
                     go.Scatter(x=historical.index, y=historical['20 SMA'], line=dict(color='purple', width=1))])


fig.show()

Which I got from following this guide

Quote

Which plots an actual graph. However I get errors because the none of the variables are defined and the algotrading101 doesn't have them stored anywhere. Seems like the guide is not helpful for beginners.

jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
March 25, 2022, 08:42:09 PM
Merited by adaseb (3)
 #2

Have you already installed a module for FTX and would you be able to remeber where you found how to do that? Unless I flicked through it too quickly I couldn't find it - I've implemented API calls for bybit before now and the documentation page looks similar but the bybit api got you to install their libraries so the endpoints were easier.

Also the requests file that's imported in one of the tutorials you linked to, was that just taken from their github?
adaseb (OP)
Legendary
*
Offline Offline

Activity: 3878
Merit: 1733


View Profile
March 25, 2022, 10:46:07 PM
 #3

Have you already installed a module for FTX and would you be able to remeber where you found how to do that? Unless I flicked through it too quickly I couldn't find it - I've implemented API calls for bybit before now and the documentation page looks similar but the bybit api got you to install their libraries so the endpoints were easier.

Also the requests file that's imported in one of the tutorials you linked to, was that just taken from their github?

Ahh I see. I think this is the FTX Github,

Quote

So I basically clone all that and put it in my Python directory correct?
vv181
Legendary
*
Offline Offline

Activity: 1932
Merit: 1273


View Profile
March 26, 2022, 12:24:45 PM
Merited by adaseb (3)
 #4

It seems you don't need to have any FTX additional library if you follow the guide. You only need Pandas and Plotly.

However I get errors because the none of the variables are defined and the algotrading101 doesn't have them stored anywhere. Seems like the guide is not helpful for beginners.
You have to set up/initialize the historical variable as on the article:

Code:
historical = requests.get('https://ftx.com/api/markets/BTC-0924/candles?resolution=3600&start_time=1609462800').json()
historical = pd.DataFrame(historical['result'])
historical.drop(['startTime'], axis = 1, inplace=True)
historical.head()
https://algotrading101.com/learn/ftx-api-guide/#historical-data

As example:

Code:
import requests
import pandas as pd
import plotly.graph_objects as go

historical = requests.get('https://ftx.com/api/markets/BTC-PERP/candles?resolution=3600&start_time=1609462800').json()
historical = pd.DataFrame(historical['result'])
historical.drop(['startTime'], axis = 1, inplace=True)
historical.head()
historical['time'] = pd.to_datetime(historical['time'], unit='ms')
historical.set_index('time', inplace=True)
historical['20 SMA'] = historical.close.rolling(20).mean()
historical.tail()

fig = go.Figure(data=[go.Candlestick(x = historical.index,
                                    open = historical['open'],
                                    high = historical['high'],
                                    low = historical['low'],
                                    close = historical['close'],
                                    ),
                     go.Scatter(x=historical.index, y=historical['20 SMA'], line=dict(color='purple', width=1))])


fig.show()

That will result as in below


You will need to configure the historical data as to what you want to achieve and make sure you correctly use the market you wanna choose.
adaseb (OP)
Legendary
*
Offline Offline

Activity: 3878
Merit: 1733


View Profile
April 04, 2022, 10:38:13 PM
 #5

After hours of installing and re-installing different dependancies I finally got it to work. I has lots of warnings and errors but the chart actually loads.

So I am wondering how I can add volumes to the current code listed below.

Quote
import requests
import pandas as pd
import plotly.graph_objects as go

historical = requests.get('https://ftx.com/api/markets/BTC-PERP/candles?resolution=3600&start_time=1609462800').json()
historical = pd.DataFrame(historical['result'])
historical.drop(['startTime'], axis = 1, inplace=True)
historical.head()
historical['time'] = pd.to_datetime(historical['time'], unit='ms')
historical.set_index('time', inplace=True)
historical['20 SMA'] = historical.close.rolling(20).mean()
historical.tail()

fig = go.Figure(data=[go.Candlestick(x = historical.index,
                                    open = historical['open'],
                                    high = historical['high'],
                                    low = historical['low'],
                                    close = historical['close'],
                                    ),
                     go.Scatter(x=historical.index, y=historical['20 SMA'], line=dict(color='purple', width=1))])


fig.show()

I found a similar plotly code and tried to rewrite it to work with FTX API however I am getting errors.

Loading this code works for AAPL

Quote
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# include candlestick with rangeselector
fig.add_trace(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']),
               secondary_y=True)

# include a go.Bar trace for volumes
fig.add_trace(go.Bar(x=df['Date'], y=df['AAPL.Volume']),
               secondary_y=False)

fig.layout.yaxis2.showgrid=False
fig.show()

So I rewrote it as best as I could to match the first code such as,

Quote
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://ftx.com/api/markets/BTC-PERP/candles?resolution=3600&start_time=1609462800')

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# include candlestick with rangeselector
fig.add_trace(go.Candlestick(x=df['startTime'],
                open=df['open'], high=df['high'],
                low=df['low'], close=df['close']),
               secondary_y=True)

# include a go.Bar trace for volumes
fig.add_trace(go.Bar(x=df['startTime'], y=df['volume']),
               secondary_y=False)

fig.layout.yaxis2.showgrid=False
fig.show()


However I get some error 403. I am pretty close and wondering if someone can look over my code and correct where my error is.
vv181
Legendary
*
Offline Offline

Activity: 1932
Merit: 1273


View Profile
April 06, 2022, 07:39:29 PM
 #6

@adaseb

The reason why it returned error 403 is that the code uses the read_csv function to fetch JSON data from the URL, note how the data source differs from your 2nd code and 3rd code. I barely know Pandas and know some Python a little bit, but here is a little bit fix for the code that shows candlesticks, volume bar, and 20 SMA.

Code:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import requests

# data
data = requests.get('https://ftx.com/api/markets/BTC-PERP/candles?resolution=3600&start_time=1609462800').json()

# data initialization
df = pd.DataFrame(data['result'])
df.head()
df['time'] = pd.to_datetime(df['time'], unit='ms')
df.set_index('time', inplace=True)
df['20 SMA'] = df.close.rolling(20).mean()
df.tail()

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# include candlestick with rangeselector
fig.add_trace(go.Candlestick(x=df['startTime'],
                open=df['open'], high=df['high'],
                low=df['low'], close=df['close']),
               secondary_y=True,)

# include a go.Bar trace for volumes
fig.add_trace(go.Bar(x=df['startTime'], y=df['volume']),
               secondary_y=False)

# include a 20 SMA
fig.add_trace(go.Scatter(x=df['startTime'], y=df['20 SMA'], line=dict(color='purple', width=1)))

fig.layout.yaxis2.showgrid=False

fig.show()

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!