Skip to content

Commit

Permalink
joseph live
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Sep 13, 2023
1 parent 37ed7ef commit 5ae58c9
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/demo/src/services-docs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
:quandl {:start (ta.data.api.quandl/set-key!
(:quandl (clip/ref :secrets)))}

:kibot {:start (ta.data.api.kibot/set-key!
(:kibot (clip/ref :secrets)))}

; goldly service returns {:ns-clj :webly ; this are used to start clj-require and webly
; :ws-watch :cljs-watch} ; this is used when stopping goldly
:goldly {:start (goldly.app.run/start-goldly
Expand Down
65 changes: 64 additions & 1 deletion lib/data/src/ta/data/api/kibot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,29 @@
; forex
; http://www.kibot.com/Files/2/Forex_tickbidask.txt


; dividends/splits:
; Request URL
; http://api.kibot.com?action=adjustments&symbol=[symbol]&startdate=[startdate]&enddate=[enddate]&splitsonly=[splitsonly]&dividendsonly=[dividendsonly]&symbolsonly=[symbolsonly]
;
; Response
;The server returns TAB separated values with the first line defining the fields and their order. Here is an example:
; Date Symbol Company Action Description
; 2/16/2010 MSFT Microsoft Corp. 0.1300 Dividend
; 5/18/2010 MSFT Microsoft Corp. 0.1300 Dividend

; ftp://hoertlehner%40gmail.com:[email protected]/



;; ApiKey Management

(defonce api-key (atom {:user "guest" :password "guest"}))

(defn set-key!
"to use api, call at least once set-key! api-key"
[key]
(info "setting kibot key..")
(warn "setting kibot key..")
(reset! api-key key)
nil ; Important not to return by chance the key, as this would be shown in the repl.
)
Expand Down Expand Up @@ -108,6 +123,46 @@



(defn snapshot [opts]
(let [{:keys [user password]} @api-key]
;(info "login user: " user "pwd: " password)
(info "kibot snapshot: " opts)
(make-request base-url
(merge
{:action "snapshot"
:user user
:password password}
opts))))

; This example will work even if you do not have a subscription:
; http://api.kibot.com/?action=snapshot&symbol=$NDX,AAPL
; return format: Symbol,Date,Time,LastPrice,LastVolume,Open,High,Low,Close,Volume,ChangePercent,TimeZone.

(comment

(snapshot {:symbol ["$NDX" "AAPL"]})

(snapshot {:type "future"
:symbol "ESZ23"})

(snapshot {:type "future"
:symbol "JYZ23"})


(snapshot {:symbol ["$NDX"
"AAPL"
"FCEL"
"MSFT"
#_"BZ0"]})

(snapshot {:symbol ["AAPL" "DAX0" "MSFT"]})

;
)




(comment

(history {:symbol "AAPL"
Expand All @@ -132,6 +187,14 @@
:timezone "UTC"
:splitadjusted 1})

(history {:symbol "SIL" ; SIL - FUTURE
:type "futures" ; Can be stocks, ETFs forex, futures.
:interval "daily"
:period 1
:timezone "UTC"
:splitadjusted 1})


(history {:symbol "SIL" ; SIL - FUTURE
:type "futures" ; Can be stocks, ETFs forex, futures.
:interval "daily"
Expand Down
72 changes: 71 additions & 1 deletion lib/data/src/ta/data/api_ds/kibot.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns ta.data.api-ds.kibot
(:require
[clojure.string :as str]
[taoensso.timbre :refer [info warn error]]
[clojure.java.io :as io]
[tick.core :as t]
Expand Down Expand Up @@ -70,11 +71,18 @@
(t/format (t/formatter "YYYY-MM-dd") dt))

(defn range->parameter [{:keys [start] :as range}]
(if (= range :full)
(cond
(= range :full)
{:period 100000}

(= range 1)
{:period 1}

:else
{:startdate (fmt-yyyymmdd start)} ; :startdate "2023-09-01"
))


(defn get-series [symbol interval range opts]
(let [symbol-map (symbol->provider symbol)
period (get interval-mapping interval)
Expand All @@ -89,8 +97,70 @@
nil)
(kibot-result->dataset result))))

(defn symbols->str [symbols]
(->> (interpose "," symbols)
(apply str)))

(defn provider->symbol [provider-symbol]
(if-let [inst (db/get-instrument-by-provider :kibot provider-symbol)]
(:symbol inst)
provider-symbol))

(comment
(symbol->provider "MES0")
;; => {:type "futures", :symbol "ES"}
(provider->symbol "ES")

(symbols->str ["MSFT" "ORCL"])
(symbols->str ["ES"]))


;

(defn symbol-conversion [col-symbol]
(map provider->symbol col-symbol)
)

(defn kibot-snapshot-result->dataset [csv]
(-> (tds/->dataset (string->stream csv)
{:file-type :csv
:header-row? true
:key-fn (comp keyword str/lower-case )
:dataset-name "kibot-snapshot"})
(tc/update-columns {:symbol symbol-conversion})
(tc/rename-columns {(keyword ":404 symbol not foundsymbol")
:symbol
})
;(tc/convert-types :date [[:local-date-time date->localdate]])
))

(defn get-snapshot [symbol]
(let [symbols-kibot (->> symbol
(map symbol->provider)
(map :symbol))
result (kibot/snapshot {:symbol (symbols->str symbols-kibot)})]
(if-let [error? (:error result)]
(do (error "kibot request error: " error?)
nil)
(kibot-snapshot-result->dataset result))))



(comment

(get-snapshot ["AAPL"])
(get-snapshot ["NG0"])
(get-snapshot ["CL0"])
(get-snapshot ["MES0"])
(get-snapshot ["RIVN" "AAPL" "MYM0"])


"RIVN" "MYM0" "RB0" "GOOGL" "FCEL"
"NKLA" "M2K0" "INTC" "MES0" "RIG"
"ZC0" "FRC" "AMZN" "HDRO" "MNQ0"
"BZ0" "WFC" "DAX0" "PLTR" "NG0"


(symbol->provider "MSFT")

(require '[ta.helper.date :refer [parse-date]])
Expand Down
3 changes: 2 additions & 1 deletion lib/joseph/resources/ext/joseph.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
:clj-services {:name "joseph"
:permission #{:dev}
:symbols [joseph.nav/calc-nav-browser
joseph.trades/load-trades]}
joseph.trades/load-trades
joseph.realtime/realtime-snapshot]}

:autoload-cljs-dir ["joseph/page"]
:cljs-routes {"joseph" {"" :joseph/trades
Expand Down
30 changes: 30 additions & 0 deletions lib/joseph/src/joseph/lib/quote_table.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns joseph.lib.quote-table
(:require
[clojure.string :as str]
[goldly.js :refer [to-fixed]]
[tick.goldly]
[tick.core :as tick]
[ui.aggrid :refer [aggrid]]
))



(defn quote-table [quotes]
[aggrid {:box :fl
:data quotes
:columns [{:field :symbol}
{:field :date}
{:field :time}
;{:field :timezone}
{:field :open}
{:field :high}
{:field :low}
{:field :close}
{:field :volume}
{:field :changepercent}
;{:field :lastprice}
;{:field :lastvolume}
]

:pagination :false
:paginationAutoPageSize false}])
60 changes: 56 additions & 4 deletions lib/joseph/src/joseph/page/live.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
[clojure.string :as str]
[goldly.page :as page]
[goldly :refer [eventhandler]]
[goldly.js :refer [to-fixed]]
[ta.viz.trades-table :refer [trades-table-live]]
[demo.goldly.lib.loader :refer [clj->p]]
[demo.goldly.lib.layout :as layout]
[joseph.lib.trade-filter :refer [filter-trades]]
[joseph.lib.quote-table :refer [quote-table]]
))

(defn select-string [{:keys [items value on-change]}]
Expand All @@ -22,23 +24,73 @@
(into #{})
(into [])))

(defn symbols [trades]
(->> trades
(map :symbol)
(into #{})
(into [])))

(defn round-number-digits
[digits number] ; digits is first parameter, so it can easily be applied (data last)
(if (nil? number) "" (to-fixed number digits)))


(defn current-pl [{:keys [qty side entry-price]} current-price]
(let [change (- current-price entry-price)
qty2 (if (= side :long)
qty
(- 0.0 qty))
pl (* qty2 change)]
(round-number-digits 0 pl)
))


(defn trades-with-pl [trades quotes]
(let [quote-dict (->> quotes
(map (juxt :symbol identity))
(into {}))]
(println "quote-dict: " quote-dict)
(map (fn [{:keys [symbol] :as trade}]
(if-let [q (get quote-dict symbol)]
(let [current-price (:close q)]
(assoc trade :current-price current-price
:current-pl (current-pl trade current-price)
))
(assoc trade :current-price ""
:current-pl "")))
trades)))


(defn live-trade-ui [trades]
(let [account-a (r/atom "")
on-change-account (fn [account _e]
(println (str "selected: " account))
(reset! account-a account))]
(reset! account-a account))
live-symbols (into [] (symbols trades))
quotes-ra (clj->p 'joseph.realtime/realtime-snapshot live-symbols)
]
(fn [trades]
(let [live-trades (filter-trades {:account @account-a
:status :live} trades)]
[layout/main-top
:status :live} trades)
quotes (or (:data @quotes-ra) [])]
(println "quotes: " quotes)
[layout/left-right-top
{:top [:div
"Live-Trades "
(str "#" (count live-trades) " ")
[select-string {:value @account-a
:items (concat [""] (accounts trades))
:on-change (eventhandler on-change-account)}]]
:main [trades-table-live live-trades]}]))))
:left [trades-table-live (trades-with-pl live-trades quotes)]
:right ; [:div "quotes" (:status @quotes-ra) " - " (pr-str @quotes-ra)]
(case (:status @quotes-ra)
:loading [:p "loading"]
:error [:p "error!"]
:data ;[:div "quotes (loaded)"]
[quote-table (:data @quotes-ra)]
[:p "unknown: status:" (pr-str @quotes-ra)])

}]))))


(defn page-joseph-live [_route]
Expand Down
Loading

0 comments on commit 5ae58c9

Please sign in to comment.