I have the following code which I am trying to find a hammer on my chart, however I have a problem. My program does find hammers ( Understanding that they should appear on the bottom, for simplicity I have removed that section). My problem is that when I find a hammer lets say bar number -50, and bar number -48 confirms that bar -50 was a hammer, instead of marking bar -48 it marks the bar number -47.
You can set the
You can set the
PHP Code:
var Tagid=0;
function preMain()
{
setPriceStudy(true);
setStudyTitle("Candle pattern");
setCursorLabelName("Candle pattern", 0);
}
function main() {
var nState=getBarState();
if (nState=BARSTATE_ALLBARS)
{
CandleHammer();
}
}
function CandleHammer()
{
var vOpen;
var vHigh;
var vLow;
var vClose;
var vHL;
var vOC;
var vOpenNextBar;
var vHighNextbar;
var vLowNextbar;
var vCloseNextbar;
var vHLNextbar;
var vOCNextbar;
var barNumber="NONE";
var SkipBars=-100;
var vPercentageOfBodytoHighLow= .3;
var vPercentageAboveTheLow=.6;
for (var allBars=-100;allBars<1;allBars++)
{
for (var i=1; i<5; i++)
{
//Load bar number ( i+allBars) and comapre it to next 3 bars
//if we have a close above the high of this candle, then mark the candle that closes above the hammer.
vOpen=open(i+allBars);
vHigh=high(i+allBars);
vLow=low(i+allBars);
vClose=close(i+allBars);
vOC=Math.abs(vOpen-vClose);
vHL=vHigh-vLow;
if ((Math.abs(vOC))<(vHL*vPercentageOfBodytoHighLow))
{
if (vOC!=0)
{
if (vOC>0)
{
if (vOpen>(vPercentageAboveTheLow*vHL+vLow))
{
for (var j=(allBars+ i+1); j<(allBars+i+4);++j)
{
Alert.addToList("allBars+ i= " + (allBars+ i));
Alert.addToList("j=" + j);
if (allBars>SkipBars)
{
vOpenNextBar=open(j);
vHighNextbar=open(j);
vLowNextbar=open(j);
vCloseNextbar=open(j);
//Alert.addToList("*Open*j="+j);
if ((vCloseNextbar>vHigh))
{
if (barNumber!=(j));
{
barNumber=j;
drawShapeRelative(allBars+ i, low(allBars+ i)-.5, Shape.UPARROW, null, Color.red, Shape.BOTTOM, "H"+ Tagid);
Tagid++;
drawShapeRelative(barNumber, low(barNumber)-.5, Shape.UPARROW, null, Color.RGB(0,255,0), Shape.BOTTOM, "H"+ Tagid);
Tagid++;
SkipBars=(Math.abs(j) +allBars );
SkipBars=allBars + SkipBars+i;
}
}
}
}
}
}
else
{
if (vClose>(vPercentageAboveTheLow*vHL+vLow))
{
for (var j=(allBars+ i+1); j<(allBars+i+4);++j)
{
Alert.addToList("allBars+ i= " + (allBars+ i));
Alert.addToList("j=" + j);
if (allBars>SkipBars)
{
vOpenNextBar=open(j);
vHighNextbar=open(j);
vLowNextbar=open(j);
vCloseNextbar=open(j);
if ((vCloseNextbar>vHigh))
{
if (barNumber!=(j));
{
barNumber=j;
drawShapeRelative(allBars+ i, low(allBars+ i)-.5, Shape.UPARROW, null, Color.red, Shape.BOTTOM, "H"+ Tagid);
Tagid++;
drawShapeRelative(barNumber, low(barNumber)-.5, Shape.UPARROW, null, Color.RGB(0,255,0), Shape.BOTTOM, "H"+ Tagid);
Tagid++;
SkipBars=(Math.abs(j) +allBars );
SkipBars=allBars + SkipBars+i;
}
}
}
}
}
}
}
}
}
}
}
Comment