Skip to content

Commit

Permalink
ta.trade notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Apr 24, 2024
1 parent dda3a24 commit d97585d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 56 deletions.
29 changes: 29 additions & 0 deletions lib/trade/resources/quanta/notebook/trade_entry_exit.clj
Original file line number Diff line number Diff line change
@@ -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)
)
32 changes: 32 additions & 0 deletions lib/trade/resources/quanta/notebook/trade_position.clj
Original file line number Diff line number Diff line change
@@ -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)


27 changes: 0 additions & 27 deletions lib/trade/src/ta/trade/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)

;
)
2 changes: 0 additions & 2 deletions lib/trade/src/ta/trade/nav/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)}))

Expand Down
2 changes: 1 addition & 1 deletion lib/trade/src/ta/trade/roundtrip/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 7 additions & 4 deletions lib/trade/src/ta/trade/roundtrip/performance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]:
Expand All @@ -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])
Expand Down
21 changes: 0 additions & 21 deletions lib/trade/src/ta/trade/signal/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)

;
)
2 changes: 1 addition & 1 deletion lib/trade/src/ta/trade/signal/exit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d97585d

Please sign in to comment.