-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-Stock.psm1
232 lines (206 loc) · 5.94 KB
/
Get-Stock.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<#
Author: Tyler Wright
Git Repo: https://github.com/tylwright/Get-Stock
.Synopsis
Shows information regarding a particular stock
.Parameter Symbol
Symbol of stock you wish to get the details of
.Example
Get-Stock -Symbol AAPL
#>
function Get-Stock
{
# Script Arguments
param
(
[Parameter(Mandatory = $true, HelpMessage = "Stock symbol (ex. AAPL)")]
[ValidateNotNullOrEmpty()]
[string]$symbol,
[Parameter(Mandatory = $false,
HelpMessage = 'Return value of specific item')]
[ValidateNotNullOrEmpty()]
[string]$select
)
# Functions
<#
.Synopsis
Method used to grab results of an API call
#>
function Get-Results
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "URL of API call")]
[ValidateNotNullOrEmpty()]
[string]$url
)
try
{
$results = Invoke-WebRequest -Uri $url
$results = $results.Content | ConvertFrom-Json
return $results
}
catch
{
Write-Error "Symbol (${symbol}) could not be found."
Exit
}
}
<#
.Synopsis
Gather open or close price of stock based on time given
.Parameter Time
Get price at open or close
.Example
Get-OpenClosePrice -Time open
.Example
Get-OpenClosePrice -Time close
#>
function Get-OpenClosePrice
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Check price at open or close")]
[ValidateNotNullOrEmpty()]
[string]$time
)
if ($time -eq 'close' -or $time -eq 'open')
{
return (Get-Results -url "https://api.iextrading.com/1.0/stock/${symbol}/ohlc").$time.price
}
else
{
Write-Error "Available options: open, close"
}
}
<#
.Synopsis
Gathers many different items from the /company API call
#>
function Get-StockCompanyInfo
{
return (Get-Results -url "https://api.iextrading.com/1.0/stock/${symbol}/company")
}
<#
.Synopsis
Returns CEO of company
.Parameter results
Returned data from Get-StockCompanyInfo
#>
function Get-CEO
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Results from Get-StockCompanyInfo")]
[ValidateNotNullOrEmpty()]
$results
)
return $results.CEO
}
<#
.Synopsis
Returns URL of company's website
.Parameter results
Returned data from Get-StockCompanyInfo
#>
function Get-Website
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Results from Get-StockCompanyInfo")]
[ValidateNotNullOrEmpty()]
$results
)
return $results.website
}
<#
.Synopsis
Returns company name
.Parameter results
Returned data from Get-StockCompanyInfo
#>
function Get-Name
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Results from Get-StockCompanyInfo")]
[ValidateNotNullOrEmpty()]
$results
)
return $results.companyName
}
<#
.Synopsis
Returns industry of company
.Parameter results
Returned data from Get-StockCompanyInfo
#>
function Get-Industry
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Results from Get-StockCompanyInfo")]
[ValidateNotNullOrEmpty()]
$results
)
return $results.industry
}
<#
.Synopsis
Returns description of company
.Parameter results
Returned data from Get-StockCompanyInfo
#>
function Get-Description
{
param
(
[Parameter(Mandatory = $true, HelpMessage = "Results from Get-StockCompanyInfo")]
[ValidateNotNullOrEmpty()]
$results
)
return $results.description
}
<#
.Synopsis
Returns current price of stock
#>
function Get-Price
{
return (Get-Results -url "https://api.iextrading.com/1.0/stock/${symbol}/price")
}
# Main
if ($select)
{
if ($select.ToLower() -notmatch 'price|openingprice|closingprice|ceo|industry|website|company')
{
throw "Select options: price, openingPrice, closingPrice, CEO, industry, website, company"
}
}
Switch ($select)
{
"price" { Get-Price }
"closingPrice" { Get-OpenClosePrice -time close }
"openingPrice" { Get-OpenClosePrice -time open }
"ceo" { Get-CEO -results (Get-StockCompanyInfo) }
"industry" { Get-Industry -results (Get-StockCompanyInfo) }
"website" { Get-Website -results (Get-StockCompanyInfo) }
"company" { Get-Name -results (Get-StockCompanyInfo) }
default
{
# If making the same API call numerous times for the same group of data, stop and just call it once here.
# From here, take the returned data and use it anywhere you wish.
# This saves us from having to call the API over and over for the same info.
$results = Get-StockCompanyInfo
# Start building report
$report = [ordered]@{
Company = Get-Name -results $results; CEO = Get-CEO -results $results; Industry = Get-Industry -results $results;
Website = Get-Website -results $results; '-' = '-';
Open = Get-OpenClosePrice -time open; Close = Get-OpenClosePrice -time close; Price = Get-Price;
}
# Print the report
$report | Format-Table -HideTableHeaders
}
}
}
Export-ModuleMember -Function Get-Stock