Announcement

Collapse
No announcement yet.

What's correct syntax to get futures contract open interest using the oi() function?

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

  • What's correct syntax to get futures contract open interest using the oi() function?

    Hello,
    What's the correct way to format a call to oi() to get the open interest of a specific futures contract?
    For example, let's say I load a chart of continuous S&P eMini futures, symbol "ES #F"
    I then want, via efs, to get the latest open interest value for some other contract, say June 2016 Eurodollars, symbol "GE M6".

    I've tried the following to accomplish this (from a script added to a chart of ES #F), none of which work:
    theSymbol = "GE M6";
    debugPrintln("1) " + oi(0, theSymbol) );
    debugPrintln("2) " + oi(0, sym(theSymbol)) );
    debugPrintln("3) " + oi(theSymbol) );
    debugPrintln("4) " + oi(sym(theSymbol)) );

    Any thoughts?
    Thanks

  • #2
    The last one is what works. You need to give oi() a Symbol object, which is resolved via sym().

    Comment


    • #3
      Actually both the second and the fourth examples will work (one returning the current value and the other the series object)
      See this article in the EFS KnowledgeBase for a complete set of examples on the use of the oi() function
      Alex

      Comment


      • #4
        Hmmm --- When I execute the following code in an EFS indicator script:
        theSymbol = "GE M16";
        try {
        debugPrintln("1) oi(0, GE M16)=" + oi(0, sym(theSymbol)) );
        } catch (err) {
        debugPrintln("1) err: "+err);
        }
        try {
        debugPrintln("-2) oi(GE M16) =" + oi(sym(theSymbol)) );
        } catch (err) {
        debugPrintln("2) err: "+err);
        }

        I get the following in the Formula Output window:
        2) err: TypeError: sym is not a function
        1) err: TypeError: sym is not a function

        What am I missing. Hopefully it's something simple. Any thoughts?

        Comment


        • #5
          rlewkov
          As I said both examples will work (see following screenshots)





          As suggested in the guidelines for this forum you should post a complete script so that others who may want to help can try to do that without having to guess what else you may be doing
          Alex


          Originally posted by rlewkov View Post
          Hmmm --- When I execute the following code in an EFS indicator script:
          theSymbol = "GE M16";
          try {
          debugPrintln("1) oi(0, GE M16)=" + oi(0, sym(theSymbol)) );
          } catch (err) {
          debugPrintln("1) err: "+err);
          }
          try {
          debugPrintln("-2) oi(GE M16) =" + oi(sym(theSymbol)) );
          } catch (err) {
          debugPrintln("2) err: "+err);
          }

          I get the following in the Formula Output window:
          2) err: TypeError: sym is not a function
          1) err: TypeError: sym is not a function

          What am I missing. Hopefully it's something simple. Any thoughts?

          Comment


          • #6
            OK, here's the complete source of my efs which is failing. This started out as a test of reading a writing a file. Then I though I'd read some symbols, get a series value and write it out in order to learn about how series work. I picked oi() for no particular reason. Then things didn't work so there must be something I'm fundamentally not understanding about how/when to call sym(), etc. The goal of the script is to read a symbol from a file, get its latest OI value and write it out.


            var bInit=false;
            function main() {
            var symbol, openInterest, iLen, i, sym, contract, bFound=false;
            var inFile, outFile;
            if (!bInit) {
            debugPrintln(">>Start");
            bInit=true;
            inFile = new File("oisymbols.txt");
            outFile = new File("openinterest.txt");

            inFile.open("rt");
            outFile.open("wt+");

            while(!inFile.eof()) {
            theSymbol = inFile.readln();
            debugPrintln("theSymbol="+theSymbol);
            aValue = 0;
            aSeries = null;
            try {
            aValue = oi(0, sym(theSymbol));
            } catch (err) {
            debugPrintln("1) err: "+err);
            }
            try {
            aSeries = oi(sym(theSymbol));
            } catch (err) {
            debugPrintln("2) err: "+err);
            }
            }
            inFile.close();
            outFile.close();
            }
            return(1);
            }

            The output window shows:
            2) err: TypeError: sym is not a function
            1) err: TypeError: sym is not a function
            theSymbol=ZS X6
            2) err: TypeError: sym is not a function
            1) err: TypeError: sym is not a function
            theSymbol=GE M6
            >>Start

            Comment


            • #7
              Originally posted by rlewkov View Post
              var symbol, openInterest, iLen, i, sym, contract, bFound=false;
              ^^^

              Classic mistake. You've just overridden the local scope to declare "sym" as a variable, hence it's not going to resolve the function named sym. Call that variable something else or just get rid of it because you don't even use it.

              Comment


              • #8
                Duhhhhh. Thanks for the catch. I've been staring at that for many hours. I've pared down my script a bit. There's still some unexpected (at least by me) results.

                var bInit=false;
                function main() {
                var symbol, openInterest, iLen, i, contract, bFound=false;
                var inFile, outFile, aValue;
                if (!bInit) {
                debugPrintln(">>Start");
                bInit=true;
                inFile = new File("oisymbols.txt");
                outFile = new File("openinterest.txt");

                inFile.open("rt");
                outFile.open("wt+");

                while(!inFile.eof()) {
                theSymbol = inFile.readln();
                debugPrintln("theSymbol="+theSymbol);
                aValue = oi(0, sym(theSymbol));
                debugPrintln(theSymbol+","+aValue);
                }
                inFile.close();
                outFile.close();
                }
                return(1);
                }

                The line "aValue = oi(0, sym(theSymbol));" always returns null. The output window shows:
                ZS X6,null
                theSymbol=ZS X6
                GE M6,null
                theSymbol=GE M6
                >>Start

                Questions:
                1) Does sym() return sync or async?
                2) If async how best to know when to be able to get values?
                3) Does a single call to sym("GE M6") returns a series containing N date, o,h,l,c,vol,oi values for GE M6?
                4) If so, what is N? ... whatever the Time template is set to?

                Comment


                • #9
                  Originally posted by rlewkov View Post
                  1) Does sym() return sync or async?
                  2) If async how best to know when to be able to get values?
                  3) Does a single call to sym("GE M6") returns a series containing N date, o,h,l,c,vol,oi values for GE M6?
                  4) If so, what is N? ... whatever the Time template is set to?
                  1. Sync.
                  2. Unlikely you'll see anything async in efs.
                  3. It returns a time series that you access with getValue(). The accessing functions 'open', 'close', 'high', 'oi', 'volume', etc return series relevant to themselves. Only if you precede with a bar-index does it return just that value and not a series. AFAIK there is no generic time series call that you can use to access multi-value row data within using something like "theseries.oi(0)" or "theseries.volume(0)" - you have to use the specific functions to do that.
                  4. N is the interval of the context you're calling the study from. You can force a different interval by using sym(symbol + "," + interval).

                  The issue with your code is that you're trying to do all the file stuff before you've even been passed a time series context from which to operate in. For instance, what does '0' even mean when you're calling this only once (due to bInit being false only once on load).

                  I don't know exactly how you're using the data or how often you want it written or what context you're using it within, but start with this:

                  Code:
                  "use strict;"
                  
                  var inFile = new File("oisymbols.txt");
                  var outFile = new File("openinterest.txt");
                  var symbols = [];
                  
                  function preMain() {
                          inFile.open("rt");
                          while(!inFile.eof()) {
                                  theSymbol = inFile.readln();
                                  if (!theSymbol) break;
                  
                                  debugPrintln(JSON.stringify(['thesymbol',theSymbol]));
                                  symbols.push(theSymbol);
                          }
                          inFile.close();
                  
                          outFile.open("wt+");   // or use a flag or null state of outFile and do this once in main()
                  }
                  
                  function main() {
                          var symbol, openInterest, iLen, i, contract, bFound=false;
                  
                          for (var i = 0; i < symbols.length; i++) {
                                  var aValue = oi(0, sym(symbols[i]));
                                  debugPrintln(JSON.stringify(['avalue',symbols[i],i,aValue]));
                          }
                  
                          return;
                  }
                  
                  function postMain() {
                          outFile.close();
                  }
                  Last edited by i960; 04-19-2016, 07:38 PM.

                  Comment


                  • #10
                    - I was calling oi with 0 once as I wanted to get just the most recent OI value for a symbol - for no particular reason than to figure out how eSignal series work.
                    - In 3) you said "It returns a time series that you access with getValue()". A time series of what?
                    For example, given s=sym("IBM"), what is in s? A time series of some sort I assume? Containing what ... OHLC, etc of IBM?
                    - In 4) when I used the term N I was thinking of N being the number of values in the time series, i.e. its length.
                    In the above (s=sym("IBM")) is "s" a timeseries with multiple values? If so, how many?

                    For example, if I wanted to get the last 365 days of IBM data what would the sym() call look like?
                    And once retrieved how would I access the individual 365 (date,o,h,l,c, etc) values;

                    I'm used to thinking of getting and accessing a time series in a manner such as:
                    ts=getTimeSeries("IBM", <some other parms including maybe timespan (last 100 days, 5 years, etc)>)
                    for (i=0; i<ts.length; ++i) { oVal=ts.oi(i) or ts(i).oi // something like that }

                    It seems that the efs model is a bit different and I'm trying to reconcile the two.
                    I appreciate the time you've taken to explain this to me.** That's all for this evening. Will check back in the morning.
                    Last edited by rlewkov; 04-19-2016, 08:00 PM.

                    Comment


                    • #11
                      Here's the actual context of what I'm thinking I want to do.
                      Let's say I have 1000 futures symbols I want to analyze in order to determine which have specific setups as of the most recent bar.
                      One thousand sounds like a lot but it's not if you're talking about spreads where one could come up with several hundred just considering a single market such as CL.
                      There are 66 pairwise spread combinations per year for any futures market that trades 12 months a year as does CL. (12*11)/2.
                      So consider CL 16,17,18,19,20 calendar spreads. Then 16-17,16-80, etc. Then add in butterfly and condors and you easily have several hundred in CL alone.

                      So my thought is to use efs to read in the set of spread symbols I want to consider as trade possibilities and output those that are setup according to my criteria.
                      This is similar to the eSignal Market Scanner but I can use futures spreads and whatever criteria I want via efs - at least that's my thought.
                      The filter criteria would include those spreads where:
                      a) oi for most recent bar of each contract that makes up the spread is > some threshold.
                      b) a trend filter such as some relationship between MAs (maybe)
                      c) others TBD

                      Market Scanner can't do this but it sounds like it could (maybe) be accomplished using a single efs script that I add to a chart or Watchlist.
                      It might run for a while but in the end it would have reduced my universe of spreads from a lot to a little in a relatively short amount of time.
                      There are probably things I haven't considered that might make this not feasible but so far it sounds doable.
                      Your thoughts?
                      Last edited by rlewkov; 04-20-2016, 09:59 AM.

                      Comment

                      Working...
                      X