Skip to content

Commit

Permalink
notebook move in from quanta-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Apr 23, 2024
1 parent 417d2f9 commit 7720e39
Show file tree
Hide file tree
Showing 52 changed files with 1,856 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/algo/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
{:paths ["src" "resources"]
:deps
{org.clojure/clojure {:mvn/version "1.11.1"}
ta/db {:local/root "../db" :deps/manifest :deps} ; bar-db
Expand Down
40 changes: 40 additions & 0 deletions lib/algo/resources/quanta/notebook/algo_aligned.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns quanta.notebook.algo-aligned
(:require
[tick.core :as t]
[modular.system]
[taoensso.timbre :refer [trace debug info warn error]]
[tablecloth.api :as tc]
[ta.algo.env.core :refer [get-bars-aligned-filled]]
[ta.calendar.core :as cal]
[ta.env.backtest :refer [run-backtest]]
[ta.engine.javelin :refer [create-env]]
[ta.engine.javelin.algo :as dsl]))

(defn get-aligned [env {:keys [calendar trailing-n] :as opts} time]
(let [cal-seq (cal/trailing-window calendar trailing-n time)
bars (get-bars-aligned-filled env opts cal-seq)]
{:time time
:cal-seq cal-seq
:bars bars}))

(def window (-> (cal/trailing-range [:us :d] 1)
;(window-as-date-time)
))

window

(def spec {:asset "MSFT"
:calendar [:us :d]
:import :kibot
:trailing-n 100})

(def env (create-env :bardb-dynamic))
(def strategy (dsl/add-time-strategy env spec get-aligned))
(run-backtest env window)
@strategy

(t/inst)

(-> (t/inst)
(t/date)
(t/at (t/time "00:04:00")))
82 changes: 82 additions & 0 deletions lib/algo/resources/quanta/notebook/algo_dummy.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(ns quanta.notebook.algo-dummy
(:require
[ta.calendar.core :as cal]
[ta.engine.protocol :as eng]
[ta.algo.env :as algo-env]
[ta.algo.backtest :refer [backtest-algo run-backtest]]))

;; 1. time-based algo spec

(defn secret [env spec time]
(str "the spec is: " spec " (calculated: " time ")"))

(def spec {:type :time
:calendar [:us :d]
:data 42
:algo 'quanta.notebook.algo-dummy/secret})

(def e (algo-env/create-env-javelin nil))

(def algo (algo-env/add-algo e spec))

algo

;; 2. test algo calculation

(def engine (algo-env/get-engine e))

engine

(eng/set-calendar! engine {:calendar [:us :d] :time :evening})

algo
@algo
;; => "the spec is: {:type :time, :calendar [:us :d], :data 42, :algo notebook.playground.algo.dummy/secret} (calculated: :evening)"

;; 3. backtest with complex syntax

(def window (cal/trailing-range [:us :d] 1))

e
(run-backtest e window)

@algo
;; => "the spec is: {:type :time, :calendar [:us :d], :data 42, :algo notebook.playground.algo.dummy/secret} (calculated: 2024-02-26T17:00-05:00[America/New_York])"

;; 4. backtest with simple syntax

(def result
(backtest-algo :duckdb spec))

@result
;; => "the spec is: {:type :time, :calendar [:us :d], :data 42, :algo notebook.playground.algo.dummy/secret} (calculated: 2024-02-26T17:00-05:00[America/New_York])"

;; 5. backtest with formulas.

(defn combine [env spec & args]
{:args args})

(defn sum [env spec & args]
(apply + args))

(def combined-spec
[:a {:calendar [:us :d] :algo 'quanta.notebook.algo-dummy/secret :type :time}
:b {:type :time :calendar [:us :d] :data 42 :algo 'quanta.notebook.algo-dummy/secret}
:c {:value 4444}
:d {:formula [:a :b] :algo 'quanta.notebook.algo-dummy/combine :type :time}
:e {:value 2222}
:f {:formula [:c :e] :algo 'quanta.notebook.algo-dummy/sum :type :time}])

(require '[ta.algo.spec.ops :refer [spec->ops]])
(spec->ops e spec)
(spec->ops e combined-spec)

(def combined-result
(backtest-algo :duckdb combined-spec))

@(:a combined-result)
@(:b combined-result)
@(:c combined-result)
@(:d combined-result)
@(:f combined-result)
combined-result
85 changes: 85 additions & 0 deletions lib/algo/resources/quanta/notebook/algo_tickerplant.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
(ns quanta.notebook.algo-tickerplant
(:require
[modular.system]
[ta.live.tickerplant :refer [start-tickerplant current-bars]]
[ta.algo.env.protocol :as algo]))

;; 1. get algo-env form clip

(def algo-env (modular.system/system :live))

;; 2. possibly start tickerplant (it is already started via clip)

(def quote-manager (modular.system/system :quote-manager))

(def t (start-tickerplant {:algo-env algo-env
:quote-manager quote-manager}))

(def t (modular.system/system :tickerplant))

t

;; define algo

(defn nil-algo [_env _opts bar-ds]
bar-ds)

(def algo-spec
[:eurusd-d {:type :trailing-bar
:trailing-n 80
:calendar [:forex :h]
:asset "EUR/USD"
:feed :fx
:algo 'quanta.notebook.algo-tickerplant/nil-algo}
:usdjpy-m {:type :trailing-bar
:trailing-n 80
:calendar [:forex :m]
:asset "USD/JPY"
:feed :fx
:algo 'quanta.notebook.algo-tickerplant/nil-algo}
:btc-m {:type :trailing-bar
:trailing-n 100
:calendar [:crypto :m]
:asset "BTCUSDT"
:feed :crypto
:algo 'quanta.notebook.algo-tickerplant/nil-algo}
:eth-m {:type :trailing-bar
:trailing-n 100
:calendar [:crypto :m]
:asset "ETHUSDT"
:feed :crypto
:algo 'quanta.notebook.algo-tickerplant/nil-algo}

:dummy {:type :trailing-bar
:trailing-n 100
:calendar [:us :m]
:asset "WILLY-WONKER"
; note that no feed is defined, so no quotes will be subscribed,
; and no bars will be generated.
:algo 'quanta.notebook.algo-tickerplant/nil-algo}])

(algo/add-algo algo-env algo-spec)

(current-bars t [:forex :m])
(current-bars t [:crypto :m])

;; => ({:asset "BTCUSD", :epoch 1}
;; {:asset "ETHUSDT",
;; :epoch 1,
;; :open 3404.72,
;; :high 3408.91,
;; :low 3402.64,
;; :close 3407.56,
;; :volume 961.2800100000002,
;; :ticks 1473})

;; => ({:asset "BTCUSD", :epoch 1}
;; {:asset "ETHUSDT",
;; :epoch 1,
;; :open 3404.72,
;; :high 3408.91,
;; :low 3402.64,
;; :close 3407.82,
;; :volume 944.0061100000004,
;; :ticks 1454})

4 changes: 1 addition & 3 deletions lib/calendar/deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{:paths ["src"
;"classes"
]
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
com.taoensso/timbre {:mvn/version "6.2.2"} ; clj/cljs logging
jarohen/chime {:mvn/version "0.3.3"} ; scheduler
Expand Down
30 changes: 30 additions & 0 deletions lib/calendar/resources/quanta/notebook/calendar_combined.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns quanta.notebook.calendar-combined
(:require
[tick.core :as t]
[ta.calendar.combined :refer [window]]
[clojure.pprint :refer [print-table]]))

(def start-dt (t/now))
start-dt

(def windows [[:crypto :h]
[:crypto :m]])

(defn print-n [windows days]
(let [end-dt (t/>> start-dt (t/new-duration days :days))
c (window start-dt end-dt windows)]
(->> c
(print-table))))

(print-n [[:crypto :h]
[:crypto :m]]
3)

(print-n [[:eu :d]
[:us :d]]
10)

(print-n [[:test-short :d]
[:test-short :h]
[:test-short :m30]]
30)
21 changes: 21 additions & 0 deletions lib/calendar/resources/quanta/notebook/calendar_live_time.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns quanta.notebook.calendar-live-time
(:require
[taoensso.timbre :as timbre :refer [info warn error]]
[manifold.stream :as s]
[ta.live.calendar-time :as ct]))

(def s (ct/create-live-calendar-time-generator))

(ct/add-calendar s [:us :m])
(ct/add-calendar s [:crypto :m])
(ct/add-calendar s [:forex :m])
(ct/add-calendar s [:eu :m])

(s/consume
(fn [msg]
(info "time event: " msg))
(ct/get-time-stream s))

(ct/remove-calendar s [:eu :m])

(ct/show-calendars s)
16 changes: 16 additions & 0 deletions lib/calendar/resources/quanta/notebook/calendar_simple.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns quanta.notebook.calendar-simple
(:require
[ta.calendar.core :refer [calendar-seq]]
[clojure.pprint :refer [print-table]]))

(def c (calendar-seq :crypto :d))

(take 1 c)

(take 10 c)

(->> (take 1000 c)
(map println))



17 changes: 17 additions & 0 deletions lib/db/resources/quanta/notebook/asset_bybit.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns quanta.notebook.asset-bybit
(:require
[ta.import.provider.bybit.raw :refer [get-assets-spot]]))

(defn save-list [assets]
(->> (map (fn [s]
{:name s
:symbol s
:category :crypto}) assets)
(pr-str)
(spit "resources/symbollist/bybit.edn")))

(-> (get-assets-spot)
(save-list))



45 changes: 45 additions & 0 deletions lib/db/resources/quanta/notebook/asset_db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns quanta.notebook.asset-db
(:require
[ta.db.asset.db :as db]
[clojure.pprint :refer [print-table]]))

(db/search "P")
(db/search "Bitc")
(db/search "BT")
(db/search "Aura")

(db/search "BT" :crypto)
(db/search "B" :equity)
(db/search "B" :equity "SG")
(db/search "B" nil nil)
(db/search "B" "" "")

(db/instrument-details "BTCUSD")
(db/instrument-details "EUR/USD")
(db/instrument-name "BTCUSD")

(-> (db/get-instruments)
print-table)

(db/get-instruments)

(db/symbols-available :crypto)
(db/symbols-available :etf)
(db/symbols-available :equity)
(db/symbols-available :fx)
(db/symbols-available :future)

(db/instrument-details "NG0")
;; => {:symbol "NG0", :kibot "NG", :name "CONTINUOUS NATURAL GAS CONTRACT", :category :future, :exchange "SG"}

(db/instrument-details "NG1223")
;; => {:symbol "NG1223", :kibot "NGZ23", :name "CONTINUOUS NATURAL GAS CONTRACT", :category :future, :exchange "SG"}

(vals @db/db)

(db/get-instrument-by-provider :kibot "NG")
(db/get-instrument-by-provider :kibot "XXXXXX")

(db/modify {:symbol "MSFT" :super 3})

(db/instrument-details "MSFT")
Loading

0 comments on commit 7720e39

Please sign in to comment.