Skip to content

Instantly share code, notes, and snippets.

@shinseitaro
Created April 10, 2021 10:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shinseitaro/05d7344bacaa95889b32667c6d425929 to your computer and use it in GitHub Desktop.
Save shinseitaro/05d7344bacaa95889b32667c6d425929 to your computer and use it in GitHub Desktop.
ftx.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ftx.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"authorship_tag": "ABX9TyOOfEYad603FRHpqgMOYb+R",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/shinseitaro/05d7344bacaa95889b32667c6d425929/ftx.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6q9Zd6oO_-xT"
},
"source": [
"# FTX の Public API で遊ぶ\n",
"\n",
"[quan-digital/ftx at v1.1](https://github.com/quan-digital/ftx/tree/v1.1)\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "FpdXAIR-7_5v"
},
"source": [
"# install \n",
"!pip install ftx ciso8601\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "w5aSkH2K8BuO"
},
"source": [
"import ftx\n",
"import pandas as pd \n",
"from datetime import datetime \n",
"\n",
"# get client \n",
"client = ftx.FtxClient()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "c_0UwhYcKgve"
},
"source": [
"## マーケット情報"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h9kyTfJDH-Ab"
},
"source": [
"# FTX に上場されている全ての銘柄を取得\n",
"# spot, perpetual futures, expiring futures, and MOVE \n",
"pd.DataFrame(client.get_markets())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_PN3MDS9sJEG"
},
"source": [
"# 特定の銘柄だけ取得\n",
"client.get_market(\"BTC/USD\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "5HDIrMV0IMm5"
},
"source": [
"# Futures だけ取得\n",
"pd.DataFrame(client.get_futures())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "gQxqZmt9sA2d"
},
"source": [
"# 特定の先物だけ取得\n",
"client.get_future(\"XRP-PERP\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "exktBF10I7eC"
},
"source": [
"# 先物のStats\n",
"client.get_future_stats(\"ZRX-PERP\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "rFTECm2pKRH5"
},
"source": [
"# 現在のFR\n",
"pd.DataFrame(client.get_funding_rates())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DOYMK0hgsaF7"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "i67dwU1VHEYk"
},
"source": [
"## データ取得"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_twSFxUL8JKD"
},
"source": [
"# get current order book\n",
"client.get_orderbook('BTC/USD', 1)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vi4CurTy8oLA"
},
"source": [
"# get current BTC/USD market data and convert to pd.Series\n",
"pd.Series(client.get_market(\"BTC/USD\"))"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jZTGzoLx8wGi"
},
"source": [
"# Get trades\n",
"# リミット指定しなければ、結果は client.get_all_trades(\"BTC/USD\") と同じ\n",
"df_trades = pd.DataFrame(client.get_trades(\"BTC/USD\",))\n",
"df_trades.head()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "TTIYmfhOBmaA"
},
"source": [
"df_trades[df_trades[\"side\"]==\"buy\"].head()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "q3IPD8H1Bi78"
},
"source": [
"df_trades[df_trades[\"side\"]==\"sell\"].head()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "63lgSzTG_SQS"
},
"source": [
"# Get OHLCV and convert to DataFrame \n",
"x = client.get_historical_data(\"BTC/USD\",\n",
" resolution=300, #window length in seconds. options: 15, 60, 300, 900, 3600, 14400, 86400\n",
" limit=5000, # max 5000\n",
" start_time=datetime(2021,1,1).timestamp(), \n",
" end_time=datetime(2021,1,3).timestamp()\n",
" )\n",
"\n",
"df_btcusd_5min = pd.DataFrame(x)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "lyQTpr7CD5I_"
},
"source": [
"# doc には、time という結果の記載はない\n",
"# https://docs.ftx.com/#get-historical-prices\n",
"\n",
"df_btcusd_5min.head()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "FyvI3LZ3F2Kn"
},
"source": [
"# 直近のデータを取得\n",
"resolution = 15\n",
"limit = 5000\n",
"end_time = datetime.now().timestamp()\n",
"start_time = end_time - resolution * limit\n",
"\n",
"y = client.get_historical_data(\"BTC/USD\",\n",
" resolution=resolution,\n",
" limit=limit,\n",
" start_time=start_time, \n",
" end_time=end_time\n",
" )\n",
"pd.DataFrame(y).tail()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ztnVo2f6tk0Z"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment