Skip to content

Commit

Permalink
algo-widget symbol change working
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Aug 29, 2023
1 parent b15110c commit 3ed9a97
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 34 deletions.
22 changes: 9 additions & 13 deletions app/demo/src/demo/goldly/page/tvalgo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
[goldly.service.core :refer [run-a]]
[goldly.page :as page]
[input]
[ta.tradingview.goldly.tradingview :as tv :refer [tradingview-chart]]
[ta.tradingview.goldly.algo.context :as c]
[ta.tradingview.goldly.feed.algo2 :refer [get-tradingview-options-algo-feed]]
;[ta.tradingview.goldly.interact :refer [set-symbol state track-range reset-data]]
[demo.goldly.lib.ui :refer [link-href]]
[demo.goldly.algo.dialog :refer [show-algo-dialog show-table-dialog]]))
[demo.goldly.algo.dialog :refer [show-algo-dialog show-table-dialog]]
[ta.tradingview.goldly.algo.tradingview :refer [tradingview-algo]]
))

(defonce algo-ctx
(c/create-algo-context "moon" {:symbol "QQQ" :frequency "D"}))
Expand Down Expand Up @@ -65,29 +65,25 @@
[: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) [])}
algo-state [:algo]]
algo-input [:algo]]
[input/select {:nav? false
:items (:symbols @algo-state)}
algo-state [:symbol]]
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 tradingview-algo [algo-ctx]
(let [{:keys [algo opts]} (c/get-algo-input algo-ctx)
symbol (:symbol opts)]
(println "showing tradingview-widget algo-mode algo: " algo " symbol: " symbol)
[tradingview-chart {:feed (get-tradingview-options-algo-feed algo-ctx)
:options {:autosize true
:symbol symbol}}]))

(defn algo-ui []
(fn []
Expand Down
13 changes: 9 additions & 4 deletions lib/tradingview/src/ta/tradingview/goldly/algo/context.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
(ns ta.tradingview.goldly.algo.context)
(ns ta.tradingview.goldly.algo.context
(:require
[reagent.core :as r]))

(defn create-algo-context [algo opts]
(let [data (atom nil)
input (atom {:algo algo
:opts opts})
(let [data (r/atom nil)
input (r/atom {:algo algo
:opts opts})
context {:input input
:data data}]
context))
Expand All @@ -14,6 +16,9 @@
(defn get-algo-name [algo-ctx]
(-> algo-ctx :input deref :algo))

(defn get-algo-input-atom [algo-ctx]
(-> algo-ctx :input))

(defn get-data [algo-ctx]
(-> algo-ctx :data deref))

Expand Down
36 changes: 36 additions & 0 deletions lib/tradingview/src/ta/tradingview/goldly/algo/interaction.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns ta.tradingview.goldly.algo.interaction
(:require
[ta.tradingview.goldly.algo.context :as c]
[ta.tradingview.goldly.interact2 :as i]
))

(defn symbol-changed? [old-value new-value]
(let [old (get-in old-value [:opts :symbol])
new (get-in new-value [:opts :symbol])]
(println "symbol old: " old " new: " new)
(if (= old new)
false
true)))

(defn on-load-finished [& args]
(println "on-load-finished: " args)
)

(defn on-symbol-change [algo-ctx tv s]
(println "symbol changed to: " s)
(let [{:keys [algo opts]} (c/get-algo-input algo-ctx)
{:keys [symbol frequency]} opts]
(c/set-algo-data algo-ctx nil)
(i/set-symbol tv s frequency on-load-finished)
nil))

(defn track-interactions [algo-ctx tv]
(let [input (c/get-algo-input-atom algo-ctx)]
(println "add-watch to algo-ctx input ..")
(add-watch input :algo-input
(fn [key state old-value new-value]
(println "algo-ctx input changed to:" new-value)
(when (symbol-changed? old-value new-value)
(on-symbol-change algo-ctx tv (get-in new-value [:opts :symbol])))

))))
69 changes: 69 additions & 0 deletions lib/tradingview/src/ta/tradingview/goldly/algo/tradingview.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(ns ta.tradingview.goldly.algo.tradingview
(:require
[reagent.core :as r]
[js-loader :refer [with-js browser-defined?]]
[ta.tradingview.goldly.tradingview :as tv :refer [tradingview-chart show-tradingview-widget shutdown-tradingview!]]
[ta.tradingview.goldly.feed.algo2 :refer [get-tradingview-options-algo-feed]]
[ta.tradingview.goldly.algo.context :as c]
[ta.tradingview.goldly.algo.interaction :as i]
))


#_(defn tradingview-algo-widget [algo-ctx]
(let [{:keys [algo opts]} (c/get-algo-input algo-ctx)
symbol (:symbol opts)]
(println "showing tradingview-widget algo-mode algo: " algo " symbol: " symbol)
[tradingview-chart {:feed (get-tradingview-options-algo-feed algo-ctx)
:options {:autosize true
:symbol symbol}}]
))


#_(defn tradingview-algo [algo-ctx]
;(i/track-interactions algo-ctx) ; interactions tracking does not work.
(tradingview-algo-widget algo-ctx)
)


(defn tradingview-widget-algo [algo-ctx]
(let [id "tv-widget-1" ;(uuid/uuid-string (uuid/make-random-uuid))
tv (r/atom nil)]
(r/create-class
{:display-name "tradingview"
:reagent-render (fn [_]
[:div {:id id :style {:width "100%" :height "100%"}}])
:component-did-mount (fn [_]
(println "TradingViewChart.ComponentDidMount")
(let [{:keys [algo opts]} (c/get-algo-input algo-ctx)
symbol (:symbol opts)
widget (show-tradingview-widget
id
{:feed (get-tradingview-options-algo-feed algo-ctx)
:options {:autosize true
:symbol symbol}})]
(reset! tv widget)
(i/track-interactions algo-ctx @tv)
(.onChartReady @tv #(println "TradingView ChartWidget has loaded!"))))
:component-will-unmount (fn [this]
(println "TradingViewChart.ComponentDid-UN-Mount")
(shutdown-tradingview! @tv))
;:component-will-receive-props (fn [this new-argv]
; (println "receive props: " new-argv))
:component-did-update (fn [this [_ prev-props prev-more]]
(println "tradingview-algo-widget did update.")
;(let [[_ new-config] (r/argv this)]
; (println "TradingViewChart.ComponentDidUpdate " new-config)
;(if (not (=
;(reset! tv (change-feed-config id new-config @tv))
; )
)})))


(defn tradingview-algo [algo-ctx]
[with-js
{(browser-defined? "TradingView") "/r/tradingview/charting_library_21/charting_library.js" ; "/r/tradingview/charting_library.min.js" ; js/TradingView
(browser-defined? "Datafeeds") "/r/tradingview/UDF_21/bundle.js" ; "/r/tradingview/UDF/bundle.js"
(browser-defined? "MyStudy") "/r/tradingview/study.js"} ; js/Datafeeds
;[:h1 "tv loaded!"]
[tradingview-widget-algo algo-ctx]])

20 changes: 10 additions & 10 deletions lib/tradingview/src/ta/tradingview/goldly/feed/algo2.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,19 @@
(get-bars algo-ctx period-clj onHistoryCallback onErrorCallback)
nil))
:subscribeBars (fn [symbolInfo resolution _onRealtimeCallback subscribeUID _onResetCacheNeededCallback]
;(println "subscribe: " symbolInfo resolution subscribeUID)
(println "subscribe-bars: " symbolInfo resolution subscribeUID)
)
:unsubscribeBars (fn [subscriberUID]
;(println "unsubscribe: " subscriberUID)
(println "unsubscribe-bars: " subscriberUID)
)
:getServerTime (fn [onTimeCallback]
(let [cb (fn [[_ data]] ; _ = event-type
(let [{:keys [result _error]} data
result-js (clj->js result)]
;(println "TV TIME: " result)
(onTimeCallback result-js)))]
(run-cb {:fun 'ta.tradingview.handler-datasource/time-handler
:args []
:cb cb})))
(let [rp (clj 'ta.tradingview.handler-datasource/server-time)]
(p/then rp (fn [time]
(println "TV TIME: " time)
(onTimeCallback (clj->js time))))
(p/catch rp (fn [err]
(println "ERROR GETTING SERVER TIME: " err)))))

:calculateHistoryDepth (fn [resolution resolutionBack intervalBack]
(println "calculate history depth:" resolution resolutionBack intervalBack))

Expand All @@ -187,6 +186,7 @@
#_(run-cb {:fun 'ta.algo.manager/algo-marks
:args [algo symbol frequency options epoch-start epoch-end]
:cb cb})
(onDataCallback (clj->js []))
))
:getTimeScaleMarks (fn [symbolInfo startDate endDate _onDataCallback resolution]
;(println "get-timescale-marks" symbolInfo startDate endDate resolution)
Expand Down
14 changes: 9 additions & 5 deletions lib/tradingview/src/ta/tradingview/goldly/interact.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
(.activeChart @tv-widget-atom))

(defn on-data-loaded [& _args]
;(println "tv data has been loaded (called after set-symbol)")
(println "tv data has been loaded (called after set-symbol)")
)

(defn set-symbol [symbol interval]
;(println "tv set symbol:" symbol "interval: " interval)
(.setSymbol @tv-widget-atom symbol interval on-data-loaded)
nil)
(defn set-symbol
([symbol interval]
;(println "tv set symbol:" symbol "interval: " interval)
(set-symbol symbol interval on-data-loaded))
([symbol interval on-load-finished]
;(println "tv set symbol:" symbol "interval: " interval)
(.setSymbol @tv-widget-atom symbol interval on-load-finished)
nil))

(defn study-list []
(-> (.getStudiesList @tv-widget-atom)
Expand Down
17 changes: 17 additions & 0 deletions lib/tradingview/src/ta/tradingview/goldly/interact2.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns ta.tradingview.goldly.interact2
(:require
[reagent.core :as r]))



(defn on-data-loaded [& _args]
(println "tv data has been loaded (called after set-symbol)"))

(defn set-symbol
([tv symbol interval]
;(println "tv set symbol:" symbol "interval: " interval)
(set-symbol tv symbol interval on-data-loaded))
([tv symbol interval on-load-finished]
;(println "tv set symbol:" symbol "interval: " interval)
(.setSymbol tv symbol interval on-load-finished)
nil))
6 changes: 6 additions & 0 deletions lib/tradingview/src/ta/tradingview/goldly/tradingview.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@
(set! (.-widget js/window) tv-widget)
tv-widget))

(defn shutdown-tradingview! [tv]
(println "shutting down tradingview ..")
(if (nil? tv)
(println "tv is nil. Not calling shutdown.")
(.remove tv)))

(defn tradingview-chart [options]
[with-js
{(browser-defined? "TradingView") "/r/tradingview/charting_library_21/charting_library.js" ; "/r/tradingview/charting_library.min.js" ; js/TradingView
Expand Down
6 changes: 4 additions & 2 deletions lib/tradingview/src/ta/tradingview/handler_datasource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(defn server-time []
(-> (now-datetime) datetime->epoch-second))

(defn time-handler [_]
(defn time-handler []
(info "tv/time")
(let [now-epoch (server-time)]
(res/response (str now-epoch))))
Expand Down Expand Up @@ -84,7 +84,7 @@
:volume_precision 0 ;Integer showing typical volume value decimal places for a particular symbol. 0 means volume is always an integer. 1 means that there might be 1 numeric character after the comma.
:pointvalue 1
; session
:data_status "streaming" ;
:data_status "endofday" ; streaming endofday pulsed delayed_streaming delayed delayed_streaming
:has_intraday true
:timezone "Etc/UTC" ; "America/New_York"
:session "0900-1600" ;"0900-1630|0900-1400:2",
Expand All @@ -93,6 +93,8 @@
; :expired true ; whether this symbol is an expired futures contract or not.
; :expiration_date (to-epoch-no-ms- (-> 1 t/hours t/ago))
}))


(comment
; stocks should have :exchange SG :type Stocks
; crypto should have :exchange BB :type Crypto
Expand Down

0 comments on commit 3ed9a97

Please sign in to comment.