NightShark : If/Else Statements
Introduction
In the world of algo-trading, decision-making is at the core of any successful strategy. NightShark, a desktop application designed for automated trading through UI automation, provides a robust framework for making these decisions. One of the most fundamental tools at your disposal in NightShark is the use of if-else
statements. This blog post aims to explore how these conditional statements can be effectively used to guide your trading algorithms.
What Are if-else
Statements?
If-else
statements are a basic form of conditional logic used in programming. They allow you to execute certain pieces of code based on whether a particular condition is met. In the context of NightShark, these statements can be used to make trading decisions based on real-time metrics captured from your trading platform.
Why Are if-else
Statements Important?
In algo-trading, the ability to make quick, accurate decisions based on real-time data is crucial. If-else
statements enable you to set specific conditions under which certain actions, like buying or selling a stock, should be executed. This allows for a high degree of customization and control over your trading strategies.
How to Use if-else
Statements in NightShark
Using if-else
statements in NightShark is straightforward. Here's a simplified example:
loop {
read_areas()
if (toNumber(area[1]) > 20) {
Click(point.a) // Executes a Buy order
} else if (toNumber(area[1]) < -10) {
Click(point.b) // Executes a Sell order
} else {
// Do nothing, or execute another action
}
Sleep 1000 // Pauses for 1 second before the next iteration
}
In this example, read_areas()
captures metrics from a predefined area (e.g., stock price). The toNumber()
function converts this metric into a numerical value. Then, an if-else
statement is used to decide what action to take:
- If the stock price is above 20, a Buy order is executed.
- If the stock price is below -10, a Sell order is executed.
- Otherwise, no action is taken (or another action could be specified).
Combining if-else
with Other Functions
The true power of if-else
statements is realized when they are combined with other NightShark functions like read_areas()
for data capture, toNumber()
for data conversion, and Click()
for action execution. This combination allows you to build complex, yet efficient, trading algorithms that can adapt to real-time market conditions.
Conclusion
If-else
statements are a fundamental building block in the architecture of automated trading strategies within NightShark. By allowing you to set specific conditions for your trades, they offer a level of customization and control that can be the difference between a profitable and a losing algorithm. With if-else
statements, you're not just coding; you're crafting intelligent, responsive trading strategies.
Happy Trading!