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

Calling two time series and order entry inside an indicator

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

    Calling two time series and order entry inside an indicator

    Hi,
    I am calculating ATR using 1 min and 5 min data series. I followed the steps in the help pages. However, the ATR of primary series is always 0 whereas the secondary series is properly calculated. Would you please help?

    Here is the relevant part in OnStateChange()
    Code:
    // Find the Sim101 account
    lock (Account.All)
    myAccount = Account.All.FirstOrDefault (a => a.Name == "Sim101");
    }
    else if (State == State.Configure)
    {
    AddDataSeries(BarsPeriodType.Minute, 5);
    }
    else if (State == State.DataLoaded)
    {
    // initialize the ATR using the primary series and assign to atr1
    atr1 = ATR(BarsArray[0], 14);
    // initialize the SMA using the secondary 5 minute series and assign to atr1
    atr2 = ATR(BarsArray[1], 14);
    }​
    and here is the relevant part in OnBarUpdate

    Code:
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
    // ensure both series have at least one bar
    if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
    return;
    
    // when the 5 minute series is processing set the secondary plot to the atr with the secondary series input
    if (BarsInProgress == 1)
    ATRSecondary[0] = atr2[0];
    
    // when the primary series is processing set the primary plot to the atr with the primary series input
    if (BarsInProgress == 0)
    {
    ATRPrimary[0] = atr1[0];
    
    // if the secondary 5 minute series did not close, set the current bar's value to the previous bar's value to prevent gaps
    if (!ATRSecondary.IsValidDataPoint(0))
    ATRSecondary[0] = ATRSecondary[1];
    }
    if (State == State.Historical || CurrentBar < 14)
    return;
    
    ​Print(ATRPrimary[1]);
    The print output is always zero.
    Thank you.

    #2
    Hello MSerag,

    Thank you for your post.

    Do you receive any errors on the Log tab of the Control Center? Since you are setting ATRPrimary[0] to equal atr1[0] I suggest printing out the value of atr1[0] as well. What might be happening is that the atr1 has a period of 14, and there might not be 14 bars on the chart yet when you try to access its value. You have a check if CurrentBar < 14 but that is after you access the value of atr1[0]. You could try adding that check more toward the beginning of OnBarUpdate() if you want that condition to apply before trying to access the atr1 or atr2 value.

    For more information about making sure you have enough bars in the data series you are accessing, please see the following page:


    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      No I do not receive errors. The secondary series is 5 min which is calculated properly, meaning that there is enough bars for the 1 min.
      Just to put you in more context. The indicator submitting an order is attached to a market analzyer and here is the print out. Apparently, it is calculated properly when I reload Ninjascript, but the primary series always gives me a zero afterwords. Below are the prints

      ATR primary series = 0.214032841232479
      ATR secondary series = 0.330427544063112
      ATR primary series = 0
      ATR secondary series = 0.283618362708742
      ATR primary series = 0
      ATR secondary series = 0.283618362708742
      ATR primary series = 0
      ATR secondary series = 0.283618362708742

      Comment


        #4
        Hello MSerag,

        Thank you for your reply.

        What were the results of trying to print the value of atr1[0]? This will let us know if atr1 is accurately reflecting the values of ATR(BarsArray[0], 14) and then if ATRPrimary is giving the unexpected behavior, it may have to do with how the plot is set up.

        Your snippet does not demonstrate where you are calling AddPlot; here is a snippet of a test script I created with two plots set to an ATR based on the primary series data and an ATR based on the secondary 5-minute series, both with a period of 14. I added the indicator to a 1-minute chart without any issues. The script prints every time OnBarUpdate() is called with the values of both plots as expected:

        Code:
                private ATR atr1, atr2;
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        AddPlot(Brushes.Orange, "ATRPrimary");
                        AddPlot(Brushes.DarkViolet, "ATRSecondary");
                    }
                    else if (State == State.Configure)
                    {
                        AddDataSeries(Data.BarsPeriodType.Minute, 5);
                    }
                    else if (State == State.DataLoaded)
                    {
                        atr1 = ATR(BarsArray[0], 14);
                        atr2 = ATR(BarsArray[1], 14);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    if (CurrentBars[0] < 14 || CurrentBars[1] < 14)
                        return;
                    ATRPrimary[0] = atr1[0];
                    ATRSecondary[0] = atr2[0];
                    Print("BIP: " + BarsInProgress + " Primary: " + ATRPrimary[0] + "Secondary: " + ATRSecondary[0]);        
                }
        ​
        I suggest printing and comparing the values of ATR(BarsArray[0], 14)[0], as well as atr1[0], and finally ATRPrimary[0] to see where the value of 0 might be coming from.

        Please let me know if I may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily, I used the exact same code and I get the below compilation errors:
          The name 'ATRPrimary' does not exist in the current context.
          The name 'ATRSecondary' does not exist in the current context.
          Would you please let me know what would be the source of the error?
          Thanks.

          Comment


            #6
            Hello MSerag,

            Thank you for your reply.

            The code provided was only a snippet; it does not include necessary information related to plots that gets added to the "Properties" region when the plots are added via the NinjaScript wizard. This is why the tips on the AddPlot() page in the help guide state "We suggest using the NinjaScript wizard to generate your plots"


            For example, see the snippet on that page for "Indicator using a public Series<double> to expose a plot with a friendly name. This is required for making plots accessible in the Strategy Builder"

            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by trilliantrader, 04-18-2024, 08:16 AM
            7 responses
            27 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by samish18, 04-17-2024, 08:57 AM
            17 responses
            64 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by rocketman7, Today, 02:12 AM
            2 responses
            17 views
            0 likes
            Last Post rocketman7  
            Started by briansaul, Today, 05:31 AM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by PaulMohn, Today, 03:49 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X