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

Add return code

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

    Add return code

    Hi, I have the following Add statement in m indicator:

    Add("ES 03-15", PeriodType.Tick, 100);

    Is there a way I can check whether that instrument exists before connecting to it or is there a way to check whether my "Add" statment worked properly?

    thanks

    #2
    Hello,

    Thank you for your note.

    If the instrument does not exist, the script will not be enabled and there will be an error message about this in the Log tab of the Control Center.

    12/12/2014 8:49:04 AM Default The indicator 'MyCustomIndicator' has called the Add() method with an invalid instrument. Either 'FAKEINSTRUMENT' does not exist in the Instrument Manager or the specified exchange has not been configured.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thanks ... is there a way i can capture this error in my code?

      Comment


        #4
        Hi pman777,

        I'm not aware of any way to capture this error with code and prevent the script from dying.

        However, an unsupported work around might be looping through the instrument list and see if its in there.

        This foreach
        foreach (Cbi.Instrument sInstrument in Cbi.Instrument.GetObjects())
        {
        if (sInstrument.FullName == "symbol")
        {
        // execute code
        }
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by pman777 View Post
          thanks ... is there a way i can capture this error in my code?
          It is .NET. If it throws an error, it should be trappable. Use a try ... catch block.

          Comment


            #6
            Hi koganam,

            I had the same idea to use a try and catch and tested it, but that did not work.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hi koganam,

              I had the same idea to use a try and catch and tested it, but that did not work.
              That sounds then like you are handling an exception using some non-standard method, or there is a bug. Your code is logging an exception, and the code is behaving as if there is an exception, other than the code does not let the user see the exception.

              Is there a particular reason why you must disable a script, without letting the user handle the cause of the exception in code?
              Last edited by koganam; 12-14-2014, 01:56 PM.

              Comment


                #8
                Is there a way I can loop through my 'default' instrument list?

                Comment


                  #9
                  koganam,

                  Originally posted by koganam View Post
                  That sounds then like you are handling an exception using some non-standard method, or there is a bug. Your code is logging an exception, and the code is behaving as if there is an exception, other than the code does not let the user see the exception.
                  I'm not positive but don't think its a .NET error. The error is logged by the Add() call if the instrument does not match an instrument in the master instrument list.

                  I think this error is placed with the Log() call but not from a .NET error.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    pman777,

                    Originally posted by pman777 View Post
                    Is there a way I can loop through my 'default' instrument list?
                    It is not supported to loop through the Default instrument list.

                    However, here is an unsupported tip to point you in the right direction.

                    Code:
                    NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Default");
                    if (list != null)
                    {
                    foreach (Instrument i in list.Instruments)
                    {
                    Print(i.FullName);
                    }
                    }
                    else
                    {
                    //Log("AddInstrumentsByListExample: There is no instrument list named Default", LogLevel.Information);
                    }
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      koganam,



                      I'm not positive but don't think its a .NET error. The error is logged by the Add() call if the instrument does not match an instrument in the master instrument list.

                      I think this error is placed with the Log() call but not from a .NET error.
                      Add() is a method in your application, which is a .NET application. That means that it is subject to the CLR. That it may be an internal method is not germane to the issue. A .NET application that throws an error does it by raising an exception. Why are you not letting us trap that exception?

                      Think about it. Your statement, carried to the logical limits of its meaning, should mean that if OnBarUpdate() throws an error because a bar does not exist, then it should just be logged and nothing else, as OnBarUpdate() is also a NT method, not directly a .NET method. That is certainly not what happens, and by the same token if there is an error in Initialize(), that exception should also be trappable, especially so, as it is not a trivial error: after all, it causes the entire indicator operation to fail.

                      It is not a matter of what particular call caused the error: it is a matter of the error being caused in the method.

                      Comment


                        #12
                        Hi koganam,

                        What I mean is, you can make any method throw a message in the log and output window without it being a .NET error, by calling Log() and Print() at any time.

                        I don't think that there is a .NET issue that is throwing an error and an exception. I think the Add() method just checks for the string being in the array. If its not it calls Log() and mentions this item does not exists. Because nothing invalid in .NET has been done to cause an error, there isn't an exception. Maybe it would be possible to cause a true .NET error.

                        I will try and get further clarity about this from development.

                        While you are correct that this is a .NET application, it is possible to send something to the Log and Output Window() without causing a .NET error that has an exception.

                        So I'm not entirely sure this is actually an error at all, rather than just a message in the Log() that this item doesn't exist.


                        Were you able to use a try and catch on this successfully and capture an exception?
                        Last edited by NinjaTrader_ChelseaB; 12-15-2014, 03:46 PM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          ... Because nothing invalid in .NET has been done to cause an error, there isn't an exception. Maybe it would be possible to cause a true .NET error.

                          ...

                          While you are correct that this is a .NET application, it is possible to send something to the Log and Output Window() without causing a .NET error that has an exception.

                          So I'm not entirely sure this is actually an error at all, rather than just a message in the Log() that this item doesn't exist.
                          That is exactly the point. If there is no exception then the rest of the class should process normally. It does not.

                          It is not just that an error is logged: the class is also disabled. To disable processing is the duty of an exception. So if you are not using an exception, then the error is being handled in a non-standard manner.

                          Not good for an application that is supposed to be extensible, which is the raison d'être of outfits and people like mine and me.
                          Were you able to use a try and catch on this successfully and capture an exception?
                          Again, that is the point. There is an error that disables all processing in the class, and I cannot catch it to handle it. Scraping a log for errors is not the design of .NET.

                          Comment


                            #14
                            Hi koganam,

                            I looked into it and I can confirm that the execution of the script stops if a symbol is used that does not exist without a .NET error being hit with an exception thrown.

                            No .NET error is hit so the try and catch don't work as previously discussed. Currently, the message is put in the Log and Output Window and then a flag is set that causes the process that runs OBU and the other methods to just return (void).

                            The current thinking is that if this message appears in the log, you will need to remove the call for that instrument from the script.

                            However, I am going to submit a feature request to have the script continue and for this call to return a bool. It will be up to our development to decide to implement this in a future version of NinjaTrader.

                            Thank you for the suggestion.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hi koganam,

                              I looked into it and I can confirm that the execution of the script stops if a symbol is used that does not exist without a .NET error being hit with an exception thrown.

                              No .NET error is hit so the try and catch don't work as previously discussed. Currently, the message is put in the Log and Output Window and then a flag is set that causes the process that runs OBU and the other methods to just return (void).

                              The current thinking is that if this message appears in the log, you will need to remove the call for that instrument from the script.

                              However, I am going to submit a feature request to have the script continue and for this call to return a bool. It will be up to our development to decide to implement this in a future version of NinjaTrader.

                              Thank you for the suggestion.
                              Good Lor' !! That sounds like the way that we handled errors when we coded in Assembler, or maybe FORTRAN!

                              Did I just date myself?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by samish18, 04-17-2024, 08:57 AM
                              16 responses
                              55 views
                              0 likes
                              Last Post samish18  
                              Started by arvidvanstaey, Today, 02:19 PM
                              3 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by jordanq2, Today, 03:10 PM
                              2 responses
                              8 views
                              0 likes
                              Last Post jordanq2  
                              Started by traderqz, Today, 12:06 AM
                              10 responses
                              18 views
                              0 likes
                              Last Post traderqz  
                              Started by algospoke, 04-17-2024, 06:40 PM
                              5 responses
                              47 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X