Skip to content

Commit

Permalink
Merge pull request #15 from jrusso1020/last-trade
Browse files Browse the repository at this point in the history
Last Trade Data API
  • Loading branch information
jrusso1020 committed Jun 20, 2020
2 parents 5b07716 + 904aea9 commit a1b31f2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/last_trade/get_failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"request": {
"body": "",
"headers": {
"APCA-API-KEY-ID": "***",
"APCA-API-SECRET-KEY": "***"
},
"method": "get",
"options": {
"recv_timeout": 30000
},
"request_body": "",
"url": "https://data.alpaca.markets/v1/last/stocks/FFFFF"
},
"response": {
"binary": false,
"body": "{\"code\":40410000,\"message\":\"resource not found\"}",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Sat, 20 Jun 2020 18:54:17 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": "48",
"Connection": "keep-alive",
"Vary": "Origin"
},
"status_code": 404,
"type": "ok"
}
}
]
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/last_trade/get_success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"request": {
"body": "",
"headers": {
"APCA-API-KEY-ID": "***",
"APCA-API-SECRET-KEY": "***"
},
"method": "get",
"options": {
"recv_timeout": 30000
},
"request_body": "",
"url": "https://data.alpaca.markets/v1/last/stocks/AAPL"
},
"response": {
"binary": false,
"body": "{\"status\":\"success\",\"symbol\":\"AAPL\",\"last\":{\"price\":347.71,\"size\":100,\"exchange\":2,\"cond1\":12,\"cond2\":0,\"cond3\":0,\"cond4\":0,\"timestamp\":1592600755847000064}}",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Sat, 20 Jun 2020 18:54:18 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": "157",
"Connection": "keep-alive",
"Vary": "Origin"
},
"status_code": 200,
"type": "ok"
}
}
]
20 changes: 20 additions & 0 deletions lib/alpaca/models/last/trade.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Alpaca.Last.Trade do
@moduledoc """
A resource that allows us to query for the last trade of a symbol
An last trade activity has the following methods we can call on it
```
get/1
```
The `get/1` method allows us to get a singulat last trade for a given symbol by calling
`Alpaca.Last.Trade.get(symbol)`. Where `symbol` is the
stock symbol you want to retrieve last trade information for.
"""
alias Alpaca.Client

use Alpaca.Resource,
endpoint: "last/stocks",
exclude: [:list, :create, :edit, :delete, :delete_all, :update],
opts: [version: "v1", api_host: Client.data_api_host()]
end
21 changes: 21 additions & 0 deletions test/alpaca/models/last/trade_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Alpaca.TradeTest do
use ExUnit.Case, async: true
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney

alias Alpaca.Last.Trade

describe "get/1" do
test "gets the last trade successfully" do
use_cassette "last_trade/get_success" do
assert {:ok, %{status: "success", symbol: "AAPL", last: last}} = Trade.get("AAPL")
end
end

test "gets the last trade unsuccessfully" do
use_cassette "last_trade/get_failure" do
assert {:error, %{status: 404, body: %{code: 40_410_000, message: "resource not found"}}} =
Trade.get("FFFFF")
end
end
end
end

0 comments on commit a1b31f2

Please sign in to comment.