Announcement

Collapse
No announcement yet.

getCurrentBarIndex

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getCurrentBarIndex

    I only want my trading system to make trades on current data not old data so I added a condition:

    if(getCurrentBarIndex() == 0) {

    This stopped my system from working. I switched the value to -1 and it started working again. What's going on? Have I just built a delay into my system? Why wouldn't it work with 0? Any ideas?

    Thanks.

  • #2
    Hey Underdog, can you post the formula so we can learn more and help?

    thx.m.
    Matt Gundersen

    Comment


    • #3
      OK, here's the whole formula. It's a little embarassing since it's such a simple system (it's the simplest system that I could conceive of I'm using it as my benchmark) and I am not a programmer. As long as you're looking at it, please let me know if you notice anything wrong. For example, why the bar colors don't come out right (they start off by a bar but as the day progresses they become completely out of whack even though the trading portion does fine). As I said earlier, the system works now that I set getCurrentBarIndex to -1.

      Thanks.

      // "OP DO" - Opening Pivot Trading System Using DynaOrder

      // Declaration of DOTS5.DLL (DynaOrder)
      var dots5 = new DLL ("dots5.dll");

      // Description of the symbol to trade:
      var symbolName = "SPY"; // Symbol Name (Stock or Futures root)
      var symbolType = "STK" // Symbol Type
      var symbolExpiry = ""; // Expiration (for Futures and Options)
      var rightInfo =""; // Right (for Options - e.g., put/call)
      var strikePrice = 0; // Strike (for Options)
      var eXchange = "SMART"; // Exchange (Order Routing)
      var cUrrency = ""; // Currency (for CASH)

      var lastOrderID = 0;
      var execPrice = 0;
      var toOpen = 0;
      var OpenPos = 0;
      var vHour = 0;
      var vMinute = 0;
      var vDTime = 0;

      OCheck = 0;
      PCheck = 0;

      function preMain() {

      setStudyTitle("OP DO D");
      setPriceStudy(true);
      setColorPriceBars(true);
      setDefaultPriceBarColor(Color.black);
      setComputeOnClose(true); // Only calculates at the close - not on every tick

      // Host Information Functions
      dots5.addFunction ("IsHostRunning", DLL.INT, DLL.STDCALL, "ISHOSTRUNNING");
      dots5.addFunction ("IsHostConnected", DLL.INT, DLL.STDCALL, "ISHOSTCONNECTED");

      // Order Execution Functions
      dots5.addFunction ("BuyMarket", DLL.INT, DLL.STDCALL, "BUYMARKET",
      DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.FLOAT, DLL.STRING, DLL.STRING, DLL.SHORT);
      dots5.addFunction ("SellMarket", DLL.INT, DLL.STDCALL, "SELLMARKET",
      DLL.STRING, DLL.STRING, DLL.STRING, DLL.STRING, DLL.FLOAT, DLL.STRING, DLL.STRING, DLL.SHORT);

      // Order Information Functions
      dots5.addFunction ("GetOrderPrice", DLL.FLOAT, DLL.STDCALL, "GETORDERPRICE", DLL.INT);
      dots5.addFunction ("GetOrderStatusN", DLL.SHORT, DLL.STDCALL, "GETORDERSTATUSN", DLL.INT);

      }

      function main(vStartHour, vStartMin, vEndHour, vEndMin) {

      var vTime = getValue("Time");

      // Trading Time Defaults
      if (vStartHour == null) {
      vStartHour = 07;
      }
      if (vStartMin == null) {
      vStartMin = 0;
      }
      if (vEndHour == null) {
      vEndHour = 13;
      }
      if (vEndMin == null) {
      vEndMin = 0;
      }

      // Setup Time Stamp
      if(getHour() < 10) {
      vHour = "0" + getHour();
      } else {
      vHour = getHour();
      }
      if(getMinute() < 10) {
      vMinute = "0" + getMinute();
      } else {
      vMinute = getMinute();
      }

      vDTime = vHour + ":" + vMinute;

      // Identify the Opening Pivot
      if(vTime != null) {
      var vFirstIndex =getFirstBarIndexOfDay(vTime);
      if(vFirstIndex != null) {
      vTodaysOpen = getValueAbsolute("open", vFirstIndex);
      }
      }

      // Trading Time Conditions
      if ( ((getHour() > vStartHour) && (getHour() < vEndHour)) ||
      ((getHour() == vStartHour) && (getMinute() >= vStartMin)) ||
      ((getHour() == vEndHour) && (getMinute() <= vEndMin)) ) {

      // Set Buy and Sell Conditions
      if( (close()>vTodaysOpen) && (close() > high(-1)) )
      toOpen = 1; // Buy (Long)
      if( (close()<vTodaysOpen) && (close() < low(-1)) )
      toOpen = -1; // Sell (Short)

      // Color Price Bars
      if(toOpen == 1)
      setPriceBarColor(Color.green);
      if(toOpen == -1)
      setPriceBarColor(Color.red);

      // Check Host Status
      var host = dots5.call ("IsHostRunning");
      var conn = dots5.call ("IsHostConnected");

      if(getCurrentBarIndex() == -1) {

      if(host == 0 || conn == 0) {
      debugPrintln(vDTime + " Problem with Dynaorder Host");
      }

      if((OpenPos == 0) && (toOpen == 1)) { // System is Flat and Signal is to Buy
      OpenPos = 1;
      lastOrderID = dots5.call("BuyMarket", "SPY", "STK", "", "", 0, "SMART", "", 100);
      debugPrintln(lastOrderID + " " + vDTime + " Long @ " + close());
      }

      if((OpenPos == 0) && (toOpen == -1)) { // System is Flat and Signal is to Sell
      OpenPos = -1
      lastOrderID = dots5.call("SellMarket", "SPY", "STK", "", "", 0, "SMART", "", 100);
      debugPrintln(lastOrderID + " " + vDTime + " Short @ " + close());
      }

      if((OpenPos == 1) && (toOpen == -1)) { // System is Long and Signal is to Sell
      OpenPos = -1
      lastOrderID = dots5.call("SellMarket", "SPY", "STK", "", "", 0, "SMART", "", 200);
      debugPrintln(lastOrderID + " " + vDTime + " SAR Short @ " + close());
      }

      if((OpenPos == -1) && (toOpen == 1)) { // System is Short and Signal is to Buy
      OpenPos = 1
      lastOrderID = dots5.call("BuyMarket", "SPY", "STK", "", "", 0, "SMART", "", 200);
      debugPrintln(lastOrderID + " " + vDTime + " SAR Long @ " + close());
      }

      if((OpenPos == 1) && (vTime.getHours() == 13)) { // System is Long and Time is EOD
      OpenPos = 0
      lastOrderID = dots5.call("SellMarket", "SPY", "STK", "", "", 0, "SMART", "", 100);
      debugPrintln(lastOrderID + " " + vDTime + " EOD Close @ " + close());
      }

      if((OpenPos == -1) && (vTime.getHours() == 13)) { // System is Short and Time is EOD
      OpenPos = 0
      lastOrderID = dots5.call("BuyMarket", "SPY", "STK", "", "", 0, "SMART", "", 100);
      debugPrintln(lastOrderID + " " + vDTime + " EOD Close @ " + close());
      }

      } // End Bracket Current Bar Index Limitation

      // Get Execution Price
      if(dots5.call("GetOrderStatusN", lastOrderID) == 4) {
      execPrice = dots5.call ("GetOrderPrice", lastOrderID);
      if(lastOrderID!=OCheck && execPrice!=PCheck) {
      debugPrintln(lastOrderID + " " + vDTime + " Executed @ " + execPrice);
      OCheck = lastOrderID;
      PCheck = execPrice;
      }
      }

      } // End Bracket Time Limitation
      } // End Bracket Main

      Comment


      • #4
        The problem is that you use setComputeOnClose(true) in the preMain() function.

        As the current bar closes, your formula is called, but the bar for which the calculations are made already has a bar number of -1.

        So you need to use if(getCurrentBarIndex() == -1) if you want to run the Formula on the bar closes only.

        DB

        Comment


        • #5
          Thank you very much. I wasn't too worried since the system did work but I feel much better understanding the problem now. Unless I hear a good reason why I should keep the compute on close, I'll get rid of it (I think it was causing a delay in getting the execution price anyway).

          If anyone can see why the colors aren't right, I'd still like to know about that.

          Thanks.

          Comment


          • #6
            Hi,


            I think if you get rid of setComputeOnClose() in you premain a 0 value will work on you getCurrentBarIndex() check. I think the issue here is that you are only calculating on the close of the current (therefore 0) bar, but if that bar will be -1 once your formula runs. Matt - correct me if I'm wrong.

            I do have some general comments:

            You have statements like this:

            if(vTime != null) {
            var vFirstIndex =getFirstBarIndexOfDay(vTime);
            if(vFirstIndex != null) {
            vTodaysOpen = getValueAbsolute("open", vFirstIndex);
            }

            But you never check to see if vTodaysOpen is valid. In general, since your whole system depends on vTodaysOpen being valid and since that depends on vFirstIndex being valid, which in turn depends on vTime being valid, I would bail out of the study on a null return of any of these. The way it is now, you could be getting into the heart of your study with a bad value for vTodaysOpen.

            Next, (Unless I'm missing something) it looks like you really don't care about anything but the most current bar. If so, why not move the check for currentBarIndex() to the top of the formula, and save CPU time for all those times that contidtion isn't ture.
            Garth

            Comment


            • #7
              1) yup, setComputeOnClose() and getCurrentBarIndex() issue makes sense

              2) "In general, since your whole system depends on vTodaysOpen being valid and since that depends on vFirstIndex being valid, which in turn depends on vTime being valid, I would bail out of the study on a null return of any of these."

              I pulled that straight out of the reference guide. Can you show me what you mean? I'm really unclear with the whole return thing.

              Do you think this is why the color gets screwed up? If so, then why is the order generation section unaffected?

              3) I've been moving getCurrentBarIndex() around as I figure out how things work. I have it on the order generation section since that's where I had to have it. It's nice to be able to see how the system did on old data but I will keep in mind the processing cost.

              Thanks.

              Comment


              • #8
                I pulled that straight out of the reference guide. Can you show me what you mean? I'm really unclear with the whole return thing.
                The issue withnot exiting on an invalid return from a function is that you can get unexpected results on your conditionals, and any calculations done on those returns.

                In this case, I would check for null returns for vTime, vFirstIndex and vTodaysOpen. So it would look more like:

                var vTime = getValue("Time");
                if(vTime == null)
                return;
                var vFirstIndex = getFirstBarIndexOfDay(vTime);
                if(vFirstIndex == null)
                return;
                vTodaysOpen = getValueAbsolute("open", vFirstIndex);
                if (vTodaysOpen == null)
                return;

                This "protects" the heart of the formula, and should prevent some problems. I'm not sure if it will fix the color bar error, but I have seen some very odd results from not doing the above checks.
                Garth

                Comment

                Working...
                X