Announcement

Collapse
No announcement yet.

If Else statement

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

  • #16
    ok, thankyou, I see that the code as posted works. I need the daily close returned every bar so I tried the following code, (I just removed the isLastBarOnChart() && section and changed the getValue to (0))

    However, Today's close is being returned all the way back to midnight last night, even though the close of the market which the current close price refers to occured at 15:30 Japan time, which was only about 5 hours ago.

    Is there a simple fix to have the daily close price change at the daily close time of 15:30?

    Thank you again



    var xClose = null;
    var bDone = false;

    function preMain() {


    setPriceStudy(false);
    setStudyTitle("DailyClose");
    setCursorLabelName("THU", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    addBand( 0, PS_DASH, 1, Color.white,"line");


    }

    function main(){
    if(getBarState()==BARSTATE_ALLBARS){
    xClose = close(sym("THU N2-TCM,D"));
    }
    if(bDone==false){
    return( xClose.getValue(0));
    bDone=true;
    }
    Last edited by maninjapan; 02-02-2012, 05:54 AM.

    Comment


    • #17
      ok, well I believe I almost have something that works. The only problem left now is the end of the week with the final trading hours going into Saturday. I found the following thread which I believe will solve my issue once and for all. I believe that I have coded this as per Alexis' directions (at the start of the thread), but receiving an error saying that barDate returns null.


      any ideas?





      PHP Code:
      var xClose null;
      var 
      bDone false;
      var 
      currDOW 0;



      function 
      preMain() {
        



          
      setPriceStudy(false);
          
      setStudyTitle("DailyClose");
          
      setCursorLabelName("THU"0);
          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarFgColor(Color.red0);
          
      setDefaultBarThickness(20);
          
      setPlotType(PLOTTYPE_LINE0);
          
      addBand0PS_DASH1Color.white,"line");


      }

      function 
      main(){
          
              var 
      barDate getValue("time");
          
              if(
      getBarState()==BARSTATE_ALLBARS){
                var 
      barDate getValue("time");
                  
      currDOW barDate.getDay();
              }
              if(
      bDone==false){
               
                      return( 
      dayOfWeek.getValue(-1));
                      
      bDone=true;
              
          }
          

      Comment


      • #18
        This is for your Javascript programming health:

        1. Declare ALL local variables after the first left curly brace of the function in which they're used.

        2. Declare all global variables BEFORE any functions are defined.

        There is no block declaration scope level in Javascript.

        I have not run that last code posted but why are you declaring a local variable twice and once inside of an if() statement block? I promise you. This is not C/C++.

        You need to read "Javascript : The Good Parts" by Douglas Crockford or look up similar type stuff on the web and query what the "bad parts" are.
        Last edited by SteveH; 02-03-2012, 07:20 AM.

        Comment


        • #19
          Thanks again steve, didnt actually mean to have that second one here. (and I really dont have any programming background )
          After a bit more playing around with Alexis' example in the thread I referenced I gotthis to work. Just a matter of adding to the main part of the code and I should be all good!!!!

          Thanks a lot for your help everyone

          PHP Code:
          function main(){
              
               

          var 
          barDate getValue("time");

          if(
          getBarState()==BARSTATE_NEWBAR ){
           

              
          currDOW barDate.getDay();
              return 
          currDOW;

           
              }


          Last edited by maninjapan; 02-03-2012, 09:52 AM.

          Comment


          • #20
            and here it is. the working code in all it's glory.

            Thanks to everyone who offered contributions

            PHP Code:
            var xClose null;
            var 
            bDone false;
            var 
            currDOW 0;

            function 
            preMain() {
              

                
            setPriceStudy(false);
                
            setStudyTitle("DailyClose");
                
            setCursorLabelName("THU"0);
                
            setDefaultBarStyle(PS_SOLID0);
                
            setDefaultBarFgColor(Color.red0);
                
            setDefaultBarThickness(20);
                
            setPlotType(PLOTTYPE_LINE0);
                
            addBand0PS_DASH1Color.white,"line");


            }

            function 
            main(){
                
                   var 
            barDate getValue("time");
                    
                if(
            getBarState()==BARSTATE_NEWBAR ){
                
            currDOW barDate.getDay();
                }
                    if(
            getBarState()==BARSTATE_ALLBARS){
                        
            xClose close(sym("THU N2-TCM,D"));
                    }
                    if(
            bDone==false){
                        if ((
            getHour(0) > 15 && getHour(0) <= 23)||(currDOW == )) {
                            return( 
            xClose.getValue(0));
                            
            bDone=true;
                        }
                        else
                        { 
                          return( 
            xClose.getValue(-1));
                            
            bDone=true;  
                        }
                    }
                } 

            Comment


            • #21
              In your code, bDone will never be set to true because you always return from the function before it's set (aka, both "bDone = true;" statements are "dead code").

              Here's how I would have coded your main() function. [sorry, it's late when I posted this so haven't run the code on a chart]

              PHP Code:
              function main() 
              {
                var 
              barDate getValue("time");
                var 
              idx;

                if (
              getBarState() == BARSTATE_NEWBAR)
                {
                  
              currDOW barDate.getDay();
                }

                if (
              getBarState() == BARSTATE_ALLBARS
                {
                  
              xClose close(sym("THU N2-TCM,D"));
                }

                if (
              bDone == false && xClose != null
                {
                  
              bDone true;
                  
              idx = ((getHour(0) > 15 && getHour(0) <= 23) || currDow == 6) ? : -1;
                  return 
              xClose.getValue(idx);
                }

              Comment

              Working...
              X