Sharing investing and trading ideas. Helping traders get started.
Advertisement

Saturday, June 20, 2009

Darvas Box

Darvas Box DB
Comes with formula, calculation steps and VBA code

The calculation of the Darvas Box for each period or bar is quite complex, out of the indicators I have seen so far, for a study that does not use anything other than the High and Low of each period.

Each day or period is classified into States. For each period, we keep a record of the Trailing High and Trailing Low. Based on the state of the period, the Trailing High and Low, we draw the upper and lower bands of the Darvas Box.

A period is classified into

State 1 under ANY of the following conditions:
  1. It is the first period.
  2. The current high is greater than or equal to the Trailing High.
  3. The previous period was in State 5 and the current High is greater than Trailing High or the current Low is smaller than the Trailing Low.

State 2 under the following condition
  1. The last period was in State 1 and the current High is lower than the Trailing High
State 3 under ANY of the following conditions:
  1. The previous period was in State 2 and the current High is lower than the Trailing High.
  2. The previous period was in State 3 or 4 and the current Low is lower than the Trailing Low.
State 4 under the following condition:
  1. The last period was in State 3 and the current High is not higher than the Trailing High and the current Low is not lower than the Trailing Low.
State 5 under the following condition:
  1. The last period was in State 4 or 5 and the current High is not higher than the Trailing High and the current Low is not lower than the Trailing Low.
The Trailing High of each period is the High of each period if it is in State 1, otherwise it retains its value from the last period.

The Trailing Low of each period is the Low of each period if it is in State 3, otherwise it retains its value from the last period. The Trailing Low is reset to 0 when a box State changes from 5 to 1.

When charting, the Upper Band and the Lower Band of the Darvas Box is the Trailing High and Trailing Low respectively until a period is in State 5. When a period is in State 5, the Trailing High and Low for that period replaces the Upper Band and Lower Band of the Darvas Box from that period all the way BACK to the last period immediately after a period in State 5.

Signals are generated when the last period is in State 5 and the Current High or Low breaks out of the Davas Box.

VBA Code
Sub Runthis()
Dim high As Range, low As Range, output As Range
Set high = Range("C2:C11955")
Set low = Range("D2:D11955")
Set output = Range("H2:H11955")
DB_1 high, low, output
End Sub

Sub DB_1(high As Range, low As Range, output As Range)
output(0, 1).Value = "State"
output(0, 2).Value = "Trailing High" output(0, 4).Value = "Darvas Upper Band"
output(0, 5).Value = "Darvas Lower Band"
Chigh = high(2, 1).Address(False, False)
Clow = low(2, 1).Address(False, False)
Lstate = output(1, 1).Address(False, False)
Thigh = output(1, 2).Address(False, False)
Tlow = output(1, 3).Address(False, False)
Cstate = output(2, 1).Address(False, False)
Cthigh = output(2, 2).Address(False, False)
Ctlow = output(2, 3).Address(False, False)
Tomhigh = output(3, 4).Address(False, False)
Tomlow = output(3, 5).Address(False, False)
output(2, 1).Value = "=IF(OR(" & Chigh & ">=" & Thigh & ",AND(" & Lstate & "=5," & Clow & "<" & _
Tlow & ")),1,IF(AND(" & Lstate & "=1," & Chigh & "<" & Thigh & "),2,IF(OR(AND(" & _
Lstate & "=2," & Chigh & "<" & Thigh & "),AND(OR(" & Lstate & "=3," & Lstate & "=4)," & _
Clow & "<" & Tlow & ")),3,IF(AND(" & Lstate & "=3," & Chigh & "<=" & Thigh & "," & _
Clow & ">=" & Tlow & "),4,IF(AND(OR(" & Lstate & "=4," & Lstate & _
"=5)," & Chigh & "<=" & Thigh & "," & Clow & ">=" & Tlow & "),5)))))"

output(2, 2).Value = "=IF(" & Cstate & "=1," & Chigh & "," & Thigh & ")"
output(2, 3).Value = "=IF(" & Cstate & "=3," & Clow & ",IF(AND(" & Lstate & "=5," & Cstate & "=1),0," & Tlow & "))"
output(2, 4).Value = "=IF(" & Tomhigh & "=""""," & Cthigh & ",IF(" & Cstate & "=5," & Cthigh & "," & Tomhigh & "))"
output(2, 5).Value = "=IF(" & Tomlow & "=""""," & Ctlow & ",IF(" & Cstate & "=5," & Ctlow & "," & Tomlow & "))"
Range(output(2, 1), output(2, 5)).Copy output
output(1, 1).Value = 1
End Sub


Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)

3 comments:

Anonymous said...

Just found your site. Looks great & just what I need. Thanks for all your hard work!

Small points. Trying to get ADX (Method B) to work, but I'm getting #DIV/0! errors originating in WWMA column? I'm still looking at it, so may be my fault!

Also, appears to be a problem with the Darvas Box code not having end of line characters when pasted to VBE.

Anonymous said...

Ahh, Ignore my comment on ADX problem. I need to work on my 'Runthis()'!

Invest said...

Hi
i came acros your site looking for Darvas calaculation and find your job well done.
But it seems like the results of the code are not correct- comapre to other graphical implementation of the Darvas boxes which i use for quite some time already.
Did yiu get such comments in the past - do you have a more updated version?
Thank you

Post a Comment