# 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? –  Milktrader Aug 22 '11 at 17:44
@Milktrader I have updated the answer with an example. –  Dmitri Nesteruk Aug 23 '11 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. –  chrisaycock Aug 23 '11 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 :) –  Dmitri Nesteruk Aug 23 '11 at 15:51
@Dmitri Is that an F#-ism? I've only ever used OCaml and SML/NJ. –  chrisaycock Aug 23 '11 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.

-