Commits

evhan committed 001bc5d

change date expression format to (y m d)

Comments (0)

Files changed (3)

 
 ### fetch-history
 
-`fetch-history` takes a single stock symbol and, as optional arguments, an
-interval symbol (one of `daily`, `weekly`, `monthly`, or `dividends`), a start
-date as a list of numbers of the form `(dd mm yyyy)`, and an end date of the
-same form. It returns a table in one of two forms:
+`fetch-history` takes a single stock symbol and, as optional arguments,
+an interval symbol (one of `daily`, `weekly`, `monthly`, or
+`dividends`), a start date as a list of numbers of the form `(y m d)`,
+and an end date of the same form. It returns a table in one of two
+forms:
 
     > (fetch-history 'aapl 'monthly)
     ((row (date "2012-08-01")
           (adjusted-close 608.15))
       ...)
     
-    > (fetch-history 'aapl 'dividends '(1 1 1987) '(1 1 1989))
+    > (fetch-history 'aapl 'dividends '(1987 1 1) '(1989 1 1))
     ((row (date "1988-11-21") (dividend 0.025))
      (row (date "1988-08-15") (dividend 0.02))
      (row (date "1988-05-16") (dividend 0.02))
   (parameterize ((form-urlencoded-separator "&"))
     (update-uri base-historical-uri
          query: `((s . ,sym)
-                  (a . ,(car start))
-                  (b . ,(cadr start))
-                  (c . ,(caddr start))
-                  (d . ,(car end))
-                  (e . ,(cadr end))
-                  (f . ,(caddr end))
+                  (a . ,(- (cadr start) 1))
+                  (b . ,(caddr start))
+                  (c . ,(car start))
+                  (d . ,(- (cadr end) 1))
+                  (e . ,(caddr end))
+                  (f . ,(car end))
                   (g . ,(case freq
                           ((d day daily) 'd)
                           ((w week weekly) 'w)
       format))))
 
 (define (fetch-history symbol . args)
-  ;; There must be a better way to get dd/mm/yyyy.
-  (call-with-values
-    (lambda ()
-      (apply values
-        (vector->list (seconds->utc-time (current-seconds)))))
-    (lambda (_ _ _ day month year _ _ _ _)
-      (let-optionals args
-        ((freq  'daily)
-         (start (list 0 0 0))
-         (end   (list day (+ month 1) (+ year 1900))))
-        (cdr ; drop headers
-         (yahoo-api-request
-          (build-historical-request-uri symbol freq start end)
-          'row ; arbitrary
-          (if (member freq '(v div dividends))
-              '(date dividend)
-              '(date open high low close volume adjusted-close)))))))))
+  (let-optionals args
+    ((freq  'daily)
+     (start (list 1900 1 1))
+     (end   (let ((time (seconds->utc-time (current-seconds))))
+              (list (+ (vector-ref time 5) 1900) ; year
+                    (+ (vector-ref time 4) 1) ; month
+                    (vector-ref time 3))))) ; day
+    (cdr ; drop headers
+     (yahoo-api-request
+      (build-historical-request-uri symbol freq start end)
+      'row ; arbitrary
+      (if (member freq '(v div dividends))
+          '(date dividend)
+          '(date open high low close volume adjusted-close)))))))
 
 {{fetch-history}} takes a single stock symbol and, as optional arguments, an
 interval symbol (one of {{daily}}, {{weekly}}, {{monthly}}, or {{dividends}}),
-a start date as a list of numbers of the form {{(dd mm yyyy)}}, and an end date
-of the same form. It returns a table in one of two forms:
+a start date as a list of numbers of the form {{(y m d)}}, and an end date of
+the same form. It returns a table in one of two forms:
 
     > (fetch-history 'aapl 'monthly)
     ((row (date "2012-08-01")
           (adjusted-close 608.15))
       ...)
     
-    > (fetch-history 'aapl 'dividends '(1 1 1987) '(1 1 1989))
+    > (fetch-history 'aapl 'dividends '(1987 1 1) '(1989 1 1))
     ((row (date "1988-11-21") (dividend 0.025))
      (row (date "1988-08-15") (dividend 0.02))
      (row (date "1988-05-16") (dividend 0.02))