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

  • frankduc
    replied
    Nice solution.

    But what if i need the 160 period or more, i cant create 160 variables?
    Is there a fast way to create 160 variables or a variable that could include the 160 period values?
    A magic trick like :

    double AllPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values
    [0,1,2,3.....160]
    [0];

    Of course that would be to easy.
    Cause this project is going to hit a wall otherwise.

    Thanks

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    If you want to access the other plots you would need to be specific and use the plot names/plot index. Using just the indicator syntax will return only the first default plots value. If you mean to access the other plots the indicator has that would mean you will need 9 variables and to collect those values from the indicator 9 times.

    This is no different than using the stock Bollinger indicator, if you want the Upper and Lower plot values you would have to call the indicator two times to get those values:
    Code:
    double upperValue = Bollinger(Low, 2, 20).Upper[0];
    double lowerValue = Bollinger(Low, 2, 20).Lower[0];


    The RMMA was not programmed to make the plots have names so you could instead use the Values collection just like they do inside of the RMMA:


    Code:
    double fistPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[B][0][/B][0];
    double secondPlot = RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5).Values[B][1][/B][0];
    The first index [0] or [1] which I bolded is the index of the plot, not a BarsAgo. The second index which are not bolded and are both [0] are BarsAgo representing the Current Value.


    I look forward to being of further assistance.

    Leave a comment:


  • frankduc
    replied
    Jesse,

    That's the principle of the RMMA. You choose your moving average in this case the VWMA (testing) or the CMA and the RMMA will return the number of moving average according to settings.

    In
    RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

    It will create VWMA from period 2 to 160 with an interval of 10. That will produce 9 VWMA in the chart. In the output window it returns the last value at the right side of the chart of each VWMA.

    2991.58
    2991.13
    2990.8
    etc shown in data box

    My if statement should test each value 2991.58, 2991.13 etc.

    If i
    Print("myrmma"+myrmma); it only return close price and not the 9 value of the VWMA. Actually Printing the RMMA on OBU will only return the last value of each 9 VWMA and not all the values of each VWMA. To get all the values of each 9 VWMA you have to Print() that part in the RMMA Values[count][0] = VWMA(i)[0];

    I need the last value of each 9 VWMA to include in the if statement and later i will need all the values of each of the 9 VWMA as a variable. My question is why when i make a variable of the
    RMMA(MultiMA1types.VWMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); its not returning the last value of each 9 variables?

    Is it possible to import as a variable the result of
    Values[count][0] = VWMA(i)[0]; in RMMA to my indicator?

    Frank
    thanks
    Attached Files

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    If you need 9 values you likely need 9 variables, the syntax you are showing is only going to return one value at a time. I am also not sure what you mean by the 9 of them, do you mean the last 9 bars or are you using 9 different periods to create 9 different RMMA values?

    The brackets after the indicator/plot name is the BarsAgo. When you are using OnBarUpdate everything is relative to "now" or the bar being processed at that time. If you wanted the current bars data, that is zero BarsAgo or [0]. If you needed father back data, you use a higher number of BarsAgo from now such as [1] which would be the previous bar.

    A quick example of the BarsAgo would be on bar 5 (5 bars have processed, CurrentBar == 5) we want the value of the last bar or CurrentBar == 4, that would equate to 1 BarsAgo or [1]. We are now processing bar 5, and 1 bar ago would be bar 4.








    I look forward to being of further assistance.

    Leave a comment:


  • frankduc
    replied
    Jesse,

    Maybe creating a variable of RMMA and sending it to OR is the right way. But it will only return one average in the my if statement.

    If i
    Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

    in OBU i get all the last value of each RMMA.VWMA but if i make a variable called double myramma =
    RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]);

    I end up with only one value and not the 9 of them.

    Is it how it suppose to look or there's something i dont get.

    Also what impact those it have if we change [0] for [1] or [2]? What do you mean by the value of now?

    Thanks

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    You are right it is working OBU but not in OR.
    Just for clarification this should not directly work in OnRender.

    This is a good opportunity to explain this difference between OBU and OR as you have been able to see it directly.

    When working in OBU the code you write is aware of what bar is being processed. For example Close[0] or SMA(13)[0], these have context and the [0] represents the value of now. When working in OnRender you loose that context, [0] BarsAgo means nothing there. With that being said, calling an Indicator or Price series with a BarsAgo will not work, you could however use the GetValueAt method with a specific bars index:

    Code:
    Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5).[B]GetValueAt[/B]([B]SomeBarsIndex[/B]));
    The purpose of OnRender is to only render over the visible bars, this is why we use an Index in OnRender because you are looping over the bars indexes. Special methods are used in OnRender to gather data because of that context change.

    I created a rmma in OBU and send the result using a variable in OR. Seems to hold for now. Not sure about the result, will test that.
    This is the right way to approach rendering values because OnBarUpdate has the context you need and OnRender can be used to display that data in some custom way. You will see this used in other indicators like the ZigZag.


    I look forward to being of further assistance.

    Leave a comment:


  • frankduc
    replied
    I created a rmma in OBU and send the result using a variable in OR. Seems to hold for now. Not sure about the result, will test that.

    thanks

    Leave a comment:


  • frankduc
    replied
    You are right it is working OBU but not in OR.
    The
    Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); return 0 in OnRender
    Is it suppose to do that or will i have to move everything in OBU?

    Leave a comment:


  • NinjaTrader_Jesse
    replied

    Hello frankduc,

    I tried printing the RMMA using the settings you provided and it outputs data for me, the problem likely resides in your logic. If your print is not being called you would see no output, try a more simple test like the following. Note the placement is the first line of OnBarUpdate and there is no other logic being used to return or prevent this from being called:

    Code:
    protected override void OnBarUpdate()
    {
         Print(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]); //before any other logic
    }
    2998.42394724058
    2997.97464908019
    2997.4915496934
    2997.83051656447
    If you now see output, you will need to figure out at what point in your logic it is stopping short of reaching the other print.




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

    Leave a comment:


  • frankduc
    replied
    Jesse,

    I did

    Code:
    if(RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5) [0]< highPrice && RMMA(MultiMA1types.EMA, 2, 160, 10, 2, 128, 0.75, 0.5)[0] > lowPrice)
    I get no error message but its like i never did any change. Tried a Print( of the RMMA); nothing come's out of the OW.

    Is it possible the RMMA.EMA is not applied to every high and low price it cross in the statement? It sees as a whole RMMA.EMA and if statement dont consider the RMMA as 15 EMA to test in the if statement?

    Wow, if its that, when its not one thing its another. How do i get to separate them.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    Are you using a different RMMA than the one you previously uploaded to this thread? From what I can see the RMMA does not take just 1 parameter, I have attached an image of the file you provided and what it is asking for.

    Code:
    RMMA(MultiMA1types maToUse, int lowPeriod, int highPeriod, int stepAverage, int plotWidth, byte opacity, double saturation, double luminosity)

    The error you are seeing seems to indicate you have used no parameters somewhere in your code or:

    Code:
    RMMA()[0]
    Please let me know if I may be of additional assistance.

    Leave a comment:


  • frankduc
    replied
    Hello Jesse,

    I am trying to use the RMMA in a if statement but i get an error once again. is it possible to use the RMMA in this context? It works with an SMA. The RMMA has a MultiMA1types.EMA; // default MA. So i suppose in theory it should throw multiple EMA.

    Code:
    if(RMMA(160)[0] < highPrice && RMMA(160)[0] > lowPrice)
    NinjaScript File Error Code Line Column
    SampleDisplayBarsAgo.cs No overload for 'RMMA' method takes no arguments 1 CS1501 329 10

    Leave a comment:


  • NinjaTrader_Jesse
    replied

    Not the Switch case statement, right? If i want to produce multiple cma's i dont need the Switch case.
    The RMMA specifically uses the switch to control what indicator is setting the plot. Only one of the indicators is used at once.


    Now is it possible to call
    Code:
    Values[count][0] = MTYCMA(i)[0];
    within the CMA indicator to create multiple cma's?
    I couldn't say, this depends on your scripts configuration. You are calling MTYCMA with the i variable, and assigning it to an unknown plot. What is count and what value does it represent? That should be a index for the plot you want to set.


    within the CMA indicator to create multiple cma's?
    I am not sure what you mean by multiple cmas, if you mean to get the CMA value while changing its period, yes you are doing that by passing i so long as your indicator uses that input.

    "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.

    "CMAtest" indicator: error on the application of the "OnBarUpdate" method on bar 0: The index is outside the limits of the table..
    These errors are for specific reasons but I couldn't tell what that is from the provided syntax. This is something you would need to debug to find what specific syntax throws the error.
    The second error means you are using something that is null, that may be where you are trying to set the plot using count as the plot index, I couldn't say you would need to debug the code further to find the answer.

    The second error relates to using an index incorrectly, this could also be related to the plot, from the sample I cannot tell. this is another situation you would need to find the specific line which causes the error and then view what values you are using in contrast to what data is available at that time.


    Now in the RMMA AddPlot end up in State == State.Configure. I dont know why he has done that.

    Cant see why it wont work.

    I don't understand what you saying, are you saying that you placed the AddPlot in State.Configure? AddPlot can go in State.SetDefaults or Configure.

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

    Leave a comment:


  • frankduc
    replied
    Hello Jesse,

    What produces the multiple moving averages in the RMMA is that part:

    Code:
    int LowPeriod = 2;
    int HighPeriod = 160;
    int StepAverage = 3;
    int count = 0;
    
       for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
       {
    
        Values[count][0] = MTYCMA(i)[0];
    
       count++;
    
       }
    Not the Switch case statement, right? If i want to produce multiple cma's i dont need the Switch case.

    Now is it possible to call
    Code:
     Values[count][0] = MTYCMA(i)[0];
    within the CMA indicator to create multiple cma's?

    Because i still get error message saying:

    "CMAtest" flag: error on the application of the "OnBarUpdate" method on bar 0: The reference of the object is not passed to an instance of an object.

    "CMAtest" indicator: error on the application of the "OnBarUpdate" method on bar 0: The index is outside the limits of the table.



    Now in the RMMA AddPlot end up in State == State.Configure. I dont know why he has done that.

    Cant see why it wont work.

    Code:
    protected override void OnBarUpdate()
      {
    
       for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
        {
        mybar = barIndex;
        }
    
    
        for(int barIndex = (mybar - Period); barIndex <= CurrentBar; barIndex++)
        {
        double closePrice = Bars.GetClose(barIndex);
        double volumes = Bars.GetVolume(barIndex); 
    
        clovol = closePrice * volumes; 
    
    
        sum1 += clovol++;
        sum2 += volumes++;
    
        cma = sum1 / sum2;
    
    
        }
    
    
       int LowPeriod = 2;
       int HighPeriod = 160;
       int StepAverage = 3;
       int count = 0;
       for (int i = LowPeriod; i <= HighPeriod; i += StepAverage)
       {
    
    
    
        Values[count][0] = MTYCMA(i)[0];
    
    
        count++;
    
       } 
    
      }

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello frankduc,

    To get the volume in sma or in volume indicator they use:

    Code:
    Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
    Most of the internal indicators have this type of logic to account for crypto currencies, if you are not using crypto you can just use Volume[0]. Otherwise you could just copy this.

    In SMA Value[0] = Input[0]; return all the close of each visible bars.
    In some indicators you will see Input used, this means the indicator supports supplying a series to it as an input. This could represent the close or whatever was passed to the indicator to calculate, it doesn't have to be the close.


    As for the plot, I am not sure what you are expecting the value to be to say this is wrong or right. I see you are multiplying the volume against an input and doing other calculations before plotting the value. If the plot is not where you expected it you would need to revise your calculation.


    So i tried to replace Input[0] by Input[Period] Period = 160.
    Strangely it is not returning what i was expecting.
    What relevance does 160 have? You are asking for the input value of 160 bars ago, it wont match what you are currently looking at.


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

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by llanqui, Yesterday, 09:59 AM
8 responses
28 views
0 likes
Last Post llanqui
by llanqui
 
Started by quicksandatl, Today, 01:39 PM
1 response
3 views
0 likes
Last Post quicksandatl  
Started by md4866, 05-01-2024, 08:15 PM
2 responses
18 views
0 likes
Last Post md4866
by md4866
 
Started by samish18, Today, 12:20 PM
0 responses
7 views
0 likes
Last Post samish18  
Started by thread, 04-15-2024, 11:58 PM
3 responses
48 views
0 likes
Last Post Georg1o
by Georg1o
 
Working...
X