Breaking and Storing Moving Averages

How do I make separate continuous moving averages with ninjascript. So that once the MA hits a certain time, it will break the line, and start a new line at the current bar, without removing the old one

Hi @Adrian_Gmoney, The bad news is that regardless of whether you want to do this inside of a NinjaScript indicator or a NinjaScript strategy, will need to write custom code to do this unless you already have a NinjaScript indicator that is a moving average that “anchors”, or “resets”, on bars based on a rule like “reset on the first bar of the session”, the way that an “AVWAP” or anchored VWAP, indicator can do.

You wouldn’t even be able to use the SMA or another non-resetting indicator to help you with your calculations, and instead would need to emulate how your preferred moving average calculates its data series, including how it first starts calculating its data when it receives the first data points on a chart and it building its first data point. So to emulate an SMA but make it resettable, you would need to use a loop that goes back X bars, or the number of bars to the resetting data point, whichever is lower, and sum the values of the data points and divide by the number of data points, then store that value in a data series. Then you would have created your own resettable moving average that would do what you need. Maybe another community member knows of a shortcut but this is what I believe you would need to do.

Thanks for the response.

Essentially, yes, I’m trying to make a time based vwap indicator for myself. I have the indicator to the point that it resets, but I can’t figure out how to stop the line from being continuous with a sharp jank. How should I tackle that? Do you require additional arrays to contain a copy of the line with all its properties? Is that even possible?

If all you’re trying to do is eliminate that sharp transition, then you can set the plot color to Transparent using PlotBrushes just for that one data point.

https://ninjatrader.com/support/helpGuides/nt8/plotbrushes.htm

1 Like

I was thinking forward, because I would like to create a property at some point that will control the number of instances it saves, for example only show the last 10 instances, and remove all others. In that case I don’t know if PlotBrushes would work, because the goal is to increase performance, by removing unnecessary plots.

Still though, PlotBrushes did the trick for now, so thanks!

1 Like