-
Notifications
You must be signed in to change notification settings - Fork 1
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
Oct 10, 2024
1 parent
b5e98d8
commit 8b65d8c
Showing
12 changed files
with
211 additions
and
254 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
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
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,75 @@ | ||
(ns quanta.trade.entry-signal.core | ||
(:require | ||
[quanta.trade.entry-signal.entry.core :as entry] | ||
[quanta.trade.entry-signal.exit.config :refer [exit-rule]] | ||
[quanta.trade.entry-signal.exit.position :as exit]) | ||
(:import | ||
[quanta.trade.entry_signal.exit.position MultipleRules])) | ||
|
||
(defn create-entrysignal-manager [{:keys [asset entry exit]}] | ||
{:positions (atom {}) | ||
:asset asset | ||
:entrysize-fn (entry/positionsize2 entry) | ||
:exit-rules (map exit-rule exit)}) | ||
|
||
;; entry | ||
|
||
(defn create-entry [this data] | ||
(println "rule/create-entry: " data) | ||
(entry/create-position this data)) | ||
|
||
; on position open/close | ||
|
||
(defn create-exit-manager-for-position | ||
"create a exit-fn for one position. | ||
this gets run on each bar, while the positon is open" | ||
[rules position] | ||
;(println "creating exit-rules for position: " position) | ||
;(println "exit rules: " rules) | ||
(let [position-rules (map #(% position) rules)] | ||
(MultipleRules. position-rules))) | ||
|
||
(defn on-position-open | ||
"on-position-open is an event that gets emitted by trade-commander. | ||
we need to start new exit-rules for a new position here." | ||
[{:keys [exit-rules positions]} position] | ||
(assert exit-rules "rule manager state needs to have :exit-rules") | ||
(println "rule/on-position-open: " position) | ||
(swap! positions assoc | ||
(:id position) | ||
{:position position | ||
:manager (create-exit-manager-for-position exit-rules position)})) | ||
|
||
(defn on-position-close [{:keys [positions]} position] | ||
(println "rule/on-position-close: " position) | ||
(swap! positions dissoc (:id position))) | ||
|
||
(defn check-exit-position [{:keys [position manager]} row] | ||
;(println "check-exit-position: " position) | ||
;; {:id 5, :asset EUR/USD, :side :long, | ||
;; :entry-price 1.1, :qty 100000} | ||
(when-let [exit (exit/check-exit manager row)] ; [:profit-prct 1.1110000000000002] | ||
(let [[reason exit-price] exit | ||
{:keys [id asset side entry-price entry-date qty]} position | ||
{:keys [idx date]} row] | ||
{:id id | ||
:asset asset | ||
:side side | ||
:qty qty | ||
:entry-price entry-price | ||
:entry-date entry-date | ||
; exit | ||
:reason reason | ||
:exit-idx idx | ||
:exit-price exit-price | ||
:exit-date date}))) | ||
|
||
(defn check-exit [{:keys [positions]} row] | ||
(println "rule/check-exit: " row) | ||
(->> (vals @positions) | ||
(map #(check-exit-position % row)) | ||
(remove nil?))) | ||
|
||
|
||
|
||
|
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,49 @@ | ||
(ns quanta.trade.entry-signal.exit.config | ||
(:require | ||
[quanta.trade.entry-signal.exit.position :as e]) | ||
(:import | ||
[quanta.trade.entry_signal.exit.position TakeProfit TrailingStopLoss MultipleRules])) | ||
|
||
(defmulti exit-rule | ||
(fn [{:keys [type] :as opts}] | ||
type)) | ||
|
||
(defmethod exit-rule :profit-prct [{:keys [label prct] | ||
:or {label :profit-prct}}] | ||
(assert prct "take-profit-prct needs :prct parameter") | ||
(fn [{:keys [entry-price side] :as position}] | ||
;(println "creating profit-prct rule for position: " position) | ||
(assert entry-price "take-profit-prct needs :position :entry-price") | ||
(assert side "take-profit-prct needs :position :side") | ||
(let [prct (/ prct 100.0) | ||
level (case side | ||
:long (* entry-price (+ 1.0 prct)) | ||
:short (/ entry-price (+ 1.0 prct)))] | ||
(TakeProfit. position level label)))) | ||
|
||
(defmethod exit-rule :trailing-stop-offset [{:keys [col label] | ||
:or {label :trailing-stop}}] | ||
(assert col "trailing-stop-offset needs :col parameter") | ||
(fn [position] | ||
;(assert entry-price "trailing-stop-offset needs :position :entry-price") | ||
;(assert side "trailing-stop-offset needs :position :side") | ||
(let [;_ (assert offset (str "trailing-stop-offset needs :row " col " value")) | ||
level-initial nil | ||
level-a (atom level-initial) | ||
new-level-fn (fn [position level row] | ||
(let [{:keys [entry-price side]} position | ||
offset (get row col) | ||
close (:close row) | ||
offset (get row col)] | ||
(if level | ||
(case side | ||
:long (- close offset) | ||
:short (+ close offset)) | ||
(case side | ||
:long (- entry-price offset) | ||
:short (+ entry-price offset)))))] | ||
(TrailingStopLoss. position level-a new-level-fn label)))) | ||
|
||
|
||
|
||
|
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
Oops, something went wrong.