Nightshark: Introducing Variables

Nightshark: Introducing Variables

Youtube Video:

Introduction

In algo-trading, efficiency and clarity are key. NightShark, a powerful tool for automated trading, offers a variety of features to make your trading algorithms both effective and easy to manage. One such feature is the use of variables. This blog post will introduce you to the concept of variables in NightShark and show you how they can simplify your code and make your trading strategies more robust.

What Are Variables?

In programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information. In the context of NightShark, variables allow you to store metrics or other data that you can later reference in your trading algorithms.

Why Use Variables?

Using variables in NightShark offers several advantages:

  1. Code Reusability: Once a variable is defined, it can be used multiple times throughout your code, eliminating the need for redundant calculations.
  2. Code Clarity: Variables give names to your data, making your code easier to read and understand.
  3. Flexibility: Variables can be easily updated or changed, allowing for more dynamic and adaptable trading strategies.

How to Define Variables

In NightShark, defining a variable is straightforward. Here's how you can define variables for current position profit/loss and day profit/loss:

CurrentPositionProfitLoss := toNumber(area[1])
DayProfitLoss := toNumber(area[2])

In this example, CurrentPositionProfitLoss and DayProfitLoss are variables that store the numerical values of area[1] and area[2], respectively.

Using Variables in Your Code

Once variables are defined, they can be used in if-else statements or other parts of your code to make decisions. Here's an example:

loop {
  read_areas()
  CurrentPositionProfitLoss := toNumber(area[1])
  DayProfitLoss := toNumber(area[2])

  if (CurrentPositionProfitLoss > 20) {
    Click(point.a)  // Executes a Buy order
  } else if (DayProfitLoss < -10) {
    Click(point.b)  // Executes a Sell order
  }
  Sleep 1000  // Pauses for 1 second before the next iteration
}

In this example, the variables CurrentPositionProfitLoss and DayProfitLoss are used within an if-else statement to decide whether to execute a Buy or Sell order. This makes the code easier to read and allows for more straightforward adjustments in the future.

Conclusion

Variables in NightShark are a powerful tool for enhancing the clarity, reusability, and flexibility of your trading algorithms. By storing important metrics or conditions in variables, you can create trading strategies that are not only effective but also easy to manage and update. With variables, you're not just simplifying your code; you're optimizing your trading strategy for success.

Happy Trading!