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

For Loop Example Requested

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

    For Loop Example Requested

    Hi there,

    I'm trying to convert a TradingView strategy onto NT8 and struggling to port over a for loop that I'd used - hoping someone may please have an example snippet of a for loop to illustrate how it should be done.

    Here's the code I've tried that is throwing an error of "cannot apply indexing with [] to an expression of type 'double'". It's within OnBarUpdate: It is trying to loop through the last 15 bars to see if each bars Close Price meets a variable (myVarOne & myVarTwo), which are and need to be doubles), and essentially counts them (aka how many of the last 15 bars did the close price equal variable X?).

    Code:
    for (int i = 0; i < 15; i++)
    {
    if (Close[i] == myVarOne[i])
    {
    //do something
    }
    if (Close[i] == myVarTwo[i])
    {
    //do something
    }
    I've also tried a foreach but no joy. This may be a C# general coding question and if so will research further, but as been at it for 90mins+ already I'm hoping to understand how others implement their loops through price bars.

    Many thanks in advance

    ChainsawDR

    #2
    Hello ChainsawDR,
    It seems you're missing parenthesis of for loop, consider below code:
    Code:
    for (int i = 0; i < 15; i++)
    {
    if (Close[i] == myVarOne[i])[INDENT]{
    //do something
    }[/INDENT]
      if (Close[i] == myVarTwo[i])[INDENT]{
    //do something
    }[/INDENT]
     }
    Be sure myVarOne & myVarTwo are double series (not just double variables). When you need to use these as double you can call them as myVarOne[0] & myVarTwo[0].
    Hope it helps!

    Comment


      #3
      Originally posted by ChainsawDR View Post
      I'm trying to convert a TradingView strategy onto NT8 and struggling to port over a for loop that I'd used - hoping someone may please have an example snippet of a for loop to illustrate how it should be done.

      Here's the code I've tried that is throwing an error of "cannot apply indexing with [] to an expression of type 'double'". It's within OnBarUpdate: It is trying to loop through the last 15 bars to see if each bars Close Price meets a variable (myVarOne & myVarTwo), which are and need to be doubles), and essentially counts them (aka how many of the last 15 bars did the close price equal variable X?).

      Code:
      for (int i = 0; i < 15; i++)
      {
      if (Close[i] == myVarOne[B][COLOR=#e74c3c][i][/COLOR][/B])
      {
      //do something
      }
      if (Close[i] == myVarTwo[B][COLOR=#e74c3c][i][/COLOR][/B])
      {
      //do something
      }
      I've also tried a foreach but no joy. This may be a C# general coding question and if so will research further, but as been at it for 90mins+ already I'm hoping to understand how others implement their loops through price bars.
      Your loop is fine, that's not the problem.

      If myVarOne and myVarTwo are simple double variables, like you say,
      then don't append the suffix '[i]' -- that makes the compiler think the
      variable is something with an index -- like an array, or a list, or a Series.

      Remove that '[i]' and you should be fine.
      Why? Because a simple double variable does not have an index.

      Also, sit down and study the compiler error message. It is spot-on,
      drop-dead 100% accurate and completely precise. It is worded
      exactly, and absolutely describes your problem -- make sure you
      embody the lesson of that error message -- the message says
      'expression' but it's referrng to myVarOne and myVarTwo.

      Comment


        #4
        Thank you both for helping put me on the right path, much appreciated!

        Comment


          #5
          In case others run up against a similar issue in future, this was resolved by:

          Code:
          //Adding the series in the intro public class section:
          private Series<double> myVarOne;
          private Series<double> myVarTwo;
          
          //adding the series within State.DataLoaded - aka at this point its empty, its just been initiated to be used later:
          myVarOne = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
          myVarTwo = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
          
          //Finally within OnBarUpdate, any Adds TO the series, or Reading FROM the series need to have the index specified, e.g.:
          
          myVarOne[0] = Close[0] //adds the close price to this variable
          Thank you again s.kinra & bltdavid for the guidance!

          ​​​​​​​ChainsawDR

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Haiasi, 04-25-2024, 06:53 PM
          2 responses
          17 views
          0 likes
          Last Post Massinisa  
          Started by Creamers, Today, 05:32 AM
          0 responses
          5 views
          0 likes
          Last Post Creamers  
          Started by Segwin, 05-07-2018, 02:15 PM
          12 responses
          1,786 views
          0 likes
          Last Post Leafcutter  
          Started by poplagelu, Today, 05:00 AM
          0 responses
          3 views
          0 likes
          Last Post poplagelu  
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,408 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Working...
          X