forked from jjttjj/trateg
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
awb99
committed
Jul 2, 2024
1 parent
e72a850
commit 615cd74
Showing
30 changed files
with
326 additions
and
270 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,15 @@ | ||
(ns ta.algo.backtest | ||
(:require | ||
[tick.core :as t] | ||
[taoensso.timbre :refer [trace debug info warn error]] | ||
[ta.calendar.core :as cal] | ||
[ta.calendar.combined :refer [combined-event-seq]] | ||
[quanta.model.protocol :as eng] | ||
[quanta.model.backtest :refer [fire-backtest-events]] | ||
[ta.algo.env.protocol :as algo-env] | ||
[ta.algo.env :as algo-env-impl])) | ||
|
||
(defn- dt->event-seq [calendars dt] | ||
(let [prior-dates (map (fn [[calendar-kw interval-kw]] | ||
(cal/prior-close calendar-kw interval-kw dt)) | ||
calendars) | ||
;prior-dates-sorted (sort prior-dates) | ||
] | ||
(map (fn [cal dt] | ||
{:calendar cal :time dt}) calendars prior-dates))) | ||
|
||
(defn- fire-calendar-events [engine window-or-dt] | ||
(let [calendars (eng/active-calendars engine) | ||
; 3 modes to create an event-seq | ||
; a map represents a window | ||
; a date represents the last date for all calendars | ||
; no date represents the current time. | ||
event-seq (if (map? window-or-dt) | ||
(combined-event-seq calendars window-or-dt) | ||
(dt->event-seq calendars (or window-or-dt | ||
(-> (t/now)(t/in "UTC"))))) | ||
] | ||
(info "backtesting: " window-or-dt " ..") | ||
; fire calendar events | ||
(doall (map #(eng/set-calendar! engine %) event-seq)) | ||
(info "backtesting window: " window-or-dt "finished!"))) | ||
[ta.algo.env :refer [create-env-javelin]])) | ||
|
||
(defn backtest-algo | ||
"runs an algo with data powered by bar-db. | ||
returns the result of the strategy." | ||
[bar-db algo-spec window-or-dt] | ||
(let [env (algo-env-impl/create-env-javelin bar-db) | ||
strategy (algo-env/add-algo env algo-spec) | ||
engine (algo-env/get-engine env)] | ||
(fire-calendar-events engine window-or-dt) | ||
(let [env (create-env-javelin bar-db) | ||
strategy (algo-env/add-algo env algo-spec) | ||
model (algo-env/get-model env)] | ||
(fire-backtest-events model window-or-dt) | ||
strategy)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
4 changes: 2 additions & 2 deletions
4
...urces/quanta/notebook/engine_calendar.clj → ...ources/quanta/notebook/model_calendar.clj
This file contains 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
6 changes: 3 additions & 3 deletions
6
...sources/quanta/notebook/engine_direct.clj → ...esources/quanta/notebook/model_direct.clj
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ources/quanta/notebook/engine_javelin.clj → ...sources/quanta/notebook/model_javelin.clj
This file contains 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
6 changes: 3 additions & 3 deletions
6
.../resources/quanta/notebook/engine_ops.clj → ...l/resources/quanta/notebook/model_ops.clj
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
(ns quanta.model.backtest | ||
(:require | ||
[tick.core :as t] | ||
[taoensso.timbre :refer [trace debug info warn error]] | ||
[ta.calendar.core :as cal] | ||
[ta.calendar.combined :refer [combined-event-seq]] | ||
[quanta.model.protocol :as mp])) | ||
|
||
|
||
(defn- dt->event-seq [calendars dt] | ||
(let [prior-dates (map (fn [[calendar-kw interval-kw]] | ||
(cal/prior-close calendar-kw interval-kw dt)) | ||
calendars) | ||
;prior-dates-sorted (sort prior-dates) | ||
] | ||
(info "date event seq: " prior-dates) | ||
(map (fn [cal dt] | ||
{:calendar cal :time dt}) calendars prior-dates))) | ||
|
||
(defn fire-backtest-events [model window-or-dt] | ||
(let [calendars (mp/active-calendars model) | ||
; 3 modes to create an event-seq | ||
; a map represents a window | ||
; a date represents the last date for all calendars | ||
; no date represents the current time. | ||
event-seq (if (map? window-or-dt) | ||
(combined-event-seq calendars window-or-dt) | ||
(dt->event-seq calendars (or window-or-dt | ||
(-> (t/now) (t/in "UTC")))))] | ||
(info "backtesting: " window-or-dt " ..") | ||
; fire calendar events | ||
(doall (map #(mp/set-calendar! model %) event-seq)) | ||
(info "backtesting window: " window-or-dt "finished!"))) |
This file contains 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
This file contains 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
Oops, something went wrong.