• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

date comparison

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

    date comparison

    hello, I need to check whether the date (not time) of a specific bar is equal to the current date. Can you direct me to an example on how to do this? Thanks

    #2
    Hello pman777,

    You can utilize the DateTime methods that are a part of C# from NinjaScript to get the Current day. Once you have todays date you can compare it to the bars date using an if statement in combination with ToDay(Time[0]) for the bars date.
    Here is an example.

    Code:
    int barDay = ToDay(Time[0]);
    int currentDay = ToDay(System.DateTime.Now);
    if(barDay == currentDay)
    {
    //do something here	
    }
    Here is some information on the ToDay method
    http://www.ninjatrader.com/support/h...html?today.htm

    Also here is a link to the MSDN documentation on DateTime
    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    Please let me know if I may be of additional assistance
    JesseNinjaTrader Customer Service

    Comment


      #3
      I have a similar question.

      I want to write an indicator that tracks open trades on the daily timeframe. I'd like to have an Input Parameter for the date the trade was entered, and then draw on the chart based on the entry date.

      Is there a way to convert a date variable into the day of the corresponding bar as it relates to CurrentBar?

      For example, if EntryDate = 07/08/2014 and today is 07/09/2014 a function that converts EntryDate to 1. Of course weekends or holidays would have to be accounted for.

      Thanks in advance.

      Comment


        #4
        Hello Grasul,

        Thank you for the question I will be happy to help.

        Can you please clarify for me what you are trying to accomplish.
        From what you have said I take it that you are trying to get the amount of days between the two dates am I correct?

        For example, if EntryDate = 07/08/2014 and today is 07/09/2014 a function that converts EntryDate to 1. Of course weekends or holidays would have to be accounted for.
        I take it by "a function that converts EntryDate to 1 would mean it takes the amount of days between EntryDate and compares it to today’s date?

        Please let me know and I will be happy to help.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Sure.

          What I'd like to do is create an indicator that took as an input parameter the Entry Date of the trade.

          So, I open a blank chart, choose the security, add this indicator to it and add as an input parameter the Entry Date. Then, presuming I keep the window open, as the days go by the indicator is calculating and plotting, targets, stops, all the stuff you pay attention to with an open trade.

          All I really want to be able to do is to relate a date to a particular Bar on the daily timeframe. So if EntryDate is today, we'd know that Close[0] was the closing price of EntryDate. If EntryDate was 5 trading days ago, Close[5] would have been the closing price that day. Once I can make that relationship, then I can write what I want the indicator to do from there.

          Comment


            #6
            Hello Grasul,

            Thank you for the reply I think I can see what you are trying to do. Let me provide the description of how I read your last post so that you can verify I am reading it right.

            So if EntryDate is today, we'd know that Close[0] was the closing price of EntryDate. If EntryDate was 5 trading days ago, Close[5] would have been the closing price that day.
            So you are trying to use an input to specify a date, and then trying to get the Close value of that day that is specified?
            So you need to convert a Date that is inputted into an int value of the amount of days from today to get the bars ago?

            If this is not correct can you provide a different example, I am just trying to determine what exactly the outcome you are looking for is.

            So far I know that you are trying to create a input that you can select a date.

            Once you have the date input I am looking to find out what exactly you need to do with this date.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Sure, sorry I'm not trying to be deliberately vague.

              I want to input a date. Then I want to be able to relate that date to a specific bar on a daily chart. I only want to enter the date once, but I want it to always be referenced to the same bar as days go by.

              Once I can do this I will do things like this:

              Draw on the chart noting which day(bar) the trade was entered.

              Calculate a target price for the trade and post it on the chart with a line beginning at the entry date bar and extending into the future.

              Calculate a stop price and plot it on the chart beginning at the entry date.

              I could do all of this simply by having my Input Parameter be an integer that equaled the number of days ago the trade was entered. Then the bars ago value could be incremented by one every OnBarUpdate. Using the date would just be a little more elegant; if its a hassle I'll just use a bars ago integer.

              Comment


                #8
                Hello Grasul,
                Thank you for giving more details on this.

                I would additionally like to ask what timeframe you are trying to use this on.

                If this is a day type chart then this would be an easy comparison as you could compare the current bars date to your input date, if the current bars date is equal to the input date then you would know what bar to use.

                This would look something like the following:
                Code:
                if (ToDay(Time[0]) == ToDay(inputDate))
                {
                int barNeeded = CurrentBar;
                }
                inputDate would be the date from your input, barNeeded would be the bar number of the bar that matches your input date. barNeeded would only be set once on a daily chart as there would only be 1 bar containing the input dates value.

                If this is on a less than daily type chart then you would have multiple bars with the same date value.

                in this situation the comparison of dates would still work you would just need to ensure that you do your calculations after this is complete and the final close value is acquired.

                Code:
                if (ToDay(Time[0]) == ToDay(inputDate))
                {
                int barNeeded = CurrentBar;
                }
                if(ToDay(Time[0]) > ToDay(inputDate))
                {
                //run code
                }
                in this situation the code is run and barNeeded is set every time the input date is matched, after the final bar sets barNeeded you would need to check if that was the final bar of that day, to do this I simply check if the current bars date is greater than the input date, if so you can use the input dates bar.

                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by gngmpwdws, Today, 09:58 PM
                0 responses
                2 views
                0 likes
                Last Post gngmpwdws  
                Started by darkgreen, Today, 03:35 PM
                1 response
                14 views
                0 likes
                Last Post lmatiukas  
                Started by imalil, Today, 09:46 PM
                1 response
                7 views
                0 likes
                Last Post Epistemophilic  
                Started by SAITON, Today, 09:03 PM
                0 responses
                6 views
                0 likes
                Last Post SAITON
                by SAITON
                 
                Started by suprsnipes, Today, 08:26 PM
                0 responses
                5 views
                0 likes
                Last Post suprsnipes  
                Working...
                X