Skip to content

Commit

Permalink
bruteforce save entire backtest to transit-json
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Oct 12, 2024
1 parent bdc9126 commit bc6af81
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.3"}
metosin/malli {:mvn/version "0.16.0"}
metosin/malli {:mvn/version "0.16.0"} ; roundtrip schemas
de.otto/nom {:mvn/version "0.3.0"}
tick/tick {:mvn/version "0.6.2"}
org.pinkgorilla/timbre {:mvn/version "0.0.7"}
Expand Down
19 changes: 9 additions & 10 deletions dev/src/dev/algo_bollinger_bruteforce.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@
:label "brute1"})
print-table)

; | [0 :asset] | [2 :day :atr-n] | :target | :trades |
; |------------+-----------------+--------------------+---------|
; | BTCUSDT | 50 | 0.6040295470644278 | 126 |
; | BTCUSDT | 20 | 0.6040295470644278 | 126 |
; | ETHUSDT | 50 | 0.4967416107940467 | 131 |
; | ETHUSDT | 20 | 0.4967416107940467 | 131 |

(-> ".data/bruteforce/brute1.edn"
slurp
read-string)
; | [0 :asset] | [2 :day :atr-n] | :target | :trades | :id |
; |------------+-----------------+--------------------+---------+--------|
; | BTCUSDT | 50 | 0.5196291340803781 | 128 | ckdJDj |
; | BTCUSDT | 20 | 0.5196291340803781 | 128 | qQsof9 |
; | ETHUSDT | 50 | 0.471267441188845 | 134 | JNSsjP |
; | ETHUSDT | 20 | 0.471267441188845 | 134 | BWR3q4 |

(bf/show-available ".data/bruteforce/")
;; => ("brute1")

(bf/load-label ".data/bruteforce/" "brute1")
39 changes: 37 additions & 2 deletions src/quanta/trade/bruteforce.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
ds-safe (nippy->ds fname-nippy)]
(ds->transit-json-file fname-transit ds-safe)))

(defn save-backtest [report-dir id backtest]
(let [fname-nippy (str report-dir id "-backtest.nippy.gz")
fname-transit (str report-dir id "-backtest.transit-json")
_ (ds->nippy fname-nippy backtest)
ds-safe (nippy->ds fname-nippy)]
(ds->transit-json-file fname-transit ds-safe)))

(defn create-algo-task [dag-env algo cell-id dt variations target-fn show-fn report-dir]
; needs to throw so it can fail.
(m/via m/cpu
Expand All @@ -72,7 +79,8 @@
(when report-dir
(spit (str report-dir id "-result.edn") (pr-str report))
(spit (str report-dir id "-raw.txt") (with-out-str (println result)))
(save-ds report-dir id (:roundtrip-ds result)))
(save-ds report-dir id (:roundtrip-ds result))
(save-backtest report-dir id result))
report)))

(defn safe-algo-one [x]
Expand Down Expand Up @@ -105,10 +113,13 @@
"
[dag-env {:keys [algo cell-id variations dt
target-fn show-fn
label data-dir]
label
template-id
data-dir]
:or {show-fn (fn [result] {})
cell-id :backtest
dt (t/instant)
template-id nil
data-dir ".data/bruteforce/"}}]
; from: https://github.com/leonoel/missionary/wiki/Rate-limiting#bounded-blocking-execution
; When using (via blk ,,,) It's important to remember that the blocking thread pool
Expand All @@ -134,8 +145,32 @@
(let [report-filename (str data-dir label ".edn")]
(spit report-filename
(pprint-str {:label label
:template-id template-id
:calculated (t/instant)
:algo (safe-algo algo)
:variations variations
:result result}))))
result)))

(defn cut-extension [filename]
(subs filename 0 (- (count filename) 4)))

;; exploration

(defn show-available
"returns a seq of labels, this are templates that have been bruteforced
with :label set; the results can be explored in quanta-studio."
[dir]
(->> (fs/list-dir dir "*.edn")
(map fs/file-name)
(map cut-extension)))

(defn load-label
"loads template-label summary for a previously generated bruteforce.
dir must have / in the end"
[dir label]
(-> (str dir label ".edn")
slurp
read-string))


0 comments on commit bc6af81

Please sign in to comment.