@pipedream/alpaca 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020 Pipedream, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-cancel-all-orders",
6
+ version: "0.0.1",
7
+ name: "Cancel All Orders",
8
+ description: "Attempts to cancel all open orders. A response will be provided for each order that is attempted to be cancelled. If an order is no longer cancelable, the server reject the request, [See the docs](https://alpaca.markets/docs/api-references/trading-api/orders/#cancel-all-orders)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ },
18
+ async run ({ $ }) {
19
+ const response = await this.app.cancelAllOrders({
20
+ $,
21
+ isPaperAPI: this.isPaperAPI,
22
+ });
23
+ // eslint-disable-next-line multiline-ternary
24
+ $.export("$summary", `${response.length} order${response.length == 1 ? "" : "s"} has been cancelled.`);
25
+ return response;
26
+ },
27
+ };
@@ -0,0 +1,36 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-cancel-order",
6
+ version: "0.0.1",
7
+ name: "Cancel Order",
8
+ description: "Attempts to cancel an open order. If the order is no longer cancelable (example: status=`filled`), the server will reject the request, [See the docs](https://alpaca.markets/docs/api-references/trading-api/orders/#cancel-an-order)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ orderId: {
18
+ propDefinition: [
19
+ app,
20
+ "orderId",
21
+ (configuredProps) => ({
22
+ isPaperAPI: configuredProps.isPaperAPI,
23
+ }),
24
+ ],
25
+ },
26
+ },
27
+ async run ({ $ }) {
28
+ const response = await this.app.cancelOrder({
29
+ $,
30
+ isPaperAPI: this.isPaperAPI,
31
+ orderId: this.orderId,
32
+ });
33
+ $.export("$summary", `Order(ID:${this.orderId}) has been cancelled.`);
34
+ return response;
35
+ },
36
+ };
@@ -0,0 +1,36 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-close-all-positions",
6
+ version: "0.0.1",
7
+ name: "Close All Positions",
8
+ description: "Closes (liquidates) all of the account’s open long and short positions. A response will be provided for each order that is attempted to be cancelled. If an order is no longer cancelable, the server will reject the request, [See the docs](https://alpaca.markets/docs/api-references/trading-api/positions/#close-all-positions)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ cancelOrders: {
18
+ propDefinition: [
19
+ app,
20
+ "cancelOrders",
21
+ ],
22
+ },
23
+ },
24
+ async run ({ $ }) {
25
+ const response = await this.app.closeAllPositions({
26
+ $,
27
+ isPaperAPI: this.isPaperAPI,
28
+ params: {
29
+ cancel_orders: this.cancelOrders,
30
+ },
31
+ });
32
+ // eslint-disable-next-line multiline-ternary
33
+ $.export("$summary", `${response.length} position${response.length == 1 ? "" : "s"} has been closed.`);
34
+ return response;
35
+ },
36
+ };
@@ -0,0 +1,36 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-close-position",
6
+ version: "0.0.1",
7
+ name: "Close Position",
8
+ description: "Closes (liquidates) the account’s open position. Works for both long and short positions, [See the docs](https://alpaca.markets/docs/api-references/trading-api/positions/#close-a-position)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ positionSymbol: {
18
+ propDefinition: [
19
+ app,
20
+ "positionSymbol",
21
+ (configuredProps) => ({
22
+ isPaperAPI: configuredProps.isPaperAPI,
23
+ }),
24
+ ],
25
+ },
26
+ },
27
+ async run ({ $ }) {
28
+ const response = await this.app.closePosition({
29
+ $,
30
+ isPaperAPI: this.isPaperAPI,
31
+ symbol: this.positionSymbol,
32
+ });
33
+ $.export("$summary", `Position(Symbol:${this.positionSymbol}) has been closed.`);
34
+ return response;
35
+ },
36
+ };
@@ -0,0 +1,26 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-get-account-info",
6
+ version: "0.0.1",
7
+ name: "Get Account Info",
8
+ description: "Returns the account info, [See the docs](https://alpaca.markets/docs/api-references/trading-api/account/)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ },
18
+ async run ({ $ }) {
19
+ const response = this.app.getAccountInfo({
20
+ $,
21
+ isPaperAPI: this.isPaperAPI,
22
+ });
23
+ $.export("$summary", "Account info has been retreived.");
24
+ return response;
25
+ },
26
+ };
@@ -0,0 +1,99 @@
1
+ import app from "../../alpaca.app.mjs";
2
+ import { ConfigurationError } from "@pipedream/platform";
3
+
4
+ export default {
5
+ type: "action",
6
+ key: "alpaca-list-orders",
7
+ version: "0.0.1",
8
+ name: "List Orders",
9
+ description: "Retrieves a list of orders for the account, filtered by the supplied query parameters, if no filter given all will be returned, [See the docs](https://alpaca.markets/docs/api-references/trading-api/orders/#get-a-list-of-orders)",
10
+ props: {
11
+ app,
12
+ isPaperAPI: {
13
+ propDefinition: [
14
+ app,
15
+ "isPaperAPI",
16
+ ],
17
+ },
18
+ //the API does not provide pagination for symbols,
19
+ //returns all at once and there are more than 3k symbols
20
+ //because of these I preferred to get them from user input
21
+ symbols: {
22
+ type: "string[]",
23
+ label: "Symbols",
24
+ description: "A list of symbols to filter by (ex. `AAPL,TSLA,MSFT`). A currency pair is required for crypto orders (ex. `BTC/USD,BCH/USD,LTC/USDT,ETC/USDT`).",
25
+ optional: true,
26
+ },
27
+ side: {
28
+ propDefinition: [
29
+ app,
30
+ "side",
31
+ ],
32
+ description: "Filters down to orders that have a matching `side` field set.",
33
+ optional: true,
34
+ },
35
+ status: {
36
+ type: "string",
37
+ label: "Status",
38
+ description: "Order status to be queried. `open`, `closed` or `all`. Defaults to `open`.",
39
+ optional: true,
40
+ options: [
41
+ "open",
42
+ "closed",
43
+ "all",
44
+ ],
45
+ },
46
+ after: {
47
+ type: "string",
48
+ label: "After",
49
+ description: "The response will include only ones submitted after this time, in ISO 8601 format, e.g. `2022-09-07` or `2022-09-07T13:26:53`",
50
+ optional: true,
51
+ },
52
+ until: {
53
+ type: "string",
54
+ label: "Until",
55
+ description: "The response will include only ones submitted until this time, in ISO 8601 format, e.g. `2022-09-07` or `2022-09-07T13:26:53`",
56
+ optional: true,
57
+ },
58
+ nested: {
59
+ type: "string",
60
+ label: "Nested",
61
+ description: "If true, the result will roll up multi-leg orders under the `legs` field of primary order.",
62
+ optional: true,
63
+ },
64
+ },
65
+ async run ({ $ }) {
66
+ const after = this.after && Date.parse(this.after);
67
+ if (this.after && isNaN(after))
68
+ throw new ConfigurationError("`After` should be in ISO 8601 format!");
69
+ const until = this.until && Date.parse(this.until);
70
+ if (this.until && isNaN(until))
71
+ throw new ConfigurationError("`Until` should be in ISO 8601 format!");
72
+ let pageSize = 25, orders = [], nextAfter = after && new Date(after).toISOString();
73
+ while (true) {
74
+ const response = await this.app.getOrders({
75
+ $,
76
+ isPaperAPI: this.isPaperAPI,
77
+ params: {
78
+ symbols: this.symbols && this.symbols.join(),
79
+ side: this.side,
80
+ status: this.status,
81
+ after: nextAfter,
82
+ until: until && new Date(until).toISOString(),
83
+ nested: this.nested,
84
+ direction: "asc",
85
+ limit: pageSize,
86
+ },
87
+ });
88
+ orders.push(...response);
89
+ if (response.length < pageSize) {
90
+ break;
91
+ } else {
92
+ nextAfter = response.pop().updated_at;
93
+ }
94
+ }
95
+ // eslint-disable-next-line multiline-ternary
96
+ $.export("$summary", `${orders.length} order${orders.length == 1 ? "" : "s"} has been retrieved.`);
97
+ return orders;
98
+ },
99
+ };
@@ -0,0 +1,27 @@
1
+ import app from "../../alpaca.app.mjs";
2
+
3
+ export default {
4
+ type: "action",
5
+ key: "alpaca-list-positions",
6
+ version: "0.0.1",
7
+ name: "List Positions",
8
+ description: "Retrieves a list of the account’s open positions, [See the docs](https://alpaca.markets/docs/api-references/trading-api/positions/#get-open-positions)",
9
+ props: {
10
+ app,
11
+ isPaperAPI: {
12
+ propDefinition: [
13
+ app,
14
+ "isPaperAPI",
15
+ ],
16
+ },
17
+ },
18
+ async run ({ $ }) {
19
+ const response = await this.app.getPositions({
20
+ $,
21
+ isPaperAPI: this.isPaperAPI,
22
+ });
23
+ // eslint-disable-next-line multiline-ternary
24
+ $.export("$summary", `${response.length} position${response.length == 1 ? "" : "s"} has been retrieved.`);
25
+ return response;
26
+ },
27
+ };
@@ -0,0 +1,126 @@
1
+ import app from "../../alpaca.app.mjs";
2
+ import { ConfigurationError } from "@pipedream/platform";
3
+
4
+ export default {
5
+ type: "action",
6
+ key: "alpaca-place-order",
7
+ version: "0.0.1",
8
+ name: "Place Order",
9
+ description: "Places a new order for the given account. An order request may be rejected if the account is not authorized for trading, or if the tradable balance is insufficient to fill the order, [See the docs](https://alpaca.markets/docs/api-references/trading-api/orders/#request-a-new-order)",
10
+ props: {
11
+ app,
12
+ isPaperAPI: {
13
+ propDefinition: [
14
+ app,
15
+ "isPaperAPI",
16
+ ],
17
+ },
18
+ symbol: {
19
+ type: "string",
20
+ label: "Symbol",
21
+ description: "Symbol, asset ID, or currency pair to identify the asset to trade (ex. `AAPL`, `BTC/USD`).",
22
+ },
23
+ qty: {
24
+ type: "string",
25
+ label: "Qty",
26
+ description: "Number of shares to trade. Can be fractionable for only `market` and `day` order types.",
27
+ optional: true,
28
+ },
29
+ notional: {
30
+ type: "string",
31
+ label: "Notional",
32
+ description: "Dollar amount to trade. Cannot work with `Qty`. Can only work for `market` order types and `day` for time in force.",
33
+ optional: true,
34
+ },
35
+ side: {
36
+ propDefinition: [
37
+ app,
38
+ "side",
39
+ ],
40
+ },
41
+ type: {
42
+ type: "string",
43
+ label: "Type",
44
+ description: "Type",
45
+ options: [
46
+ "market",
47
+ "limit",
48
+ "stop",
49
+ "stop_limit",
50
+ "trailing_stop",
51
+ ],
52
+ },
53
+ timeInForce: {
54
+ type: "string",
55
+ label: "Time in Force",
56
+ description: "Please see [this doc](https://alpaca.markets/docs/trading/orders/#time-in-force) for more info on what values are possible for what kind of orders.",
57
+ options: [
58
+ "day",
59
+ "gtc",
60
+ "opg",
61
+ "cls",
62
+ "ioc",
63
+ "fok",
64
+ ],
65
+ },
66
+ limitPrice: {
67
+ type: "string",
68
+ label: "Limit Price",
69
+ description: "Required if type is `limit` or `stop_limit`",
70
+ optional: true,
71
+ },
72
+ stopPrice: {
73
+ type: "string",
74
+ label: "Stop Price",
75
+ description: "Required if type is `stop` or `stop_limit`",
76
+ optional: true,
77
+ },
78
+ trailPrice: {
79
+ type: "string",
80
+ label: "Trail Price",
81
+ description: "This or `trail_percent` is required if type is `trailing_stop`",
82
+ optional: true,
83
+ },
84
+ trailPercent: {
85
+ type: "string",
86
+ label: "Trail Percent",
87
+ description: "This or `trail_price` is required if type is `trailing_stop`",
88
+ optional: true,
89
+ },
90
+ },
91
+ async run ({ $ }) {
92
+ if (!this.qty && !this.notional) {
93
+ throw new ConfigurationError("Either `Qty` or `Notional` is required!");
94
+ }
95
+ if (this.qty && this.notional) {
96
+ throw new ConfigurationError("`Qty` and `Notional` cannot be given at the same time!");
97
+ }
98
+ if ((this.type == "limit" || this.type == "stop_limit") && !this.limitPrice) {
99
+ throw new ConfigurationError("`Limit Price` is required when `Type` is `limit` or `stop_limit`!");
100
+ }
101
+ if ((this.type == "stop" || this.type == "stop_limit") && !this.stopPrice) {
102
+ throw new ConfigurationError("`Stop Price` is required when `Type` is `limit` or `stop_limit`!");
103
+ }
104
+ if (this.type == "trailing_stop" && !this.trailPrice && !this.trailPercent) {
105
+ throw new ConfigurationError("`Trail Price` or `Trail Percent` is required when `Type` is `trailing_stop`!");
106
+ }
107
+ const response = await this.app.placeOrder({
108
+ $,
109
+ isPaperAPI: this.isPaperAPI,
110
+ data: {
111
+ symbol: this.symbol,
112
+ qty: this.qty,
113
+ notional: this.notional,
114
+ side: this.side,
115
+ type: this.type,
116
+ time_in_force: this.timeInForce,
117
+ limit_price: this.limitPrice,
118
+ stop_price: this.stopPrice,
119
+ trail_price: this.trailPrice,
120
+ trail_percent: this.trailPercent,
121
+ },
122
+ });
123
+ $.export("$summary", `Order(ID:${response.id}) has been placed.`);
124
+ return response;
125
+ },
126
+ };
package/alpaca.app.mjs ADDED
@@ -0,0 +1,161 @@
1
+ import { axios } from "@pipedream/platform";
2
+ //Did not use Alpaca JS SDK because it does not work with OAuth
3
+ export default {
4
+ type: "app",
5
+ app: "alpaca",
6
+ propDefinitions: {
7
+ isPaperAPI: {
8
+ type: "boolean",
9
+ label: "Paper API",
10
+ description: "Set to `true` if Paper API is used. Default is `false`.",
11
+ optional: true,
12
+ },
13
+ cancelOrders: {
14
+ type: "boolean",
15
+ label: "Cancel Orders",
16
+ description: "If `true` is specified, cancel all open orders before liquidating all positions.",
17
+ optional: true,
18
+ },
19
+ side: {
20
+ type: "string",
21
+ label: "Side",
22
+ description: "`buy` or `sell`",
23
+ options: [
24
+ "buy",
25
+ "sell",
26
+ ],
27
+ },
28
+ orderId: {
29
+ type: "string",
30
+ label: "Order Id",
31
+ description: "Order Id",
32
+ async options({
33
+ prevContext,
34
+ isPaperAPI,
35
+ }) {
36
+ const pageSize = 25;
37
+ const after = prevContext.nextAfter;
38
+ const orders = await this.getOrders({
39
+ isPaperAPI,
40
+ params: {
41
+ limit: pageSize,
42
+ after,
43
+ direction: "asc",
44
+ },
45
+ });
46
+ if (!orders.length) {
47
+ return [];
48
+ }
49
+ const nextAfter = orders[orders.length - 1]?.updated_at;
50
+ return {
51
+ options: orders.map((order) => ({
52
+ label: `${order.symbol} ${order.type} ${order.side} - ${order.qty ?? order.notional}`,
53
+ value: order.id,
54
+ })),
55
+ context: {
56
+ nextAfter,
57
+ },
58
+ };
59
+ },
60
+ },
61
+ positionSymbol: {
62
+ type: "string",
63
+ label: "Position Id",
64
+ description: "Position Id",
65
+ async options({ isPaperAPI }) {
66
+ const positions = await this.getPositions({
67
+ isPaperAPI,
68
+ });
69
+ return positions.map((position) => ({
70
+ label: `${position.symbol} ${position.side} - ${position.qty}`,
71
+ value: position.symbol,
72
+ }));
73
+ },
74
+ },
75
+ },
76
+ methods: {
77
+ _getUrl(path, isPaperAPI) {
78
+ // eslint-disable-next-line multiline-ternary
79
+ return `https://${isPaperAPI ? "paper-" : ""}api.alpaca.markets/v2${path}`;
80
+ },
81
+ _accessToken() {
82
+ return this.$auth.oauth_access_token;
83
+ },
84
+ _getHeaders(headers = {}) {
85
+ return {
86
+ "Authorization": `Bearer ${this._accessToken()}`,
87
+ "Content-Type": "application/json",
88
+ "Accept": "application/json",
89
+ "User-Agent": "@PipedreamHQ/pipedream v0.1",
90
+ ...headers,
91
+ };
92
+ },
93
+ async _makeRequest({
94
+ $ = this, isPaperAPI = false, path, headers, ...otherConfig
95
+ } = {}) {
96
+ const config = {
97
+ url: this._getUrl(path, isPaperAPI),
98
+ headers: this._getHeaders(headers),
99
+ ...otherConfig,
100
+ };
101
+ return axios($, config);
102
+ },
103
+ async getOrders({ ...args } = {}) {
104
+ return this._makeRequest({
105
+ path: "/orders",
106
+ ...args,
107
+ });
108
+ },
109
+ async getPositions({ ...args } = {}) {
110
+ return this._makeRequest({
111
+ path: "/positions",
112
+ ...args,
113
+ });
114
+ },
115
+ async cancelOrder({
116
+ orderId, ...args
117
+ } = {}) {
118
+ return this._makeRequest({
119
+ method: "DELETE",
120
+ path: `/orders/${orderId}`,
121
+ ...args,
122
+ });
123
+ },
124
+ async cancelAllOrders({ ...args } = {}) {
125
+ return this._makeRequest({
126
+ method: "DELETE",
127
+ path: "/orders",
128
+ ...args,
129
+ });
130
+ },
131
+ async closePosition({
132
+ symbol, ...args
133
+ } = {}) {
134
+ return this._makeRequest({
135
+ method: "DELETE",
136
+ path: `/positions/${symbol}`,
137
+ ...args,
138
+ });
139
+ },
140
+ async closeAllPositions({ ...args } = {}) {
141
+ return this._makeRequest({
142
+ method: "DELETE",
143
+ path: "/positions",
144
+ ...args,
145
+ });
146
+ },
147
+ async getAccountInfo({ ...args } = {}) {
148
+ return this._makeRequest({
149
+ path: "/account",
150
+ ...args,
151
+ });
152
+ },
153
+ async placeOrder({ ...args } = {}) {
154
+ return this._makeRequest({
155
+ method: "POST",
156
+ path: "/orders",
157
+ ...args,
158
+ });
159
+ },
160
+ },
161
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@pipedream/alpaca",
3
+ "version": "0.0.1",
4
+ "description": "Pipedream Alpaca Components",
5
+ "main": "alpaca.app.mjs",
6
+ "keywords": [
7
+ "pipedream",
8
+ "alpaca"
9
+ ],
10
+ "homepage": "https://pipedream.com/apps/alpaca",
11
+ "author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "@pipedream/platform": "^1.1.1"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ }
19
+ }