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 14, 2024
1 parent
27cf370
commit 792fe73
Showing
9 changed files
with
102 additions
and
57 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
2 changes: 1 addition & 1 deletion
2
lib/trade/src/quanta/trade/position/size.clj → .../src/quanta/trade/position/entry/size.clj
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,4 @@ | ||
(ns quanta.trade.position.size) | ||
(ns quanta.trade.position.entry.size) | ||
|
||
|
||
(defmulti positionsize | ||
|
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,29 +1,54 @@ | ||
(ns quanta.trade.position.exit.price | ||
(:require | ||
[tick.core :as t] | ||
[missionary.core :as m] | ||
[quanta.trade.position.exit :refer [get-exit-rule]])) | ||
[missionary.core :as m] | ||
[quanta.trade.position.exit.rule :refer [get-exit-rule]] | ||
[quanta.trade.position.working :refer [working-position]])) | ||
|
||
(defn trailing-return [position] | ||
(let [working-position (working-position position)] | ||
(m/latest :ret-prct working-position))) | ||
|
||
(defn profit-trigger | ||
"returns a missionary task that emits :profit when target is met. | ||
if no profit-percent exit-rule is specified it returns nil | ||
if not task continues running." | ||
[algo-opts position] | ||
(let [[_ target] (get-exit-rule algo-opts :profit-percent)] | ||
(when target | ||
(let [prct (trailing-return position) | ||
rf (fn [_ tp] | ||
(println "trailing profit " (:asset position) ": " tp " target: " target) | ||
(when (> tp target) | ||
(reduced :profit)))] | ||
(m/reduce rf nil prct))))) | ||
|
||
(defn loss-trigger | ||
"returns a missionary task that emits :loss when target is met. | ||
if no :loss-percent exit-rule is specified it returns nil. | ||
if not task continues running." | ||
[algo-opts position] | ||
(let [[_ target] (get-exit-rule algo-opts :loss-percent)] | ||
(when target | ||
(let [prct (trailing-return position) | ||
rf (fn [_ tp] | ||
(println "trailing loss " (:asset position) ": " tp " target: " target) | ||
(when (< tp (- 0.0 target)) | ||
(reduced :loss)))] | ||
(m/reduce rf nil prct))))) | ||
|
||
(comment | ||
(def algo-opts {:asset "BTCUSDT" | ||
:exit [:profit-percent 1.0]}) | ||
|
||
(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))))) | ||
(def position | ||
{:asset "BTCUSDT" | ||
:qty 500 | ||
:entry-price 10000.0}) | ||
|
||
(m/? (profit-trigger algo-opts position)) | ||
|
||
; | ||
) | ||
|
||
(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))) | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
lib/trade/src/quanta/trade/position/exit.clj → ...e/src/quanta/trade/position/exit/rule.clj
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,46 @@ | ||
(ns quanta.trade.position.exit.signal | ||
(:require | ||
[missionary.core :as m] | ||
[quanta.trade.position.exit.time :refer [get-exit-time time-trigger]] | ||
[quanta.trade.position.exit.price :refer [profit-trigger loss-trigger]])) | ||
|
||
|
||
(defn exit-signal | ||
"returns a missionary task. | ||
task will eventually return either of :time :profit :loss" | ||
[algo-opts position] | ||
(let [exit-time (get-exit-time algo-opts (:entry-date position)) | ||
exit-tasks (->> [(profit-trigger algo-opts position) | ||
(loss-trigger algo-opts position) | ||
(when exit-time | ||
(time-trigger exit-time))] | ||
(remove nil?))] | ||
(apply m/race exit-tasks))) | ||
|
||
|
||
(comment | ||
|
||
(require '[tick.core :as t]) | ||
(def algo-opts {:calendar [:crypto :m] | ||
:exit [:loss-percent 2.0 | ||
:profit-percent 100.0 | ||
:time 2]}) | ||
(def position {:asset "BTCUSDT" | ||
:side :long | ||
:entry-date (t/instant) | ||
:entry-price 10000.0 | ||
:qty 0.1}) | ||
|
||
|
||
(m/? (exit-signal algo-opts position)) | ||
;; => :time or :profit or :loss | ||
|
||
;; this one has a position that is older and older, so | ||
;; it might be that this task returns immediately, because | ||
;; the current time is already below the time of the exit-date | ||
|
||
(m/? (exit-signal algo-opts {:entry-date (t/instant)})) | ||
;; => :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
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