From bb21d54da6d27daf14d71144f047aefd3a41910d Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Sun, 15 Nov 2020 18:55:49 -0500 Subject: [PATCH] Working iterative api query applied over a list of stock symbols. Read from file in scheme, not yet so for perl. --- .gitignore | 3 +++ stocklist.conf | 1 + stockminder.pl | 40 +++++++++++++++++++++++++++++++++++ stockminder.scm | 56 +++++++++++++++++++++++++++++++++---------------- 4 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 stocklist.conf create mode 100755 stockminder.pl diff --git a/.gitignore b/.gitignore index 726c3ca..996e333 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .*.sw* +backup_* +test.out +test.scm diff --git a/stocklist.conf b/stocklist.conf new file mode 100644 index 0000000..4fa88f8 --- /dev/null +++ b/stocklist.conf @@ -0,0 +1 @@ +VIU.TO VCN.TO VUN.TO diff --git a/stockminder.pl b/stockminder.pl new file mode 100755 index 0000000..e01ee5d --- /dev/null +++ b/stockminder.pl @@ -0,0 +1,40 @@ +#!/bin/env perl + +# Raw uri https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=VBU.TO&apikey=EC4C0NRKZAK1Q2UG +use strict; +use warnings; +use HTTP::Request; + +my $api_uri = "https://www.alphavantage.co/"; +my $api_key = "EC4C0NRKZAK1Q2UG"; + +my @symbols = +( + "VIU.TO", + "VCN.TO", + "VUN.TO" +); + +my %functions = +( + "timeseries_intraday", "TIME_SERIES_INTRADAY", + "timeseries_daily", "TIME_SERIES_DAILY", + "timeseries_daily_adj", "TIME_SERIES_DAILY_ADJUSTED", + "timeseries_weekly", "TIME_SERIES_WEEKLY", + "timeseries_weekly_adj", "TIME_SERIES_WEEKLY_ADJUSTED", + "timeseries_monthly", "TIME_SERIES_MONTHLY", + "timeseries_monthly_adj", "TIME_SERIES_MONTHLY_ADJUSTED" +); + +foreach my $stock_symbol (@symbols) +{ +print $api_uri, + "query?". + "function=", + $functions{'timeseries_daily'}, + "&symbol=", + $stock_symbol, + "&apikey=", + $api_key, + "\n"; +} diff --git a/stockminder.scm b/stockminder.scm index 2987abc..44e5993 100755 --- a/stockminder.scm +++ b/stockminder.scm @@ -1,24 +1,44 @@ #!/bin/env gxi -(import :std/net/request) -#|(print (request-text - (http-get "https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/v2/get-quotes?symbols=AMD%252CIBM%252CAAPL®ion=US" - headers: '( ("x-rapidapi-host" . "apidojo-yahoo-finance-v1.p.rapidapi.com") - ("x-rapidapi-key" . "98c32048f8mshe6f4d8a686ddce9p18cc5ajsn8b8888e7e256") ) - ) -) ) -(http-get "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=VBU.TO&apikey=EC4C0NRKZAK1Q2UG") -(print "Hello, Asshole!") -|# +(import :std/net/request + :std/format + :std/xml + :std/text/json) +#| Yahoo Finance Headers +headers: '( ("x-rapidapi-host" . "apidojo-yahoo-finance-v1.p.rapidapi.com") + ("x-rapidapi-key" . "98c32048f8mshe6f4d8a686ddce9p18cc5ajsn8b8888e7e256") ) +|# +; Declare the file from which we'll load our stockminder configuration +; Things like stock symbols, favourite functions, timing preferences, etc. +(define-values + (stockminder-config stocklist output-file) + (values + (open-input-file "stocklist.conf") +; Initialize our stocklist so we can throw stuff in there after we parse +; the config file. + `() + (open-output-file "test.out") ) ) +; The procedure we'll use to iterate through config values and add symbols +; to our stocklist +(define read-config (lambda (config-file) + (let read-loop ((config-chomp (read config-file))) + (if (eof-object? config-chomp) + (close-input-port config-file) + (and (set! stocklist (append stocklist `(,config-chomp))) + (read-loop (read config-file)) ) ) ) )) +; Now the business end; parse the config file +(read-config stockminder-config) +(define-values + (api-function api-url api-key) + (values + "TIME_SERIES_DAILY" + "https://www.alphavantage.co/" + "EC4C0NRKZAK1Q2UG" ) ) (let - ((stock-symbol "VCN.TO") - (api-function "TIME_SERIES_DAILY") - (api-key "EC4C0NRKZAK1Q2UG") - (api-url "https://www.alphavantage.co/") ) - - (print (request-text (http-get - (string-join (list api-url "query?function=" api-function - "&symbol=" stock-symbol "&apikey=" api-key) "" ) ))) ) + ((stock-symbols stocklist)) + (map (lambda (x) (display (string-append + api-url "query?function=" api-function "&symbol=" (symbol->string x) "&apikey=" api-key "\n"))) + stock-symbols ) )