Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Value of Instrument.FullName in Initialize()

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

    Value of Instrument.FullName in Initialize()

    I'm trying to access the name of the instrument in the Initialize() but it is returning unexpected values.

    I am running the strategy in the Strategy Analyzer and I have selected a particular instrument in the left hand column, but in the Initialize() it is giving me the name of another instrument.

    Do I need to refer to the instrument through a particular object in Initialize()?

    Thanks

    #2
    I have just found old posts on the forum where NT support stated that the correct value for Instrument isn't available in the Initialize() method.

    Maybe somebody can give me a work-around for this:

    I run a multi-instrument strategy with 20 forex pairs. I have several different sources of historical data, so I have $EURUSD_IB as an example for Interactive Brokers instruments, $EURUSD_DTC for DTC data, $EURUSD_FXCM for IQFeed/FXCM data and more.

    Instead of hard-coding all the forex pair instrument names into the strategy to add them as a new Bars series, I hardcoded the basis names e.g. "$EURUSD" in an array and then I check to see what the primary instrument is. So if I choose the primary instrument to be $EURUSD_IB, then the strategy knows it must load up all the IB forex pairs as additional instruments. It just appends "_IB" to the basis names and does all the Add().

    That was the theory. I now realize I can't do this because the primary instrument isn't given in the Initialize() where I need it. Is there anything I can do other than adding a parameter that specifies the instrument name suffix?

    I figure that this isn't so desirable because I still have to choose the primary instrument, and so I have to write a routine to error-check that I don't choose an instrument name suffix that doesn't match the primary instrument chosen.

    Thanks, if there is any alternative suggestion!

    Comment


      #3
      adamus, unfortunately I'm not aware of a supported workaround other than adding a string input for the base so you can specify what you need at runtime.

      If you like to look into another (async) way to load additional series, you can review the default Pivots indicator code (GetBarsNow).

      Comment


        #4
        adamus,
        I had the same problem, maybe this is a workaround you like:

        Comment


          #5
          I have found this exposes me to a major waste of time if I do make a mistake, because the Strategy Analyzer will load up and run through all the data for all the instruments, even though I have a flag to bail out of the onBarUpdate() immediately on this kind of error.

          Comment


            #6
            Thanks for the input. Didn't see that before I posted.

            Comment


              #7
              Hi,

              after ignoring this problem for the last 6 months or so I have made some changes to my strategy in the Initialize() event which I can see no way to solve.

              Every time I use the strategy analyzer, the strategy's Initialize() event is called twice, as documented here, but in this case it is problematic because the code I am using in Initialize() cannot handle the random instrument that is set for the first call to Initialize(), so I see an error everytime.

              I assume this will carry on when I implement the strategy in the strategy runner, which I can't allow, so I need a work-around.

              There are numerous temporary work-arounds, such as checking for the random instrument selected in the first call to Initialize(), which seems to remain constant for a certain time, but this isn't robust enough since it would require constant editing and recompilation whenever it does change.

              Is there anything else that changes in the same way as Instrument?

              Is there any other way I could detect that the call to Initialize() is the first call and not the second call?

              Thanks
              Last edited by adamus; 06-27-2011, 09:26 AM.

              Comment


                #8
                I use a lot without a problem.

                Comment


                  #9
                  Hi adamus, we discussed here and unfortunately are not aware of a way to distinguish the mulitple Init calls. For some of that reasons OnStartUp() was introduced so custom resources could be allocated.

                  Comment


                    #10
                    Originally posted by Baruch View Post
                    I use a lot without a problem.
                    In strategies? I assume you don't have anything in your Iniitialize() event which breaks when it's called before the instrument name is initilazed?

                    Comment


                      #11
                      Hi Bertrand,

                      I guess you know I need to carry out Add() calls based on the Instrument.FullName. This obviously won't work in OnStartUp(). I have no idea abot the event model behind strategy running, but I hope NinjaTrader can find a way to facilitate this at some point in the future. It's definitely one of the rough edges you need to polish on this software.

                      Thanks.

                      Comment


                        #12
                        In strategies? I assume you don't have anything in your Iniitialize() event which breaks when it's called before the instrument name is initilazed?
                        I guess I don't.
                        This is my Init template:
                        Code:
                          
                        protected override void Initialize()
                          {
                        //ManageTF Last 1 
                           Add(PeriodType.Minute, 1);
                        //ManageTF Ask 2
                           if (UseASK && BackTest) 
                           {
                            LEntrShExitOnBar = 2;
                            Add(Instrument.FullName, PeriodType.Minute, 1, MarketDataType.Ask);
                           }
                           CurInstrument = TInst.ToString();
                           USDQuote = GetUSD ();
                           if (USDQuote == "") 
                           {
                            if (CurInstrument.Substring(0,3) == "USD") ConvertMethod = 2;
                            else ConvertMethod = 1;
                           }
                           else if (USDQuote.Substring(0,4) == "$USD") ConvertMethod = 4;
                           else ConvertMethod = 3;
                           if (ConvertMethod > 2) 
                           {
                        //Series 3 Not Always
                            Add(USDQuote, PeriodType.Minute, 30);
                            if (UseASK && BackTest) USDSerie = 3;
                            else USDSerie = 2;
                           }
                           
                           ExitOnClose   = false;
                           CalculateOnBarClose = true;
                        //   ExcludeWeekend = true;
                        //   TraceOrders = true;
                        //   Slippage = 1;
                           EntriesPerDirection = 1;
                           BarsRequired = 20;
                           IncludeCommission = true;
                           QuantityType = NinjaTrader.Strategy.QuantityType.Strategy;
                        //   SessionData = "CME FX Futures ETH";
                        //   SessionData = "<Use instrument settings>";
                           SessionData = "Default 24/7";
                           EntryHandling = NinjaTrader.Strategy.EntryHandling.AllEntries;
                           EntriesPerDirection = 1;
                           MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                           RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction; 
                           TimeInForce = Cbi.TimeInForce.Gtc;
                           Enabled = true;
                          }

                        Comment


                          #13
                          That all looks like it's error-free as far as the Initialize event without the instrument is concerned.

                          I am running a multi-instrument strategy to force NT7 to give me portfolio-level stats like account drawdown for all the instruments I trade it against.

                          I add all the extra instruments in the Initialize event, obviously. I need to check that I have selected the correct primary instrument that is alphabetically the first in my portfolio, because that is what my portfolio is hard-coded with. Ideally I would hard code the primary instrument too, but that's not possible.

                          So I check that the primary instrument is the instrument I expect in my code. This is what is giving me the annoying error message every time the strategy analyzer reloads - which is frequent. (My own error message - or alert to be specific).

                          Eureka! Of course, I don't need to run the check in the Initialize event - I can shift it to the StartUp event. It's funny how explaining the whole thing in public can bring out the obvious solution.

                          Thanks for being there.
                          Last edited by adamus; 06-30-2011, 04:32 AM.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          646 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          367 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          107 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          569 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          573 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X