Visibility and Scope of Variables [Lesson 6]
Introduction
In this blog post, we will explore the concept of variable visibility and scope within NinjaTrader 8 trading strategies. Understanding how variables are scoped and accessed is crucial for writing efficient and organized code. We will delve into the differences between local and global variables, and discuss strategies for managing variable visibility to enhance coding efficiency.
Understanding Local Variables
Local variables are declared within a specific code block or method and are accessible only within that block. They have a limited scope, meaning they exist and retain their values only for the duration of that block. Local variables are typically used to store temporary data or intermediate results within a specific context. They provide encapsulation and prevent unintended side effects in your code.
Example:
private void methodThatUsesLocalVariables()
{
if (CurrentBar < 1)
return;
int barCount = CurrentBar;
double sum = 0;
for (int i = 0; i < barCount; i++)
{
sum += Close[i];
}
double average = sum / barCount;
// Perform calculations using the average, for the sake of this example we will just print it.
Print("The average price of the loaded bars is: " + average);
}
In this example, barCount
,sum
and average
are local variables declared within the methodThatUsesLocalVariables
method. They are accessible only within the method and are destroyed once the method completes execution.
Understanding Global Variables
In NinjaTrader 8 trading strategies and indicators, global variables play a vital role in storing and sharing data across different methods within a class. They have a broader scope, allowing you to access them throughout the entire class. In this section, we will explore the concept of global variables and their usage in trading strategies.
Let's consider the following code snippet as an example:
private double volumeSum;
protected override void OnBarUpdate()
{
calculateVolumeSum();
double averageVolume = calculateVolumeAverage();
printAverageVolume(averageVolume);
}
private void calculateVolumeSum()
{
volumeSum += Volume[0];
}
private double calculateVolumeAverage()
{
double average = volumeSum / CurrentBar;
return average;
}
private void printAverageVolume(double average)
{
Print("The average volume of the loaded bars is: " + average);
}
In this code, we have declared a global variable called volumeSum
at the class level. This variable is accessible throughout the entire class, allowing different methods to modify and access its value.
The OnBarUpdate()
method serves as the entry point for strategy execution. Within this method, we call three other methods: calculateVolumeSum()
, calculateVolumeAverage()
, and printAverageVolume()
.
The calculateVolumeSum()
method increments the volumeSum
variable by the current volume value (Volume[0]
).
The calculateVolumeAverage()
method calculates and returns the average volume based on the volumeSum
and CurrentBar
values.
The printAverageVolume()
method takes the calculated average volume as a parameter and prints it using the Print()
method.
By using a global variable like volumeSum
, we can accumulate data across multiple bars and perform calculations that depend on historical values. This allows us to analyze and derive insights from the accumulated data in our trading strategies.
Download the Example Code: If you'd like to experiment with the example provided above, you can download the code using the button below and import the NinjaScript add-on into NinjaTrader 8.
Managing Variable Visibility
To ensure efficient and organized code, it is important to manage the visibility of variables effectively. Here are some strategies to consider:
- Use local variables whenever possible to encapsulate data and limit their scope to where they are actually needed.
- Avoid unnecessary global variables to prevent clutter and potential conflicts in your code.
- Consider using method parameters to pass data between methods instead of relying solely on global variables.
- Utilize variable naming conventions that clearly indicate their purpose and scope.
Conclusion
Understanding the visibility and scope of variables in NinjaTrader 8 trading strategies and indicators is essential for writing efficient and organized code. By differentiating between local and global variables and managing their visibility appropriately, you can improve code clarity, prevent unintended side effects, and enhance overall coding efficiency.