Skip to content
All library documents

Choosing a Programming Language for DeMark Indicators

Article Quant Q&A · Author: Milktrader

Summary

The document considers how to implement DeMark indicator rules, which often track a sequence of price conditions across multiple bars. A bearish price flip is given as an example: a close exceeds the close four bars earlier and is followed by a close below that earlier reference. The question compares procedural code, spreadsheet formulas, statistical languages, and functional approaches.

The responses argue that the rules can be written in many languages. One suggests OCaml or F# because pattern matching can make multi-bar conditions clearer and process a sequence in groups, while others recommend familiar procedural or statistical tools such as R, Python, MATLAB, or a charting platform’s scripting language. The practical guidance favors readable code, available statistical libraries, and the programmer’s existing experience. No benchmark or complete validated implementation is provided, and the code fragments are illustrative rather than a definitive specification of DeMark rules.

Key ideas

  • DeMark indicators translate sequential price conditions into signals across multiple bars.
  • Pattern matching in a functional language can express multi-bar conditions in a readable way.
  • Procedural and statistical languages can also implement the rules, especially when suitable data tools are available.
  • For a relatively simple indicator, language familiarity and readable logic may matter more than a particular paradigm.
  • The code examples are illustrative and do not establish a complete or validated implementation.

Tags

Full text
# What programming language is best suited for implementing DeMark?


# 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.

## Answer by Dmitri Nesteruk (score 7, accepted)

https://quant.stackexchange.com/a/1716

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.

## Answer by Barry Chopper (score 3)

https://quant.stackexchange.com/a/1721

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.

## Answer by Tal Fishman (score 1)

https://quant.stackexchange.com/a/1699

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.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.