Skip to content

Commit

Permalink
algo-charts symbol switching working
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Sep 4, 2023
1 parent ddf458c commit 9a0b448
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 162 deletions.
91 changes: 71 additions & 20 deletions app/demo/src/demo/algo/moon.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,46 @@
(:require
[tablecloth.api :as tc]
[tech.v3.datatype :as dtype]
[ta.algo.manager :refer [add-algo]]
[ta.algo.manager :refer [add-algo] :as am]
[astro.moon :refer [inst->moon-phase-kw phase->text]]
[ta.tradingview.chart.shape :as shapes]
[ta.tradingview.chart.plot :refer [plot-type]]
[ta.tradingview.chart.color :refer [color]]
))
))

(defn add-moon-indicator [ds-bars _]
(tc/add-column
ds-bars
:phase
(dtype/emap inst->moon-phase-kw :object (:date ds-bars))))
(defn moon-phase [col-date]
(dtype/emap inst->moon-phase-kw :object col-date))

(defn calc-moon-signal [phase]
(defn moon-phase->signal [phase]
(if phase
(case phase
:i1 :flat
:full :buy
:hold)
:hold))

(defn moon-signal [moon-phase]
(dtype/emap moon-phase->signal :object moon-phase))

(defn buy-signal->text [signal]
(if (= signal :buy)
1.0
nil))

(defn add-buy-signal-bool [ds-bars]
(tc/add-column
ds-bars
:signal-text
(dtype/emap buy-signal->text :bool (:signal ds-bars))))
(defn signal-text [signal]
(dtype/emap buy-signal->text :object signal))

(defn moon-signal [ds-bars options]
(let [ds-study (add-moon-indicator ds-bars options)
signal (into [] (map calc-moon-signal (:phase ds-study)))]
(-> ds-study
(tc/add-columns {:signal signal})
(add-buy-signal-bool))))
(defn moon-study [ds-bars options]
(let [col-date (:date ds-bars)
col-moon-phase (moon-phase col-date)
col-moon-signal (moon-signal col-moon-phase)
col-signal-text (signal-text col-moon-signal)]
(-> ds-bars
(tc/add-columns {:moon-phase col-moon-phase
:signal col-moon-signal
:signal-text col-signal-text
})
)))

;; SHAPES

Expand Down Expand Up @@ -71,7 +73,7 @@
(add-algo
{:name "moon"
:comment "very good - 2:1"
:algo moon-signal
:algo moon-study
:charts [;nil ; {:trade "flags"}
;{:trade "chars" #_"arrows"}
{:signal-text {:type "chars"
Expand All @@ -84,3 +86,52 @@
:options {:symbol "SPY"
:frequency "D"}})


(comment

(require '[ta.helper.date :refer [parse-date]])
(def ds
(tc/dataset [{:date (parse-date "2023-01-01")
:b 2
:c 3}
{:date (parse-date "2023-01-01")
:b 5
:c 6}]))

(def phase (moon-phase (:date ds)))
(def signal (moon-signal phase))
(def ds-demo (tc/add-columns
ds
{:phase phase
:signal signal})
)

(require '[ta.warehouse :as wh])
(require '[ta.data.settings :refer [determine-wh]])
(require '[tablecloth.api :as tc])

(def w (determine-wh "GOOGL"))
(def ds-bars (wh/load-symbol :stocks "D" "SPY"))
(tc/info ds-bars)

(require '[ta.backtest.signal :refer [trade-signal]])
(require '[ta.backtest.roundtrip-backtest :refer [calc-roundtrips]])
(trade-signal ds-demo)

(-> (trade-signal ds-demo)
(calc-roundtrips {})
)

(trade-signal ds-bars)



(am/algo-run "moon" {:symbol "SPY" :frequency "D"})
(am/algo-run "moon" {:symbol "GOOGL" :frequency "D"})



;
)


57 changes: 7 additions & 50 deletions app/demo/src/demo/goldly/page/tvalgo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,91 +16,48 @@

;; symbol/algo switcher

(defonce algo-state
(defonce menu-ctx
(r/atom {:algos []
:symbols ["TLT" "SPY" "QQQ" "EURUSD"
"RIVN" "GOOGL" "FCEL" "NKLA" "INTC" "FRC" "AMZN" "WFC" "PLTR"
"BZ0" "NG0" "RB0" "ZC0" "MES0" "M2K0" "MNQ0" "MYM0"
]}))

(run-a algo-state [:algos]
(run-a menu-ctx [:algos]
'ta.algo.manager/algo-names) ; get once the names of all available algos

(defn algo-info [algo]
(let [algo-loaded (r/atom nil)]
(when algo
(when-not (= @algo-loaded algo)
(run-a algo-state [:algoinfo] 'ta.algo.manager/algo-info algo)
(run-a menu-ctx [:algoinfo] 'ta.algo.manager/algo-info algo)
nil))))


(defn tradingview-modifier [symbol _frequency]
(let [symbol-showing (r/atom symbol)]
(fn [symbol frequency]
(when-not (= symbol @symbol-showing)
(reset! symbol-showing symbol)
(.log js/console "tv symbol change detected!")
;(set-symbol symbol frequency)
nil))))

(defn algo-modifier [_algo algoinfo]
(let [showing (r/atom algoinfo)]
(fn [algo algoinfo]
(when-let [charts (:charts algoinfo)]
(when (> (count charts) 0)
(when-not (= algoinfo @showing)
(reset! showing algoinfo)
(println "TV ALGO CHANGED TO: " algo " charts: " charts)
;(set! (.-datafeed @tv-widget-atom) (tradingview-algo-feed algo))
;(set! (-> js/window .-widget .-datafeed) (tradingview-algo-feed algo))
;(set! (.-text obj) text)
;Object.getPrototypeOf (widget) .datafeed
;(set! (.-datafeed
; (.getPrototypeOf js/Object js/widget))
;(js/setTimeout #(add-algo-studies charts) 300)
; (js/setTimeout #(track-range) 300)
;(add-algo-studies charts)
nil))))))

#_(defn tv-status []
(fn []
[:span (pr-str @state)]))

(defn algo-menu []
(let [algo-input (c/get-algo-input-atom algo-ctx)]
(fn []
[:div.flex.flex-row.bg-blue-500
[link-href "/" "main"]
[input/select {:nav? false
:items (or (:algos @algo-state) [])}
:items (or (:algos @menu-ctx) [])}
algo-input [:algo]]
[input/select {:nav? false
:items (:symbols @algo-state)}
:items (:symbols @menu-ctx)}
algo-input [:opts :symbol]]
[input/button {:on-click #(show-algo-dialog algo-ctx)} "options"]
;[input/button {:on-click #(reset-data)} "R!"]
[input/button {:on-click #(show-table-dialog algo-ctx)} "table"]
;[input/button {:on-click get-window-demo} "get window"]
;[tv-status]
])))




(defn algo-ui []
(fn []
(let [{:keys [_algos algo algoinfo]} @algo-state]
(let [{:keys [_algos algo algoinfo]} @menu-ctx]
[:div.flex.flex-col.h-full.w-full
[algo-menu]
[algo-info algo]
;[tradingview-modifier symbol frequency]
;[algo-modifier algo algoinfo]
[:div.h-full.w-full
[tradingview-algo algo-ctx]
]

;[page-renderer data page]
])))
[tradingview-algo algo-ctx]]])))

(defn tvalgo-page [_route]
[:div.h-screen.w-screen.bg-red-500
Expand Down
2 changes: 1 addition & 1 deletion app/resources/symbollist/equity-style.edn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

;Large-Cap All
;{:cap :large :style :all :name "Diamond Trust Series 1" :symbol "DIA"}
;{:cap :large :style :all :name "NASDAQ 100 Index Tracking Stock" :symbol "QQQ"}
{:cap :large :style :all :name "NASDAQ 100 Index Tracking Stock" :symbol "QQQ"}
;{:cap :large :style :all :name "IShares S&P 100" :symbol "OEF"}
{:cap :large :style :all :name "IShares S&P 500" :symbol "IVV" :category :etf}
;{:cap :large :style :all :name "IShares NYSE 100" :symbol "NY"}
Expand Down
4 changes: 2 additions & 2 deletions app/resources/symbollist/test.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[{:symbol "QQQ" :name "nasdaq" :category :etf}
{:symbol "MSFT" :name "Microsoft" :category :equity}
[{:symbol "MSFT" :name "Microsoft" :category :equity}
{:symbol "XOM" :name "Exxon Mobile" :category :equity}
{:symbol "AMZN" :name "Amazon" :category :equity}
{:symbol "GOOG" :name "Google" :category :equity}
{:symbol "GOOGL" :name "Google" :category :equity}
{:symbol "TSLA" :name "Tesla" :category :equity}
{:symbol "SPY" :name "S&P 500" :category :etf}
{:symbol "DEMO" :name2 "intentionally missing name (name2)" :category :wild}
Expand Down
5 changes: 3 additions & 2 deletions lib/astro/src/astro/marks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@
(defn moon-aspect? [{:keys [a b] :as aspect}]
(or (= a :Moon) (= b :Moon)))

(defn astro-marks [symbol resolution options from to]
(let [aspects (load-aspects symbol resolution options from to)
(defn astro-marks [options from to]
(let [{:keys [symbol frequency]} options
aspects (load-aspects symbol frequency options from to)
aspects (if (:show-moon options)
aspects
(remove moon-aspect? aspects))]
Expand Down
8 changes: 6 additions & 2 deletions lib/helper/src/ta/helper/ago.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
(xf result)))))

(defn xf-future [xf]
(let [first (atom true)]
(let [first (atom true)
last (atom nil)]
(fn
;; SET-UP
([]
(reset! first true)
(xf))
;; PROCESS
([result input]
(when-not (nil? input)
(reset! last input))
(if @first
(do (reset! first false)
result) ; unchanged collection for first element
Expand All @@ -52,7 +55,7 @@
;; TEAR-DOWN
([result]
(when-not @first
(xf result nil))
(xf result @last))
(xf result)))))


Expand All @@ -68,5 +71,6 @@
(into [] xf-ago-pair [3 4 5 6 7])

(into [] xf-future [3 4 5 6 7])
(into [] xf-future [4 5 6])
;
)
13 changes: 9 additions & 4 deletions lib/tradingview/src/ta/tradingview/goldly/algo/interaction.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,16 @@
(when (or symbol-changed algo-changed)
(println "resetting algo-ctx data..")
(c/set-algo-data algo-ctx nil)
(i/reset-data tv)
(c/set-cache-needed)
;(i/reset-data tv)
;(c/set-cache-needed)
(println "switching tv symbol..")
(i/set-symbol tv symbol frequency (fn [modus opts]
(on-load-finished algo-ctx tv modus opts)
(i/set-symbol tv symbol "1D" #_frequency (fn [modus opts]
(println "ON-SET-SYMBOL FINISHED: modus: " modus " opts: " opts)
(let [modus nil
opts nil]
(on-load-finished algo-ctx tv modus opts)
)

)))

nil))
Expand Down
Loading

0 comments on commit 9a0b448

Please sign in to comment.