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

Use Instrument List in Code for Strategy

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

    Use Instrument List in Code for Strategy

    Hi all,

    I have a question regarding Instrument Lists in Code. My goal is to catch a pre-configured instrument list in Ninjatrader 8, get each instrument from this list, add it as a DataSeries Object in State.Configure and treat exactly this instrument with buy/sell orders in OnBarUpdate().

    The problem is that during back testing, only the instrument selected under Instrument in the strategy analyzer is treated with buy/sell orders. My custom instrument coming in as secondary DataSeries Object is always ignored. I'm expecting that i have to fire the order with the symbol coming from the instrument list but how can i configure a custom symbol for an order entry?

    Code:
        ...
        if (State == State.Configure)
        {
            Collection<NinjaTrader.Cbi.InstrumentList> instrumentListAll = NinjaTrader.Cbi.InstrumentList.All;
            foreach (InstrumentList instrumentList in instrumentListAll)
            {
                if (instrumentList.Name == "My List") // List contains AMZN MSFT GOOG NFLX
                {
                    foreach (Instrument instrument in instrumentList.Instruments)
                    {
                        AddDataSeries(instrument.FullName, Data.BarsPeriodType.Day, 1);
                    }
                }
            }
        }
    }
    
    protected override void OnBarUpdate()
    {
        // Primary instrument is AAPL
        if (CurrentBar < BarsRequiredToTrade)
            return;
    
        if (CrossAbove(SMA(BarsArray[1], 20), SMA(BarsArray[1], 50), 1))
            EnterLong(); // Trades are only executed for AAPL
    
        else if (CrossBelow(SMA(BarsArray[1], 20), SMA(BarsArray[1], 50), 1))
            EnterShort(); // Trades are only executed for AAPL
    
    }
    Attached is a sample code
    Attached Files
    Last edited by Sweet&Sour; 09-02-2018, 06:20 AM.

    #2
    Hello Sweet&Sour,

    Thank you for your note.

    Dynamically adding data series like this would not be supported however I will submit your vote to a feature request tracking this.

    With what you’re trying to do, you may be able to specify a bars in progress to submit the order to, to get orders sent to the additional data series.

    Using the syntax,

    EnterShort(int barsInProgressIndex, int quantity, string signalName)


    Code:
    EnterShort(1,1, “OrderForBIP1”);
    Does the above submit orders to the non primary series?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Not that urgent. The idea behind this is only to prevent a single strategy per stock. If i have 10 Stocks for a single strategy then this will create 10 strategies in Ninja. Furthermore some kind of parameters should be included per stock. I'm also able to solve this using a little text file.

      BTW: Let's assume I would save the stock symbols and a bunch of parameters in a little sql express database. In which State would you recommend to read parameters like a period for a single stock from the sql express database using the code below?

      Code:
      using (sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Ninjatrader;Integrated Security=True;"))
      {
          try
          {
              using (SqlCommand command = new SqlCommand("SELECT Period FROM SymbolsTable where Symbol = '" + Instrument.FullName.ToString() + "';", sqlConnection))
              {
                  sqlConnection.Open();
                  using (SqlDataReader reader = command.ExecuteReader())
                  {
                      if (reader.HasRows)
                      {
                          while (reader.Read())
                          {
                              int period = reader.GetInt32(1);
                          }
                      }
                  }
              }
          }
          finally
          {
              sqlConnection.Close();
          }
      }

      Thanks
      Last edited by Sweet&Sour; 09-05-2018, 02:51 AM.

      Comment


        #4
        Hello Sweet&Sour,

        State.Configure would be the state to pull this from.

        I put together a sample indicator which will read a text file in the NT8 my docs folder, if its not there it will create a text file "symFile.txt" containing the symbol "TSLA".

        When applied to a chart, state.configure will be triggered pulling this symbol from file and passing it to the AddDataSeries call and then print the close price to the output window.

        Please let us know if you need further assistance.
        Attached Files
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by timmbbo, Today, 08:59 AM
        1 response
        2 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by KennyK, 05-29-2017, 02:02 AM
        2 responses
        1,281 views
        0 likes
        Last Post marcus2300  
        Started by fernandobr, Today, 09:11 AM
        0 responses
        3 views
        0 likes
        Last Post fernandobr  
        Started by itrader46, Today, 09:04 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by bmartz, 03-12-2024, 06:12 AM
        5 responses
        33 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Working...
        X