//@LibraryID:870,0
//@Name:Darvas S&R
//@Description:Highs and lows following n lower highs and n higher lows
//@Future:No
/* Sorry, no boxes, but I believe this gives the tops and bottoms of the boxes as defined in "Trend Trading" by Daryl Guppy.
The classic initiation of an investment by this means was after a new 52 week high which you can search for in Sharescope.
He suggests looking for a period of 0 to 5 days after the new high. Note the Sharescope search looks for a highest close,
Darvas used a highest high, also he wanted a new high not an equal high so if you follow his guide you will have to check
this from the results that native Sharescope gives.
Guppy suggests using a break by the high of the bar as the initiation of the trade and a penetration of the low of the box by
the close of the bar as way of initiating and closing a trade. He also suggests the use of "Ghost Boxes" to cover the situation
where there has been a considerable run upwards before a new bottom can be defined. I have not implemented this but it's easy
if you apply it only to the shares you have bought which will be few. All it is is to measure the high to low of the last
completed box and trail that distance up to any new high, using the bottom of the ghost box as the stop rather than the last
defined stop. I have included a 50% retracement value for every "box".
Guppy suggests (I suspect using US and possibly Australian shares) that wins result from 80% of 52 week highs and 70% of 26 week
highs. Lower than that the %age declines. If you want to use it anyway I would suggest an independent means of assessing the
trend which would otherwise not be required.
Don't look for a joyful experience in reading this book - a tiresome style.
*/
//This code by Roger Harmer
var p1 = 3;
var colour1 = Colour.Green;
var colour2 = Colour.Red;
var pen1 = 0;
var pen2 = 0;
var width1 = 0;
var width2 = 0;
function init(status)
{
if (status == Loading || status == Editing)
{
colour1 = storage.getAt(0);
width1 = storage.getAt(1);
pen1 = storage.getAt(2);
colour2 = storage.getAt(3);
width2 = storage.getAt(4);
pen2 = storage.getAt(5);
}
if (status == Adding || status == Editing)
{
var dlg = new Dialog("Settings...", 250, 45);
dlg.addOkButton();
dlg.addCancelButton();
dlg.addColLinePicker("VAL1",50,5,-1,-1,"Upper Line:","",colour1,pen1,width1);
dlg.addColLinePicker("VAL2",125,5,-1,-1,"Lower Line:","",colour2,pen2,width2);
if (dlg.show() == Dialog.Cancel)
return false;
colour1 = dlg.getValue("VAL1").colour;
pen1 = dlg.getValue("VAL1").pen;
width1 = dlg.getValue("VAL1").width;
colour2 = dlg.getValue("VAL2").colour;
pen2 = dlg.getValue("VAL2").pen;
width2 = dlg.getValue("VAL2").width;
storage.setAt(0, colour1);
storage.setAt(1, width1);
storage.setAt(2, pen1);
storage.setAt(3, colour2);
storage.setAt(4, width2);
storage.setAt(5, pen2);
}
setTitle("Darvas S&R");
setSeriesColour(0, colour1);
setSeriesLineStyle(0, pen1, width1);
setSeriesColour(1, colour2);
setSeriesLineStyle(1, pen2, width2);
setRange(Range.Parent);
}
function getGraph(share, data)
{
var h1 = new Array();
var l1 = new Array();
var h2 = new Array();
var l2 = new Array();
var lastmove;
var hup = 0;
var ldn = 1;
h1[p1-1] = Math.max(data[0].high, data[1].high, data[2].high);
l1[p1-1] = Math.min(data[0].low, data[1].low, data[2].low);
h2[p1-1] = h1[p1-1];
l2[p1-1] = l1[p1-1];
for (var i=p1; i= Math.max(data[i-1].high, data[i-2].high, data[i-3].high) && data[i].high > Math.max(data[i+1].high, data[i+2].high, data[i+3].high))
h1[i] = data[i].high;
h2[i] = h2[i-1];
if (h1[i] > h2[i-1] && h1[i] > h1[i-1])
{
h2[i] = h1[i];
lastmove = hup;
}
if (h1[i] < h2[i-1] && lastmove == ldn && h1[i] != h1[i-1])
{
h2[i] = h1[i];
}
l2[i] = l2[i-1];
if (l1[i] < l2[i-1] && l1[i] < l1[i-1])
{
l2[i] = l1[i];
lastmove = ldn;
}
if (l1[i] > l2[i-1] && lastmove == hup && l1[i] != l1[i-1])
{
l2[i] = l1[i];
}
}
h2[data.length-1] = h2[data.length-2] = h2[data.length-3] = h2[data.length-4];
l2[data.length-1] = l2[data.length-2] = l2[data.length-3] = l2[data.length-4];
return [h2, l2];
}