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

nested for loop of a barIndex

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

  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    I am not even sure yet it returns the right answer because of the slowness and recurent printing.
    Yes this type of calculation would not be suggested for OnRender.

    If you are using 0 to the count, that is for all bars in the series which is not the intended use for OnRender. In most cases on render should only be used to render data for the visible bars. Is there a reason you are not using OnBarUpdate for this use case? OnBarUpdate would be a good candidate for 0 to count processing, that covers each bar.

    OnRender is called very frequently, which can be ever faster than 1 tick frequency so it will use a lot of CPU if you are doing something that takes time to calculate. It would likely be suggested to pre calculate the data in OnBarUpdate and then just render that data from OnRender for this type of use case.

    If you are trying to use the RMMA, you could copy its logic to OnBarUpdate and then use OnRender to display that in a custom way, that would be the most efficient way to approach this. If there is more context to the problem that I am missing you could certainly add that so we can cover that as well.

    I look forward to being of further assistance.





    Leave a comment:


  • frankduc
    replied
    Well, the cma returns the cumulative average of all bars between foundIndex and
    ChartBars.ToIndex. It return one value outside the brackets like 2230.23
    I am trying to reproduce the RMMA indicators with you can set as many moving average as there is bars.

    The RMMA use a for loop

    for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
    {
    switch (MaToUse)
    {
    case MultiMA1types.DEMA:
    Values[count][0] = DEMA(i)[0];
    break;

    So i was thinking i could simply use a nested for loop to increment from 0 to
    ChartBars.ToIndex and replace foundIndex by i from the first for loop to get every cma's for every bars.

    That in return produce a large amount of datas (cma's) like 2230.23, 2231.25, 2232.21 etc one for each bars (representing a cma for each bars) The RMMA when you use with a
    StepAverage of 1 create as many MA as there is bars and can be quite slow. I am not even sure yet it returns the right answer because of the slowness and recurent printing.

    My nested for loop just lead to freezing after a moment when displayed in Output Window. If the cma returns only one value it means it returns one value for all my codes. I need the cma to return multiple values, like Print inside the brackets, outside to work for the rest of the other cma variables in the code of the indicator.

    Thanks for the info and webpages i didn't know you could test in the browser codes.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    Is it possible to declare a variable with NT method as a class level? Like private double closePrice = Bars.GetClose(barIndex); of course i tried and it didn't work.
    No, this is not specific to NinjaScript. You cannot use runtime variables as a variable declaration like this. If you want to use the Bars object it needs to be defined before you used it. This would need to be called from within OnBarUpdate or where it has a value. Class level variables need a non-variable default such as = 0, later you can reset it once the object in question has a value.

    Its because i try to nest a for loop in my cma and if i print inside the bracket i get the result of many cma's , but outside the brackets i get only one return. I can figure out why my first for loop wont apply outside the brackets? Do i need to declare all variables as private?
    In what you provided you are:
    • Resetting the value for cma over and over. Each time you loop, cma gets a new value because you used a variable outside the loop and you are assigning a new value in the loop.
    • The print directly below it should be printed many times or as many as your loop iterates because it is inside the loop.
    • The print which is outside of the loop can only print one time, its not in the loop. That would also be the last value set for cma, or the last iteration of your loop.

    I am not really certain what you are going for with the supplied code however you are exploring concepts of C# which are not specific to NinjaScript. If this is a difficult concept to tackle in NinjaScript I would suggest to start more simple with C# for loops outside of NinjaScript. Here are some ways you can do that.

    MSDN allows you to run the code in the browser so you don't need to install visual studio, I would suggest reviewing some of the looping samples if using for loops is not yet clear: https://docs.microsoft.com/en-us/dot...for#code-try-1

    You can also use public compiler services like https://dotnetfiddle.net/ to test C# code in the browser. This is helpful if you want to just test C# code, you wont need to make a whole indicator or setup a test chart to test it.

    One way to simplify this question so you can work through the problem would be to stop using NinjaScript variables and hard code all values. For example, replace the ChartBars.ToIndex with a specific value to give the loop a finite amount of iterations. Also replace the Bars.GetClose(barIndex); code with specific values essentially removing all NinjaScript code. Next take your loop into one of the testing platforms like dotnetfiddle or visual studio and test the loop and output the values.



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

    Leave a comment:


  • frankduc
    replied
    Is it possible to declare a variable with NT method as a class level? Like private double closePrice = Bars.GetClose(barIndex); of course i tried and it didn't work.

    Its because i try to nest a for loop in my cma and if i print inside the bracket i get the result of many cma's , but outside the brackets i get only one return. I can figure out why my first for loop wont apply outside the brackets? Do i need to declare all variables as private?

    private double cma = 0;
    double sum1 =0;
    double sum2 = 0;

    for (int i = 0; i <=
    ChartBars.ToIndex
    ; i++)
    {
    for(int barIndex = i; barIndex <= ChartBars.ToIndex; barIndex++)
    {
    double closePrice = Bars.GetClose(barIndex);
    double volumes = Bars.GetVolume(barIndex);

    double clovol = closePrice * volumes;


    sum1 += clovol++;
    sum2 += volumes++;

    cma = sum1 / sum2;

    Print(cma);
    }
    }

    Print(cma);
    Last edited by frankduc; 08-21-2019, 06:22 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    This is not specific to NinjaScript, this is a general C# error. You can read about this error in the following public page: https://docs.microsoft.com/en-us/dot...essages/cs0201

    Without seeing what line this was reported for it would be hard to say, however the following syntax looks odd:

    Code:
    xlist[x];
    You may try removing that line to see if the error resolves.

    It also looks like you are using the list later in the condition without an index, you likely have a few problems in this syntax and will need to undo and re-try whatever you were attempting.

    If you need assistance with using List<T> I would suggest using C# educational sites to learn more about their use. Lists are not a NinjaScript type so this is not going to be a documented/supported item in the help guide however there are a large number of sites which offer information on lists in C#.


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

    Leave a comment:


  • frankduc
    replied
    I solve the nested loop but i am having error script related to NT or c#.

    My cma will evolve in time and i want to test every price in the loop in my if statement.

    double Price = Bars.GetClose(barIndex);

    List<double> xlist = new List<double>() {Price};

    for(int x = 0; x < xlist.Count; x++)
    {
    xlist[x];
    }
    if(xlist < highPrice && xlist > lowPrice)

    Its returning error:

    NinjaScript File Error Code Line Column
    SampleDisplayBarsAgo.cs Only an assignment, call, increment, decrement, wait, and new object expressions can be used as a CS0201 instruction.



    thanks

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    It looks like this is a general C# question, I would suggest using external resources for this type of question. You could start at the following public link for nesting loops in C#: https://www.tutorialspoint.com/cshar...sted_loops.htm There are many other guides and samples for this question, you can search online for "C# nested for loop".

    The error you are seeing is caused by using a variable where it is not defined. It may be due to the structure you used however the sample you provided is not clear enough to see the problem.

    I would suggest to review C# materials surrounding nesting for loops first. You can compare some samples against what you created to see if anything sticks out as the problem. You have used barIndex in one of your loops so It could be related to where you have used barIndex in the script or if you tried to use this variable outside the loop.



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

    Leave a comment:


  • frankduc
    started a topic nested for loop of a barIndex

    nested for loop of a barIndex

    I am trying to get the volume between specific barIndexes.

    Anyone know how you nest this:

    for(int barIndex = index; barIndex <= ChartBars.ToIndex; barIndex++)
    {
    double highPrice = Bars.GetHigh(barIndex);
    double lowPrice = Bars.GetLow(barIndex);
    double vols = Bars.GetVolume(barIndex);

    sumvolms += vols;


    if(cma < highPrice && cma > lowPrice)
    {
    for(int barIndex0 = barindex; barIndex0 <= ChartBars.ToIndex; barIndex++)
    {

    It says that barIndex dont exist. I just want the volume between index and each price that cross the if statement.

    any hints?
    thanks

Latest Posts

Collapse

Topics Statistics Last Post
Started by cre8able, Today, 03:20 PM
0 responses
5 views
0 likes
Last Post cre8able  
Started by Fran888, 02-16-2024, 10:48 AM
3 responses
47 views
0 likes
Last Post Sam2515
by Sam2515
 
Started by martin70, 03-24-2023, 04:58 AM
15 responses
114 views
0 likes
Last Post NinjaTrader_Jesse  
Started by The_Sec, Today, 02:29 PM
1 response
7 views
0 likes
Last Post NinjaTrader_Jesse  
Started by jeronymite, 04-12-2024, 04:26 PM
2 responses
31 views
0 likes
Last Post NinjaTrader_BrandonH  
Working...
X