Announcement

Collapse
No announcement yet.

DrawShapeRelative help needed

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

  • DrawShapeRelative help needed

    I'm really new to working with EFS and having problems doing what I'd like to do and wonder if anyone can help me tweak my first code.

    On the image, I'm using DrawShapeRelative on the Stochastic when it either goes above 75 or below 25 it produces either a red or green line at the bottom. I'm actually wanting the green to be on top of the Stochastic and can't seem to get it there. Also I have colored my bars to replicate this but I'm wanting to also have them go red when my fast Stochastic has crossed over the slow, again I'm lost. Each time I add the line in my code to do this it completely changes my colored bars from what are now.

    any guidance would be appreciated.

    tks
    PHP Code:
    var vStoch14_3 = new StochStudy(1433);


    function 
    preMain() {
      
        
    setPriceStudy(false);
        
    setStudyTitle("Stochastic Price Bars 4");
        
    setStudyMin(0);
        
    setStudyMax(100);
        
    setCursorLabelName("StoK"0);
        
    setCursorLabelName("StoD"1);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarFgColor(Color.red1);
        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setPlotType(PLOTTYPE_LINE0);
        
    setPlotType(PLOTTYPE_LINE1);
        
    addBand(75PS_SOLID1Color.navy"a");
        
    addBand(25PS_SOLID1Color.navy"b");


    }

    function 
    main() {
        


            if (
    vStoch14_3.getValue(StochStudy.FAST) > 75 &&
                
    vStoch14_3.getValue(StochStudy.SLOW) > 75) {
                
    setPriceBarColor(Color.lime);
                
    setBarThickness(2);
                
    setBarFgColor(Color.blue);
                
    drawShapeRelative(0low(), Shape.SQUARE""Color.RGB(0,128,18), Shape.ONTOP);
            }  
            else if (
    vStoch14_3.getValue(StochStudy.FAST) < 25 &&
                
    vStoch14_3.getValue(StochStudy.SLOW) < 25)  {
                
    setPriceBarColor(Color.red);
                
    setBarThickness(2);
                
    setBarFgColor(Color.red);
                
    drawShapeRelative(0low(), Shape.SQUARE""Color.RGB(255,0,0), Shape.ONTOP);
            }
            
        return new Array(
            
    vStoch14_3.getValue(StochStudy.FAST),
            
    vStoch14_3.getValue(StochStudy.SLOW)
        );


    Attached Files

  • #2
    Hello Shane,

    Thanks for posting your code and chart image.

    Regarding the location of the squares, they are appearing at the bottom of the window pane because you are using "low()" for the y-axis value. The Stochastic scale is from 0 to 100. Since the security price you are using the formula with is trading below 1.0, the squares are being drawn at the bottom of the stochastic scale. Try changing the yValue parameter to be the value of the stochastic indicator like below.

    PHP Code:
    //drawShapeRelative(0, low(), Shape.SQUARE, "", Color.RGB(0,128,18), Shape.ONTOP);
    drawShapeRelative(0vStoch14_3.getValue(StochStudy.FAST), Shape.SQUARE""Color.RGB(0,128,18), Shape.ONTOP); 
    and

    PHP Code:
    //drawShapeRelative(0, low(), Shape.SQUARE, "", Color.RGB(255,0,0), Shape.ONTOP);
    drawShapeRelative(0vStoch14_3.getValue(StochStudy.FAST), Shape.SQUARE""Color.RGB(255,0,0), Shape.ONTOP); 
    For the price bar coloring upon a cross of the fast and slow, you'll first need to remove or comment out the setPriceBarColor() calls in your existing conditions. Then create a new set of conditions that compare the last two bar's values for the fast and slow stochastic. To reference the previous bar's value for the stochastics, pass the specified bar index parameter like below.

    // 1 bar prior
    vStoch14_3.getValue(StochStudy.FAST, -1)
    vStoch14_3.getValue(StochStudy.SLOW, -1)

    // 2 bars prior
    vStoch14_3.getValue(StochStudy.FAST, -2)
    vStoch14_3.getValue(StochStudy.SLOW, -2)

    You can compare the current bar to bar -1 or look for the new bar state using getBarState() and then look for a confirmed cross by looking at the values from bars -1 and -2.

    If you compare the current bar to bar -1, then you'll need to be aware that you can get many signals during the current interval in real time if they cross and uncross multiple times during the interval. If you go this route then you'll also want to have an else condition that paints the price bar to a default (or neutral) color to remove the colors set when a cross occurs. You won't need to do this if you are looking at confirmed crosses at the new bar state, which is the open of the current bar.

    One helpful item to add to your formula would be to create some local variables that are assigned to the indicator values you want to use for your conditions. This will shorten up the code a bit and make it slightly more efficient. Inside main(), add the following.

    PHP Code:
            var nK_0 vStoch14_3.getValue(StochStudy.FAST0);
            var 
    nK_1 vStoch14_3.getValue(StochStudy.FAST, -1);
            var 
    nK_2 vStoch14_3.getValue(StochStudy.FAST, -2);
            var 
    nD_0 vStoch14_3.getValue(StochStudy.SLOW0);
            var 
    nD_1 vStoch14_3.getValue(StochStudy.SLOW, -1);
            var 
    nD_2 vStoch14_3.getValue(StochStudy.SLOW, -2); 
    Then use these variables in your conditionals statements rather than making the .getValue() calls multiple times.

    Here's an example of what the current bar cross might look like.

    PHP Code:
            if (nK_0 nD_0 && nK_1 <= nD_1) {  // cross up
                
    setPriceBarColor(Color.lime);
            } else {
                
    setPriceBarColor(Color.grey);  // reset color if not crossed
            

    Do the opposite for the cross down.

    Here's what the confirmed cross might look like.

    PHP Code:
            if (getBarState() == BARSTATE_NEWBAR && nK_1 nD_1 && nK_2 <= nD_2) {
                
    setPriceBarColor(Color.lime);
            } 
    And again, do the opposite for the cross down.
    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


    • #3
      Thanks JasonK, I appreciate the guidance that did the trick and it functions they way I'd like it to.

      Comment


      • #4
        You're most welcome.
        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