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

Question on a PriorWeekOHLC indicator (NT8) I want to code

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

    Question on a PriorWeekOHLC indicator (NT8) I want to code

    Hi. I want to code my own PriorWeekOHLC indicator as the ones I have found on the indicators section of the forum all rely on intraday intervals and I want to have one that draws plot lines on a daily chart. The primary data I want to use is a weekly chart from which I can get the requisite OHLC values but I want to transfer and plot these values on a daily chart. I do not know exactly how to draw a connection between these two data sets in my script. I was planning to use the indicator on a daily chart but in its code add the weekly data series. If you have any ideas how I can accomplish this, I'd highly appreciate it.

    [[This is my own idea but I am not sure if it viable. I was thinking of getting the relevant week number from the weekly data (out of the 52 weeks in a year) and check the daily bars against this number. If they match, plot the OHLC values. However, I don't know the syntax to get the week number of the bars on the weekly chart (even though I can read it from the data box of the weekly chart) and secondly, I don't know the syntax to get the week number of a bar on the daily chart. Once I know these two and link them, I believe the bulk of my confusion will be dispelled and coding the rest should be fairly easy. Just my idea though - feel free to comment on it. ]]

    #2
    Hello mbesha, and thank you for your question.

    First, to work with multiple data series, you will want to carefully review this section of the help guide. If there are any specific questions we can answer please do not hesitate to ask them here.



    Generally speaking, you will want your primary data series to be your daily data, and your secondary data series to be your weekly one. You can create the second data series like this :

    AddDataSeries(BarsPeriodType.Week,1);

    You will need to use BarsInProgress inside of OnBarUpdate to determine which data series you are currently working with.

    To your second question about retrieving week numbers, Time[] members are C# DateTime objects. You can use these with the C# function Calendar.GetWeekOfYear to retrieve this number. You will need to make two decisions :

    • Whether the first week of the year is the week that includes the first day, the first four day week, or the first full week
    • Which day of the week the week starts on

    I am including some publicly available C# documentation for more information.











    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks a ton JessicaP for your prompt and very insightful response. That'll definitely get me going in the right direction.

      Comment


        #4
        Question about "Addplot"

        Hi. I have a question about the "AddPlot" feature. How are the "Values[]" assigned in the case of for loops? I have the following logic that I have used in my indicator but nothing is being plotted.

        (I used 4 AddPlot statements)

        for(int i = 0; i < CurrentBars[1] ; i++)
        for(int j = 0; i < CurrentBars[0] ; j++)
        {
        if(condition for Dataseries 1 == condition for Dataseries 2)
        {
        Values[0] = Low[1][i];
        Values[1] = High[1][i];
        Values[2] = Close[1][i];
        Values[3] = Open[1][i];
        }
        }

        Is such assignment of variables to the plots proper? Please advise. Thanks.

        Comment


          #5
          Hello mbesha, and thank you for your question.

          By the time OnBarUpdate() is called, all available members of Values[] will be populated and available. That is, if you had 2 extra instruments each on 1 minute bars, and your primary data series was 1 minute bars, every OnBarUpdate, Values[1][0] and Values[2][0] will be populated.

          If you have data series that update asynchronously - say we had 1 minute bars for the primary series, 2 minute for the 2nd series, and 3 minute bars for the 3rd series - you will want to use the BarsInProgress member to determine which data series you are updating from. I believe this section being missing is what is preventing you from seeing plots. I am including a link to the documentation for more information.



          All of this happens before any logic - including for loops - inside of OnBarUpdate() is processed.
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Still not plotting

            I tried adding the BarsInProgress property but there is still nothing being plotted. I have attached my script for your reference. It is fairly short and simple. Please advise. Thanks.
            Attached Files

            Comment


              #7
              Hello mbesha,

              I noticed when I tried to run this that your code attempts to write to a read-only instance. When you assign DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo; , dfi is a reference to the read-only CurrentInfo , not an in-stack-memory copy. In order to make a writeable copy, you can do this :

              DateTimeFormatInfo dfi = (DateTimeFormatInfo) DateTimeFormatInfo.CurrentInfo.Clone();

              Please let us know if there are any further ways we can help.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Still nothing is being plotted

                Thanks for your feedback. I tried changing the code as you instructed but there is still nothing being plotted. What else do you think could be the problem?

                Comment


                  #9
                  Hello mbesha,

                  I believe your first approach works better than the approach you took in the file you just sent me. I believe it was what I addressed in my most recent post that was preventing that approach from working, since where you were correctly assigning to Values occurred afterward in your code.

                  I have made the following changes and plotted your indicator in its own panel on my system successfully. Please let me know if they do not work for you, or if there are any other ways we can help.

                  This section

                  Code:
                  [FONT=Courier New]
                                PriorWeekOpen[j]  = priorWeekOpen;
                                PriorWeekHigh[j]  = priorWeekHigh;
                                PriorWeekLow[j]    = priorWeekLow;
                                PriorWeekClose[j]  = priorWeekClose;[/FONT]
                  became

                  Code:
                  [FONT=Courier New]
                  /*
                                PriorWeekOpen[j]  = priorWeekOpen;
                                PriorWeekHigh[j]  = priorWeekHigh;
                                PriorWeekLow[j]    = priorWeekLow;
                                PriorWeekClose[j]  = priorWeekClose;
                  */
                                Values[0][0] = priorWeekOpen;
                                Values[1][0] = priorWeekHigh;
                                Values[2][0] = priorWeekLow;
                                Values[3][0] = priorWeekClose;[/FONT]
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    Progress but still not quite there

                    Thanks for your continued support. After making the changes you suggested, the indicator did plot but not as expected. I have attached screenshots for your reference. I have also attached a screenshot showing how I intend for the indicator to plot. I have also attached my latest script that incorporates all the aforementioned changes.Please advise.
                    Attached Files

                    Comment


                      #11
                      Hello mbesha,

                      I am glad your indicator is now successfully plotting to your chart. While we are approaching the edge of the scope of our support, I did notice that you are looping over several weekly bar values, and assigning only a single value to each array. That is, this code,

                      Code:
                      [FONT=Courier New]
                      for(int i = 0; i < CurrentBars[1] ; i++)
                      {
                        // ...
                        priorWeekOpen = Opens[1][i];
                        // ...
                        Values[0][0] = priorWeekOpen;
                      }[/FONT]
                      is equivalent to

                      Code:
                      [FONT=Courier New]
                      int i = [/FONT][FONT=Courier New][FONT=Courier New]CurrentBars[1] - 1;
                      [/FONT]Values[0][0] = [/FONT][FONT=Courier New][FONT=Courier New]Opens[1][i][/FONT];[/FONT]
                      Based on the names of your variables, I believe this may not be what you intended; as stands, you are basing everything on the oldest bar available.

                      Therefore, I would recommend the following change. I think these lines...

                      Code:
                      [FONT=Courier New]
                                          priorWeekOpen    = Opens[1][i];
                                          priorWeekHigh    = Highs[1][i];
                                          priorWeekLow    = Lows[1][i];
                                          priorWeekClose    = Closes[1][i];[/FONT][FONT=Courier New][/FONT]
                      ... should instead be ...

                      Code:
                      [FONT=Courier New]
                                          priorWeekOpen    = Opens[1][1];
                                          priorWeekHigh    = Highs[1][1];
                                          priorWeekLow    = Lows[1][1];
                                          priorWeekClose    = Closes[1][1];[/FONT]
                      or in other words, changing the oldest available bar to the most recent bar.

                      Further optimizations, such as refactoring out your for loops, are optional but recommended.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Current situation

                        Thanks for your kind patience and gracious support this far. I am also trying my best on my end to avoid bugging you as much as possible. I changed up the logic of my script somewhat and so far I can say that the values calculated by the script are accurate or as per my expectations. However, the problem now is how they are being plotted on the daily chart. Some of the values are being plotted but some are not. I do not understand why there is this discrepancy. I have attached a screenshot of the aforementioned scenario and the latest script for your reference. If you could just help me nail down the plotting aspect, I'd highly appreciate it and I'll be more or else done with this indicator (I might just put it up on the NT8 indicators section with credits to you of course). Thanks.
                        Attached Files

                        Comment


                          #13
                          Hello mbesha,

                          I have reviewed your changes, and believe I understand them, but it does appear that you are attempting to update your entire data series every single bar. Ninja is designed in such a way that the 0th member of a data series is always the current and most recent bar, with the rest of the series extending back away. That is, if you saw something trade for $1, $2, $3, $4, and then $5, it would look like this :

                          1st bar
                          Close = {1}

                          2nd bar
                          Close = {2, 1}

                          3rd bar
                          Close = {3, 2, 1}

                          etc.

                          Given this, I would recommend eliminating your remaining for loop, and simply updating the 0th member of all your data series every time OnBarUpdate is called.

                          Please let us know if there is any other way we can help.
                          Jessica P.NinjaTrader Customer Service

                          Comment


                            #14
                            Does not work when using 0th member only

                            Hi. Thanks for your feedback. As you know, the indicator works by comparing bars on the daily chart with bars on the weekly charts in such a way that the 0th member of the former can not always work with the 0th member of the latter (as it is a prior week OHLC indicator). I tried only using the 0th member and it did not work. This is precisely why I use the for loop to get the relevant member. What other way do you recommend for getting the relevant bar besides using a for loop?

                            Comment


                              #15
                              Hello mbesha,

                              Could you please clarify what your goals are? I am not sure I am following along. As long as the BarsInProgress is reflecting your daily bars, your 1st member of your weekly data series will always be the prior week's bars.
                              Last edited by NinjaTrader_JessicaP; 07-06-2016, 07:12 AM.
                              Jessica P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FrancisMorro, Today, 03:24 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post FrancisMorro  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              10 responses
                              1,770 views
                              0 likes
                              Last Post Leafcutter  
                              Started by Rapine Heihei, 04-23-2024, 07:51 PM
                              2 responses
                              31 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by Shansen, 08-30-2019, 10:18 PM
                              24 responses
                              944 views
                              0 likes
                              Last Post spwizard  
                              Started by Max238, Today, 01:28 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Working...
                              X