Filling in a region between two DataSeries objects
---
I'd like to shade an area between 2 DataSeries objects such as 2 EMA's but would like to avoid keeping too many objects in memory to minimize the CPU usage.
My first thought was to first find the barindex on the left side of the visible portion of the chart and use a loop to re-draw the many regions from the left barindex up to the barindex to the right side or to the CurrentBar
I am looking for a short example how i could implement such a script. Any suggestion would be appreciated. Thanks.
I Was able to script something usin the onrender method and a loop to draw the régions which correspond to the viewing area. This is much faster when the chart contains a lot of bars.
NinjaTrader creates draw objects that are loaded when an indicator is added to a chart. The process of adding an indicator can be painfully long if you have a high resolution chart with many draw objects.
However, once loaded these objects do not need to be recalculated for display, so they do not affect CPU too much when the indicator is running. It is just the pain adding them to the chart.
In order to shade the area between two moving averages you would use many slices of DrawRegion, but not use a new draw object for every bar.
When a new bar is added without triggering a MA cross, you would extend the shaded area to the right side. A new draw object is added after a MA cross.
Other than the inconvenient of the loading time, there is a visual problem. The color change will not align to the MA cross but to the last price bar prior to the cross leaving you with the color transition prior to the cross.
To avoid this problem, you will need to add a custom section for OnRender(). If you go to the download section, you will find the amaMultiMACross indicator (Multiple Moving Averages Crosses).
You may check the plot, as it colors the space between the moving averages.
In case you have further technical questions, please send me a private message.
The following user says Thank You to Fat Tails for this post: