Skip to content

Commit

Permalink
nav ui/calculation improved
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Sep 11, 2023
1 parent d6201f4 commit 9c0560e
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 89 deletions.
11 changes: 11 additions & 0 deletions app/demo/src/demo/goldly/lib/layout.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns demo.goldly.lib.layout
(:require
[spaces]))


(defn left-right-top [{:keys [top left right]}]
[spaces/viewport
[spaces/top-resizeable {:size 50} top]
[spaces/fill
[spaces/left-resizeable {:size "50%" :scrollable false} left]
[spaces/fill {:scrollable false} right]]])
10 changes: 9 additions & 1 deletion app/demo/src/demo/goldly/page/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@
[link-dispatch [:bidi/goto :joseph/nav] "joseph-nav"]
]

; test
[:div.bg-blue-300.m-5
[:p.text-blue.text-xl "test"]
[link-dispatch [:bidi/goto :test/test] "test-page"]
[link-dispatch [:bidi/goto :test/layout1] "layout-1"]
[link-dispatch [:bidi/goto :test/layout2] "layout-2"]

]
; goldly developer tools
[:div.bg-blue-300.m-5
[link-dispatch [:bidi/goto :user/test] "test-page"]

[:p.text-blue.text-xl "goldly developer tools"]
[link-dispatch [:bidi/goto :viewer :query-params {}] "notebook viewer"]
[link-dispatch [:bidi/goto :scratchpad] "scratchpad"]
Expand Down
19 changes: 19 additions & 0 deletions app/demo/src/demo/goldly/page/test/layout.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns demo.goldly.page.test.layout
(:require
[goldly.page :as page]
[site :refer [ipsum]]
[demo.goldly.lib.layout :as layout]))

(defn page-layout-1 [_route]
(layout/left-right-top {:top "top"
:left [:div.bg-blue-500.w-full.h-full "left"]
:right [:div.bg-green-500.w-full.h-full "right"]}))

(defn page-layout-2 [_route]
(layout/left-right-top {:top "menu"
:left [:div.bg-blue-500.w-full.h-full.overflow-scroll (ipsum 5)]
:right [:div.bg-green-500.w-full.h-full (ipsum 100)]}))


(page/add page-layout-1 :test/layout1)
(page/add page-layout-2 :test/layout2)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns demo.goldly.page.test
(ns demo.goldly.page.test.test
(:require
[reagent.core :as r]
[goldly.page :as page]
Expand Down Expand Up @@ -38,4 +38,4 @@



(page/add test-page :user/test)
(page/add test-page :test/test)
8 changes: 6 additions & 2 deletions app/demo/src/ta-config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
:goldly {:lazy true
:src-dir "src"
; runtime
:autoload-cljs-dir ["demo/goldly/page"]
:autoload-cljs-dir ["demo/goldly/page"
"demo/goldly/page/test"]
:routes {:app {"" :user/main
"test" :user/test}
"test/" {"test" :test/test
"layout1" :test/layout1
"layout2" :test/layout2
}}
:api {"arrow" :dataset/arrow}}}

:reval {:rdocument {:storage-root "../../output/rdocument/"
Expand Down
21 changes: 17 additions & 4 deletions lib/joseph/src/joseph/nav.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@

(defn calc-nav [symbol]
(let [trades (load-trades-valid)
symbols (->> trades (map :symbol) (into #{}) (into []))
accounts (->> trades (map :account) (into #{}) (into []))
trades (if (or (nil? symbol) (str/blank? symbol))
trades
(filter #(= symbol (:symbol %)) trades))
ds-nav (portfolio trades)]
{:nav ds-nav
:trades trades}))
:trades trades
:symbols symbols
:accounts accounts
}))


(defn calc-nav-browser [symbol]
(let [{:keys [nav trades]} (calc-nav symbol)]
{:nav (ds->map nav)
:trades trades}))
(let [{:keys [nav] :as data} (calc-nav symbol)]
(merge data
{:nav (ds->map nav)})))


(comment
(-> (calc-nav nil)
:symbols)

(-> (calc-nav nil)
:nav)

Expand All @@ -33,6 +41,11 @@
(-> (calc-nav-browser nil)
:nav)

(-> (calc-nav-browser nil)
:symbols)

(-> (calc-nav-browser nil)
:accounts)
;
)

Expand Down
64 changes: 50 additions & 14 deletions lib/joseph/src/joseph/page/nav.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(ns joseph.page.nav
(:require
[tick.core :as t]
[re-frame.core :as rf]
[goldly.page :as page]
[goldly :refer [eventhandler]]
[container :refer [tab]]
[ta.viz.nav-vega :refer [nav-vega]]
[ta.viz.nav-table :refer [nav-table]]
[ta.viz.trades-table :refer [trades-table]]
[demo.goldly.lib.loader :refer [clj->p]]))
[demo.goldly.lib.loader :refer [clj->p]]
[demo.goldly.lib.layout :as layout]
))

(defn hack-date [row]
; inst converts a tick data to a javascript/date
Expand All @@ -16,24 +20,56 @@
(->> nav
(map hack-date)))

(defn layout-test [{:keys [top left right]}]
[:div.w-screen.h-screen.flex.flex-cols
;vega nav chart
[:div.w-full.h-full ;.overflow-scroll
left
]
;aggrid table (nav or trades)
[:div.w-full.h-full.overflow-scroll
]])


(defn select-string [{:keys [items value on-change]}]
(into [:select {:value value
:on-change on-change
}]
(map (fn [s] [:option {:value s} s]) items)))

(defn page-joseph-nav [{:keys [query-params] :as route}]
(let [symbol (:symbol query-params)
nav (clj->p 'joseph.nav/calc-nav-browser symbol)]
(let [symbol (or (:symbol query-params)
"")
account (or (:account query-params)
"")
nav (clj->p 'joseph.nav/calc-nav-browser symbol)
on-change-symbol (fn [symbol _e]
(println (str "selected: " symbol))
(rf/dispatch [:bidi/goto :joseph/nav :query-params {:symbol symbol :account account}]))
on-change-account (fn [account _e]
(println (str "selected: " account))
(rf/dispatch [:bidi/goto :joseph/nav :query-params {:symbol symbol :account account}]))
]
(fn [& args]
(case (:status @nav)
:loading [:p "loading"]
:error [:p "error!"]
:data [:div.w-screen.h-screen.flex.flex-cols
;vega nav chart
[:div.w-full.h-full.overflow-scroll
[nav-vega (vega-data-hacks (:nav (:data @nav)))]]
;aggrid table (nav or trades)
[:div.w-full.h-full.overflow-scroll
[tab {:box :fl}
"nav"
[nav-table (:nav (:data @nav))]
"trades"
[trades-table (:trades (:data @nav))]]]]
:data [layout/left-right-top
{:top [:div
[select-string {:value symbol
:items (concat [""] (:symbols (:data @nav)))
:on-change (eventhandler on-change-symbol)}]
[select-string {:value account
:items (concat [""] (:accounts (:data @nav)))
:on-change (eventhandler on-change-account)}]
]
:left [:div.w-full.h-full.overflow-y-scroll
[nav-vega (vega-data-hacks (:nav (:data @nav)))]]
:right [tab {:box :fl}
"nav"
[nav-table (:nav (:data @nav))]
"trades"
[trades-table (:trades (:data @nav))]]}]
[:p "unknown: status:" (pr-str @nav)]))))


Expand Down
7 changes: 6 additions & 1 deletion lib/joseph/src/joseph/trades.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@
:exit-price 420.10}])


(defn entry-vol [{:keys [entry-price qty direction]}]
(* entry-price qty))

(defn load-trades []
(->> (slurp "../resources/trades-upload.edn")
(edn/read-string)
;(filter #(= :equity (:category %)))
(map #(update % :entry-date parse-date))
(map #(update % :exit-date parse-date))))
(map #(update % :exit-date parse-date))
(map #(assoc % :entry-vol (entry-vol %)))
))

(def cutoff-date (parse-date "2022-01-01"))

Expand Down
67 changes: 34 additions & 33 deletions lib/trateg/src/ta/multi/nav_trades.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
#(map inc (% :position/trades-open))})
))

(defn idxs-last [idxs]
(let [v (last idxs)]
[v]))

(defn set-col-win [ds win col val]
;(println "set-col val: " val)
Expand Down Expand Up @@ -91,28 +88,37 @@
))

(defn trade-effect [ds-bars {:keys [qty direction
entry-price entry-date
exit-price exit-date] :as trade}]
entry-date exit-date]
:as trade}]
(assert entry-date "trade does not have entry-dt")
(assert exit-date "trade does not have entry-dt")
(assert direction "trade does not have side")
(assert qty "trade does not have qty")
;(info "calculating trade-effect " trade)
(let [full# (tc/row-count ds-bars)
ds-eff (no-effect full#)
idxs-exit (argops/argfilter #(t/= exit-date %) (:date ds-bars))
idxs-win (if (t/= entry-date exit-date)
(argops/argfilter #(t/= entry-date %) (:date ds-bars))
(argops/argfilter #(t/<= entry-date % exit-date) (:date ds-bars)))
idxs-exit (idxs-last idxs-win)
ds-w (tc/select-rows ds-bars idxs-win)
price-w (:close ds-w)
w# (tc/row-count ds-w)]
(if (= w# 0)
(do (warn "cannot calculate effect for trade: " trade)
ds-eff)
(do (trade-unrealized-effect ds-eff idxs-win price-w w# trade)
(trade-realized-effect ds-eff idxs-exit trade)
ds-eff))))
nil
(argops/argfilter #(and (t/<= entry-date %)
(t/< % exit-date))
(:date ds-bars)))]
; unrealized effect
(when idxs-win
(let [ds-w (tc/select-rows ds-bars idxs-win)
price-w (:close ds-w)
w# (tc/row-count ds-w)]
(if (= w# 0)
(warn "cannot calculate unrealized effect for trade: " trade)
(trade-unrealized-effect ds-eff idxs-win price-w w# trade))))
; realized effect
(let [ds-x (tc/select-rows ds-bars idxs-exit)
x# (tc/row-count ds-x)]
(if (= x# 0)
(warn "cannot calculate REALIZED effect for trade: " trade)
(trade-realized-effect ds-eff idxs-exit trade)))
; return
ds-eff))

(defn effects+ [a b]
(tc/dataset
Expand Down Expand Up @@ -153,14 +159,14 @@
ds (reduce effects+
(no-effect (tc/row-count (:calendar calendar)))
(map calc-symbol symbols))

]
(tc/add-columns ds {:date (-> calendar :calendar :date)
:pl-r-cum (dfn/cumsum (:pl-r ds))
})
))


; result
pl-r-cum (dfn/cumsum (:pl-r ds))]

(tc/add-columns
ds
{:date (-> calendar :calendar :date)
:pl-r-cum pl-r-cum
:pl-cum (dfn/+ (:pl-u ds) pl-r-cum)})))


(comment
Expand Down Expand Up @@ -190,14 +196,9 @@

(tc/select-rows ds1 [0 5])

(require '[joseph.trades :refer [load-trades]])
(def cutoff-date (parse-date "2022-01-01"))
(defn invalid-date [{:keys [entry-date exit-date]}]
(or (t/<= entry-date cutoff-date)
(t/<= exit-date cutoff-date)))
(def trades
(->> (load-trades)
(remove invalid-date)))
(require '[joseph.trades :refer [load-trades-valid]])

(def trades (load-trades-valid))
(count trades)

(def trades-googl
Expand Down
19 changes: 10 additions & 9 deletions lib/trateg/src/ta/viz/nav_table.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
(if (nil? number) "" (to-fixed number digits)))

(defn nav-table [nav]
[aggrid {:data nav
[aggrid {:box :fl
:data nav
:columns [{:field :date :format fmt-yyyymmdd}
{:field :open# }
{:field :long$ :format #(round-number-digits 0 %)}
{:field :short$ :format #(round-number-digits 0 %)}
{:field :net$ :format #(round-number-digits 0 %)}
{:field :pl-u :format #(round-number-digits 0 %)}
{:field :pl-r :format #(round-number-digits 0 %)}
{:field :pl-r-cum :format #(round-number-digits 0 %)}]
:box :fl
{:field :open# :type "rightAligned"}
{:field :long$ :format #(round-number-digits 0 %) :type "rightAligned"}
{:field :short$ :format #(round-number-digits 0 %) :type "rightAligned"}
{:field :net$ :format #(round-number-digits 0 %) :type "rightAligned"}
{:field :pl-u :format #(round-number-digits 0 %) :type "rightAligned"}
{:field :pl-r :format #(round-number-digits 0 %) :type "rightAligned"}
{:field :pl-r-cum :format #(round-number-digits 0 %) :type "rightAligned"}]

:pagination :false
:paginationAutoPageSize false}])
Loading

0 comments on commit 9c0560e

Please sign in to comment.