Description
Coded for WLD3 by
cclee (HERE)
System Concept: Nicolas Darvas was a successful professional dancer who also made millions in the stock market in the 50s using a charting method he described in his classic book
How I Made $2,000,000 in the Stock Market. His basic approach (not including the stock selection process) is essentially a breakout technique.
Darvas defined consolidations as "boxes" and traded upside breakouts of these boxes. A Darvas Box is formed when both the box top and box bottom have been established, as follows:
- When a stock fails to make a new high for three days, the most recent high that is higher than the three subsequent highs becomes the box top.
- If price breaks out of the box top, repeat step 1.
- When a stock fails to make new lows after three days, the most recent low that is lower than the three subsequent lows becomes the box bottom.
- The box is broken when today's high price breaks out above the box top or below the box bottom.
Trade rules:- Go long when the price exceeds the top of the Darvas Box.
- Sell when the price of the stock falls below the bottom of the Darvas Box.
The Darvas Box system has the benefit of being very simple and it has the potential to beat the market with proper money management.
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class DarvasBox : WealthScript
{
protected override void Execute()
{
double BoxTop, BoxBottom;
bool BoxBottomDef, BoxTopDef, BoxDef, First;
int BoxTopCount, BoxBotCount, BoxDays, LastBoxBar;
WealthLab.LineStyle ls = WealthLab.LineStyle.Solid;
First = true;
BoxDays = 2;
LastBoxBar = 0;
// my initialization
BoxTop = Bars.High[0];
BoxBottom = Bars.Low[0];
BoxTopCount = BoxBotCount = 0;
BoxTopDef = BoxBottomDef = BoxDef = false;
for(int bar = 5; bar < Bars.Count; bar++)
{
if( First == true )
{
BoxTop = Bars.High[bar];
First = false;
BoxTopCount = 0;
BoxBotCount = 0;
BoxBottomDef = false;
BoxTopDef = false;
LastBoxBar = bar;
}
if( ( BoxDef == false ) & ( Bars.High[bar] > BoxTop ) )
{
BoxTopCount = 0;
BoxTop = Bars.High[bar];
// Resets BoxBotCount if New High is made again
BoxBotCount = 0;
}
if( ( BoxDef == false ) & ( Bars.High[bar] <= BoxTop ) )
BoxTopCount++;
if(( BoxDef == false ) & ( BoxTopCount == BoxDays ) & ( Bars.High[bar] <= BoxTop ))
BoxTopDef = true;
if(( BoxDef == false ) & ( BoxTopDef == true ) & ( BoxBottomDef == false ) & ( BoxTopCount >= BoxDays+1 ))
{
if( BoxBotCount == 0 )
BoxBottom = Bars.Low[bar];
if( Bars.Low[bar] < BoxBottom )
{
BoxBotCount = 0;
BoxBottom = Bars.Low[bar];
}
if( Bars.Low[bar] >= BoxBottom )
BoxBotCount++;
if( BoxBotCount == BoxDays+1 )
BoxBottomDef = true;
}
// test
if(( BoxDef == false ) & ( BoxTopDef == true ))
{
if( Bars.High[bar] > BoxTop )
First = true;
}
if(( BoxDef == false ) & ( BoxBottomDef == true ) & ( BoxTopDef == true ) )
{
BoxDef = true;
BoxBottomDef = false;
BoxTopDef = false;
}
// Darvas Box defined
if( BoxDef == true )
{
DrawLine( PricePane, bar, BoxTop, LastBoxBar, BoxTop, Color.Green, ls, 2 );
DrawLine( PricePane, bar, BoxBottom, LastBoxBar, BoxBottom, Color.Red, ls, 2 );
if( Bars.High[bar] > BoxTop )
{
BoxDef = false;
if( !IsLastPositionActive )
BuyAtStop( bar+1, BoxTop );
DrawLine( PricePane, LastBoxBar, BoxBottom, LastBoxBar, BoxTop, Color.Green, ls, 2 );
DrawLine( PricePane, bar, BoxBottom, bar, BoxTop, Color.Green, ls, 2 );
First = true;
BoxTop = Bars.High[bar];
}
if( Bars.Low[bar] < BoxBottom )
{
BoxDef = false;
if( IsLastPositionActive )
SellAtStop( bar+1, LastPosition, BoxBottom );
DrawLine( PricePane, LastBoxBar, BoxBottom, LastBoxBar, BoxTop, Color.Green, ls, 2 );
DrawLine( PricePane, bar, BoxBottom, bar, BoxTop, Color.Red, ls, 2 );
First = true;
BoxTop = Bars.High[bar];
}
}
// second true
if( First == true )
{
BoxTop = Bars.High[bar];
First = false;
BoxTopCount = 1;
BoxBotCount = 0;
BoxBottomDef = false;
BoxTopDef = false;
LastBoxBar = bar;
}
}
}
}
}