Author

Topic: Need help getting FTX Historical Data using API (Read 118 times)

legendary
Activity: 1932
Merit: 1273
@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()

legendary
Activity: 3808
Merit: 1723
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.
legendary
Activity: 1932
Merit: 1273
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.
legendary
Activity: 3808
Merit: 1723
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?
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
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?
legendary
Activity: 3808
Merit: 1723
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.

Jump to: