Using Anchored VWAP and SwingVWAP Programmatically in NinjaScript

Using Anchored VWAP and SwingVWAP Programmatically in NinjaScript

One of the advantages of the CodeNTrade Anchored VWAP is that the indicators can be accessed directly from NinjaScript. This allows you to incorporate Anchored VWAP logic into your own custom strategies and automated trading systems.

This post walks through the basics of using both the Anchored VWAP Indicator and SwingVWAP programmatically.


Why Use the Anchored VWAP Indicator Instead of the Drawing Tool?

NinjaTrader drawing tools generally are not designed to be accessed programmatically from NinjaScript. This makes it difficult (or impossible) to reliably fetch values from an Anchored VWAP drawing object.

That is exactly why the CodeNTrade Anchored VWAP Indicator exists.

The indicator version exposes the VWAP data directly through NinjaScript, making it easy to integrate into your own code.


Anchoring an Anchored VWAP to a Specific Bar

You can create an Anchored VWAP by anchoring it to a specific bar index.

// anchoring to a specific bar by id (2nd parameter)
var vwap = AnchoredVWAPIndicator(
null,
CurrentBar,
CodeNTradeAnchoredVWAPPrice.Typical,
0
);
// get the value of the VWAP for the current bar
var currentVwapValue = vwap.Values[0][0];

Parameters

  • First parameter: Date/time anchor (null when using a bar anchor)
  • Second parameter: Bar index to anchor to
  • Third parameter: Price type
  • Fourth parameter: Standard deviation bands to plot

Anchoring an Anchored VWAP to a DateTime

You can also anchor the VWAP to a specific date and time instead of a bar number.

// anchoring to a specific datetime
var vwap = AnchoredVWAPIndicator(
"2023-03-20 00:00",
null,
CodeNTradeAnchoredVWAPPrice.Typical,
0
);
// get the value of the VWAP for the current bar
var currentVwapValue = vwap.Values[0][0];

This approach is useful when anchoring to:

  • Economic events
  • Session opens
  • Gap fills
  • Earnings releases
  • Important market turning points

Using SwingVWAP Programmatically

SwingVWAP can also be accessed through NinjaScript.

// create a swing VWAP
// first parameter = swing strength
// second parameter = number of VWAPs
// third parameter = number of standard deviations to plot
var swingVwap = SwingVWAP(10, 1, 0);
// fetch the underlying anchored VWAP indicators
var vwaps = swingVwap.GetVwaps();
// fetch the current value from one of the VWAPs
var currentVwapValue = vwaps[0].Values[0][0];

Important Consideration When Using SwingVWAP

SwingVWAP dynamically updates as new swings form in the market.

This means:

  • Old VWAPs may be removed
  • New VWAPs may be added
  • The VWAP array returned by GetVwaps() can change over time

If your strategy stores references to specific VWAPs or depends on array ordering remaining constant, this behavior can produce unexpected results.

It is important to design your logic with this dynamic behavior in mind.


Common Use Cases

Some common ways traders use these indicators programmatically include:

  • Automated support/resistance detection
  • Mean reversion strategies
  • Trend confirmation systems
  • VWAP crossover signals
  • Multi-timeframe analysis
  • Alert generation
  • Dynamic trade management
  • Institutional accumulation/distribution analysis

Final Thoughts

The ability to access Anchored VWAPs directly from NinjaScript opens up a wide range of possibilities for custom strategy development inside NinjaTrader.

By exposing the VWAP values programmatically, the CodeNTrade Anchored VWAP products make it possible to build sophisticated trading systems that would not be practical using drawing tools alone.

If you have additional questions about using the indicators programmatically, feel free to reach out through the contact page.

Published by CodeNTrade

The nexus of Software Development and Trading

Leave a comment

Leave a Reply

Discover more from Code-N-Trade

Subscribe now to keep reading and get access to the full archive.

Continue reading