Skip to content

Commit

Permalink
Added klines support
Browse files Browse the repository at this point in the history
  • Loading branch information
qbereau committed Jun 24, 2021
1 parent 42d110f commit 1dd4e68
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 0 deletions.
44 changes: 44 additions & 0 deletions fixture/vcr_cassettes/klines_symbol_interval_limit_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": [],
"request_body": "",
"url": "https://testnet.binance.vision/api/v3/klines?interval=1m&limit=1&symbol=BTCUSDT"
},
"response": {
"binary": false,
"body": "[[1624559520000,\"34970.90000000\",\"35047.03000000\",\"34800.01000000\",\"35047.03000000\",\"0.41947200\",1624559579999,\"14686.87819470\",37,\"0.41391700\",\"14492.85555020\",\"0\"]]",
"headers": {
"Content-Type": "application/json;charset=UTF-8",
"Content-Length": "166",
"Connection": "keep-alive",
"Date": "Thu, 24 Jun 2021 18:32:42 GMT",
"Server": "nginx",
"x-mbx-uuid": "36439ccd-17c4-4960-bbd3-d5fefb5c391a",
"x-mbx-used-weight": "3",
"x-mbx-used-weight-1m": "3",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains",
"X-Frame-Options": "SAMEORIGIN",
"X-Xss-Protection": "1; mode=block",
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'self'",
"X-Content-Security-Policy": "default-src 'self'",
"X-WebKit-CSP": "default-src 'self'",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"X-Cache": "Miss from cloudfront",
"Via": "1.1 1437ff2cfbc1ea8c7a36e6b0ce6e935a.cloudfront.net (CloudFront)",
"X-Amz-Cf-Pop": "ZRH50-C1",
"X-Amz-Cf-Id": "LrF2FY1xDr4aXxnYrzDl906yiAoMqEMoU6u1PeqAniO-UIJLOqJUOg=="
},
"status_code": 200,
"type": "ok"
}
}
]
45 changes: 45 additions & 0 deletions fixture/vcr_cassettes/klines_symbol_interval_ok.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": [],
"request_body": "",
"url": "https://testnet.binance.vision/api/v3/klines?endTime=1624440000000&interval=1m&limit=2&startTime=1624438800000&symbol=BTCUSDT"
},
"response": {
"binary": false,
"body": "[[1624438800000,\"33800.00000000\",\"33800.00000000\",\"33666.01000000\",\"33784.38000000\",\"0.64906000\",1624438859999,\"21900.06475101\",52,\"0.45358400\",\"15296.35728097\",\"0\"],[1624438860000,\"33789.26000000\",\"33845.40000000\",\"33785.58000000\",\"33845.40000000\",\"0.48879200\",1624438919999,\"16527.62739828\",35,\"0.48791700\",\"16498.06501578\",\"0\"]]",
"headers": {
"Content-Type": "application/json;charset=UTF-8",
"Content-Length": "331",
"Connection": "keep-alive",
"Date": "Thu, 24 Jun 2021 18:32:37 GMT",
"Server": "nginx",
"Vary": "Accept-Encoding",
"x-mbx-uuid": "36439ccd-17c4-4960-bbd3-d5fefb5c391a",
"x-mbx-used-weight": "1",
"x-mbx-used-weight-1m": "1",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains",
"X-Frame-Options": "SAMEORIGIN",
"X-Xss-Protection": "1; mode=block",
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'self'",
"X-Content-Security-Policy": "default-src 'self'",
"X-WebKit-CSP": "default-src 'self'",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"X-Cache": "Miss from cloudfront",
"Via": "1.1 1437ff2cfbc1ea8c7a36e6b0ce6e935a.cloudfront.net (CloudFront)",
"X-Amz-Cf-Pop": "ZRH50-C1",
"X-Amz-Cf-Id": "lgzm2fvUwFdq9eqYqkwha14oWkLsc3KKTk9gRoGNXtzgx-TXDVmBiQ=="
},
"status_code": 200,
"type": "ok"
}
}
]
21 changes: 21 additions & 0 deletions lib/ex_binance/kline.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule ExBinance.Kline do
@moduledoc """
Struct for representing the result returned by /api/v3/klines
"""

defstruct [
:open_time,
:open,
:high,
:low,
:close,
:volume,
:close_time,
:quote_asset_volume,
:number_of_trades,
:taker_buy_base_asset_volume,
:taker_buy_quote_asset_volume
]

use ExConstructor
end
29 changes: 29 additions & 0 deletions lib/ex_binance/public.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,33 @@ defmodule ExBinance.Public do
{:ok, ExBinance.OrderBook.new(data)}
end
end

def klines(symbol, interval, limit \\ nil, start_time \\ nil, end_time \\ nil) do
params = %{symbol: symbol, interval: interval}
|> maybe_put(:limit, limit)
|> maybe_put(:startTime, start_time)
|> maybe_put(:endTime, end_time)
with {:ok, data} <- get("/api/v3/klines", params) do
{:ok, Enum.map(data, fn x -> ExBinance.Kline.new(build_kline_object(x)) end)}
end
end

defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)

defp build_kline_object(kline_data) do
%{
:open_time => Enum.at(kline_data, 0),
:open => Enum.at(kline_data, 1),
:high => Enum.at(kline_data, 2),
:low => Enum.at(kline_data, 3),
:close => Enum.at(kline_data, 4),
:volume => Enum.at(kline_data, 5),
:close_time => Enum.at(kline_data, 6),
:quote_asset_volume => Enum.at(kline_data, 7),
:number_of_trades => Enum.at(kline_data, 8),
:taker_buy_base_asset_volume => Enum.at(kline_data, 9),
:taker_buy_quote_asset_volume => Enum.at(kline_data, 10)
}
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule ExBinance.MixProject do
{:httpoison, "~> 1.0"},
{:jason, "~> 1.1"},
{:exconstructor, "~> 1.1"},
{:hackney, "~> 1.17"},
{:ex_doc, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 1.0", only: [:dev], runtime: false},
{:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
Expand Down
78 changes: 78 additions & 0 deletions test/ex_binance/public_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,82 @@ defmodule ExBinance.PublicTest do
end
end
end

describe ".klines" do
test "returns klines for a given symbol and interval" do
use_cassette "klines_symbol_interval_ok" do
{:ok, klines} = ExBinance.Public.klines("BTCUSDT", "1m")

assert [%ExBinance.Kline{
close: "33336.52000000",
close_time: 1624529639999,
high: "33336.52000000",
low: "33325.49000000",
number_of_trades: 28,
open: "33334.78000000",
open_time: 1624529580000,
quote_asset_volume: "10243.20474104",
taker_buy_base_asset_volume: "0.24031200",
taker_buy_quote_asset_volume: "8009.99722824",
volume: "0.30731600"
} | _tail] = klines

assert length(klines) == 500
end
end

test "returns klines for a given symbol, interval and limit" do
use_cassette "klines_symbol_interval_limit_ok" do
{:ok, klines} = ExBinance.Public.klines("BTCUSDT", "1m", 1, nil, nil)

assert [%ExBinance.Kline{
close: "35047.03000000",
close_time: 1624559579999,
high: "35047.03000000",
low: "34800.01000000",
number_of_trades: 37,
open: "34970.90000000",
open_time: 1624559520000,
quote_asset_volume: "14686.87819470",
taker_buy_base_asset_volume: "0.41391700",
taker_buy_quote_asset_volume: "14492.85555020",
volume: "0.41947200"
}] = klines
end
end

test "returns klines for a given symbol, interval, start time, end time and limit" do
use_cassette "klines_symbol_interval_with_time_and_limit_ok" do
{:ok, klines} = ExBinance.Public.klines("BTCUSDT", "1m", 2, 1624438800000, 1624440000000)
assert klines == [
%ExBinance.Kline{
close: "33784.38000000",
close_time: 1624438859999,
high: "33800.00000000",
low: "33666.01000000",
number_of_trades: 52,
open: "33800.00000000",
open_time: 1624438800000,
quote_asset_volume: "21900.06475101",
taker_buy_base_asset_volume: "0.45358400",
taker_buy_quote_asset_volume: "15296.35728097",
volume: "0.64906000"
},
%ExBinance.Kline{
close: "33845.40000000",
close_time: 1624438919999,
high: "33845.40000000",
low: "33785.58000000",
number_of_trades: 35,
open: "33789.26000000",
open_time: 1624438860000,
quote_asset_volume: "16527.62739828",
taker_buy_base_asset_volume: "0.48791700",
taker_buy_quote_asset_volume: "16498.06501578",
volume: "0.48879200"
}
]
end
end
end
end

0 comments on commit 1dd4e68

Please sign in to comment.