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

Store the price value for a specified time

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

    Store the price value for a specified time


    Hello.

    I have a system which to open a new position needs to know the price of the day before at 17:30.

    My question is how can I store this value in a variable or in some indicator.

    Any ideas?

    #2
    Hello jalro,

    Thank you for your inquiry.

    You can use Bars.GetBar to find the bar index of a specific bar and then subtract that from the CurrentBar value to get the barsAgo value for the bar at that time. You can then use that bars ago value to get the close of that bar:



    You can then store that retrieved value to a variable for later usage.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3

      Hello.
      Thanks for your answer.

      With Bars.GetBar I get the number of bars with respect to a specific date, that is, I have to indicate the year, month, day, and time. But what I want is that each day the number of bars is calculated until the data of the day before at 17:30.

      Any idea how you might indicate it?

      Also, it must have been an open market day.

      Comment


        #4
        Hello jairo,

        Thank you for your reply.

        It's going to require some manipulation of DateTime objects.

        I would advise taking a look at this example from our help guide that shows some examples of manipulating DateTime objects:



        You'll need to account for weekends as well as holidays, so that's going to be a bit complicated for this to work in every situation. For example, if the market closes early on a given day, do you want to look at the previous day's value?

        Some publicly available information on DateTime.Add() is available below:

        Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.


        The following would return the number of bars ago of yesterday at 17:30:

        DateTime yesterday = Time[0]. AddDays(-1);

        int barsAgo = CurrentBar - Bars.GetBar(new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 17, 30, 0));

        I would recommend checking what you get as far as the day of week for the yesterday variable and adjusting the number of days to subtract if it's a Monday or holiday, but this would need to be programmed into your script.

        Please let us know if we may be of further assistance to you.



        Kate W.NinjaTrader Customer Service

        Comment


          #5

          Hello, thanks for the answer.

          An alternative occurs to me, and that would be to create an indicator that has the close of the candle at 17:30 as a fixed value. In this way, today, whenever it is before 5:30 p.m., it would have the value of the previous day as a reference.

          But I have no experience creating indicators, could you tell me a tutorial or idea to create this type of indicator?

          Thank you.

          Comment


            #6

            Hello Impeesa,

            Thank you for your reply.

            You could try something like this, where I've added a 1 minute data series and get the price at 17:30, then only calculate using it on the primary series if it's gotten a price assigned:

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
            public class GetPriceAt1730 : Indicator
            {
            private double SeventeenThirty;
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Indicator here.";
            Name = "GetPriceAt1730";
            Calculate = Calculate.OnBarClose;
            IsOverlay = false;
            DisplayInDataBox = true;
            DrawOnPricePanel = true;
            DrawHorizontalGridLines = true;
            DrawVerticalGridLines = true;
            PaintPriceMarkers = true;
            ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
            //Disable this property if your indicator requires custom values that cumulate with each new market data event.
            //See Help Guide for additional information.
            IsSuspendedWhileInactive = true;
            SeventeenThirty = 0;
            }
            else if (State == State.Configure)
            {
            AddDataSeries(BarsPeriodType.Minute, 1);
            }
            }
            
            protected override void OnBarUpdate()
            {
            DateTime TimeToGet = DateTime.Parse("17:30", System.Globalization.CultureInfo.InvariantCulture) ;
            
            if (Times[1][0].TimeOfDay == TimeToGet.TimeOfDay)
            {
            SeventeenThirty = Closes[1][0];
            }
            if(BarsInProgress == 0)
            {
            if(SeventeenThirty != 0)
            {
            // do something
            }
            }
            }
            }
            }
            Please let us know if we may be of further assistance to you.

            Kate W.NinjaTrader Customer Service

            Comment


              #7

              Hi thanks for the info, I got it to work.

              But now an extra question arises, how can I check if the closing price of the current candle is higher than the maximum of the previous day between 9 in the morning and 5 in the afternoon.

              Thanks for your help

              Comment


                #8
                Hello jairo,

                Thank you for your reply.

                You could do that in a very similar manner to the prior example above. I'm attaching a simple example indicator that calculates the highest high of a time period and then draws an arrow below a bar if the close price of the bar is greater than the highest high of that prior time frame.

                Please let us know if we may be of further assistance to you.
                Attached Files
                Kate W.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by trilliantrader, 04-18-2024, 08:16 AM
                5 responses
                22 views
                0 likes
                Last Post trilliantrader  
                Started by Davidtowleii, Today, 12:15 AM
                0 responses
                3 views
                0 likes
                Last Post Davidtowleii  
                Started by guillembm, Yesterday, 11:25 AM
                2 responses
                9 views
                0 likes
                Last Post guillembm  
                Started by junkone, 04-21-2024, 07:17 AM
                9 responses
                70 views
                0 likes
                Last Post jeronymite  
                Started by mgco4you, Yesterday, 09:46 PM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X