Announcement

Collapse
No announcement yet.

Custom Open/Close on Intraday chart

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

  • Custom Open/Close on Intraday chart

    I am trying to create an Indicator that tracks the difference between open/Close based on custom times to display on an intraday chart. I am able to show just the open or the close, but seem to be getting something wrong when I try to display the difference. Below is the code as I currently have it (not working). Have I just made a couple of small errors here, or am I way off track?

    Thanks in Advance

    Code:
    function preMain() {
        setPriceStudy(false);
        setStudyTitle("ES BASE Price");
        setCursorLabelName("ES BASE Price", 0);
        setDefaultBarStyle(PS_SOLID, 0);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarThickness(2, 0);
        setPlotType(PLOTTYPE_LINE, 0);
    }
        var DOpen=null
        var DClose=null
        var NKD_sym = "NKD U2";
    
    function main (){
    
        
        if (getHour(0,sym(NKD_sym)) == 8 && getMinute(0,sym(NKD_sym)) == 30-getInterval(0,sym(NKD_sym))) { // Is your 8:30am-inv bar
        DOpen= close(0,sym(NKD_sym));
        }
        
            if (getHour(0,sym(NKD_sym)) == 20 && getMinute(0,sym(NKD_sym)) == 0-getInterval(0,sym(NKD_sym))) { // Is your 8:30am-inv bar
        DClose= close(0,sym(NKD_sym)) - DOpen();
        }
        
      
    
        return DClose;
    }
    Last edited by maninjapan; 09-14-2012, 08:17 AM.

  • #2
    Still looking for a solution to this. Is anyone able to assist?

    Thanks!!

    Comment


    • #3
      The problem is that you are subtracting an interval from a time, as in:
      PHP Code:
      if (getHour(0,sym(NKD_sym)) == 20 && getMinute(0,sym(NKD_sym)) == 0-getInterval(0,sym(NKD_sym))) { 
      So subtracting a 3 minute interval from a time of 20 hours and 00 minutes should give you 1957 but your equation results in 20==20 for hours and 0==-3 for minutes (or something like that).

      Correcting the time equation becomes complicated but can be done.

      However, since you want the close of the previous bar of the passed symbol & interval try the following to see if it fits your needs:
      PHP Code:
      //instead of subtracting an interval from the minutes get the close of the previous bar when the start or end times are reached
      debugClear();
      function 
      preMain(){
          
      setPriceStudy(false);
         
      /* setStudyTitle("ES BASE Price");
          setCursorLabelName("ES BASE Price", 0);
          setDefaultBarStyle(PS_SOLID, 0);
          setDefaultBarFgColor(Color.red, 0);
          setDefaultBarThickness(2, 0);
          setPlotType(PLOTTYPE_LINE, 0);*/
          
      setCursorLabelName("Open",0);
          
      setCursorLabelName("Close",1);
          
      setCursorLabelName("Time",2);
      }
          var 
      DOpen=null
          
      var DClose=null
          
      var NKD_sym "NKD U2";
          

      function 
      main (){

          
      //var gt=(getHour(0,sym(NKD_sym))*100)+getMinute(0,sym(NKD_sym)); //sym() is not needed for getHour() & getMinute()

          //the next line of code combines hours & minutes to time without the ":" delimiter as in 2000 instead of 20:00
          
      var gt=(getHour(0,NKD_sym)*100)+getMinute(0,NKD_sym);
          if (
      gt == 830) { // Is your 8:30am-inv bar
              
      DOpenclose(-1,sym(NKD_sym));
              
      DOpenclose(-1,sym(NKD_sym));
          }
          if (
      gt == 2000) { // Is your 20:00pm-inv bar
              
      DCloseclose(-1,sym(NKD_sym));
          }
          return [
      DOpen+"",DClose+"",gt+""];

      FWIW: I would use the open of the bar of the start time not the close of the previous bar. As in:
      PHP Code:
      DOpenopen(0,sym(NKD_sym)); 
      Last edited by waynecd; 09-30-2012, 10:24 AM.

      Comment


      • #4
        The following line of code is duplicated in my previous post. Please delete one of the duplicates if you use the code.
        PHP Code:
        DOpenclose(-1,sym(NKD_sym)); 

        Comment


        • #5
          Thanks Wayne, I believe this is almost what I am after, However what I was trying to do with:
          Code:
           DClose= close(0,sym(NKD_sym)) - DOpen();
          was to return the difference between the 2 prices, creating a custom daily range.

          Below is your code as I have it, it almost works however it should only return one value per day, after DClose and should remain the same value till the following day's Dclose.
          Code:
          //instead of subtracting an interval from the minutes get the close of the previous bar when the start or end times are reached
          debugClear();
          function preMain(){
              setPriceStudy(false);
             setStudyTitle(" BASE Price");
              setCursorLabelName(" BASE Price", 0);
              setDefaultBarStyle(PS_SOLID, 0);
              setDefaultBarFgColor(Color.red, 0);
              setDefaultBarThickness(2, 0);
              setPlotType(PLOTTYPE_LINE, 0);*/
              setCursorLabelName("Open",0);
              setCursorLabelName("Close",1);
              setCursorLabelName("Time",2);
          }
              var DOpen=null
              var DClose=null
              var DDiff=null
            
              
          
          function main (){
          
              //var gt=(getHour(0,sym(NKD_sym))*100)+getMinute(0,sym(NKD_sym)); //sym() is not needed for getHour() & getMinute()
          
              //the next line of code combines hours & minutes to time without the ":" delimiter as in 2000 instead of 20:00
              var gt=(getHour(0)*100)+getMinute(0);
              if (gt == 830) { // Is your 8:30am-inv bar
                  DOpen= close(-1);
          
              }
              if (gt == 2000) { // Is your 20:00pm-inv bar
                  DClose= close(-1);
              }
             
              
              return DClose - DOpen;
          }

          Comment


          • #6
            PHP Code:
            //make sure the chart's interval allows for a bar formation at your start and end times
            //for example if the chart's interval is 5 minutes and your start time is 829 instead of 830
            //"DOpen" won't be assigned a value since "gt" will never equal 829
            debugClear();
            function 
            preMain(){
                
            setPriceStudy(false);
                
            setCursorLabelName("Daily Range",0);
            }
            var 
            DOpen=null
            var DClose=null
            var NKD_sym "NKD U2";
            var 
            Ddiff;

            function 
            main (){
                if(!
            isIntraday()) return; //otherwise "gt" won't equal the start and end times 
                
            var gt=(getHour(0,NKD_sym)*100)+getMinute(0,NKD_sym);
                if (
            gt == 830) { 
                    
            DOpenclose(-1,sym(NKD_sym));
                }
                if (
            gt == 2000) {
                    
            DCloseclose(-1,sym(NKD_sym));
                }
                
            Ddiff DClose DOpen
                
            return Ddiff;

            Comment

            Working...
            X