Skip to content

Commit

Permalink
Add account config and activity
Browse files Browse the repository at this point in the history
Add account config and activity endpoints. Structured as submodules
of Account
  • Loading branch information
jrusso1020 committed Jun 11, 2020
1 parent 884a33d commit a8cf838
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 8 deletions.
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/account_activity/get_not_found.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://paper-api.alpaca.markets/v2/account/activities/FFFFF"
},
"response": {
"binary": false,
"body": "{\"code\":40010001,\"message\":\"invalid activity type: FFFFF\"}",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Thu, 11 Jun 2020 02:19:55 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": "58",
"Connection": "keep-alive",
"Vary": "Origin"
},
"status_code": 422,
"type": "ok"
}
}
]
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/account_activity/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://paper-api.alpaca.markets/v2/account/activities/FILL"
},
"response": {
"binary": false,
"body": "[{\"id\":\"20200603093014199::486a1a31-4d0c-494b-aaca-afa36477ddb8\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:14.199812Z\",\"type\":\"fill\",\"price\":\"886.55\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"c47ff82f-fa83-4083-9121-3623f4b3cb8d\",\"cum_qty\":\"5\"},{\"id\":\"20200603093004179::5fcf57bf-f076-4e6b-97b9-85407d7258f0\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:04.17912Z\",\"type\":\"fill\",\"price\":\"324.9\",\"qty\":\"2\",\"side\":\"sell\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"888e4b05-b624-48b9-a6cd-a1edf680acbf\",\"cum_qty\":\"2\"},{\"id\":\"20200603093002274::f8f99c37-0633-4c5b-83cf-0ac21225dbbe\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:02.274467Z\",\"type\":\"fill\",\"price\":\"1437.01\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"3dd828ae-7f70-449f-a54b-28758db2323d\",\"cum_qty\":\"5\"},{\"id\":\"20200602093229623::deef5aa4-42d7-414d-a467-07952a29ce5b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:32:29.623585Z\",\"type\":\"fill\",\"price\":\"889.68\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"5\"},{\"id\":\"20200602093022170::6e0a9fb0-1505-4021-b7f7-cdc2f86d201a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:22.170536Z\",\"type\":\"partial_fill\",\"price\":\"893.5\",\"qty\":\"4\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"1\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"4\"},{\"id\":\"20200602093021488::83d1ccf5-d406-4441-9b61-2c657f168a7a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:21.488152Z\",\"type\":\"fill\",\"price\":\"321.41\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"142b4101-86a9-42f5-b43e-be2f15419c83\",\"cum_qty\":\"1\"},{\"id\":\"20200602093015030::d088575b-d22b-43f0-8581-9273eb36d8bb\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:15.030543Z\",\"type\":\"fill\",\"price\":\"1431.01\",\"qty\":\"5\",\"side\":\"buy\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"c8c18333-c7b0-4513-9e26-b66d110f64cd\",\"cum_qty\":\"5\"},{\"id\":\"20200602093005948::a594c3f5-aa4a-4718-b565-12f3e5b68d6b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:05.948784Z\",\"type\":\"fill\",\"price\":\"321.25\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"534fcff6-a666-4495-bb61-5c405b39bada\",\"cum_qty\":\"1\"}]",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Thu, 11 Jun 2020 02:19:56 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Vary": "Accept-Encoding"
},
"status_code": 200,
"type": "ok"
}
}
]
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/account_activity/list_specific_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://paper-api.alpaca.markets/v2/account/activities?activity_types=FILL%2CTRANS"
},
"response": {
"binary": false,
"body": "[{\"id\":\"20200603093014199::486a1a31-4d0c-494b-aaca-afa36477ddb8\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:14.199812Z\",\"type\":\"fill\",\"price\":\"886.55\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"c47ff82f-fa83-4083-9121-3623f4b3cb8d\",\"cum_qty\":\"5\"},{\"id\":\"20200603093004179::5fcf57bf-f076-4e6b-97b9-85407d7258f0\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:04.17912Z\",\"type\":\"fill\",\"price\":\"324.9\",\"qty\":\"2\",\"side\":\"sell\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"888e4b05-b624-48b9-a6cd-a1edf680acbf\",\"cum_qty\":\"2\"},{\"id\":\"20200603093002274::f8f99c37-0633-4c5b-83cf-0ac21225dbbe\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:02.274467Z\",\"type\":\"fill\",\"price\":\"1437.01\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"3dd828ae-7f70-449f-a54b-28758db2323d\",\"cum_qty\":\"5\"},{\"id\":\"20200602093229623::deef5aa4-42d7-414d-a467-07952a29ce5b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:32:29.623585Z\",\"type\":\"fill\",\"price\":\"889.68\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"5\"},{\"id\":\"20200602093022170::6e0a9fb0-1505-4021-b7f7-cdc2f86d201a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:22.170536Z\",\"type\":\"partial_fill\",\"price\":\"893.5\",\"qty\":\"4\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"1\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"4\"},{\"id\":\"20200602093021488::83d1ccf5-d406-4441-9b61-2c657f168a7a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:21.488152Z\",\"type\":\"fill\",\"price\":\"321.41\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"142b4101-86a9-42f5-b43e-be2f15419c83\",\"cum_qty\":\"1\"},{\"id\":\"20200602093015030::d088575b-d22b-43f0-8581-9273eb36d8bb\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:15.030543Z\",\"type\":\"fill\",\"price\":\"1431.01\",\"qty\":\"5\",\"side\":\"buy\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"c8c18333-c7b0-4513-9e26-b66d110f64cd\",\"cum_qty\":\"5\"},{\"id\":\"20200602093005948::a594c3f5-aa4a-4718-b565-12f3e5b68d6b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:05.948784Z\",\"type\":\"fill\",\"price\":\"321.25\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"534fcff6-a666-4495-bb61-5c405b39bada\",\"cum_qty\":\"1\"}]",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Thu, 11 Jun 2020 02:19:35 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Vary": "Accept-Encoding"
},
"status_code": 200,
"type": "ok"
}
}
]
31 changes: 31 additions & 0 deletions fixture/vcr_cassettes/account_activity/list_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://paper-api.alpaca.markets/v2/account/activities"
},
"response": {
"binary": false,
"body": "[{\"id\":\"20200603093014199::486a1a31-4d0c-494b-aaca-afa36477ddb8\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:14.199812Z\",\"type\":\"fill\",\"price\":\"886.55\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"c47ff82f-fa83-4083-9121-3623f4b3cb8d\",\"cum_qty\":\"5\"},{\"id\":\"20200603093004179::5fcf57bf-f076-4e6b-97b9-85407d7258f0\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:04.17912Z\",\"type\":\"fill\",\"price\":\"324.9\",\"qty\":\"2\",\"side\":\"sell\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"888e4b05-b624-48b9-a6cd-a1edf680acbf\",\"cum_qty\":\"2\"},{\"id\":\"20200603093002274::f8f99c37-0633-4c5b-83cf-0ac21225dbbe\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-03T13:30:02.274467Z\",\"type\":\"fill\",\"price\":\"1437.01\",\"qty\":\"5\",\"side\":\"sell\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"3dd828ae-7f70-449f-a54b-28758db2323d\",\"cum_qty\":\"5\"},{\"id\":\"20200602093229623::deef5aa4-42d7-414d-a467-07952a29ce5b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:32:29.623585Z\",\"type\":\"fill\",\"price\":\"889.68\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"0\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"5\"},{\"id\":\"20200602093022170::6e0a9fb0-1505-4021-b7f7-cdc2f86d201a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:22.170536Z\",\"type\":\"partial_fill\",\"price\":\"893.5\",\"qty\":\"4\",\"side\":\"buy\",\"symbol\":\"TSLA\",\"leaves_qty\":\"1\",\"order_id\":\"36e3e15f-6fe2-4a32-8229-543a08a92fbf\",\"cum_qty\":\"4\"},{\"id\":\"20200602093021488::83d1ccf5-d406-4441-9b61-2c657f168a7a\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:21.488152Z\",\"type\":\"fill\",\"price\":\"321.41\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"142b4101-86a9-42f5-b43e-be2f15419c83\",\"cum_qty\":\"1\"},{\"id\":\"20200602093015030::d088575b-d22b-43f0-8581-9273eb36d8bb\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:15.030543Z\",\"type\":\"fill\",\"price\":\"1431.01\",\"qty\":\"5\",\"side\":\"buy\",\"symbol\":\"GOOG\",\"leaves_qty\":\"0\",\"order_id\":\"c8c18333-c7b0-4513-9e26-b66d110f64cd\",\"cum_qty\":\"5\"},{\"id\":\"20200602093005948::a594c3f5-aa4a-4718-b565-12f3e5b68d6b\",\"activity_type\":\"FILL\",\"transaction_time\":\"2020-06-02T13:30:05.948784Z\",\"type\":\"fill\",\"price\":\"321.25\",\"qty\":\"1\",\"side\":\"buy\",\"symbol\":\"AAPL\",\"leaves_qty\":\"0\",\"order_id\":\"534fcff6-a666-4495-bb61-5c405b39bada\",\"cum_qty\":\"1\"}]",
"headers": {
"Server": "nginx/1.16.1",
"Date": "Thu, 11 Jun 2020 02:19:54 GMT",
"Content-Type": "application/json; charset=UTF-8",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Vary": "Accept-Encoding"
},
"status_code": 200,
"type": "ok"
}
}
]
22 changes: 22 additions & 0 deletions lib/alpaca/models/account/activity.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Alpaca.Account.Activity do
@moduledoc """
A resource that allows us to perform operations on an Account Activity
An account activity has the following methods we can call on it
```
get/1
list/1
```
The `get/1` method allows us to get a singular account activity by calling
`Alpaca.Account.Activity.get(activity_type)`. Where `activity_type` is the
type of activity you want to retrieve information for.
The `list/1` method allows us to list all account activities by calling `Alpaca.Account.Activity.list(params)`.
Where the params is a map, and in this case takes only one param which is `activity_types`. A comma separated
string of activities types
"""
use Alpaca.Resource,
endpoint: "account/activities",
exclude: [:create, :edit, :delete, :delete_all]
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Alpaca.AccountConfiguration do
defmodule Alpaca.Account.Configuration do
@moduledoc """
A resource that allows us to query an AccountConfiguration from Alpaca
A resource that allows us to query an Account Configuration from Alpaca
"""

alias Alpaca.Client
Expand All @@ -9,7 +9,7 @@ defmodule Alpaca.AccountConfiguration do
Retrieve your Alpaca trading account configuration info
## Example
iex> {:ok, %{} = account_config} = Alpaca.AccountConfiguration.get()
iex> {:ok, %{} = account_config} = Alpaca.Account.Configuration.get()
Allows us to retrieve our own account configuration info as a result tuple {:ok, %{}}
if successful. If not success we will get back a result tuple {:error, {status: http_status_code, body: http_response_body}}
Expand All @@ -23,7 +23,7 @@ defmodule Alpaca.AccountConfiguration do
Edit your Alpaca trading account configuration info
## Example
iex> {:ok, %{} = account_config} = Alpaca.AccountConfiguration.edit(%{trade_confirm_email: "none"})
iex> {:ok, %{} = account_config} = Alpaca.Account.Configuration.edit(%{trade_confirm_email: "none"})
Allows us to edit our own account configuration info and return the new configuration as a result tuple {:ok, %{}}
if successful. If not success we will get back a result tuple {:error, {status: http_status_code, body: http_response_body}}
Expand Down
45 changes: 45 additions & 0 deletions test/alpaca/models/account/activity_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule Alpaca.Account.ActivityTest do
use ExUnit.Case, async: true
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney

alias Alpaca.Account.Activity

describe "list/1" do
test "gets the list of account activities successfully" do
use_cassette "account_activity/list_success" do
assert {:ok, account_activities} = Activity.list()
assert length(account_activities) == 8
end
end

test "gets the list of account activities successfully with defined types" do
use_cassette "account_activity/list_specific_success" do
assert {:ok, account_activities} = Activity.list(%{activity_types: "FILL,TRANS"})
assert length(account_activities) == 8
end
end
end

describe "get/1" do
test "gets the Account Activity successfully by symbol" do
use_cassette "account_activity/get_success" do
assert {:ok, account_activities} = Activity.get("FILL")
assert length(account_activities) == 8

Enum.each(account_activities, fn aa ->
assert aa.activity_type == "FILL"
end)
end
end

test "cannot find the Account Activity" do
use_cassette "account_activity/get_not_found" do
activity_type = "FFFFF"
message = "invalid activity type: FFFFF"

assert {:error, %{status: 422, body: %{code: 40_010_001, message: ^message}}} =
Activity.get(activity_type)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Alpaca.AccountConfigurationTest do
defmodule Alpaca.Account.ConfigurationTest do
use ExUnit.Case, async: true
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney

alias Alpaca.AccountConfiguration
alias Alpaca.Account.Configuration

describe "get/0" do
test "gets the account config successfully" do
Expand All @@ -13,7 +13,7 @@ defmodule Alpaca.AccountConfigurationTest do
no_shorting: false,
suspend_trade: false,
trade_confirm_email: "all"
}} = AccountConfiguration.get()
}} = Configuration.get()
end
end
end
Expand All @@ -27,7 +27,7 @@ defmodule Alpaca.AccountConfigurationTest do
no_shorting: false,
suspend_trade: false,
trade_confirm_email: "none"
}} = AccountConfiguration.edit(%{trade_confirm_email: "none"})
}} = Configuration.edit(%{trade_confirm_email: "none"})
end
end
end
Expand Down

0 comments on commit a8cf838

Please sign in to comment.