/***************************************Provided By : eSignal (c) Copyright 2005Description: Darvas BoxVersion 1.0 4/12/2005Notes:June 2005 Issue - "Something Darvas, Something New" by Daryl Guppy This study uses EFS2 functionality available in eSignalversion 7.9 or later.Formula Parameters: Defaults:High periods (Bars): Intraday 4High periods (Months): DWM 6***************************************/function preMain() { setPriceStudy(true); setStudyTitle("Darvas Box "); setShowTitleParameters(false); setCursorLabelName("Darvas High", 0); setCursorLabelName("Darvas Low", 1); setDefaultBarFgColor(Color.blue, 0); setDefaultBarFgColor(Color.blue, 1); setDefaultBarThickness(2, 0); setDefaultBarThickness(2, 1); setPlotType(PLOTTYPE_FLATLINES, 0); setPlotType(PLOTTYPE_FLATLINES, 1); // Number of bars to define Darvas Box High on intraday intervals. var fp1 = new FunctionParameter("iLength", FunctionParameter.NUMBER); fp1.setName("High periods (Bars): Intraday"); fp1.setLowerLimit(1); fp1.setDefault(4); // Number of Monthly bars to define a Darvas Box High on D,W,M intervals. var fp2 = new FunctionParameter("dLength", FunctionParameter.NUMBER); fp2.setName("High periods (Months): DWM"); fp2.setLowerLimit(1); fp2.setDefault(6);}var bVersion = null;var dh = null; // Darvas Highvar dl = null; // Darvas Lowvar bBox = false; // Darvas Box existsvar gBox = false; // Ghost Boxvar gBoxHeight = 0; // Ghost Box Heightvar gBoxLength = 0; // Ghost Box Lengthvar gBoxCntr = 0; // Current Ghost Box Lengthvar gBoxNum = 0; // Ghost Box number.var gBoxBase = 0 ; // Base start value for bottom of Ghost Box.var bDraw = false; // Allow new Ghost Box to be drawn.var CurIndex = null;// Current Bar Index;var CurIndex1 = null;var nCntr = 0; // Number of bars to start a boxvar nBoxCntr = 0;var nIndex = 0; // Bar index of start of last boxfunction main(iLength, dLength) { if (bVersion == null) bVersion = verify(); if (bVersion == false) return; var nState = getBarState(); if (nState == BARSTATE_NEWBAR) { CurIndex1 = CurIndex; if (bBox == true) adjustIndex(); nCntr++; } CurIndex = getCurrentBarIndex(); if (CurIndex1 == null) return; // New Darvas Box if (bBox == false && nState == BARSTATE_NEWBAR && nCntr >= 4) { var bSetdh = false; if (dh == null || high(0) > dh) { // set dh var h = high(-4); var nTestHigh = testHigh(iLength, dLength); if (high(-1) < h && high(-2) < h && high(-3) < h && h >= nTestHigh) { dh = h; bSetdh = true; nIndex = CurIndex - 4; // bar index of dh } } if (dl == null && dh != null) { // set dl for (var i = nIndex; i < getCurrentBarIndex()-3; i++) { var index = i-getCurrentBarIndex(); var l = low(index); if (low(index+1) > l && low(index+2) > l && low(index+3) > l) { dl = l; i = 0; } } if (dl == null && bSetdh == false) adjustIndex(); } if (dh != null && dl != null) { // Darvas Box found if (bSetdh == false && (CurIndex - CurIndex1) == 0) adjustIndex(); bBox = true; removeGhostBox(gBoxNum); gBox = false; nBoxCntr++; var aBox = new Array(dh, dl); var i = getCurrentBarIndex()-1; var j = -1; while (i >= nIndex) { // plot box back to the dh bar index. setBar(Bar.Value, j, aBox); j--; i--; } // New D_Box range has formed. Alert.playSound("Blip.wav"); drawLineRelative(j+1, dh, j+1, dl, PS_SOLID, 2, Color.blue, "left"+nBoxCntr); } } // Check for breach of Darvas Box if (nState == BARSTATE_NEWBAR && bBox == true) { if (close(-1) > dh) { // Entry Trigger: Bar closed above top of D_Box Alert.playSound("beam1.wav"); gBox = true; // Draw Ghost Box gBoxHeight = dh - dl; gBoxLength = (CurIndex -nIndex) -1; gBoxBase = dh; gBoxNum++; gBoxCntr = 0; bDraw = true; drawGhostBox(); } else if (close(-1) < dl) { // Exit Trigger: Bar closed below D_Box Alert.playSound("buzz.wav"); } if (close(-1) > dh || close(-1) < dl) { // reset globals to find next box. bBox = false; drawLineRelative(-1, dh, -1, dl, PS_SOLID, 2, Color.blue, "right"+nBoxCntr); dh = null; dl = null; nIndex = 0; nCntr = 0; } } if (gBox == true && nState == BARSTATE_NEWBAR) drawGhostBox(); if (dh == null || dl == null) { return new Array(null, null); } else { return new Array(dh, dl); }}/***** Support Functions *****/function removeGhostBox(num) { //if (gBox == true) { removeLine("gBoxLeft"+num); removeLine("gBoxRight"+num); removeLine("gBoxTop"+num); removeLine("gBoxBottom"+num); //} return;}function drawGhostBox() { if (close(-1) < gBoxBase) { // Exit Trigger: Bar closed below G_Box Alert.playSound("buzz.wav"); gBox = false; return; } if (gBoxCntr > gBoxLength) { gBoxBase += gBoxHeight; gBoxCntr = 0; gBoxNum++; bDraw = true; } gBoxCntr++; if (bDraw == true) { drawLineRelative(-1, gBoxBase, -1, gBoxBase+gBoxHeight, PS_SOLID, 3, Color.RGB(45, 185, 45), "gBoxLeft"+gBoxNum); drawLineRelative(-1+gBoxLength, gBoxBase, -1+gBoxLength, gBoxBase+gBoxHeight, PS_SOLID, 3, Color.RGB(45, 185, 45), "gBoxRight"+gBoxNum); drawLineRelative(-1, gBoxBase+gBoxHeight, -1+gBoxLength, gBoxBase+gBoxHeight, PS_SOLID, 3, Color.RGB(45, 185, 45), "gBoxTop"+gBoxNum); drawLineRelative(-1, gBoxBase, -1+gBoxLength, gBoxBase, PS_SOLID, 3, Color.RGB(45, 185, 45), "gBoxBottom"+gBoxNum); bDraw = false; } return;}function testHigh(i, d) { var nH = null; var aH; if (isDWM()) { nH = high(0, inv("M")); for (var j = 1; j < d; j++) { nH = Math.max(nH, high(-j, inv("M"))); } } else { nH = high(-5); for (var j = i-1; j > 0; j--) { nH = Math.max(nH, high(-j-5)); } } return nH;}function adjustIndex() { if (isComputeOnClose() && getCurrentBarIndex() == -1) { nIndex--; } else if (getCurrentBarIndex() == 0) { nIndex--; } return;}function verify() { var b = false; if (getBuildNumber() < 700) { drawTextRelative(65, 35, "This study requires version 7.9 or later.", Color.white, Color.blue, Text.BOLD|Text.LEFT|Text.RELATIVETOLEFT|Text.RELATIVETOBOTTOM, null, 14, "error"); drawTextRelative(65, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", Color.white, Color.blue, Text.BOLD|Text.LEFT|Text.RELATIVETOLEFT|Text.RELATIVETOBOTTOM, null, 14, "upgrade"); return b; } else { b = true; } return b;} |