Skip to content

Instantly share code, notes, and snippets.

@joelreymont
Created December 31, 2009 18:57
Show Gist options
  • Save joelreymont/266856 to your computer and use it in GitHub Desktop.
Save joelreymont/266856 to your computer and use it in GitHub Desktop.
(define-strategy ((ema-length 20) ; strategy inputs
(patience-threshold 5) ; can have default values
(points-target 1.5)
(points-risked 2))
;; declare variables
(let ((x-up 0) (x-down 0)
(factor1 0) (factor2 0)
(factor3 0) (factor4 0))
;; set things up
(set factor1 (* (- (high) (low)) 2.5)
factor2 (/ (+ close (high) (low)) 3)
factor3 (x-average factor1 ema-length)
factor4 (x-average factor2 ema-length)
x-up (+ factor4 factor3)
x-down (- factor4 factor3))
;; entry or exit according to market position
(cond ((= 'neutral (market-position))
;; enter since we are neither long nor short
(let ((n1 0) (n2 0) (frac 0)
(whole 0) (ema 0) (trigger 0))
;; low, high, etc. are properties
;; of the current bar which is
;; built automatically
(set n1 (lowest-bar (low) 15)
n2 (highest-bar (high) 15)
ema (x-average (close) ema-length)
frac (frac-portion ema)
whole (int-portion ema))
;; conditional statement returns a value
(set trigger (cond ((<= frac 0.125)
whole)
((and (> frac 0.125) (<= frac 0.375))
(+ whole 0.25))
((and (> frac 0.375) (<= frac 0.625))
(+ whole 0.50))
((and (> frac 0.625) (<= frac 0.875))
(+ whole 0.75))
((> frac 0.875)
(+ whole 1.00))))
;; (back n1 (low)) is the value of (low) n1 bars ago.
;; series are built automatically whenever the use
;; of back is detected.
(when (and (< (back n1 (low)) (back n1 x-down))
(< (int-portion (/ time 100)) 15)
(< (rate-of-change ema 2) 0))
;; entries can be tagged
(short-at-limit trigger :tag "SE"))
(when (and (> (back n2 (high)) (back n2 x-up))
(< (int-portion (/ time 100)) 15)
(> (rate-of-change ema 2) 0))
(buy-at-limit trigger :tag "LE"))
))
((= 'long (market-position))
;; exit rules
(cond ((>= (bars-since-entry) patience-threshold)
(sell-at-market :tag "LX Dead"))
((>= (bars-since-entry) 1)
(sell-at-limit (+ entry-price points-target)
:tag "LX Trgt")
(sell-at-stop (- entry-price points-risked)
:tag "LX Stop"))))
((= 'short (market-position))
;; we are short
(cond ((>= (bars-since-entry) patience-threshold)
(cover-at-market :tag "SX Dead"))
((>= (bars-since-entry) 1)
(sell-at-limit (- entry-price points-target)
:tag "SX Trgt")
(sell-at-stop (+ entry-price points-risked)
:tag "SX Stop"))))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment