diff --git a/lib/trade/resources/quanta/notebook/trade_entry_exit.clj b/lib/trade/resources/quanta/notebook/trade_entry_exit.clj new file mode 100644 index 00000000..c0704b62 --- /dev/null +++ b/lib/trade/resources/quanta/notebook/trade_entry_exit.clj @@ -0,0 +1,29 @@ +(ns quanta.notebook.trade-entry-exit + (:require + [tick.core :as t] + [tablecloth.api :as tc] + [ta.trade.signal.core :refer [create-positions]] + [ta.trade.metrics :refer [metrics]] + [ta.viz.ds.metrics :refer [metrics-render-spec-impl]])) + +(def ds (tc/dataset {:date (repeatedly 6 #(t/instant)) + :close [100.0 104.0 106.0 103.0 102.0 108.0] + :high [100.0 104.0 106.0 103.0 102.0 108.0] + :low [100.0 104.0 106.0 103.0 102.0 108.0] + :entry [:long :nil nil :short :nil :nil]})) + +ds + +(def rts (-> (create-positions {:asset "QQQ" + :entry [:fixed-qty 3.1] + :exit [:time 2 + :loss-percent 2.5 + :profit-percent 5.0]} + ds) + :roundtrips)) + +rts + +(-> (metrics rts) + (metrics-render-spec-impl) + ) diff --git a/lib/trade/resources/quanta/notebook/trade_position.clj b/lib/trade/resources/quanta/notebook/trade_position.clj new file mode 100644 index 00000000..53ac1c6a --- /dev/null +++ b/lib/trade/resources/quanta/notebook/trade_position.clj @@ -0,0 +1,32 @@ +(ns quanta.notebook.trade-position + (:require + [tick.core :as t] + [tablecloth.api :as tc] + [ta.trade.position :refer [signal->roundtrips]] + [ta.trade.metrics :refer [metrics]] + [ta.viz.ds.metrics :refer [metrics-render-spec-impl]])) + +(def signal-ds (tc/dataset {:date [(t/instant "2020-01-01T00:00:00Z") + (t/instant "2020-01-02T00:00:00Z") + (t/instant "2020-01-03T00:00:00Z") + (t/instant "2020-02-04T00:00:00Z") + (t/instant "2020-03-05T00:00:00Z") + (t/instant "2020-04-06T00:00:00Z") + (t/instant "2020-05-07T00:00:00Z")] + :close [1.0 2.0 3.0 4.0 5.0 6.0 7.0] + :signal [:long :hold :flat ;rt1 + :short :hold :hold :flat ; rt2 + ]})) +signal-ds + +(def rts (signal->roundtrips signal-ds)) + +(def r (metrics rts)) + +(:roundtrip-ds r) +(:metrics r) +(:nav-ds r) + +(metrics-render-spec-impl r) + + diff --git a/lib/trade/src/ta/trade/core.clj b/lib/trade/src/ta/trade/core.clj index f07bffda..65b8b07f 100644 --- a/lib/trade/src/ta/trade/core.clj +++ b/lib/trade/src/ta/trade/core.clj @@ -7,30 +7,3 @@ (let [roundtrip-ds (signal->roundtrips bar-signal-ds)] (m/metrics roundtrip-ds))) -(comment - (require '[tick.core :as t]) - (require '[tablecloth.api :as tc]) - - (def signal-ds (tc/dataset {:date [(t/instant "2020-01-01T00:00:00Z") - (t/instant "2020-01-02T00:00:00Z") - (t/instant "2020-01-03T00:00:00Z") - (t/instant "2020-02-04T00:00:00Z") - (t/instant "2020-03-05T00:00:00Z") - (t/instant "2020-04-06T00:00:00Z") - (t/instant "2020-05-07T00:00:00Z")] - :close [1.0 2.0 3.0 4.0 5.0 6.0 7.0] - :signal [:long :hold :flat ;rt1 - :short :hold :hold :flat ; rt2 - ]})) - signal-ds - - (signal->roundtrips signal-ds) - - (def r (trade-summary signal-ds)) - - (:roundtrip-ds r) - (:metrics r) - (:nav-ds r) - -; - ) \ No newline at end of file diff --git a/lib/trade/src/ta/trade/nav/metrics.clj b/lib/trade/src/ta/trade/nav/metrics.clj index 43a3bf11..aa7b07dc 100644 --- a/lib/trade/src/ta/trade/nav/metrics.clj +++ b/lib/trade/src/ta/trade/nav/metrics.clj @@ -5,11 +5,9 @@ (defn calc-nav-metrics [roundtrip-perf-ds] (assert (:cum-ret-log roundtrip-perf-ds) "to calc nav-metrics :cum-ret-log column needs to be present!") (assert (:nav roundtrip-perf-ds) "to calc nav-metrics :nav column needs to be present!") - ;(println "NAV metrics...") (let [nav (:nav roundtrip-perf-ds) cum-ret-log (:cum-ret-log roundtrip-perf-ds) dd (drawdowns-from-value nav)] - ;(println "NAV-metrics .. done!") {:cum-pl (last cum-ret-log) :max-dd (apply max dd)})) diff --git a/lib/trade/src/ta/trade/roundtrip/metrics.clj b/lib/trade/src/ta/trade/roundtrip/metrics.clj index facabef2..5991af65 100644 --- a/lib/trade/src/ta/trade/roundtrip/metrics.clj +++ b/lib/trade/src/ta/trade/roundtrip/metrics.clj @@ -68,7 +68,7 @@ (assert (:bars roundtrips-ds) "to calc metrics :bars column needs to be present!") (assert (:win? roundtrips-ds) "to calc metrics :win? column needs to be present!") ;(println (tc/select-columns roundtrips-ds [:win? :ret-log :bars])) - (println "calc-roundtrip-metrics " roundtrips-ds) + (println "calc-roundtrip-metrics ..") (let [wl-stats (win-loss-stats roundtrips-ds) metrics (win-loss-performance-metrics wl-stats)] metrics)) diff --git a/lib/trade/src/ta/trade/roundtrip/performance.clj b/lib/trade/src/ta/trade/roundtrip/performance.clj index 1725d8b6..3d00964f 100644 --- a/lib/trade/src/ta/trade/roundtrip/performance.clj +++ b/lib/trade/src/ta/trade/roundtrip/performance.clj @@ -14,13 +14,13 @@ (dtype/emap sign-switch :float64 side-vec val-vec)) (defn add-performance [roundtrip-ds] - (println "add-performance roundtrips: " roundtrip-ds) + (println "add-performance roundtrips .. ") (let [{:keys [side entry-price exit-price exit-idx entry-idx]} roundtrip-ds _ (assert side) _ (assert entry-price) _ (assert entry-idx) _ (assert exit-price) - _ (assert exit-price) + _ (assert exit-idx) ret-abs (adjust (dfn/- exit-price entry-price) side) ret-prct (-> 100.0 (dfn/* ret-abs) (dfn// entry-price)) ret-log (adjust (dfn/- (dfn/log10 entry-price) (dfn/log10 exit-price)) side) @@ -42,8 +42,8 @@ :exit-idx [2 3 4 5] :entry-date [:d1 :d2 :d3 :d4] :exit-date [:d2 :d3 :d4 :d5] - :entry-price [1 2 3 4] - :exit-price [2 3 4 5]})) + :entry-price [1.0 2.0 3.0 4.0] + :exit-price [2.0 3.0 4.0 5.0]})) (add-performance ds) ;; => _unnamed [4 14]: @@ -55,6 +55,9 @@ ;; | :long | 3 | 4 | :d3 | :d4 | 3 | 4 | 1 | 33.33333333 | -0.12493874 | true | 1 | -0.24987747 | 1.75012253 | ;; | :short | 4 | 5 | :d4 | :d5 | 4 | 5 | -1 | -25.00000000 | 0.09691001 | false | 1 | -0.15296746 | 1.84703254 | + (-> (add-performance ds) + (tc/info)) + (Math/pow 10 2.8132) (dfn/cumsum [1 2 3]) diff --git a/lib/trade/src/ta/trade/signal/core.clj b/lib/trade/src/ta/trade/signal/core.clj index f04b25ad..d2c1d88a 100644 --- a/lib/trade/src/ta/trade/signal/core.clj +++ b/lib/trade/src/ta/trade/signal/core.clj @@ -62,24 +62,3 @@ {:roundtrips (tc/dataset @roundtrips) :exit exit-signal :ds bar-entry-exit-ds})) - -(comment - (require '[tick.core :as t]) - - (def ds (tc/dataset {:date (repeatedly 6 #(t/instant)) - :close [100.0 104.0 106.0 103.0 102.0 108.0] - :high [100.0 104.0 106.0 103.0 102.0 108.0] - :low [100.0 104.0 106.0 103.0 102.0 108.0] - :entry [:long :nil nil :short :nil :nil]})) - - ds - - (create-positions {:asset "QQQ" - :entry [:fixed-qty 3.1] - :exit [:time 15 - :loss-percent 2.5 - :profit-percent 5.0]} - ds) - -; - ) \ No newline at end of file diff --git a/lib/trade/src/ta/trade/signal/exit.clj b/lib/trade/src/ta/trade/signal/exit.clj index 42c4ad5f..f6cedea5 100644 --- a/lib/trade/src/ta/trade/signal/exit.clj +++ b/lib/trade/src/ta/trade/signal/exit.clj @@ -5,7 +5,7 @@ (defn- create-exit [position rule row] (assoc position :exit-rule rule - :exit-index (:idx row) + :exit-idx (:idx row) :exit-date (:date row))) (defmulti exit