Skip to content

Commit

Permalink
time and profit prct added
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Oct 10, 2024
1 parent 784a96a commit 8d8c9f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
21 changes: 10 additions & 11 deletions dev/src/dev/algo_bollinger.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
[quanta.algo.dag.spec :refer [spec->ops]]
[quanta.algo.options :refer [apply-options]]
[quanta.trade.backtest :as b1]
[quanta.trade.backtest2 :as b2]
))
[quanta.trade.backtest2 :as b2]))

(defn entry-one [long short]
(cond
Expand All @@ -31,10 +30,9 @@
long-signal (cross-up (:close ds-bollinger) (:bollinger-upper ds-bollinger))
short-signal (cross-up (:close ds-bollinger) (:bollinger-lower ds-bollinger))
entry (dtype/clone (dtype/emap entry-one :keyword long-signal short-signal))
ds-signal (tc/add-columns ds-bollinger {:entry entry
:atr (ind/atr {:n n} ds-bars)

})]
ds-signal (tc/add-columns ds-bollinger {:entry entry
:atr (ind/atr {:n n} ds-bars)})]

ds-signal))

(defn bollinger-stats [opts ds-d ds-m]
Expand Down Expand Up @@ -64,15 +62,16 @@
:backtest {:formula [:day]
:algo b2/backtest
:entry {:type :fixed-qty :fixed-qty 1.0}
:exit [{:type :trailing-stop-offset :col :atr}]}
:exit [{:type :trailing-stop-offset :col :atr}
{:type :stop-prct :prct 2.0}
{:type :profit-prct :prct 1.0}
{:type :time :max-bars 10}]}
:backtest-old {:formula [:day]
:algo b1/backtest
:entry [:fixed-amount 100000]
:exit [:loss-percent 2.0
:profit-percent 1.0
:time 5]}

])
:profit-percent 1.0
:time 5]}])

(spec->ops bollinger-algo)
;; => [[:day {:calendar [:forex :d],
Expand Down
1 change: 1 addition & 0 deletions dev/src/dev/algo_bollinger_backtest.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@

(dag/start-log-cell bollinger :backtest)


;; see .data/ for dag logfile.

28 changes: 27 additions & 1 deletion src/quanta/trade/entry_signal/exit/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
(:require
[quanta.trade.entry-signal.exit.position :as e])
(:import
[quanta.trade.entry_signal.exit.position TakeProfit TrailingStopLoss MultipleRules]))
[quanta.trade.entry_signal.exit.position
TakeProfit
StopLoss TrailingStopLoss
MaxTime
MultipleRules]))

(defmulti exit-rule
(fn [{:keys [type] :as opts}]
Expand All @@ -21,6 +25,28 @@
:short (/ entry-price (+ 1.0 prct)))]
(TakeProfit. position level label))))

(defmethod exit-rule :stop-prct [{:keys [label prct]
:or {label :stop-prct}}]
(assert prct "stop-prct needs :prct parameter")
(fn [{:keys [entry-price side] :as position}]
;(println "creating profit-prct rule for position: " position)
(assert entry-price "stop-prct needs :position :entry-price")
(assert side "stop-prct needs :position :side")
(let [prct (/ prct 100.0)
level (case side
:long (/ entry-price (+ 1.0 prct))
:short (* entry-price (+ 1.0 prct)))]
(StopLoss. position level label))))

(defmethod exit-rule :time [{:keys [label max-bars]
:or {label :time}}]
(assert max-bars "stop-time needs :max-bars parameter")
(fn [{:keys [entry-idx] :as position}]
(assert entry-idx "stop-time needs :position :entry-idx")
(let [max-idx (+ entry-idx max-bars)]
(MaxTime. position max-idx label))))


(defmethod exit-rule :trailing-stop-offset [{:keys [col label]
:or {label :trailing-stop}}]
(assert col "trailing-stop-offset needs :col parameter")
Expand Down

0 comments on commit 8d8c9f3

Please sign in to comment.