What programming language is best suited for implementing DeMark?

Jason Perl's book DeMark Indicators details rules for calculating signals developed by Thomas DeMark. These rules are not complex in themselves, but there is no dirth of ifelse structure to incrementing a signal's progression.

An example of a rule from Perl's book:

"A Bearish TD Price Flip occurs when the market records a close greater than the close four bars earlier, immediately followed by a close less than the close four bars earlier."

It's possible to implement these rules in something as clumsy as Excel, but what programming language would implement it most gracefully? The control structure suggests a C/C++ approach, but I'm wondering if Haskell, R, Python or even Prolog might be better suited.

UPDATE: here is a sample of what an R implementation might look like:

S\$deMark <- ifelse(Lag(Cl(S) < Lag(Cl(S), k=5)) & Cl(S) < Lag(Cl(S), k=4), 1,0)


Where S is an xts object.

Any kind of language would work. I suppose that if you really want it to be 'prettier than most', go with a language such as OCaml or F#. Pattern-matching will let you make code more readable than having lots of if statements.

In the example you give, instead of treating S as monolithic, I'm going to treat it as a List of values. That being the case, your problem is solved with something like

let rec deMark S =
match S with
| a, b, _, _, now when now < Cl(a) && now < Cl(b) -> 1
| h :: t -> deMark t
| _ -> 0


Something like that. The moral of the story is that instead of backtracking the list, which is what I assume Lag is doing (I don't know R), you pattern-match the list 5 elements at a time.

• Readability is an important aspect of implementation, because it makes it easier to see if you have it right or not. Demark can get quite complex and nested if/else control flows can become Gordian. Thanks for the concept of 'pattern matching'. Can you show a code snippet of OCaml or F# similar to my R snippet? Aug 22, 2011 at 17:44
• @Milktrader I have updated the answer with an example. Aug 23, 2011 at 8:32
• I'll add that one nice thing about pattern matching in a language like ML is that the compiler can warn the programmer if the cases are non-exhaustive. That will hopefully reduce errors. Aug 23, 2011 at 14:59
• @chrisaycock yeah, but this can come back to bite you: for example, if you have an enum and you cover all the cases, VS doesn't think it's enough - you also need to cover the else case :) Aug 23, 2011 at 15:51
• @Dmitri Is that an F#-ism? I've only ever used OCaml and SML/NJ. Aug 23, 2011 at 17:48

For financial analysis I use Amibroker. It uses a "C" like syntax for implementing custom indicators. A brief article on some of DeMark's indicators and Amibroker source code can be found here.

Given the relative simplicity of these rules (and that your alternative appears to be Excel), I would recommend any procedural programming language, and preferably one with statistical routines built in. Examples include R and Python with various extensions such as NumPy or SciPy (add as needed), as well as Matlab/Octave or any other statistical programming language. Object-oriented languages will probably require you to write many more lines of code than necessary for this relatively simple task.

However, at the end of the day, go with what you know.

• I wouldn't touch Excel with a 10-foot algorithm. Also not adverse to becoming better acquainted with a tool (language) if it makes the job more funner. I would also think OO would be a bit verbose and ugly. But not convinced imperative trumps declarative. Or that functional wouldn't be a really good fit. Aug 17, 2011 at 15:21
• @milktrader while I sympathize with your desire to learn, I believe that the sorts of trading rules you describe are the bread-and-butter of the statistical languages. Also glad to hear you weren't seriously considering Excel :). Aug 17, 2011 at 15:26