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


Unsynched Array to EMA

  • Filter
  • Time
  • Show
Clear All
new posts

    Unsynched Array to EMA

    Hi NinjaExperts,
    Any idea how to calculate an EMA for values stored in an unsynched array?
    I log values of interest (VOI), which can occur whenever, in an array. The VOI array is on purpose not synched to the primary series. Thus, if I have 100 bars in the primary, I might only have 15 values in the VOI array. I do not want to fill the "gaps" (85 bars) in the VOI array with nonsense values (either zero or a dummy copy of the respective last value until the next "real" value occurs).
    Until here, everything works exactly as expected. I can log data points as needed and correctly pull them again (e.g. VOI[14]), whenever I like to do so. .
    However, I also need to be able to calculate (custom) indicators on values of the VOI array for n occurrences. Easy for a SMA using "for loops" over desired length of index and divide the total by the index length, but how to access the items of the array if I really need each individual value of n for further calcs, like for an EMA?
    Direct access to the array is apparently not available. And I don't seem to be able to convert an array into a series either.
    Any hint how to solve the coding challenge (or to otherwise serve my needs laid out above) is greatly appreciated.


    Hi NT-Roland, thanks for your question.

    Any Series<T> object is going to have a slot index for every bar on the chart or underlying data series. You would need to use a custom List<T> to prevent a sparsely populated array, you can call List<t>.Add() to add a new item to the list. A moving average for this array would need to be calculated manually in a custom function since you would no longer be using the Series<T> and do not have a value for every bar.

    Best regards,

    Chris L.NinjaTrader Customer Service


      Hi ChrisL,
      Thanks for confirming that I'm not missing something obvious.
      I will continue with custom indicator calcs using "for loops" straight from the unsynched array or first convert n values of this array to a list and then work against that extract.
      Would be cool, if n items of lists/unsynched arrays could be exposed to indicator calcs straight away. I can imagine quite some use cases where you would want to calculate an indicator on just n occurrences of last valid data points rather than on each bar (with dummy values in between), but use the result of such indicator on your primary series.


        Have you thought about creating a Series object to be used as a temporary
        data placeholder, like a middle-man dummy object, who's only purpose is
        to be used for the calculations made by an indicator?

        The trick is completely re-populating this dummy Series on the fly, with
        whatever array data you have, such as your VOI array.

        That is, when you want to perform calculations from a Indicator class
        on your VOI array, you first copy all VOI elements into the most recent
        positions of this dummy Series object -- you are effectively gutting
        the contents of the dummy Series object and completely reinstalling
        the values it holds -- but all this can be done with a few simple loops.

        Then use this dummy Series object as input to whatever indicator
        you're interested in.

        However ...

        If you're really only interested in one or two indicators, then I'd
        recommend you roll your own class that handles working directly
        with whatever data you give it. I made a CalcEMA class just for
        this purpose -- it kept it's own list of data values (naturally, I add
        items to the list with a Ninja-like 'Set' method) and the value of
        the EMA calculation was updated each time you added a new
        item to the list via Set.


          Hi bltdavid,
          Excellent suggestion.
          I think, I will try the "dummy Series" override first because it would give me access to all indicators in one go.
          Thank you very much.


          Latest Posts


          Topics Statistics Last Post
          Started by DerkWehler, Today, 11:35 AM
          9 responses
          Last Post NinjaTrader_Jesse  
          Started by dbennett3, Today, 01:59 PM
          0 responses
          1 view
          Last Post dbennett3  
          Started by webus, Today, 06:22 AM
          4 responses
          Last Post webus
          by webus
          Started by DayTradingDEMON, 07-11-2024, 08:54 AM
          16 responses
          Last Post DayTradingDEMON  
          Started by DawnTreader, 05-08-2024, 05:58 PM
          41 responses
          Last Post DawnTreader