Announcement

Collapse
No announcement yet.

Reset on FirstBarOfDay

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

  • Reset on FirstBarOfDay

    Would someone please see if the following will set

    Start=0;

    on the first tick of the first bar of the day, regardless of the time interval and time template settings? Thanks.

    function preMain(){
    setPriceStudy(true);
    setStudyTitle("FBreset");
    debugClear();
    }
    var Start=null;

    function main(){

    var vTime = getValue("Time");
    if(vTime != null&&getBarState()==BARSTATE_NEWBAR&&getCurrentBa rIndex()==0) {
    var vFirstIndex = getFirstBarIndexOfDay(vTime);
    if(vFirstIndex == 0) {
    debugPrintln("Start=0;"+vFirstIndex);
    Start=0;
    }
    }
    }

  • #2
    Hi David,

    This should work for you.

    PHP Code:
    var Start 0;

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("FBreset");
        
    debugClear();
    }

    var 
    Start=null;

    function 
    main() {   //returns first bar of day in real time as the market opens.
        
    var vTime getValue("Time");
        var 
    vFirstIndex null;
        if(
    vTime != null && getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == 0) {
            
    vFirstIndex getFirstBarIndexOfDay(vTime);
        }
        if(
    vFirstIndex != null && getCurrentBarIndex() == 0) {
            
    debugPrintln("Start=0;  " vFirstIndex);
            
    Start 0;
        }
        return;

    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


    • #3
      here is what I have finally ended up with to reset my program at midnight, taking advantage of the time stamps. If the time of the current bar is < than the previous, which happens at midnight, reset eveything.

      var cBarTimeInt;
      var cBarTimeIntOld;
      var start=0;

      function main(){

      if(getBarState() == 0
      && getCurrentBarIndex() == 0)
      {
      BarHr = getHour();
      BarMin = getValue("Minute");
      BarSec = getValue("Second");

      cBarTimeInt =CurHours*10000+CurMinutes*100+CurSeconds*1;
      }

      if(start==0){ //this will do a reset when the efs is first invoked
      Xstart(); //this routine holds all the restart stuff and sets start=1;
      }
      //the following line will do a reset around midnight
      if(cBarTimeInt < cBarTimeIntOld && getCurrentBarIndex() == 0){
      Xstart(); //this routine holds all the restart stuff and sets start=1;
      }

      cBarTimeIntOld=cBarTimeInt; //save the time of the current bar for later

      //the rest of the efs goes here
      }

      function Xstart(){
      //goes here
      }

      Comment


      • #4
        I was trying to find something similar to this for a backtesting routine I was working on. What I ended up with may not have been ideal for my purposes, but I did come up with a simple end of day detector

        function preMain()
        var tDay;

        function main()
        tDay = getDay(0,-2);
        if(tDay[0] != tDay[1]) {... whatever needs to be done

        I'm still a bit fuzzy on the indexing business.. why do I set the length of the getDay to MINUS 2, and then index the data as [0] and [PLUS 1]?

        What I was after is a way to close a position at the close of the last bar of a day, and open a new position on the last of a series of bars the next day having the same time stamp (not always the first bar of the day- like for Renko chart on a gap day, or tick charts) I settled for closing positions when my day condition was met, unfortunately at the open of the first of the next day bars (works OK for Renko) and opening a new one on the first bar AFTER 9:30.

        Have you done anything similar I could look at that will work on any interval based chart??

        Comment


        • #5
          Hello Dan,

          The index values start with -XXX for the oldest bar loaded in your chart and increment towards 0, which is most recent bar. Bar index of 0 is today, bar index of –1 is yesterday, -2 is two days ago etc.

          When you set tDay = getDay(0, -2), you will get an array of data, which has a size or length of 2. Arrays are always 0 based where array[0] is the first element of the array. It seems like reverse logic, which can play tricks on your mind. You can do a couple of things to help yourself keep it straight. Try reversing the data in the array. If you like to think of array[0] as the oldest day, then do this: tDay = getDay(-1, 2). Now, tDay[0] = -1 bar (or day) ago and tDay[1] = the current bar (or bar index of 0).

          Personally, I prefer the method you are using in your example. I like to think of tDay[0] as the current bar and tDay[1] as bar index of –1.

          Another option here is to avoid using an array.
          var tDayToday = getDay();
          var tDayYesterday = getDay(-1);
          if (tDayToday != tDayYesterday) { … }


          To get your back testing strategy to exit a position at the close of the last bar of the day you can look at the next bar to check for the last bar. This will only work for back testing and not real time indicators of course.

          If (getCurrentBarIndex() == 0) return; // no point in checking for a bar that doesn’t exist yet.

          var tDayToday = getDay();
          var tDayTomorrow = getDay(1);
          if (tDayToday != tDayTommorrow) {
          Strategy.doSell(“Sell”, Strategy.CLOSE, Strategy.THISBAR);
          }

          These examples will all work with interval charts such as 5 minutes etc.
          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


          • #6
            Thanks Jason,

            I would prefer to have the current day index be zero as well. I was just groping for a way to look ahead to the NEXT day for the comparison. I guess that if I use tDay = getDay(0,2) I will get the current bar as tDay[0] and the FOLLOWING bar as tDay[1]?? I recognized the fallacy of trying to do that in real time, and had not taken the trouble to write a different formula for bactesting than the one for the real time chart. I'll give this a try.

            Comment

            Working...
            X