Announcement

Collapse
No announcement yet.

Finding a hammer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finding a hammer

    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

    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=1i<5i++)
                        {
                            
                            
    //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=(allBarsi+1); j<(allBars+i+4);++j)
                                            {
                                            
    Alert.addToList("allBars+ i= " + (allBarsi));
                                                
    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(allBarsilow(allBarsi)-.5Shape.UPARROWnullColor.redShape.BOTTOM"H"Tagid);
                                                        
    Tagid++;
                                                        
                                                        
    drawShapeRelative(barNumberlow(barNumber)-.5Shape.UPARROWnullColor.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=(allBarsi+1); j<(allBars+i+4);++j)
                                            {
                                                
    Alert.addToList("allBars+ i= " + (allBarsi));
                                                
    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(allBarsilow(allBarsi)-.5Shape.UPARROWnullColor.redShape.BOTTOM"H"Tagid);
                                                        
    Tagid++;
                                                        
                                                        
    drawShapeRelative(barNumberlow(barNumber)-.5Shape.UPARROWnullColor.RGB(0,255,0), Shape.BOTTOM"H"Tagid);
                                                        
    Tagid++;
                                                        
                                                        
    SkipBars=(Math.abs(j) +allBars );
                                                        
    SkipBars=allBars SkipBars+i;
                                                        
                                                    }    

                                                    
                                                }
                                            }
                                            
                                            }
                                            
                                        }
                                        
                                    
                                    }

                                }
                            }
                            
                            
                        }

                        
                             
                }
                
        } 
    Tony Gof

  • #2
    Hello Tony,

    The first error I see that may cause a logic problem for you is the if() statement in main() that checks for BARSTATE_ALLBARS. You have a single "=" in the condition which is the assignment operator. I think you intended that to be the "==" comparison operator. However, if you change it to "==" your formula will not process as you had intended. Because of the single "=" operator, CandleHammer() is being processed once for each bar loaded in the chart. This may make all or some of the looping routines you have in CandleHammer() unnecessary. Before looking into the location issue of shapes you may want to revisit the CandleHammer() logic if you intended for this to only process once at ALLBARS.

    Aside from this issue, assuming that CandleHammer() is supposed to be processed on each bar, here's some guidance that should help you solve this problem. The answer lies in the logic that determines your x-coordinate, or bar index value, used for the drawShapeRelative() calls. This may be overstating the obvious to you. Anyway, I don't have the specific solution for you as I do not understand all of the code logic. To figure this out I recommend that you comment out all of the drawShapeRelative() calls except the first one. Then add the following debugPrintln() statement on the line before it. This will print to the formula output window the bar index that is being processed followed by the value being passed for the bar index location.



    I ran this on a daily chart of INTC. In the chart image below there is a red arrow being drawn at bar -20.



    This was drawn while the routine was processing bar -1. The bar index value passed to drawShapeRelative() was -19. Keep in mind that the bar index value passed to the drawing functions is relative to the bar that is being processed. So 19 bars prior to bar -1 is bar -20. The question is, which bar did you intend for this particular arrow to be drawn?

    This should help you understand the changes that need to be made to pass the correct bar index value for this drawShape call. If so, then repeat this process for the other drawShape calls.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X