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

Get Last Price inside of OnBarUpdate?

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

    Get Last Price inside of OnBarUpdate?

    i can get current ask, get current bid... but no get current price?

    i'm lost...

    #2
    Hello,

    Close[0] would give you the current traded price


    Please let me know if I can be of further assistance.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Ok... looking through this in the debugger... for some reason my OnBarUpdate has stopped firing. I initially blamed the Close[0] as this had been working before. I've stripped it back to bare essentials... and still no dice:

      protected override void Initialize()
      {
      SetProfitTarget("", CalculationMode.Percent, 5);
      SetStopLoss("", CalculationMode.Percent, 5, false);
      CalculateOnBarClose = false;
      }
      private bool juststarted = false;
      protected override void OnStartUp()
      {
      Log("System Started", LogLevel.Information);
      juststarted = true;
      }
      protected override void OnTermination()
      {
      Log("System Stopped", LogLevel.Information);
      }
      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      Log("Close:"+Close[0], LogLevel.Information);
      }
      Any idea what's gone wrong!?

      Comment


        #4
        Hello,

        Could you please attach the source code so that I may test this on my end?
        Located in (MY)Documents\NinjaTrader 7\bin\Custom\Indicator
        or (MY)Documents\NinjaTrader 7\bin\Custom\Strategy

        I look forward to assisting you further.
        LanceNinjaTrader Customer Service

        Comment


          #5
          attached code

          here ya go...
          Attached Files

          Comment


            #6
            Hello,

            I just tried running the system on my end (with default settings) and it seems to be functioning properly.

            Additionally you might consider ignoring historical data to prevent flooding the Log

            if(Historical)
            return;


            Also, if you are not aware you can use Print() to print out to the Output window (Tools -> output window) so that you do not clutter your log file.

            Attached is my log tab with what is being printed out.

            Please try restarting NinjaTrader and if the issue persists
            Please send me your log and trace files for today so that I may look into what occurred.
            You can do this by going to the Control Center-> Help-> Mail to Support.
            Please reference the following ticket number in the body of the email: ATTN Lance 758601


            Let me know if I can be of further assistance.
            Attached Files
            LanceNinjaTrader Customer Service

            Comment


              #7
              Thanks for the help... working fine now and I feel like a bonehead for not thinking to just restart it first. This is what coding at 5am gets you...

              Log files sent.

              Comment


                #8
                Ok... that sorted me out fine, everything is working in my dev environment with the simulated feed/etc. However. I deployed it to my client, ran through it in the simulated feed... everything worked fine as well. Then today he's all kinds of upset because when he connected it to his live feed to use (via his brokerage account) it's hitting on the wrong amounts. Specifically (as i'm logging the close amounts) it's saying that Close[0] is different that what the actual price is... !?!?

                Huh?!

                I'm fairly confused by this... any advice?

                Comment


                  #9
                  Hello,

                  Your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

                  First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.

                  For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders

                  It may also help to add drawing objects to your chart for signal and condition confirmation - Drawing Objects.

                  If there is a specific error that you would like me to look into please post your source code or simplified version of your code so that I may investigate further.

                  If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings - Click here for a list of certified NinjaScript Consultants

                  Please let me know if I can assist further.
                  LanceNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Lance,
                    It's the same code I already attached in this thread (well, the simplified version is) on the 17th... and I'm using logging to trace the values (i prefer to see them inline with what else is happening)... Close[0], in this case, was showing 1428.5 when in reality the same feed it was being fed from was showing 1443, a HUGE difference and it hadn't been anywhere near 1428 as it was in a downward trend.

                    This was with a live data feed.

                    When I test it from the simulated feed (also the only place I can attach a debugger, because I don't have a broker account. only the guy i'm making this for does), this all preforms perfectly as expected... i'm baffled as to why the live feed would provide wrong information for Close[0].

                    Comment


                      #11
                      Hello,

                      Can you please confirm that you have tested this exact code on his live feed and are receiving incorrect Close[0] values?

                      I am not able to replicate the error on my end with the code provided on either a live connection or simulated.
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        i have sent him back a version to just do massive descriptive logging. my code (that was failing for him) does have some logic in it but it's pretty simple stuff.... a Property he sets as StartValue and then logic inside of OnBarUpdate() to only Log if Close[0] >= that value and <= (that value + 1)... he set it for the 1428 (meaning it would trigger when the price was greater than 1428 and less than 1429) and while it was still 1443 it logged saying it had hit 1428. In the full code I'm actually then calling a webservice at that point, however that's irrelevant to this behavior so I left it out... what was key is that it was getting a false value and giving a false start...

                        I'll update you when he has installed and run the heavy logger version (logs close value, start range from start property, etc when it "triggers" int the logic), including log copies/etc.

                        interesting behavior, it also is set with an end value, and set to trigger a "stop" at that value... it hit them simultaneously, making the think in 1 tick the value was wrong, and in another it was right (still being well above the end value which he had at like 1430 something).

                        at first i questioned my logic, but i've been over it and over it and it's so simple there's just no room for it to be wrong, it's literally (isStarted is false until this is hit, then set to true... to prevent starting every tick while it's in the trade):
                        if ((!isStarted) && ( Close[0] >= P100_StartValue && Close[0] <= P100_StartValue+1))

                        Comment


                          #13
                          Code

                          Here's the exact code (with his URL removed for privacy reasons) I just sent him to test.

                          I have commented out all of the webservice calls and added in a lot more logging.

                          (also, if anyone out there ever wondered how to connect to a webservice from NinjaTrader strategies... here it is...)
                          Attached Files

                          Comment


                            #14
                            Hello,

                            I commented out line 239 to allow it to compile on my end as HttpUtility is not available by default.

                            After doing so I ran the strategy and get one line posted to my log. (see attached)

                            We are not aware of issues causing Close[0] to post incorrect data, the script you provided works ok in all our tests. If there's any issues we're happy to investigate but need it rotted out to a reproducible scenario and simple steps.

                            If it can be reproduced with live market data only, please enable the recorder so you can send us the specific data to playback: http://www.ninjatrader.com/support/h...l?set_up12.htm

                            Please let me know if I can be of further assistance.
                            Attached Files
                            LanceNinjaTrader Customer Service

                            Comment


                              #15
                              An update on this... it works fine. I remoted into his machine and ran it myself with the simulated feed and it did the same thing. Restarted his NinjaTrader and it worked fine... he apparently hasn't closed NinjaTrader for weeks and I guess something was "stuck" that was making it behave oddly.

                              Thanks for all of the help and support, and if you like you could change the title of this thread to something indicating that it has code in it to show people how to consume a web service from a strategy, surely something people could potentially find useful.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cocoescala, 10-12-2018, 11:02 PM
                              7 responses
                              939 views
                              0 likes
                              Last Post Jquiroz1975  
                              Started by oviejo, Today, 12:28 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by geddyisodin, Yesterday, 05:20 AM
                              7 responses
                              46 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by gbourque, Today, 06:39 AM
                              2 responses
                              5 views
                              0 likes
                              Last Post gbourque  
                              Started by cre8able, Yesterday, 07:24 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X