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

Mr. Lee

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

    Mr. Lee

    Hello,

    I'm attempting to run multiple instruments through a single strategy. As an example, I would like to use the stochastics oscillator but pass different values of D's and K's for each instrument in a single script .I'm using the 'add' function in the initialize to add the different instruments to the script. Is this doable? Can you show me the proper form for using the stochastic oscillator on different bars arrays? Thank you.


    protectedoverridevoid Initialize()
    {

    Add("AAPL", PeriodType.Minute, 60);
    Add(
    "AIG", PeriodType.Minute, 60);




    CalculateOnBarClose =
    true;
    }
    protectedoverridevoid OnBarUpdate()

    {
    if (Stochastics(BarsArray[2],7, 14, 3).D[0] < Variable0)
    {
    nShares = (
    int) (1000/High[2]);
    EnterLong(nShares,
    "BuyMkt2");

    }
    if (Stochastics(BarsArray[2],6, 15, 3).D[0] < Variable1)
    {
    nShares = (
    int) (1000/High[2]);
    EnterLong(nShares,
    "BuyMkt2");

    }

    }

    #2
    Hello ciro1963,

    The usage of BarsArray[2] (referring to the AIG added series) is correctly being supplied as a parameter to the Stochastics method.

    Are you receiving any error messages when compiling the script?

    If so, what do these say?

    The logic for the script, however, may have some items that would cause run-time errors.

    When adding multiple series its important to ensure each series has enough bars.

    When calling High[2] this would cause an error if called on the first bar as on the first bar there will not be a bar 2 bars ago.

    To prevent indexing errors check current bar is greater than 2 for each series.

    if (CurrentBars[0] < 2 && CurrentBars[1] < 2 && CurrentBars[2] < 2)
    return;

    Below are publicly available links to the help guide.



    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you ChelseaB. One more question with regard to the following line...

      , if (Stochastics(BarsArray[2],7, 14, 3).D[0] < Variable0)

      Can you tell me the significance of .D[0] ? I don't get any error as would be the case if it were just .D. I thought D was just a constant. I've used the following statement before without error. Why does the statement need to follow the above syntax when dealing with different bars arrays?

      Stochastics(D, K, 3).D, 1, 0) > Variable0.

      Thanks again!

      Comment


        #4
        Hello ciro1963,

        The .D[0] refers to the most recent value of the D plot. The Stochastics indicator has two plots, the D plot and the K plot.

        These are both of the type Series<double>.

        Below are publicly available links to the help guide on Stochastics and Series<Type>.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chelsea. That's what I thought.

          How do I use the BarsSinceExit() command when dealing with multiple instruments? As you can see in the script I posted, I have multiple barsarrays in use. Do I use some sort of specifier for the particular instrument? In my script I have AIG as BarsArray[3].

          Comment


            #6
            Hello ciro196,

            The BarsSinceExit() should use the same BarsInProgress value that the entry order was submitted on.

            BarsSinceExit(int barsInProgressIndex, string signalName, int exitsAgo)

            If the entry is placed on BarsInProgress 2, then the check for BarsSinceExit() should be on BarsInprogress 2.

            Below is a publicly available link to the help guide on BarsInProgress.


            And BarsSinceExit().
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, 04-17-2024, 06:40 PM
            3 responses
            26 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by bmartz, 03-12-2024, 06:12 AM
            3 responses
            28 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by Aviram Y, Today, 05:29 AM
            2 responses
            8 views
            0 likes
            Last Post Aviram Y  
            Started by gentlebenthebear, Today, 01:30 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by cls71, Today, 04:45 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X