Announcement

Collapse
No announcement yet.

If Else statement

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

  • If Else statement

    As part of an indicator I need to use the Daily close price for a product which is calculated at 15:30. For the day session leading up to the 15:30 close I need to use the previous day's close. I have attempted to use the following statement. however it doesnt seem to be quite working as I expect it to. This is my first attempt at an if else statement so I am sure I have just coded it incorrectly. Any help would be much appreciated.

    Code:
     if (getHour(0,sym(yen_sym)) >9 && getHour(0,sym(yen_sym)) < 16  ) { // Refering to previous days close for 9am-4pm
            nCloseDay1=close(-1,sym(TCM_sym_day));
              }
            else 
            {xClose = close(sym(TCM_sym_day)); ; 
            }
    Last edited by maninjapan; 01-26-2012, 08:31 AM.

  • #2
    Your question is not clear, atleast to me. What do you want, and why are you checking the hour in your IF ?

    Comment


    • #3
      gansal, sorry if my question wasn't clear. I will try and explain it more clearly.

      I have am using this on an intraday chart and I want to display the daily settlement. Until 15:30 it should just return the previous day's settlement,but currently during the day session it is showing the close price of the intraday bar, not just the previous day's settlement.
      There should be just one change in price each day, but as you can see from the image, that is not the case.
      Attached Files

      Comment


      • #4
        Why are you not using this : nCloseDay1=close(-1,inv("D"))
        This would always return closing price of last day.

        Comment


        • #5
          Thanks for the suggestion, I tried the following but when I recalculated I recieved an error say that xClose equals null. Have I formatted this correctly?

          xClose = close(-1,sym("THU N2-TCM=2,D"));

          Comment


          • #6
            Just use: xClose=close(-1,inv("D"));

            Comment


            • #7
              I can't use that as I am not referring to to the symbol on the chart. This is part of a spread indicator using two symbols.

              Comment


              • #8
                Just a tip. If you post a small test script with some debugPrintln() statements then I could insert MSFT and IBM instead of those symbols you're using (which I probably don't have access to) and then I could help you without taking too much of my time in the process. You attract more free help (other users) if you make it easy for them to "load and test".

                Do that, and I'll be over your post to give you a second opinion instead of you waiting for days in frustration for the hired staff to show up.

                Comment


                • #9
                  Steve, very kind of you and very much appreciated. Here is a short script. My goal is to return the previous close of the daily.

                  PHP Code:

                  var xClosenull;

                  function 
                  preMain() {
                    

                      
                  setPriceStudy(false);
                      
                  setStudyTitle("TCL1530");
                      
                  setCursorLabelName("TCL1530"0);
                      
                  setDefaultBarStyle(PS_SOLID0);
                      
                  setDefaultBarFgColor(Color.red0);
                      
                  setDefaultBarThickness(20);
                      
                  setPlotType(PLOTTYPE_LINE0);


                  }



                  function 
                  main() {
                    
                      
                  xClose close(-1,sym("ES H2,D"));

                       
                      
                    
                  debugPrintln"The current close value is: " xClose ); 




                  Comment


                  • #10
                    I took your script and modified it below to the smallest test case. You only care about the prior day of some other symbol when you're finished with the historical data load and you're on the last bar of your chart, right? Then, you don't want to check it anymore for the rest of the day if you're on an intraday chart.

                    [note: On the weekend, you'll get Thursday's close because close(0) is still Friday]

                    On eSignal 10.6, I get the following output (which is what you would expect):

                    The current close value is: 1315.25

                    On eSignal 11.3 SP1, the debug output is:

                    The current close value is: 1315.25
                    The current close value is: null

                    Huh??? I am pulling this data early Sat morning at 3:00 AM EST. isLastBarOnChart() should only return true ONCE. Instead, it is returning true TWICE and, on the second pass, you're getting the value for the prior close that you're expecting.

                    Why is 11.3 SP1 doing this?

                    PHP Code:
                    var xClosenull;
                    function 
                    preMain()
                    {
                      
                    setPriceStudy(true);
                      
                    setStudyTitle("TCL1530");
                      
                    setCursorLabelName("TCL1530"0);
                    }
                    function 
                    main()
                    {
                      if (
                    xClose == null && isLastBarOnChart())
                      {
                        
                    xClose close(-1,sym("ES H2,D"));
                        
                    debugPrintln"The current close value is: " xClose);
                      }

                    Comment


                    • #11
                      Steve, thanks for pointing out it worked on 10.6, here I was tearing my hair out trying to get the code right when it was right all along...

                      I use this as part of an indicator in real time on an intraday chart so it needs to return the most recent Daily close every bar.

                      The weird thing though is it is now 9pm so I have todays closing price, but instead of changing back to fridays closing price at 15:30 it keeps showing todays close back until 9am this morning. I get Fridays from the close on Friday all the way back to Midnight thursday night.....

                      Comment


                      • #12
                        FWIW with regards to the script executing two passes and returning null on the first the same behavior can occur in 10.6 [and in any version prior to that one that uses efs2] as you can see in the following image so it is not new to 11



                        The reason is explained in the following quote [from Dion Loy who developed efs2 in the first place]
                        <SNIP>Every time there is an efsExternal() or efsInternal() call (or actually any time that a call is made to a builtin study on another symbol or interval), the main EFS has to stop execution, and queue a delayed execution. The engine then goes ahead and executes the called efs or efsInternal, or other symbol/interval FIRST, and then re-executes the original main EFS
                        This is the only way in which we can manage the dependencies... as the main EFS depends on those other calls.<SNIP>
                        That said there is a difference between 10.6 and 11 when reloading the script or loading it after a chart for the same barset [ie symbol and interval] was already created. As I understand it this is due to how version 11 handles barset caching.
                        Unlike version 10.6 which uses only global barset caches [which are not destroyed unless you reset the DB or restart the application or exceed the defined number of barsets] version 11 instead creates in each chart an additional local barset cache for all the studies.
                        When a script is reloaded this local barset cache is destroyed [unless it is invoked within the same chart by another study] at which point the variable will be null on the first call and return instead a value on the second call
                        The barset cache instead remains active if there is another study within the same chart that uses it. You can easily verify this by running a second instance of the script in the same chart in which case you should see that it will not return null but will instead return a value [since it is now using the same barset cache as the first instance of the study]
                        Conversely if you open a second chart and load in it the same study this will return null on the first call and a value on the second. This is because this chart is using a different barset cache than the other chart.
                        Alex

                        Comment


                        • #13
                          Alexis, thanks for the post.
                          Unfortunately Im still below slime on the ladder of evolution in regards to writing script in Esignal.....

                          What I know is that the following returns the close in 10.6, but returns null in 11.0

                          xClose = close(-1,sym("TCL M2-TCM,D"));


                          And also when I use this expression to retrieve the close for that day in 10.6, it returns that day's close back to midnight the day before. I'M just trying to get the most current close from the daily chart to display correctly on an intraday chart in v11. Any pointers as to where I am going wrong and how I can fix this would be much appreciated.

                          Thanks!!
                          Last edited by maninjapan; 01-30-2012, 10:20 AM.

                          Comment


                          • #14
                            It does work on v11. Look at the modified sample I posted.

                            You have to pass over (ignore) the first null value you encounter when isLastBarOnChart() returns true and then take its value on the 2nd pass through that if() statement.

                            You'll have to hold off on everything else which depends upon it in your script until you get that non-null value back.

                            Comment


                            • #15
                              maninjapan
                              As Steve said it does work in version 11.
                              Another way to do this [which would avoid having to hold off for the second value and more importantly is more efficient] is to initialize the series at BARSTATE_ALLBARS [which will still execute two times - for the reason I explained in my previous post – but that will be in effect transparent to you] and then retrieve its value for the prior bar using the getValue() method of the series object
                              See the enclosed image for an example of this [note that the image was captured after loading the study on the chart and as you can see it returned only one print with the appropriate value in the Formula Output Window].
                              Alex

                              Comment

                              Working...
                              X