Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

GetBar from week open

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

    GetBar from week open

    Hi guys!

    I'd like to draw a line from (week open and month open) bar to currentBar.

    I use with this to get bars from daily open...

    barsAgoDailyOpen = CurrentBar - Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day));

    How can I get from week and month open bar to now.
    Which is the easiest and simplest solution?

    many thx
    w.

    #2
    Weekly Open:

    You would need to check for the date of the current trading day. If there is a new date (first bar of new trading day) check whether it is a Monday. If it is a Monday store the value of the CurrentBar and you can use it to draw your line from the open.

    Code:
    #region Variables
    private DateTime  currentDate = Cbi.Globals.MinDate;
    private DateTime  lastDate = Cbi.Globals.MinDate;
    private int weeklyOpen = 0;
    ........
    #endregion
    
    protected override void OnBarUpdate()
    {
           currentDate = Bars.GetTradingDayFromLocal(Time[0]);
           if(currentDate > lastDate)
            { 
                 if(currentDate.DayOfWeek == DayOfWeek.Monday) 
                      weeklyOpen = CurrentBar;
                 lastDate = currentDate;
             }
    }
    For a monthly open just check whether the current date is the first business day of a month.

    The indicator is available in the downloads (search for SessionPivots V42), you can always try to copy parts of the code.
    Attached Files
    Last edited by Harry; 07-05-2013, 03:55 PM.

    Comment


      #3
      Thanks Harry!

      Its a bit confuse for me. Im newbie...
      Is there any easiest way?

      Meanwhile i try to do your way....

      Comment


        #4
        Originally posted by wokitun View Post
        Thanks Harry!

        Its a bit confuse for me. Im newbie...
        Is there any easiest way?

        Meanwhile i try to do your way....
        This is only 5 simple lines of code in OnBarUpdate() to collect the index of the bar for the weekly open. I would consider this pretty simple!

        Comment


          #5
          Ok...
          What happen if Monday is not trading day //celebrate..market close...// as a result of this not weekly open that day.
          What i find.. .If weekly first day starts at Tuesday...draw line begins from Tuesday...

          example...
          My result at Monthly open.

          barsAgo = CurrentBar - Bars.GetBar(Time[0].AddDays(-Time[0].Day));

          Not the best and properly solution...

          Or I understand something wrong?

          thx
          w.

          Comment


            #6
            Originally posted by wokitun View Post
            Ok...
            What happen if Monday is not trading day //celebrate..market close...// as a result of this not weekly open that day.
            What i find.. .If weekly first day starts at Tuesday...draw line begins from Tuesday...

            example...
            My result at Monthly open.

            barsAgo = CurrentBar - Bars.GetBar(Time[0].AddDays(-Time[0].Day));

            Not the best and properly solution...

            Or I understand something wrong?

            thx
            w.
            You are absolutely right. Easiest solution is to convert the DayOfWeek to an integer. Sunday has the value 0, Saturday has the value 6. So you could check for
            (int)Time[0].DayOfWeek to decrease. The bar where that value decreases marks the beginning of the week, whatever day of week it is.
            Last edited by Harry; 07-06-2013, 03:38 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Waxavi, Today, 02:10 AM
            0 responses
            5 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            11 views
            0 likes
            Last Post TradeForge  
            Started by Waxavi, Today, 02:00 AM
            0 responses
            2 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by elirion, Today, 01:36 AM
            0 responses
            4 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by gentlebenthebear, Today, 01:30 AM
            0 responses
            5 views
            0 likes
            Last Post gentlebenthebear  
            Working...
            X