Skip to content

Commit d7a4a38

Browse files
committed
polygon init
1 parent cc775a1 commit d7a4a38

File tree

7 files changed

+524
-2
lines changed

7 files changed

+524
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import polygon from "../../polygon.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "polygon-get-company-financials",
6+
name: "Get Company Financials",
7+
description: "Retrieves financial details for a specific company by stock ticker. [See the documentation](https://polygon.io/docs/stocks/get_v3_reference_financials).",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
polygon,
12+
stockTicker: {
13+
propDefinition: [
14+
polygon,
15+
"stockTicker",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const financialDetails = await this.polygon.getFinancialDetails();
21+
$.export("$summary", `Successfully retrieved financial details for ${this.stockTicker}`);
22+
return financialDetails;
23+
},
24+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import polygon from "../../polygon.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "polygon-get-historical-prices",
6+
name: "Get Historical Prices",
7+
description: "Fetches historical price data for a specified stock ticker within a date range. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
polygon,
12+
stockTicker: {
13+
propDefinition: [
14+
polygon,
15+
"stockTicker",
16+
],
17+
},
18+
fromDate: {
19+
propDefinition: [
20+
polygon,
21+
"fromDate",
22+
],
23+
},
24+
toDate: {
25+
propDefinition: [
26+
polygon,
27+
"toDate",
28+
],
29+
},
30+
},
31+
async run({ $ }) {
32+
const response = await this.polygon.getHistoricalPriceData();
33+
$.export("$summary", `Fetched historical prices for ${this.stockTicker} from ${this.fromDate} to ${this.toDate}.`);
34+
return response;
35+
},
36+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import polygon from "../../polygon.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "polygon-get-stock-price",
6+
name: "Get Stock Price",
7+
description: "Retrieves the current price of a specified stock ticker. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
polygon,
12+
stockTicker: {
13+
propDefinition: [
14+
"polygon",
15+
"stockTicker",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.polygon.getCurrentPrice();
21+
const currentPrice = response?.tickers?.[0]?.lastTrade?.p;
22+
23+
$.export("$summary", `Current price of ${this.stockTicker} is $${currentPrice}`);
24+
return response;
25+
},
26+
};

components/polygon/polygon.app.mjs

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,143 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "polygon",
4-
propDefinitions: {},
6+
version: "0.0.{{ts}}",
7+
propDefinitions: {
8+
stockTicker: {
9+
type: "string",
10+
label: "Stock Ticker",
11+
description: "The stock ticker symbol, e.g., AAPL, MSFT",
12+
},
13+
keywords: {
14+
type: "string[]",
15+
label: "Keywords",
16+
description: "Keywords to filter news articles",
17+
optional: true,
18+
},
19+
newsStockTickers: {
20+
type: "string[]",
21+
label: "News Stock Tickers",
22+
description: "Stock tickers to filter news articles related to specific stocks",
23+
optional: true,
24+
},
25+
timeInterval: {
26+
type: "string",
27+
label: "Time Interval",
28+
description: "Desired time interval for daily price summary",
29+
optional: true,
30+
},
31+
fromDate: {
32+
type: "string",
33+
label: "From Date",
34+
description: "Start date for historical price data (YYYY-MM-DD)",
35+
optional: true,
36+
},
37+
toDate: {
38+
type: "string",
39+
label: "To Date",
40+
description: "End date for historical price data (YYYY-MM-DD)",
41+
optional: true,
42+
},
43+
},
544
methods: {
6-
// this.$auth contains connected account data
45+
// Logs authentication keys for debugging purposes
746
authKeys() {
847
console.log(Object.keys(this.$auth));
948
},
49+
// Returns the base URL for Polygon API
50+
_baseUrl() {
51+
return "https://api.polygon.io";
52+
},
53+
// Makes an HTTP request using axios with the provided options
54+
async _makeRequest(opts = {}) {
55+
const {
56+
$ = this, method = "GET", path = "/", headers, ...otherOpts
57+
} = opts;
58+
return axios($, {
59+
...otherOpts,
60+
method,
61+
url: `${this._baseUrl()}${path}`,
62+
params: {
63+
apiKey: this.$auth.api_key,
64+
...(otherOpts.params || {}),
65+
},
66+
headers: {
67+
...headers,
68+
"user-agent": "@PipedreamHQ/pipedream v0.1",
69+
},
70+
});
71+
},
72+
// Retrieves trade events for the specified stock ticker
73+
async getTradeEvents() {
74+
return this._makeRequest({
75+
path: `/v2/trades/${this.stockTicker}`,
76+
});
77+
},
78+
// Retrieves news articles related to the stock ticker with optional filters
79+
async getNewsArticles() {
80+
const params = {
81+
tickers: this.stockTicker,
82+
...(this.keywords && this.keywords.length > 0
83+
? {
84+
keywords: this.keywords.join(","),
85+
}
86+
: {}),
87+
...(this.newsStockTickers && this.newsStockTickers.length > 0
88+
? {
89+
tickers: this.newsStockTickers.join(","),
90+
}
91+
: {}),
92+
};
93+
return this._makeRequest({
94+
path: "/v2/reference/news",
95+
params,
96+
});
97+
},
98+
// Retrieves the daily price summary for the specified stock ticker
99+
async getDailyPriceSummary() {
100+
const today = new Date().toISOString()
101+
.split("T")[0];
102+
const path = `/v1/open-close/${this.stockTicker}/${today}`;
103+
const params = {
104+
adjusted: true,
105+
...(this.timeInterval
106+
? {
107+
interval: this.timeInterval,
108+
}
109+
: {}),
110+
};
111+
return this._makeRequest({
112+
path,
113+
params,
114+
});
115+
},
116+
// Retrieves the current price for the specified stock ticker
117+
async getCurrentPrice() {
118+
return this._makeRequest({
119+
path: `/v2/snapshot/locale/us/markets/stocks/tickers/${this.stockTicker}`,
120+
});
121+
},
122+
// Fetches historical price data for the specified stock ticker within the date range
123+
async getHistoricalPriceData() {
124+
if (!this.fromDate || !this.toDate) {
125+
throw new Error("Both fromDate and toDate must be provided for historical price data.");
126+
}
127+
return this._makeRequest({
128+
path: `/v2/aggs/ticker/${this.stockTicker}/range/1/day/${this.fromDate}/${this.toDate}`,
129+
params: {
130+
adjusted: true,
131+
sort: "asc",
132+
limit: 120,
133+
},
134+
});
135+
},
136+
// Retrieves financial details for the specified stock ticker
137+
async getFinancialDetails() {
138+
return this._makeRequest({
139+
path: `/v3/reference/financials/${this.stockTicker}`,
140+
});
141+
},
10142
},
11143
};
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import {
2+
axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
3+
} from "@pipedream/platform";
4+
import polygon from "../../polygon.app.mjs";
5+
6+
export default {
7+
key: "polygon-new-market-news",
8+
name: "New Market News",
9+
description: "Emit new events when a news article related to the stock market is published. [See the documentation]()",
10+
version: "0.0.{{ts}}",
11+
type: "source",
12+
dedupe: "unique",
13+
props: {
14+
polygon,
15+
db: "$.service.db",
16+
timer: {
17+
type: "$.interface.timer",
18+
default: {
19+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
20+
},
21+
},
22+
stockTicker: {
23+
propDefinition: [
24+
polygon,
25+
"stockTicker",
26+
],
27+
},
28+
keywords: {
29+
propDefinition: [
30+
polygon,
31+
"keywords",
32+
],
33+
optional: true,
34+
},
35+
newsStockTickers: {
36+
propDefinition: [
37+
polygon,
38+
"newsStockTickers",
39+
],
40+
optional: true,
41+
},
42+
},
43+
hooks: {
44+
async deploy() {
45+
const newsArticles = await this.polygon.getNewsArticles();
46+
const articles = newsArticles.results || [];
47+
48+
// Sort by datetime ascending
49+
articles.sort((a, b) => new Date(a.datetime) - new Date(b.datetime));
50+
51+
// Keep the last 50 articles
52+
const recentArticles = articles.slice(-50);
53+
54+
for (const article of recentArticles) {
55+
this.$emit(
56+
article,
57+
{
58+
id: article.id || Date.parse(article.datetime),
59+
summary: article.title,
60+
ts: article.datetime
61+
? Date.parse(article.datetime)
62+
: Date.now(),
63+
},
64+
);
65+
}
66+
67+
if (recentArticles.length > 0) {
68+
const latestArticle = recentArticles[recentArticles.length - 1];
69+
const latestTimestamp = latestArticle.datetime
70+
? Date.parse(latestArticle.datetime)
71+
: Date.now();
72+
await this.db.set("lastTimestamp", latestTimestamp);
73+
}
74+
},
75+
async activate() {
76+
// No specific activation logic required
77+
},
78+
async deactivate() {
79+
// No specific deactivation logic required
80+
},
81+
},
82+
async run() {
83+
const lastTimestamp = (await this.db.get("lastTimestamp")) || 0;
84+
const newsArticles = await this.polygon.getNewsArticles();
85+
const articles = newsArticles.results || [];
86+
const newArticles = articles.filter((article) => article.datetime && Date.parse(article.datetime) > lastTimestamp);
87+
88+
// Sort by datetime ascending
89+
newArticles.sort((a, b) => new Date(a.datetime) - new Date(b.datetime));
90+
91+
for (const article of newArticles) {
92+
this.$emit(
93+
article,
94+
{
95+
id: article.id || Date.parse(article.datetime),
96+
summary: article.title,
97+
ts: article.datetime
98+
? Date.parse(article.datetime)
99+
: Date.now(),
100+
},
101+
);
102+
}
103+
104+
if (newArticles.length > 0) {
105+
const latestArticle = newArticles[newArticles.length - 1];
106+
const latestTimestamp = latestArticle.datetime
107+
? Date.parse(latestArticle.datetime)
108+
: Date.now();
109+
await this.db.set("lastTimestamp", latestTimestamp);
110+
}
111+
},
112+
};

0 commit comments

Comments
 (0)