forked from jjttjj/trateg
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
awb99
committed
Jul 13, 2024
1 parent
68e1e6a
commit 27cf370
Showing
8 changed files
with
198 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns quanta.trade.position.exit) | ||
|
||
(defn get-exit-rule [algo-opts rule-kw] | ||
(let [{:keys [exit]} algo-opts | ||
rule (->> exit | ||
(partition 2) | ||
(filter (fn [[rule rule-opts]] | ||
(= rule rule-kw))) | ||
first)] | ||
(when rule | ||
(into [] rule)))) | ||
|
||
(comment | ||
(get-exit-rule {:exit [:profit 2.0 | ||
:loss 0.3 | ||
:time 1]} | ||
:time) | ||
;; => [:time 1] | ||
|
||
(get-exit-rule {:exit [:profit 2.0 | ||
:loss 0.3]} | ||
:time) | ||
;; => nil | ||
|
||
(get-exit-rule {:exit [:profit 2.0 | ||
:loss 0.3]} | ||
:profit) | ||
;; => [:profit 2.0] | ||
|
||
(get-exit-rule {:exit [:profit 2.0 | ||
:loss 0.3]} | ||
:loss) | ||
;; => [:loss 0.3] | ||
|
||
; | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns quanta.trade.position.exit.price | ||
(:require | ||
[tick.core :as t] | ||
[missionary.core :as m] | ||
[quanta.trade.position.exit :refer [get-exit-rule]])) | ||
|
||
|
||
|
||
|
||
|
||
(defn get-exit-profit [algo-opts position] | ||
(let [{:keys [calendar]} algo-opts | ||
[exchange-kw interval-kw] calendar | ||
bars (get-time-bars algo-opts)] | ||
(when bars | ||
(let [cal-seq (calendar-seq exchange-kw interval-kw entry-date) | ||
window (take bars cal-seq)] | ||
#_{:start (first window) | ||
:end (last window)} | ||
(last window))))) | ||
|
||
(defn profit-trigger [exit-time] | ||
(let [exit-long (-> exit-time t/instant t/long) | ||
now-long (-> t/instant t/long) | ||
diff-ms (* 1000 (- exit-long now-long)) | ||
diff-ms (max diff-ms 1)] | ||
(m/sleep diff-ms :time))) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,81 @@ | ||
(ns quanta.trade.position.working) | ||
(ns quanta.trade.position.working | ||
(:require | ||
[tick.core :as t] | ||
[missionary.core :as m] | ||
[quanta.quote.random :refer [get-quote]] | ||
[quanta.trade.roundtrip :refer [return-prct return-abs return-log]]) | ||
(:import [missionary Cancelled])) | ||
|
||
(defn value-position [position | ||
{:keys [date last] :as quote}] | ||
(println "value-position: " position " quote: " quote) | ||
(let [roundtrip (assoc position | ||
:exit-date date | ||
:exit-price last) | ||
abs-ret (return-abs roundtrip)] | ||
(if quote | ||
(assoc roundtrip | ||
:ret-abs abs-ret | ||
:ret-prct (return-prct roundtrip) | ||
:ret-log (return-log roundtrip) | ||
:win? (> abs-ret 0.0)) | ||
roundtrip))) | ||
|
||
(defn calculate-position [position] | ||
(m/ap | ||
; startup | ||
(println "start calculating position: " position) | ||
(let [asset (:asset position) | ||
quote (get-quote asset) | ||
current-quote (m/?> quote)] | ||
(value-position position current-quote)))) | ||
|
||
(comment | ||
(require '[tick.core :as t]) | ||
(m/? (m/reduce println | ||
(calculate-position {:asset "BTCUSDT" | ||
:qty 500 | ||
:entry-price 1000.0 | ||
:entry-date (t/instant)}))) | ||
|
||
(def positions [{:asset "BTCUSDT" | ||
:side :long | ||
:qty 500 | ||
:entry-price 1000.0 | ||
:entry-date (t/instant)} | ||
{:asset "BTCUSDT" | ||
:side :short | ||
:qty 500 | ||
:entry-price 1000.0 | ||
:entry-date (t/instant)} | ||
{:asset "ETHUSDT" | ||
:side :long | ||
:qty 500 | ||
:entry-price 1000.0 | ||
:entry-date (t/instant)} | ||
{:asset "ETHUSDT" | ||
:side :short | ||
:qty 500 | ||
:entry-price 1000.0 | ||
:entry-date (t/instant)} | ||
]) | ||
|
||
(require '[clojure.pprint :refer [print-table]]) | ||
(defn print-positions [& positions] | ||
(print-table [:asset :side | ||
:exit-price | ||
:ret-prct | ||
:win?] positions)) | ||
|
||
(let [flows (map calculate-position positions)] | ||
(m/? | ||
(m/reduce (constantly nil) | ||
(apply m/latest print-positions flows)))) | ||
|
||
; | ||
) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
(ns quanta.trade.roundtrip) | ||
|
||
|
||
(defn- sign-switch [side v] | ||
(case side | ||
:long v | ||
:short (- 0.0 v) | ||
v)) | ||
|
||
(defn return-abs [{:keys [exit-price entry-price side] :as _roundtrip}] | ||
(sign-switch side (- exit-price entry-price))) | ||
|
||
(defn return-prct [{:keys [entry-price] :as roundtrip}] | ||
(-> 100.0 (* (return-abs roundtrip) (/ entry-price)))) | ||
|
||
(defn return-log [{:keys [entry-price exit-price side] :as _roundtrip}] | ||
(sign-switch side (- (Math/log10 exit-price) (Math/log10 entry-price)))) | ||
|
||
(defn set-exit-price-percent [{:keys [entry-price side] :as roundtrip} percent] | ||
(let [m (+ 1.0 (/ (sign-switch side percent) 100.0))] | ||
(assoc roundtrip :exit-price (* m entry-price)))) | ||
|
||
(comment | ||
|
||
(return-abs {:entry-price 100.0 :exit-price 101.0 :side :long}) | ||
(return-prct {:entry-price 100.0 :exit-price 101.0 :side :long}) | ||
(return-log {:entry-price 100.0 :exit-price 101.0 :side :long}) | ||
|
||
(return-abs {:entry-price 100.0 :exit-price 101.0 :side :short}) | ||
(return-prct {:entry-price 100.0 :exit-price 101.0 :side :short}) | ||
(return-log {:entry-price 100.0 :exit-price 101.0 :side :short}) | ||
|
||
(set-exit-price-percent {:entry-price 100.0 :side :long} 5.0) | ||
(set-exit-price-percent {:entry-price 100.0 :side :short} 5.0) | ||
|
||
; | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters