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

Closes[][] Giving Wrong Values

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

    Closes[][] Giving Wrong Values

    Hello,

    Trying to obtain the price of a data series and it's giving the wrong price. To confirm it wasn't the added data series the problem I tried getting the price of the actual chart instrument via Closes[0][0]. For CADCHF the price is 0.72772 but Closes[0][0] gave 0.71726.

    For the added data series, USDCHF, Closes[1][0] gave 0.90402 but the actual price is 0.91807. It's not as if it's yesterday's close or anything either.

    Click image for larger version

Name:	3bctkSL.png
Views:	326
Size:	87.5 KB
ID:	1144661

    Thanks
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    Thanks for your post.

    Please note that if you are referencing Closes[][] to get the current price of another data series, you will receive the last price that the script has seen for that data series. If you are calculating OnBarClose, OnBarUpdate will be processed as a bar closes, and the time one bar closes may not be when another bar closes. (Bar closures are signaled by the first tick of a new bar.)

    While you may see the current price on the chart, please keep in mind the above since what you see visually on a separate chart may not be how the data series that is in the script is updating.

    I have attached a test indicator that can be applied to CADCHF which uses Calculate.OnPriceChange so each data series updates with each price change, and I print out the price that the script sees in a BarsInProgress check for each data series.

    More information on working with multiple data series and referencing price data from added data series can be found below.

    https://ninjatrader.com/support/help...nstruments.htm

    We look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      In this case, it gets the price at a click of a button, so it should be getting the most up-to-date price at the time of clicking the button. However, it's getting completely the wrong value. It's WAYY off, a few days off. I've checked the documentation.

      Code:
      // From State.Configured
      AddDataSeries("USDCHF");
      
      // What runs once the button is clicked
      Print ("Conversion Pair Price: " + Closes[1][0]);
      Print ("Actual chart Price: " + Closes[0][0]);
      Thanks

      Edit: Perhaps I should list what the goal is. To get the price of another instrument, in this case USDCHF, from a script that is running on CADCHF on command.
      Last edited by Unsuitable; 03-03-2021, 01:00 PM.
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment


        #4
        Hello Unsuitable,

        If you are making BarsAgo references from a button press, TriggerCustomEvent should be used. Could you confirm if you are using it?

        Can you also confirm that the script uses Calculate.OnPriceChange so each data series in the script is up to date with the current price?

        TriggerCustomEvent - https://ninjatrader.com/support/help...ustomevent.htm
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          I am not using it nor have I needed it before when making custom buttons and getting values from the chart. If you're curious how the button is implemented I'm following this thread.
          Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've


          Can you also confirm that the script uses Calculate.OnPriceChange so each data series in the script is up to date with the current price?
          What exactly do you mean by this? Does the script need to re-add the data or manually update it every time? Pressing the button should result in getting the most up-to-date value. After that, the script DOES NOT need to update the value unless the user clicks on the button again.

          Thanks
          Unsuitable
          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

          Comment


            #6
            Hello Unsuitable,

            You will need to use TriggerCustomEvent when making BarsAgo references from the button press event. BarsAgo references will only be valid when called from data driven methods like OnBarUpdate.

            When you add a data series, all data driven methods that are used by the script (OnBarUpdate, OnMarketData) will update for the primary data series and the data series you added. OnBarUpdate will update for all data series following the Calculate mode of the script.

            When you press a button and access Closes[1][0] for the secondary data series, it is getting the most recent price that was updated within the script for that data series.

            All data series that are used in the script update based on the Calculate mode. Calculate will need to be set to OnPriceChange so each data series that the script processes updates with each price change. If this is left to OnBarClose, the closest value you can get would be the last bar closure that the script has seen for that data series. (TriggerCustomEvent would also be needed.)

            That way when you access Closes[1][0] from your button press using TriggerCustomEvent, you get the last updated price of that data series.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello Jim,

              I tried the script with calculate on price change and that did nothing. I must be missing something cause what you're explaining doesn't apply. If I access the current chart via Closes[0][0] that should at the very least give a value that is near to the current price. HOWEVER, it gives a value from 3 days ago. I'm running the script with a FRESH CHART, so the closes should be very close as the data series is added when the chart is first loaded (with the most recent values). When getting the value from a different data series, it gives the wrong value as well. Not from a minute ago, not from 5 minutes, not from this morning but from 3 days ago. Not sure how I can convey how much the values are off. I doubt that using TriggerCustomEvent is going to help here.

              Could you run this by a colleague to make sure I'm not going crazy here? haha In all reality I just want the price of USDCHF, not the price of a few bars back.

              Thanks
              Unsuitable
              NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

              Comment


                #8
                Unsuitable, I do get paid by NinjaTrader to answer these questions. I can't help if you won't try what I am suggesting.

                See my attached screenshot showing TriggerCustomEvent being used along with using Calculate.OnPriceChange. As we can see TriggerCustomEvent is what should be used when we are making BarsAgo references from non-data-driven events like OnBarUpdate.

                If you are getting different results when doing this, please let me know.
                Attached Files
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jim,

                  Implemented the code compiled and reload of indicators and the same result. However, after a last ditch effort to reboot NT it shows the correct value. Honestly, no clue what going on with NT, but it works. Cross my fingers it continues to works.

                  Well, thanks for the help. It appears to be in working order now. Another quick question, I noticed in the AddDataSeries documentation it strongly suggests to hard code values. Is it possible/recommend to hard code a value but can change it in run time? I'm leaning no
                  Unsuitable
                  NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                  Comment


                    #10
                    Hello Unsuitable,

                    Be sure to use TriggerCustomEvent and Calculate.OnPriceChange going forward or you may see your issue come back.

                    You can't change a data series at run time, but you can request bar data in the middle of a script's life with a BarsRequest. Please note that a BarsRequest cannot be used to drive indicators (indicators would have to be reimplemented to work on a BarsRequest) and that BarsRequests are asynchronous (the rest of the script will continue while the BarsRequest is made)

                    BarsRequest - https://ninjatrader.com/support/help...arsrequest.htm

                    Another option would be to load all data series needed, and to switch to using different data series by branching your code to different data series. BarsInProgress can be checked to see which data series is running in OnBarUpdate. (See Multi Time Frame and Instruments linked in post #2.)

                    We look forward to assisting.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jim,

                      Gotcha. At the moment the code dynamically adds a data series. I know it's a sin according to the documentation, but all the possible values are hardcoded in a string[]. If the code can't find the relevant instrument in the string array it doesn't call AddDataSeries(). The intended use case is to get the price of a specific currency. When it comes to calculating the position size for forex, it may depend on another currency's price if the quote currency of the chart doesn't match the account currency. Furthermore, adding each data series for each currency pair wastes resources, there are over 40 forex pairs.

                      Thanks
                      Unsuitable
                      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                      Comment


                        #12
                        Hello Unsuitable,

                        While dynamically adding data series is not supported, you can get away with it in many cases. With anything unsupported, you should "expect the unexpected" and if you encounter something unexpected, you should test with a hard coded data series to confirm if your symptoms are due to dynamically adding data. (We would require this before we could offer any kind of support.)
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jim,

                          Understandable. When the OP was first submitted, the code used hardcoded values. If there were issues with the dynamic logic I wouldn't expect NT to handle it as it's not supported as you mentioned. Anyways thanks for the help.
                          Unsuitable
                          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by tsantospinto, 04-12-2024, 07:04 PM
                          5 responses
                          67 views
                          0 likes
                          Last Post tsantospinto  
                          Started by cre8able, Today, 03:20 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post cre8able  
                          Started by Fran888, 02-16-2024, 10:48 AM
                          3 responses
                          47 views
                          0 likes
                          Last Post Sam2515
                          by Sam2515
                           
                          Started by martin70, 03-24-2023, 04:58 AM
                          15 responses
                          114 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by The_Sec, Today, 02:29 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X