- The SimpleVWAPIndicator class maintains a list of VWAP8PPClass instances called vwapInstances.
- The OnBarUpdate() method of the SimpleVWAPIndicator class is called on every new bar. It checks if it's time to create a new VWAP instance based on the newVwapEvery variable. If it's time, a new VWAP8PPClass instance is created with the CurrentBar, Close[0] (closing price of the current bar), and the vwapDuration as arguments. This new instance is then added to the vwapInstances list.
- The groupCounter is incremented, and if the groupCounter is a multiple of groupSize, a new plot group is created to display the VWAP lines. The purpose of this is so that I can display the VWAP from multiple instances as one plot object. Eventually I would like to implement some more complex logic around how the instances are grouped.
- The OnBarUpdate() method then iterates through the vwapInstances list and updates each VWAP8PPClass instance with the current bar information by calling the UpdateVWAPPPClass() method. This method updates the VWAP calculation for the instance if the instance is still active (i.e., within its duration, which was set earlier).
- For each VWAP8PPClass instance, the SimpleVWAPIndicator calculates a plotIndex by dividing the index of the instance in the vwapInstances list by the groupSize. This groups the VWAP instances together for display purposes. How this is supposed to work is when the OnBarUpdate() method iterates through the vwapInstances list, it updates and plots the VWAP value for each instance, creating separate lines for each instance within a plot group.
- The MyVWAP property of each VWAP8PPClass instance is then used to set the value of the corresponding plot at the current bar. The MyVWAP property calculates the VWAP based on the accumulated sum of the price-volume product and the sum of volumes. If the sum of volumes is zero, it returns the anchor price of the instance.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Managing Anchored VWAP Instances
Collapse
X
-
Managing Anchored VWAP Instances
I am trying to figure out the best way to make an Indicator to create and manage multiple instances of anchored VWAPs on the chart (SimpleVWAPIndicator). At the moment, I am attempting to use partial public classes so I can use the anchored VWAP generation logic (VWAP8PPClass) elsewhere. I don't think I'm doing this right as I am not having any luck getting anything to display on the chart. I will share the code I am running with you and describe how it works (or is supposed to work). Considering the code do you think partial classes is the way to proceed here? Or is there a better way to integrate these two files so they achieve the desired output on the chart? I may be missing other details as well. -
Hello StoneMan78,
If you are trying to make logic that is reused its generally best to just make a indicator out of that logic. A simple example would be making a indicator similar to the SMA which plots a value based on the user input supplied to it. You can call the SMA as many times as you wanted from another indicator to calculate values that can be used in your parent indicator. You could make your indicator as complex as you need or do any calculations you need and then return a value to the parent indicator. You would then just call the indicator by name from OnBarUpdate from the parent script to calculate ite.
Partial classes would not be suggested for anything other than making generic methods on a type such as Indicator that are shared across your local indicators, that would not be good to use if you plan to export the script.
-
So If I made VWAP8PPClass a stand alone indicator I could still call it as many times as I wanted from SimpleVWAPIndicator and store those instances in a list for plotting?
Comment
-
Hello StoneMan78,
Yes that would be possible.
In the parent indicator you can call the same indicator as many times as you want however if you don't change the user inputs it uses a cached indicator. For example calling SMA(12) twice will re used the same indicator to get values to help with performance. If you called SMA(12) and SMA(13) those are two separate instances that use different user inputs so two total indicators are used. Depending on your use case you may need to add a user input so you can make separate instances if you need to call it many times.
If your data is not a plotted value like the IsActive or MyVwap in your sample then you also need to use the Update() method in the public property if you make that into an indicator. There is a sample of exposing data that is not a plot here: https://ninjatrader.com/support/help...alues_that.htm
You may also want to take a look at the Swing indicator, that uses plots to store non-consistent data and uses some extra logic to find instances of swings. You could do something similar if you needed to return more than 1 instance at a time from your indicator to build a list. You could use a custom method like the swings SwingHighBar method to return any type of data you wanted to the parent indicator or make a method that is more robust so you don't have to call a lot of indicators in a row, you would just use the same indicator and give it different parameters to its method.
Comment
-
One of the things I'd actually like to do in the future is place anchored VWAPs at the swing high and swing low points of the swing indicator. A potential path forward could be to modify the logic that that indicator uses and replace the horizontal lines with VWAPs. When I try to turn VWAP8PPClass into an indicator I get the error code saying it does not contain a constructor that takes zero arguments. Are zero argument constructors required for indicator formation? I'm just curious how much of the provided code I could salvage or if I should start over to properly call multiple instances of the same indicator.
Comment
-
Hello StoneMan78,
Constructors are not used for indicators because they generate their own constructors based on the public user inputs you make. If you have something to pass into the indicator you would use a public property rather than a constructor parameter.
If you look at the SMA that takes an int value as a constructor value even though it doesn't have any constructors in its code, it has a Period property near the bottom of the file. When you compile an indicator it generates extra code at the bottom which makes the constructors. Instead of calling new MyIndicatorName you just call the indicator name:
I would likely suggest to just use what you made as a reference for naming and make a whole new indicator. That would help to avoid random errors while you remake what you had. You can still make custom methods and properties like you had but you will need to structure it a little bit differently when in an indicator. The swing is a good example of a custom method in an indicator, the link in the last reply is a good example of exposing any other kind of data.Code:SMA mySma = SMA(12); print(mySma[0]); prints the plot value
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
581 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
338 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
552 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment