• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

nested for loop of a barIndex

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

  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    But i am disappointed, Printing(value) after the if statement does show that every value are processed in the if statement and i even check in my formulas and its working.

    Like i said at the end there is a list that gather all result from the formulas and a lambda is choosing the closest value from my variable close[0].
    From what is provided I can't see what you are printing to understand this statement. If you are mentioning a problem here you would likely need to continue to debug the situation using prints and/or use C# learning resources related to what you are doing to explore that further.



    The variable nearclose at the very end of the bottom line should of produce 15 returns instead of only one since there is 15 values tested in the if statement.

    Is there a way that the 15 values tested in the if statement be tested one by one and not as a group of values?
    It does not appear you included this syntax in your sample, I am not able to see any variables named "nearclose" shown. As such I would not be able to provide any detail there without the full context. Because your questions relate to using if statements and also seems to include a loop I could suggest to use C# educational resources to gather more information about the syntax you are using and how it can be used. Alternatively I can also suggest to outsource this work to a third party developer to expedite the development process.


    Please let me know if I may be of further assistance.

    Leave a comment:


  • frankduc
    replied
    Hello Jesse,

    I did what you were suggesting and move everything to OnBarUpdate.

    So now you can say that every values of the RMMA are applied to the if statement.

    Code:
    for(int i = 0; i < RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values.Count(); i++)
            {
                value = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[i][0];
    
                       
    
          if(value < highPrice && value > lowPrice)
           {
    But i am disappointed, Printing(value) after the if statement does show that every value are processed in the if statement and i even check in my formulas and its working.

    Like i said at the end there is a list that gather all result from the formulas and a lambda is choosing the closest value from my variable close[0].

    The variable nearclose at the very end of the bottom line should of produce 15 returns instead of only one since there is 15 values tested in the if statement.

    Is there a way that the 15 values tested in the if statement be tested one by one and not as a group of values?


    Frank

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    This is what you provided, but i dont see how it relates to barsago. I dont understand what you mean by using barago. Its not different from using select or a foreach?
    In the select statement you are specifically selecting one value, what I had provided is the normal way to access a series which includes the BarsAgo. I suggested this because this relates to the normal ways to access data or how you would access data in all other areas of OnBarUpdate. If you wnat to use Linq and C# methods you can but that would be something you need to learn about/execute yourself.

    BarsAgo and Series are NinjaScript concepts you can read about in the help guide. If you are having difficulty understanding how to use the series from the indicator, I would suggest to research the help guide further or do more simple tests to better understand the syntax/framework you are using.

    If i try to Print(value); outside of the brackets i get nothing even if i call a variable

    So nothing happen in my if statement: it return nothing and i dont understand why.
    It seems there is still some confusion surrounding the core C# concepts used here such as loops and scope. I am not able to walk through using the loop/if statements again at this point as this is a C# language feature. I would suggest to either use C# tutorials to gain a firm understanding of C# syntax before you continue or to outsource this work to a NinjaScript developer.


    Can i move the for loop of the RMMA from OBU to OR? Wont work? Its always the same issue outside or inside the bracket and you get a different result.
    OnRender and OnBarUpdate are for different purposes. I know you have tried this in our past conversations, I would not suggest trying to do that. If you are able to execute your goal from OnBarUpdate successfully you could look at using OnRender if required, however OnRender is just for rendering we do not suggest using that for calculation logic.

    Please let me know if I may be of further assistance.

    Leave a comment:


  • frankduc
    replied
    Jesse,

    Edit update:

    I forgot to remove the local variable double value.

    Now the variable value is working in the if statement but it only returns one value not all of them (15 values). Obviously the last one in the list of the OW.

    Can i move the for loop of the RMMA from OBU to OR? Wont work? Its always the same issue outside or inside the bracket and you get a different result.

    Frank
    ty



    Leave a comment:


  • frankduc
    replied
    Jesse,

    First, this is working. It returns all values in RMMA if i print.

    Code:
    for(int i = 0; i < RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values.Count(); i++)
            {
                double value = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[i][0];
    
             Print(value);
              }

    This is what you provided, but i dont see how it relates to barsago. I dont understand what you mean by using barago. Its not different from using select or a foreach?

    Code:
    for(int i = 0; i < Values.Count(); i++) { double value = Values[i][0]; }
    If i try to Print(value); outside of the brackets i get nothing even if i call a variable

    Code:
    private double value = 0;
    So nothing happen in my if statement: it return nothing and i dont understand why.

    Code:
    if(value < highPrice && value > lowPrice)
    TY

    Frank

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    What you provided will not compile, are you certain that you compiled successfully before re testing?

    You are now trying to print the series<double> directly instead of using a BarsAgo and getting a value from that series. In what I provided previously that shows how to also use a BarsAgo with a Values collection. You would need to form the syntax to use a BarsAgo to get the value you wanted.




    Please let me know if I may be of further assistance.

    Leave a comment:


  • frankduc
    replied
    Ok but i still get a bunch of zero's:

    Code:
    for(int i = 0; i < RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values.Count(); i++)
        {
                double value = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[i];
    
               Print(value);
            }

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    In your second sample you would need to use the indicators values collection from within the loop as well, just like you did with the Count. You are using the parent indicators "Values" collection from within the loop and have not used the RMMA's Values collection. Replace "Values" in the loop with your indicator syntax and its Values collection.

    Please let me know if I may be of further assistance.

    Leave a comment:


  • frankduc
    replied
    Hello,

    Ok but how do we use it?

    I tried

    Code:
    myrmma = RMMA(MultiMA1types.VWMA, 1, 160, 1, 2, 128, 0.75, 0.5) [0]; for(int i = 0; i < Values.Count(RMMA); i++) // or
    Values.Count(myrmma)
    { double value = Values[i][0]; Print(value); }

    I get errors

    NinjaScript File Error Code Line Column
    SampleDisplayBarsAgo.cs' NinjaTrader.NinjaScript.Series <double> [] 'does not contain a definition for' Count 'and the best overload of the extension method' System.Linq.Enumerable.Count <TSource> (System.Collections. Generic.IEnumerable <TSource>, System.Func <TSource, bool>) 'contains invalid arguments CS1928 105 23


    NinjaScript File Error Code Line Column
    SampleDisplayBarsAgo.cs Argument 2: Can not convert group methods to System.Func <NinjaTrader.NinjaScript.Series <double>, bool> 'CS1503 105 36


    This is returning a bunch of zero's

    Code:
    for(int i = 0; i < RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values.Count(); i++)
         {
                double value = Values[i][0];
    
               Print(value);
         }


    Frank
    TY



    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    If you are trying to loop over the plots you can just loop over the Values collection directly. The Select is not needed to loop over the existing plots, I would likely suggest using just a for loop for simplicity here.

    This would be a basic loop over an indicators Values collection:

    Code:
    for(int i = 0; i < Values.Count(); i++)
    {
           double value = Values[i][0];
    }
    This would give you access to the whole series and BarsAgo from within the loop to do any calculations.





    Please let me know if i may be of further assistance.

    Leave a comment:


  • frankduc
    replied
    Hello,
    I have been able to create a variable with all the plots from the RMMA.

    Code:
    NinjaTrader.NinjaScript.Series<double>[] series = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values;
    
                    IEnumerable<double> firstValues = series.Select(s => s[0]);
    
        foreach (double firstValue in firstValues)
        {
           Print(firstValue);
        }
    For some reason if i try to install firstValue into the if statement it returns nothing.

    Code:
    if(
    firstValue
    < highPrice &&
    firstValue
    > lowPrice) //Print(
    firstValue); ////return none//
    Any link to ninjatrader?

    Thanks

    Leave a comment:


  • frankduc
    replied
    Hi Jesse,

    You are right on one thing i am not sure what it takes to solve that issue.

    In my indicator a for loop returns a variable call foundIndex which is equal to a certain barIndex.
    foundIndex can return bar 1020, 500, 600, etc.
    From foundIndex i create my moving average, so 160 is an exemple because foundIndex change constantly. foundIndex is moving in time and i choose 160 because there is 160 periods between foundIndex and CurrentBar for the exemple.

    Now i need to plot all the moving averages between foundIndex and CurrentBar. Meaning one moving average from bar 160 to CB, bar 159 to CB, 158 to CB, etc.

    RMMA allow me to do that. If i input 160, 2, 1 it will create 160 plots of VWMA.

    Is there a way i can enter in the same if statement the two variables or all the plots and test them in one shot?

    Code:
    double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
    double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];
    Code:
    if(fistPlot< highPrice &&fistPlot> lowPrice)
    
    if(secondPlot< highPrice &&secondPlot> lowPrice)
    I cannot repeat the if statement for each 160 plots, it will never end. If i do that i will also have to repeat all the structure of the formulas. There must be a more productive way to do that.

    That's not my only problem. After the if statement the result is tested by passing throughout many formulas. At the end there is a lambda expression that chose one result i call the nearclose.
    The closest value from close.
    I need nearclose to return a value for each plot. Meaning 160 plots, 160 value returned by nearclose.

    My collection attempt like you said is not made correctly because at the end nearclose is returning one answer.

    I am not blaming you if you are not follow me, without the code and all structure or logic behind the project its difficult to grasp.

    Thank you for doing the best that you can.
    Last edited by frankduc; 10-21-2019, 11:34 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    From the given information it is still not clear what you are trying to accomplish.

    A collection is not likely the correct solution for the goal, this was only mentioned based on your note:
    But what if i need the 160 period or more, i cant create 160 variables?
    If you had 160 variables defined a collection may be good for that use case to avoid using that many variables. The syntax you have shown you are only using two specific plots and the collection is likely not being used correctly. Additionally not all the relevant code was provided with your sample for me to understand what you were attempting to do here.

    If you think you may need a collection, start by only using variables to accomplish the goal. Make the code work using only variables even if it takes 190 variables. Once you have it working, if you feel that you have used too many variables we could look at how to reduce the code. Because you are not certain right now if a collection should be used, or how it should be used I would likely suggest to remove that entirely and start with a more simple test. Collections are also not something our support will assist with, you can learn about C# collections and how to use them in external learning resources.

    Lets step back to your last question again and try to clarify your needs further:

    But what if i need the 160 period or more, i cant create 160 variables?
    The RMMA produces 9 plots and takes an input period which you supplied as 160. The internal logic of that indicator uses the 160 to produce the 9 plots. If you needed a 160 period RMMA you would supply 160 as its period and call it like you have shown. If you instead wanted a different period like 140 you would enter 140 instead of 160. In the case you needed both the 160 RMMA and 140 RMMA at the same time, you will need to call the RMMA two times so you can provide the period each time.

    For your question are you asking how to call the RMMA more than one time within a bar to get more than 9 values at once? What specifically are you trying to do with the RMMA?




    Please let me know if i may be of further assistance.








    Leave a comment:


  • frankduc
    replied
    Jesse,

    Technically, if it was a CMA it would be only one moving average (one plot). But RMMA throw 160 averages of one kind of ma with a stepaverage of 10 producing 9 plots or (9 VWMA's) in this case.
    The more ma's the more precise, so i will probably lower stepaverage to 1, so 1 ma by 160. But 160 is an exemple it could be actually many more.

    About the collection the only way i see, it would be possible, is if i create one variable per plot? That makes to many variable to create to put in a collection. For exemple:

    Code:
    double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[15][0];
    double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[14][0];
    
       var moavrs = new List<double> { fistPlot, secondPlot};
    
       foreach (var moavr in moavrs)
                  {
                   myavo = moavr;
    
                  }
    Than i include myavo in the if statement. But i have to create as many variables as need it to install them into a collection.

    Code:
    if(myavo < highPrice && myavo > lowPrice)
    Was this the way you were seeing this? Cause for now with my knowledge that is the best i can think of. Is there another way we can include in the collection all the 9 plots or more plots automatically without having to create a variable for each?

    Something is strange, when Print(myavo); it flash the result of plot 14 and 15 but its often flashing more than 2 answers. Sometimes 4 or 6, i was expecting to return only 2 answers?

    Frank
    Thanks

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    But what if i need the 160 period or more, i cant create 160 variables?
    You are talking about two different things here. The period is not the same as your plots, you need 9 variables because you have 9 plots and you want to use the value in OnRender. You can use any period you want and re calculate, the 9 plots would not change only the calculated value which is being plotted changes. The period is a parameter you are providing to the indicator.

    If you actually mean you need 9 plots, plus 160 variations of 9 plots, yes that will be a lot of variables because that is 160 X 9 if you wanted to plot that. In that situation you wouldn't use variables and would use a collection.


    I look forward to being of further assistance.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by TazoTodua, Today, 03:01 PM
2 responses
5 views
0 likes
Last Post NinjaTrader_Jesse  
Started by Mykro, Today, 02:16 PM
1 response
10 views
0 likes
Last Post NinjaTrader_Jesse  
Started by tonynt, Today, 02:06 PM
3 responses
6 views
0 likes
Last Post NinjaTrader_Jesse  
Started by itrader46, Today, 01:53 PM
1 response
3 views
0 likes
Last Post NinjaTrader_Jim  
Started by maindoor, Today, 01:06 PM
1 response
10 views
0 likes
Last Post NinjaTrader_PatrickG  
Working...
X