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

BarsArray strange behavior

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

    BarsArray strange behavior

    I have a fairly simple EA that uses 2 different time frames. I use BarsArray[1] and BarsAray[2] to access the price information for those 2 time frames. For some reason when I add this line of code
    Code:
    Add(PeriodType.Minute, LowTimeframe);
    after the first two calls to Add, my EA results change. The lower I make the value of "LowTimeframe" the more trades my EA takes in backtesting. This is confusing to me as I have not yet written any code that calls upon that third series of time data, so why would my EA be behaving any differently. My first two time periods are 1 hour and 15 minutes.

    #2
    Hi krugman25,

    I recommend that you add the following print to the beginning of OnBarUpdate:

    if (BarsInProgress == 1)
    {
    Print(BarsPeriods[1].Id + " - " + BarsPeriods[1].Value + " - " + Time[0]);
    }

    then open the output window and refresh the script (right-click chart -> select Reload NinjaScript).

    Take a look at what appears in the output window.

    If you re-run this do you get different prints or the same?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi krugman25,

      I recommend that you add the following print to the beginning of OnBarUpdate:

      if (BarsInProgress == 1)
      {
      Print(BarsPeriods[1].Id + " - " + BarsPeriods[1].Value + " - " + Time[0]);
      }

      then open the output window and refresh the script (right-click chart -> select Reload NinjaScript).

      Take a look at what appears in the output window.

      If you re-run this do you get different prints or the same?
      No matter what I change the third time series to it always outputs this.

      ---------------------
      Minute - 60 - 6/19/2012 9:00:00 AM
      Minute - 60 - 6/19/2012 10:00:00 AM
      Minute - 60 - 6/19/2012 11:00:00 AM
      Minute - 60 - 6/19/2012 12:00:00 PM
      ---------------------

      I want to add in that no where in my code do I reference BarsArray[3].
      Last edited by krugman25; 05-08-2014, 07:46 AM.

      Comment


        #4
        Hi krugman25,

        I miss understood. When you mentioned in your first post that you are using two time frames, I thought that the primary was the first time frame and the Add(PeriodType.Minute, LowTimeframe); was adding the second time frame.

        If there is a 3rd time frame that you are wanting to look at use:

        Code:
        if (BarsInProgress == 2)
        {
        Print(BarsPeriods[2].Id + " - " + BarsPeriods[2].Value + " - " + Time[0]);
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi krugman25,

          I miss understood. When you mentioned in your first post that you are using two time frames, I thought that the primary was the first time frame and the Add(PeriodType.Minute, LowTimeframe); was adding the second time frame.

          If there is a 3rd time frame that you are wanting to look at use:

          Code:
          if (BarsInProgress == 2)
          {
          Print(BarsPeriods[2].Id + " - " + BarsPeriods[2].Value + " - " + Time[0]);
          }
          The results of printing the information for BarsPeriods[2] is...
          ------------------
          Minute - 15 - 1/24/2012 12:00:00 PM
          Minute - 15 - 1/24/2012 12:15:00 PM
          Minute - 15 - 1/24/2012 12:30:00 PM
          Minute - 15 - 1/24/2012 12:45:00 PM
          ...
          ------------------

          According to your documentation array element 0 is going to hold whatever time increment you chose for the backtest, and for each additional time frame added you increase that element by 1. So in my case I am using 1 minute "ticks" per the Data Series input field. Array element 0 holds the price data for those 1 minute ticks. But I am adding 3 more data series on top of that; 1 Hour, 15 Minutes, 5 Minutes. I originally added the 1 Hour and 15 Minute time frames and backtested my EA. When I added in the 5 minute time frame, it changed my backtest results, even though I had not yet written code to utilize that 3rd new time time frame series. When I print out the values of BarsArray, it comes back as I would expect:
          BarsArray[0] = 1 minute(Backtest tick size)
          BarsArray[1] = 60 minutes(First added time series)
          BarsArray[2] = 15 minutes(Second added time series)
          BarsArray[3] = 5 minutes(Third added time series)

          Comment


            #6
            Hi krugman25,

            Just to avoid confusion, lets refer to chart data as bars. For example a 1 minute bar or 14 minute bar. A tick is a received piece of information. You can also have a 1 tick chart where each bar represents 1 tick.

            In your code, if you add additional data series you also need to prevent the code from executing in those data series.

            By using:

            if (BarsInProgress == 2)

            this will ensure that all code is run on the 3rd data series.

            If there is no BarsInProgress check then the code is run on each data series and this would definitely change your results.

            Below is a link to the help guide on BarsInProgress.
            http://www.ninjatrader.com/support/h...inprogress.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hi krugman25,

              Just to avoid confusion, lets refer to chart data as bars. For example a 1 minute bar or 14 minute bar. A tick is a received piece of information. You can also have a 1 tick chart where each bar represents 1 tick.

              In your code, if you add additional data series you also need to prevent the code from executing in those data series.

              By using:

              if (BarsInProgress == 2)

              this will ensure that all code is run on the 3rd data series.

              If there is no BarsInProgress check then the code is run on each data series and this would definitely change your results.

              Below is a link to the help guide on BarsInProgress.
              http://www.ninjatrader.com/support/h...inprogress.htm
              I believe we are on the same page now with the data, I just want to make clear though that the bars in element 0 are not the bars I added in the code, but the option I chose in "Data Series". For example, if I decided to choose 1 second, I would have 1 second bars in element 0. Any bars I add on top of that in the code using the Add() function goes into element 1,2,3 etc.

              I didn't originally have if (BarsInProgress == x), I added in if (BarsInProgress == 0), and now the 5 minute bars aren't affecting my results anymore. That is still really confusing to me though, and perhaps you can help me understand. If the bars closings line up, then the close of the 5 minute bar should be the same close of the 1 minute bar, even if it executes twice(once for 1 minute bar close and once for 5 minute bar close) it will already have made its trading decision on the first bar close, and so the second is redundant and wouldn't change anything. I can see having "barsInProgress == 0" would help on CPU usage and limiting redundancy, but it shouldn't change the back test results.
              Last edited by krugman25; 05-08-2014, 09:26 AM.

              Comment


                #8
                Hi krugman25,

                Yes, BarsInProgress 0 and BarsArray[0] refer to the primary data series which is the data series chosen for the backtest or chart.

                If you have multiple data series and no check for BarsInProgress then any entries you have will be done for both data series even if they both trigger at the same time. Having calculations and actions done in one OnBarUpdate run does not prevent the calculations and actions being run again the next time OnBarUpdate is run.

                In other words, OnBarUpdate is run for every bar close in every bars in progress independently. Meaning if you have EnterLong with 3 data added data series that all close at the same minute you can expect 3 orders to be placed.

                If each series is making orders, wouldn't it be expected the results would be different if there is a data series added and now there are new orders being made?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  My EA is configured to only allow 1 order at a time by checking if an order is already open, so lets say the 1 and 5 minute bars happen to be closing at the same time, and I don't have the code if(BarsInProgress == 0). It will execute my logic for the 1 minute bar close and then the 5 minute close consecutively. Lets say on the 1 minute bar close it opens a long position. Then on the 5 minute bar close it should see that I have already opened an order and so the second OnBarClose call shouldn't be opening any additional orders.

                  Also if what you are saying is true, if I change my 5 minute bar series to a 1 minute bar series(I.E. I now have two 1 minute bar series), it should effectively double the number of trades my EA takes. But when I test that out, the number of orders my EA takes goes from 930 to 950, so about a 2% increase. I agree that something is happening at the code level, but it doesn't sound like the behavior you are describing.
                  Last edited by krugman25; 05-08-2014, 09:54 AM.

                  Comment


                    #10
                    Hi krugman25,

                    How are you determining that you are already in a position?

                    If you add prints to where the entry orders are in the code, (as in within the same branching command) do the prints not show as expected?

                    Also, changing from a 5 minute series to a 1 minute series does not guarantee more orders if you have conditions for the entry. Its all about when the conditions are true.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hi krugman25,

                      How are you determining that you are already in a position?

                      If you add prints to where the entry orders are in the code, (as in within the same branching command) do the prints not show as expected?
                      I check for open positions with
                      Code:
                      "if (Position.MarketPosition == MarketPosition.Flat)"
                      .

                      Originally posted by NinjaTrader_ChelseaB View Post
                      Also, changing from a 5 minute series to a 1 minute series does not guarantee more orders if you have conditions for the entry. Its all about when the conditions are true.
                      You mentioned in a previous post that all bars closes for all series will execute if I don't add the proper code to prevent it. If I have two 1 minute bar series, and don't implement code preventing them both from executing my trade logic, then why would it not double the amount of positions it opens. if the first iteration of OnBarUpdate() opened a long, why wouldn't it the second time it called it? How would that explain then a 2% increase in opened positions???

                      I would expect one of two outcomes, either this code "if (Position.MarketPosition == MarketPosition.Flat)" doesn't work, in which case it should double the positions it is opening. The logic should go, if it opens long on the first 1 minute bar series close, it would again open another long position when it calls OnBarUpdate() the second time from the second 1 minute bar series. The second possible outcome is that my "if (Position.MarketPosition == MarketPosition.Flat)" does work, in which case it should matter how many bar series I add above 1 minute, the first call to OnBarUpdate() opens the long position, and any redundant calls after that are ignored since it sees a position was already opened. So to me it seems that either it should be doubling my positions or having zero affect, I can't think of a reason why it would only increase it by 2%. If what I am saying doesn't make sense please let me know.
                      Last edited by krugman25; 05-08-2014, 10:54 AM.

                      Comment


                        #12
                        Hi krugman25,

                        If you are using multiple data series, the position will be only for the position of the current processing data series.

                        If you want to check all data series positions you will need to do this one by one using Positions.

                        For example:

                        if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat && Positions[2].MarketPosition == MarketPosition.Flat)}
                        {
                        // execute code
                        }

                        Below is a link to the help guide on Positions.
                        http://www.ninjatrader.com/support/h.../positions.htm

                        Regarding what would enter for an additional 1 minute series I cannot say without viewing the code. It all depends on the conditions used.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hi krugman25,

                          If you are using multiple data series, the position will be only for the position of the current processing data series.

                          If you want to check all data series positions you will need to do this one by one using Positions.

                          For example:

                          if (Positions[0].MarketPosition == MarketPosition.Flat && Positions[1].MarketPosition == MarketPosition.Flat && Positions[2].MarketPosition == MarketPosition.Flat)}
                          {
                          // execute code
                          }

                          Below is a link to the help guide on Positions.
                          http://www.ninjatrader.com/support/h.../positions.htm

                          Regarding what would enter for an additional 1 minute series I cannot say without viewing the code. It all depends on the conditions used.

                          I appreciate you pointing out the positions! I didn't realize each series can open it's own position.

                          I don't see why you would need to see the code. It is simply executing my logic twice at the end of every minute bar close. Why would the code come up with a different results on the second time looping through? If it said open long on the first loop, it would have to come up with the same conclusion on the second loop. Like I mentioned before, adding that second 1 minute bar series should effectively double it's orders. If you disagree, I would want you to explain how you can run the same block of logic twice consecutively and get different answers or show me a simple example of how that is possible.

                          Originally posted by NinjaTrader_ChelseaB View Post
                          In other words, OnBarUpdate is run for every bar close in every bars in progress independently. Meaning if you have EnterLong with 3 data added data series that all close at the same minute you can expect 3 orders to be placed.
                          Chelsea, you even admitted here yourself if you have enter long with 3 data series, and they all close at the same time, you will get 3 longs, guaranteed. This is essentially what I am saying. In my case it opened up 2% more positions by adding a second 1 minute bar data series, so what happened to the other 98%.
                          Last edited by krugman25; 05-08-2014, 11:14 AM.

                          Comment


                            #14
                            Hi krugman25,

                            That is if there are no conditions in your script and EnterLong() is called for all 3.

                            If you have conditions, then it would depend on those conditions.

                            Attached is a sample that addes the exact same data series as the primary.

                            Try running this once and look at the historical trade performance.

                            Then comment out the Add line on line 28 and run the script again and look at the performance.

                            I realize your conditions won't be like mine, but this should demonstrate that we can cause different behavior depending on how the strategy is coded.
                            Attached Files
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by sofortune, Today, 10:28 AM
                            4 responses
                            9 views
                            0 likes
                            Last Post sofortune  
                            Started by Haiasi, 04-25-2024, 06:53 PM
                            6 responses
                            82 views
                            0 likes
                            Last Post joselube001  
                            Started by gaz0001, Today, 10:09 AM
                            1 response
                            7 views
                            0 likes
                            Last Post Red_70
                            by Red_70
                             
                            Started by DawnTreader, 05-08-2024, 05:58 PM
                            11 responses
                            41 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by tkaboris, Today, 06:27 AM
                            4 responses
                            13 views
                            0 likes
                            Last Post tkaboris  
                            Working...
                            X