Announcement

Collapse
No announcement yet.

Last 60-Minute Bar

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

  • Last 60-Minute Bar

    I have some custom studies, and some built-in studies, running on advanced chart(s). I noticed that the intraday and DWM charts all plot a value for the last bar -- except when you select 60-minutes as the interval. This is because this bar technically isn't a completed bar -- right?

    Here's what I'm doing -- essentially. I can't copy the entire script as it is too long.

    preMain -- setComputeOnClose(false)
    main
    var nState = getBarState()
    switch(nState)
    2=Allbars (just return)
    1=Currentbar(return)
    0=Newbar (do the processing)

    If I reload my custom scripts, they will add a final bar -- but that won't happen if you just click the intervals on the menu bar. The volume script does not add a final bar (basicVolume.efs -- from my eSignal folder) if reloaded on a 60-minute chart. All of them have a final bar if I click the daily time interval. Am I supposed to be processing one last time for the current bar also?

    I checked and close(0) does return the correct close price for the last bar on a 60-minute chart.
    Last edited by AssetHound; 11-04-2006, 01:17 PM.

  • #2
    Hi AssetHound,

    I am not sure I can answer your question directly (i.e. without guessing) as there are many variables that can affect what you are seeing.

    I think that your idea of using a switch statement is pretty innovative. I recommend that you do not use any break statements though. Here is how I configured an efs to use the switch conditionals:

    PHP Code:
    /***********************************
    By Alexis C. Montenegro for eSignal © December 2004    
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a 
    description of any changes you make.           
    ***********************************/
    // modified to use switch statement S.Hare 11 4 2006
    // notice that no break statements are used

    var fpArray = new Array();

    function 
    preMain() {

      
    setStudyTitle("MACD");
      
    setCursorLabelName("MACD",0);
      
    setCursorLabelName("MACDSig",1);
      
    setCursorLabelName("MACDHist",2);
      
    setDefaultBarFgColor(Color.blue,0); 
      
    setDefaultBarFgColor(Color.red,1);
      
    setDefaultBarFgColor(Color.magenta,2); 
      
    setPlotType(PLOTTYPE_LINE,0);
      
    setPlotType(PLOTTYPE_LINE,1);
      
    setPlotType(PLOTTYPE_HISTOGRAM,2); 
      
    setDefaultBarThickness(1,0);
      
    setDefaultBarThickness(1,1);
      
    setDefaultBarThickness(1,2);
      
    askForInput();
      
      var 
    x=0;
      
    fpArray[x] = new FunctionParameter("Fast"FunctionParameter.NUMBER);
     
    with(fpArray[x++]){
        
    setLowerLimit(1);  
        
    setDefault(12);
      }
     
    fpArray[x] = new FunctionParameter("Slow"FunctionParameter.NUMBER);
     
    with(fpArray[x++]){
        
    setLowerLimit(1);  
        
    setDefault(26);
      }
     
    fpArray[x] = new FunctionParameter("Smoothing"FunctionParameter.NUMBER);
     
    with(fpArray[x++]){
        
    setLowerLimit(1);  
        
    setDefault(9);
      }
     
    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
     
    with(fpArray[x++]){
        
    addOption("open"); 
        
    addOption("high");
        
    addOption("low");
        
    addOption("close");
        
    addOption("hl2");
        
    addOption("hlc3");
        
    addOption("ohlc4"); 
        
    setDefault("close"); 
      }
      
    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
      
    with(fpArray[x++]){
        
    setDefault();
     }
     
    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
     
    with(fpArray[x++]){
        
    setDefault();
      }
      
    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
     
    with(fpArray[x++]){
        
    setName("Show Parameters");
        
    setDefault(false);
      }
    }

    var 
    bInit false;
    var 
    xMACD null;
    var 
    xMACDSig null;
    var 
    xMACDHist null;

    function 
    main(Fast,Slow,Smoothing,Source,Symbol,Interval,Params){
     
     switch(
    getBarState()){
      case (
    BARSTATE_ALLBARS): // script is initializing
       
      
    if(Symbol == nullSymbol getSymbol();
      if(
    Interval == nullInterval getInterval();
      var 
    vSymbol Symbol+","+Interval;
      
    xMACD macd(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
      
    xMACDSig macdSignal(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
      
    xMACDHist macdHist(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
      
    addBand(0,PS_SOLID,1,Color.black,"Centerline");
      
    setShowTitleParameters(eval(Params));
      
      
    //end case (BARSTATE_ALLBARS): // script is initializing // do not use break;
      
      
    case (BARSTATE_NEWBAR): //the first tick of a new bar has arrived 
      
      // end case (BARSTATE_NEWBAR): //the first tick of a new bar has arrived // no break
      
      
    case (BARSTATE_CURRENTBAR):// a new tick has arrived in the current bar 
      
      
    return new Array (getSeries(xMACD),getSeries(xMACDSig),getSeries(xMACDHist));
      
      
    // end case (BARSTATE_NEWBAR): //the first tick of a new bar has arrived // no break
     
    }

    I ordered this a bit differently than what you described and did some testing to verify the output matches the original efs.

    See if there is any difference in output here from what you are seeing, perhaps therein lies the answers you seek.

    Comment


    • #3
      stevehare,

      This is interesting. I do all my processing under newbar. Doesn't the script you wrote process every tick? It works great, however.

      I wanted to avoid processing under currentbar just because of possible processor(s) overload. Seeing what you sent, I'll have to review my code again.

      Something is weird. In running both codes again, I see that I'm getting a final bar on all time intervals -- but it disappears quickly after it is plotted. The script seems to be making "one last pass" and comes up with some zero or NaN's so it recalcs the bar and comes up with nothing -- which is what I'm getting. On reloading, the bar shows up.

      I guess we're going to have to call this a logic error on the part of the programmer. This really helped isolate things. Thanks.

      AH
      (I guess I need to come up with new initials also)

      --------------

      [QUOTE]Originally posted by stevehare2003
      [B]Hi AssetHound,

      I am not sure I can answer your question directly (i.e. without guessing) as there are many variables that can affect what you are seeing.

      I think that your idea of using a switch statement is pretty innovative. I recommend that you do not use any break statements though. Here is how I configured an efs to use the switch conditionals:

      Comment


      • #4
        This script has the same problem we've been talking about.

        function preMain() {}

        function main() {
        var nState;
        var prceNow;
        nState = getBarState();
        if (nState == BARSTATE_ALLBARS) {
        //the bars are being loaded by the script. This happens when a script is first loaded
        debugPrint("Script is loading\n");
        }
        else if (nState == BARSTATE_NEWBAR) {
        //this flag is set when a new bar is coming in
        prceNow = Math.round(high()*100) / 100;
        debugPrint("The first tick of a new bar has arrived -- " + prceNow + "\n");
        return prceNow;
        }
        else if (nState == BARSTATE_CURRENTBAR) {
        //this flag is set as each new tick comes in
        debugPrint("A new tick has arrived\n");
        }
        }

        When it first loads, all is fine. Here is the last line printed by the above script.

        The first tick of a new bar has arrived -- 45.9

        This is the correct price for the bar shown. If you click on "daily" again, the last plot from the above is dropped. Here are the last two lines printed.

        A new tick has arrived.
        The first tick of a new bar has arrived -- 45.9

        I'm not clear on why this happens. It happens on any time interval selected. Any help will be appreciated.

        Comment


        • #5
          Hi AH,

          The chart is drawn to every tick. You are drawing at every new bar, which is the first tick after the end of the last bar.

          So, the way it is designed is that as every tick before the new bar comes in, it draws to the chart (as did every other tick starting with the last newbar), each time overwriting what had been drawn as the bar was developing.

          When a new bar comes in, the chart advances 1 bar (no longer overwriting the last point drawn on the chart, and draws the new point starting the cycle all over again.

          Your issue seems to be that you return data to the chart that very first tick of a new bar, thereafter, as the bar is developing, you are returning nothing back to the chart, overwriting your initial return with basically a null. This overwrites your first drawn point in the current bar (with nothing), resulting in what you are seeing.

          This is why the numerous efs's on your computer and available ones on the forum are written as they are (and why I had formatted the select structure the way I did). This ensures something is returned to the chart every tick, regardless of the barstate.

          I hope this helps.

          Comment


          • #6
            Hello AssetHound,

            Steve has accurately described why the plot is disappearing on the current bar due to the local declaration of your prceNow variable. Try moving that declaration outside of main() to the global scope. This should solve the problem. Also make sure you're not using setComputOnClose() in preMain().
            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


            • #7
              Thanks, Jason and Steve. I did not know that the bar was being redrawn on every tick and thought it was only redrawn when a return statement was encountered. This helps a lot!
              ---------------

              Originally posted by JasonK
              Hello AssetHound,

              Steve has accurately described why the plot is disappearing on the current bar due to the local declaration of your prceNow variable. Try moving that declaration outside of main() to the global scope. This should solve the problem. Also make sure you're not using setComputOnClose() in preMain().

              Comment

              Working...
              X