Skip to content

Commit

Permalink
trailing stop long/short working
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Oct 10, 2024
1 parent 1d508d7 commit 8541a15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
30 changes: 16 additions & 14 deletions dev/src/dev/backtest/backtest.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@
(t/instant "2024-09-08T00:00:00Z")
(t/instant "2024-09-09T00:00:00Z")
(t/instant "2024-09-10T00:00:00Z")
(t/instant "2024-09-11T00:00:00Z")]
(t/instant "2024-09-11T00:00:00Z")
(t/instant "2024-09-12T00:00:00Z")
(t/instant "2024-09-13T00:00:00Z")
]
:open [100 100 120 120
100 100 120
100 90 100 100]
100 90 100 100 110 110]
:high [100 100 120 120
100 100 120
100 90 100 100]
100 90 100 100 110 110]
:low [100 100 120 120
100 100 120
100 90 100 100]
100 90 100 100 110 110]
:close [100 100 120 120
100 100 120
100 90 100 100]
100 90 100 100 110 110]
:atr [ 5 5 5 5
5 5 5
5 5 5 5]
5 5 5 5 5 5]
:entry [nil :long nil nil
:long nil nil
:short nil nil nil]}))
:short nil nil nil nil]}))



bar-ds
Expand Down Expand Up @@ -70,9 +74,6 @@ bar-ds
:exit [{:type :trailing-stop-offset :col :atr }
]}
bar-ds)
;; => Evaluation was interrupted


(print-table [; :id
; :asset
:side
Expand All @@ -86,7 +87,8 @@ bar-ds
; :exit-idx
]))

; | :side | :qty | :entry-price | :exit-price | :entry-date | :exit-date | :reason |
; |-------+------+--------------+-------------+----------------------+----------------------+----------------|
; | :long | 1.0 | 100 | 115 | 2024-09-02T00:00:00Z | 2024-09-05T00:00:00Z | :trailing-stop |
; | :long | 1.0 | 100 | 115 | 2024-09-05T00:00:00Z | 2024-09-08T00:00:00Z | :trailing-stop |
; | :side | :qty | :entry-price | :exit-price | :entry-date | :exit-date | :reason |
; |--------+------+--------------+-------------+----------------------+----------------------+----------------|
; | :long | 1.0 | 100 | 115 | 2024-09-02T00:00:00Z | 2024-09-05T00:00:00Z | :trailing-stop |
; | :long | 1.0 | 100 | 115 | 2024-09-05T00:00:00Z | 2024-09-08T00:00:00Z | :trailing-stop |
; | :short | 1.0 | 100 | 105 | 2024-09-08T00:00:00Z | 2024-09-12T00:00:00Z | :trailing-stop |
2 changes: 1 addition & 1 deletion src/quanta/trade/entry_signal/exit/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(let [{:keys [entry-price side]} position
close (:close row)
offset (get row col)]
(println "new-level offset: " offset)
(println "trailing offset: " offset " close: " close " level: " level "entry: " entry-price)
(if level
(case side
:long (- close offset)
Expand Down
18 changes: 11 additions & 7 deletions src/quanta/trade/entry_signal/exit/position.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@
(defrecord TrailingStopLoss [position level-a new-level-fn label]
IExit
(check-exit [_ {:keys [high low] :as row}]
(let [; first check if there is an exit at curent level
_ (println "chekcintg TrailingStopLoss...")
(let [; first check if there is an exit at current level
r (when (not (nil? @level-a))
(case (:side position)
:short
Expand All @@ -89,19 +88,24 @@
(when (<= low @level-a)
[label @level-a])))
; second calculate new level, and possibly move level
new-level (new-level-fn position @level-a row)
unchecked-level (new-level-fn position @level-a row)
_ (println "trailing unchecked-level: " unchecked-level)
new-level (case (:side position)
:short
(when (or (nil? @level-a)
(< new-level @level-a))
new-level)
(< unchecked-level @level-a))
unchecked-level)
:long
(when (or (nil? @level-a)
(> new-level @level-a))
new-level))]
(> unchecked-level @level-a))
unchecked-level))]
(when new-level
(println "TrailingStopLoss changes from " @level-a " to: " new-level)
(reset! level-a new-level))
(when (not new-level)
(println "TrailingStopLoss unchanged level: " @level-a
" side: " (:side position) " unchecked level: " unchecked-level)
)
r)))


Expand Down

0 comments on commit 8541a15

Please sign in to comment.