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

Cannot get Close[0] > to work on historical end of day data

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

    Cannot get Close[0] > to work on historical end of day data

    Sorry, my computer shut off in the middle of typing the earlier edit. It is probably more correct to change CalculateOnBarClose = true; to false. And I forgot to mention I am doing the backtest on the strategy. Thanks again.
    Last edited by RMnon-programmer; 08-23-2015, 02:39 PM. Reason: to add a view comments

    #2
    Hello,

    Thank you for the note.

    It looks like you have removed the original question from this post. I wanted to confirm is this now resolved and the edit is your solution? Or does the existing question still exist?

    If you still have a question, please edit your post to include the question as well otherwise I will mark this as resolved.

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

    Comment


      #3
      Hmm, that is strange because when I clicked on New Posts after posting the original portion I could see it was there, just greyed out, I assumed because I hadn't been approved yet as a new member. Maybe my computer shutting down had something to do with the first post since I was editing it, and only the edits seem to have taken. I am not at my computer at present so paste in the issue and script, but perhaps I can summarize the main parts now so that I will hopefully have a reply this evening:
      Basically, I am using end of day kinetic data and simulated trading to do a simple strategy, but it returns the same incorrect answer of ALL results when I click on the DOW 30 Instrument list for example and choose Backtest and change it from minute to Day and have the compiled code below:
      if (Close[0] > Close[5] // for example (get the same result for operand of == or < too).
      enterlong();

      I apologize if the syntax might be slightly wrong, as I am going from memory and a newbie.

      Basically, why does it not seem to work: when I view the resultant charts it is very clear that most of them have the close of the current day price value less than the close price of 5 days ago, yet it prints the chart.
      Thanks much.

      Comment


        #4
        Hello,

        Thank you for the reply.

        It would be helpful if you could post the script when you are at your PC so I can see what logic is being used.

        One item I have noted is that you are using the Kinetick End of day connection which provides only historical daily data. When using the backtest this would limit you to only using 1 day or greater intervals, a minute interval would not work on this connection. Are you using 1 day or greater for the back test or a different period?

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

        Comment


          #5
          Hi Jesse,
          Yes, I mentioned in the previous post that I changed it from Minute to Day in the Backtest. Is there another place I need to look to change to Day? I am only interested in the close/end of day results.

          The logic really is all here, there is no more code in the script as I reduced it to the bare bones so I could manually check the charts to see if it was working, and it compiled fine. It includes all the correct variable declarations, etc. because I ran the strategy wizard to make sure everything was correct, so I posted the logic above (even though I forgot the right ")" in the "if" statement ). But I will paste in the full (short) calculated script from the Strategy Wizard this evening.

          Here is the resultant script from the strategy wizard, plus my original comments:
          I cannot get a simple strategy to output expected results. The strategy compiles and outputs
          Resultant charts, but it outputs ALL instruments in that Instrument list when it should just be outputting
          When Close[0] > Close[5] for example:
          .
          .
          ///<summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          ///</summary>
          protectedoverridevoid Initialize()
          {
          CalculateOnBarClose = true;
          }

          ///<summary>
          /// Called on each bar update event (incoming tick)
          ///</summary>
          protectedoverridevoid OnBarUpdate()
          {
          // Condition set 1
          if (Close[1] > Close[5])
          {
          EnterLong();
          }
          }

          #region Properties
          [Description("")]
          [GridCategory("Parameters")]
          publicint MyInput0
          {
          get { return myInput0; }
          set { myInput0 = Math.Max(1, value); }
          }
          #endregion
          }
          }
          Also, what does the “protected override void” mean?


          Thanks,
          Rob
          Last edited by RMnon-programmer; 08-24-2015, 05:28 PM. Reason: addition of the strategy wizard output script

          Comment


            #6
            Hello,

            I am unsure what your actual question is here, this is what I am having trouble understanding:

            but it outputs ALL instruments in that Instrument list when it should just be outputting
            When Close[0] > Close[5] for example
            The statement Close[0] > Close[5] would apply to all instruments that this strategy is run on, so if you have selected a list with 5 instruments this code would be backtested 5 times, once for each instrument.

            Can you elaborate on what the issue is regarding the selected instrument list and the conditions, or could you explain what you are expecting to happen that is not? If you can provide an image that may also be helpful in understanding the question.

            If this is related to the executions on the chart occurring outside of the condition Close[0] > Close[5], please provide a screen shot of what you are seeing as that would greatly help in understanding.

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

            Comment


              #7
              Sorry, it is hard for me to explain being a NinjaTrader beginner and not really knowing what kind of output to expect. Maybe that is the problem: the expected output is not what I expected. I will try to explain and if this explanation is not clear, I will insert a screenshot of the results this evening when I can run the backtest:
              Using "Close[0] > Close[5] in a backtest on the DOW30, I get 30 output records but each record has a profit value, gross profit(?), etc., whether it is positive or negative. Negative values are in red. So my question is, how can the close of the current day for all of the 30 Dow instruments be greater than the close of those instruments over a period of the last 5 days, especially when I view the charts and see that this is not the case for at least some of these charts? Thank you for explaining to me that the output will contain results from all instruments that the backtest is run on, I was not sure this was the case being a newbie. So then, What will the output look like when the condition above is satisfied, i.e. true? I am assuming I will have a row of zero's in the resultant backtest for instruments where the condition Close[0] > Close[5] was NOT true? Is this a correct assumption? In other words, I was just assuming that the output rows (text, not chart image) of resultant backtest data would somehow show me which rows satisfied the true condition, and which did not. Thanks much for your patience in understanding this newbie issue I am having.

              Comment


                #8
                hello,

                Using the syntax provided, if a complete loops is made to enter and exit a position, I would expect a number of trades to be made over the entire period if time the strategy is set to test for.

                If the condition does not get met a single time in the entire timespan it has run for, I would expect 0 trades.

                For this, to find the values your condition is using I would suggest using Print() statements to print out the values to the Tools -> Output Window.

                This would be a way of debugging to find out what values are being used and when.


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

                Comment


                  #9
                  Thanks Jesse, I had failed to mention that I did print the results of a similar script and the results in the output window were correctly calculating the close value on the correct day), but the charts were still showing that the condition was not true for all long entries in the backtest summary.

                  But, I will scratch my head again (if I have any hair left) and go through the link you provided for troubleshooting. Thank you for that link. It will be difficult with little programming experience. I have not
                  even figured out how to debug with break statements, I "think" I have figured out how to do print statements (BUT PERHAPS I AM GETTING THE CORRECT RESULTS BECAUSE IT IS THE LAST RECORD/LINE/VALUE IN THE OUTPUT WINDOW AND THE DO LOOP IS MESSING UP THE RESULTS?????) but I will figure it out with a simple strategy/program like this one first.

                  Comment


                    #10
                    Hello,

                    Thank you for the additional details.

                    It would be strange if the prints were not lining up with the conditions, I think one more go through with the prints wouldn't hurt.

                    I would print the values and view the executions in the chart side by side. if they are still not lining up, please take a screen shot of the prints with the chart and executions, this would help figuring out what is happening.

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

                    Comment


                      #11
                      additional documentation

                      Originally posted by NinjaTrader_Jesse View Post
                      Hello,

                      Thank you for the additional details.

                      It would be strange if the prints were not lining up with the conditions, I think one more go through with the prints wouldn't hurt.

                      I would print the values and view the executions in the chart side by side. if they are still not lining up, please take a screen shot of the prints with the chart and executions, this would help figuring out what is happening.

                      I look forward to being of further assistance.
                      I hope the information below and attachments help you figure this out. Thanks ahead of time.
                      Strategy Script (added print statements):
                      #region Using declarations
                      using System;
                      using System.ComponentModel;
                      using System.Diagnostics;
                      using System.Drawing;
                      using System.Drawing.Drawing2D;
                      using System.Xml.Serialization;
                      using NinjaTrader.Cbi;
                      using NinjaTrader.Data;
                      using NinjaTrader.Indicator;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Strategy;
                      #endregion

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      /// <summary>
                      /// Enter the description of your strategy here
                      /// </summary>
                      [Description("Enter the description of your strategy here")]
                      public class junk : Strategy
                      {
                      #region Variables
                      // Wizard generated variables
                      private int myInput0 = 1; // Default setting for MyInput0
                      // User defined variables (add any user defined variables below)
                      #endregion

                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
                      CalculateOnBarClose = false;
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      // Condition set 1
                      if (Close[1] > Close[5])
                      {
                      EnterLong();
                      Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss") + "," + Close[0]);
                      Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss") + "," + Close[1]);
                      Print(Time[0].ToString("dd/MM/yyyy") + "," + Time[0].ToString("HH:mm:ss") + "," + Close[5]);
                      double value0 = Close[0];
                      Print("The current Close value is: " + value0.ToString());
                      double value1 = Close[1];
                      Print("The current Close value yesterday was: " + value1.ToString());
                      double value5 = Close[5];
                      Print("The current Close value 5 days ago was: " + value5.ToString());
                      }
                      }

                      #region Properties
                      [Description("")]
                      [GridCategory("Parameters")]
                      public int MyInput0
                      {
                      get { return myInput0; }
                      set { myInput0 = Math.Max(1, value); }
                      }
                      #endregion
                      }
                      }

                      The first candle on all of these charts is August 21.
                      19/08/2015,14:00:00,25.73 :This is actually the close reading of the 3rd candle from the right, of date August 19, 2015 by the data box hovering over the chart candle for GE.
                      19/08/2015,14:00:00,26.07 :This is actually the close reading of the 4th candle from the right, of date August 18, 2015 by the data box
                      (so the date is wrong on this output).
                      19/08/2015,14:00:00,25.86 :This is actually the close reading of the 8th candle from the right, of date August 12, 2015 by the data box (why doesn’t the output window even print August 12?).
                      The current Close value is: 25.73
                      The current Close value yesterday was: 26.07
                      The current Close value 5 days ago was: 25.86
                      Also, when I look at the results of the 2nd instrument, it outputs the close values of the 3rd, 4th, and 7th
                      candles (starting again from the right side of the chart).

                      I do not understand the resultant instrument rows in the resultant backtest results: like I was saying earlier, all instruments print
                      out, so I cannot tell which ones satisfied the condition close[1] > close[5]. Revelation: It appears from the output window that it printed all
                      instruments because it didn’t stop until it satisfied the condition, as verified by the last few records of each instrument output
                      where it is always true that the current Close value yesterday is greater than the current Close value 5 days ago, even though it is using the
                      incorrect bars (and date) to reach this conclusion. I want it to go back one day from the candle furthest to the right of the chart and see if that close of that day is greater than the close of 5 days ago (again, from the candle furthest to the right of the chart). Perhaps I do not understand Close and Closes.
                      In addition:
                      I do not understand why it is doing the calculation on the 3rd and 4th candle/bar, and then the 7th or 8th.
                      I do not understand how it prints the trade on the chart (often it enters the trade way back and not based on my criteria close[1] > close[5].
                      I do not understand why it did not start the output from the first candle (on the right of the chart). Do I need to
                      Use Close[0][0]? I should not need to specify an offset.
                      Is there a specific procedure for updating all of the instruments in NinjaTrader besides going to the Historical
                      Data Manager and selecting Day instead of tics and the date range?
                      Attached Files

                      Comment


                        #12
                        Hello,

                        Thank you for the reply.

                        There are a few items I will address here so please excuse the long message.

                        I do not understand the resultant instrument rows in the resultant backtest results: like I was saying earlier, all instruments print
                        out, so I cannot tell which ones satisfied the condition close[1] > close[5].
                        Correct, if you use a List or do a Basket backtest, all instruments would be run and the prints would be confusing as you are only printing price data with nothing unique to say which instrument printed the data.

                        You could instead use something like this to determine which print is from what instrument:

                        Print("The current Close value for " + Instrument.FullName + " is: " + value0.ToString());

                        Instrument.FullName will print the instrument name and expiry.

                        I want it to go back one day from the candle furthest to the right of the chart and see if that close of that day is greater than the close of 5 days ago (again, from the candle furthest to the right of the chart). Perhaps I do not understand Close and Closes.
                        Are you only trying to calculate the Close[1] > Close[5] from a specific point in time? Currently this logic would be evaluated on every bar of the chart.

                        Lets step through this, Assume you load this strategy on a chart or in a backtest. OnBarUpdate gets called starting on bar 0 or the left most bar of the chart. it then increments its way through the bars calling OnBarUpdate for every bar on the chart until it reaches the end on the right of the chart. That means this would be executed over and over and may become true over and over throughout the historical data.

                        If you do not want this, please tell me, is there a specific time of day this needs to occur or a specific event?



                        I do not understand why it is doing the calculation on the 3rd and 4th candle/bar, and then the 7th or 8th.
                        I do not understand how it prints the trade on the chart (often it enters the trade way back and not based on my criteria close[1] > close[5].
                        I do not understand why it did not start the output from the first candle (on the right of the chart).
                        These would all be related to the prior statement, at no point will the script ever evaluate right to left, OnBarUpdate will start at bar 0 and work its way to the right of the chart.

                        Is there a specific procedure for updating all of the instruments in NinjaTrader besides going to the Historical
                        Data Manager and selecting Day instead of tics and the date range?
                        Are you asking how to update the historical data? If so, if you are connected to a data provider when you run the backtest, historical data for the chart type selected would be downloaded. Otherwise local data will be used if you are not connected. You can also update historical data using a chart by viewing the time period, or through the historical data manager.

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

                        Comment


                          #13
                          Thank you, I think we are making progress now.

                          Yes, I am only trying to calculate the Close[1] > Close[5] from a specific point in time, i.e. from/starting at the most recent, fully closed, end-of-day bar (first bar on the right, if looking at end of day data which I am). Please see my example in more detail in the next paragraph below. How can I get it to look in the past, starting at the most recent end of day bar/time? Use negative numbers? Perhaps OnBarUpdate is not the one for me to use to accomplish this?

                          OK, thank you again, that is some critical information I needed to know, i.e. the direction of updating is from left to right on the chart, so my first bar that I thought was at closing price of the currect, rightmost bar, is actually way to the left, I assume at the first date of the backtest date interval I specified in the backtest. So, assuming this is August 26 and the last bar on the chart printed a close value fully on August 25 for this end of day data, how do I get NinjaTrader to look at the current [0] bar on August 25 initially, and do the Close[1] > Close [5] calculation from that bar as the starting point? I want to calculate the Close of yesterday (from the CURRENT bar/date's close), which should be August 24, is greater than the close of 5 days ago (again from the CURRENT bar/date's close, which I assume would be August 20), and where it is true, I want to go long, or trade (the next day after the most recent close day, August 25, which I am assuming cannot be done because that day, August 26, in this example, has not closed/finished yet, and it won't have finished when I run the script, because it is always tomorrow's open when I want to go long).

                          Thank you for the additional information on updating historical data. I did not know that was how it worked.

                          I am anxious to hear from you on how I can get NT to start today and calculate the condition from today's close.

                          Comment


                            #14
                            Hello,

                            For your first question: How can I get it to look in the past, starting at the most recent end of day bar/time?

                            Because NinjaTrader is processing left to right, you would need to know ahead of time the point in the day when you want to execute the logic. for example, you said end of day bar, I assume you mean the closing bar of the session? if so you would need to determine when the end of the session is. You could use a simple time check or there are other ways as well. once the bar that is equal to that time is processed the logic would be executed. There is more information on how the bars work in NinjaTrader here:


                            For your second question: how do I get NinjaTrader to look at the current [0] bar on August 25 initially, and do the Close[1] > Close [5] calculation from that bar as the starting point?

                            I assume August 25th is arbitrary and you just are referring to the current day correct? The logic is to execute on Current day as a starting point and then look 5 days in the past, is this correct?

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

                            Comment


                              #15
                              Thank you for pointing me to this information. Sounds like homework for me over the weekend.

                              Referring to my 2nd question (which is kind of another way of phrasing my first question): Yes, August 25th is arbitrary in that I desire to look at the most recent full market day, i.e. the last end of day close date. That is where I want to start the execution of the logic at, so yes, the logic is to execute on the most recent (current end of day) fully closed day bar, and then look 1 and 5 days in the past from there. The enter long will be for me to enter long at market open tomorrow (August 28 in the next example), something that I could make happen if the condition is true. In words and actions, here is what I desire: I run the script this evening after the market has closed, update the data or connect, and the script goes long where "the close one day from the current day is greater than the close PERIOD over the last 5 days from today's current close". In other words, in this example, the close of August 26 (end of day bars) is greater than the close of any of the market close days of August 26, 25, 24, 23, and 22. So, in this case, I only want to look at these days and looking at previous days to August 22 only confuses things. So I realize the formula might have to be something like Close[0][1] > Close[0][5], or something like that.

                              I hope that helps, and thanks again for your help.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              12 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              15 views
                              0 likes
                              Last Post AaronKoRn  
                              Working...
                              X