Last active
April 18, 2020 15:48
-
-
Save jjttjj/956f9c358c42f32b9807f7e98ff02ac4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns strat2 | |
(:require [dev.jt.clojiex :as iex] | |
[tick.alpha.api :as t] | |
[clojure.repl :refer :all] | |
[clojure.pprint :refer :all])) | |
;;gist version | |
(set! *print-length* 20) | |
(set! *print-level* 5) | |
;rate of change with transducer version | |
(defn roc | |
([n] | |
(let [xs-atom (atom [])] | |
(map (fn [x] | |
(let [[fst :as xs] @xs-atom] | |
(if (= (count xs) n) | |
(do (swap! xs-atom #(-> % (subvec 1 n) (conj x))) | |
(/ x fst)) | |
(do (swap! xs-atom conj x) | |
nil))))))) | |
([n xs] | |
(concat (repeat n nil) (map / (drop n xs) xs)))) | |
(def SYMS ["FB" "AAPL" "AMZN" "NFLX" "GOOGL"]) | |
(def BARS (atom (zipmap SYMS (repeat [])))) | |
;;(def t2 (-> (t/yesterday) t/midnight (t/in (t/zone "America/New_York")))) | |
;;(def t1 (t/- t2 (t/new-period 300 :days))) | |
(def iex-client | |
{:version "stable" | |
:url-base "https://sandbox.iexapis.com" | |
:sse-url-base "https://sandbox-sse.iexapis.com" | |
:token "<redacted>"}) | |
(doseq [sym SYMS] | |
(iex/get | |
iex-client | |
[:stock/chart {:range "2y" :symbol sym}] | |
#(swap! BARS assoc sym %)) | |
(Thread/sleep 500)) | |
(def best-ct 2) | |
(def worst-ct 2) | |
(->> @BARS | |
(mapcat (fn [[sym bars]] | |
(let [bars (map (fn [old new] | |
(assoc new :ret | |
(- (/ (:close new) (:close old)) 1))) | |
bars (rest bars))] | |
(map #(-> %1 | |
(update :date t/date) | |
(select-keys [:date :open :high :low :close :ret]) | |
(assoc :roc %2 :sym sym)) | |
bars | |
(roc 30 (map :close bars)))))) | |
(group-by :date) | |
(into (sorted-map)) | |
(map (fn [[time bars]] | |
(->> bars | |
(filter (fn [{:keys [roc]}] roc)) | |
(sort-by (fn [{:keys [roc]}] | |
roc)) | |
(map-indexed | |
(fn [ix bar] | |
(assoc bar | |
:pos (cond | |
(< ix worst-ct) | |
-1 | |
;;drop worst + middle count | |
(> ix | |
(dec (+ worst-ct | |
(- (count SYMS) worst-ct best-ct)))) | |
1 | |
:else 0))))))) | |
(remove empty?) | |
(apply concat) | |
(group-by :sym) | |
(reduce (fn [acc [sym bars]] | |
(-> acc | |
(assoc-in [sym :total-ret] | |
(reduce (fn [total {:keys [ret pos]}] | |
(+ total (* ret pos))) | |
0 bars)) | |
(assoc-in [sym :bar-count] (count bars)))) {}) | |
time | |
pprint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment