NightShark | Part 6 : Using the Loop Until Syntax

NightShark | Part 6 : Using the Loop Until Syntax


Introduction

Among the various tools available for automating trading strategies, the "loop until" construct stands out as a powerful yet often overlooked feature. This construct is especially useful for monitoring signals to place orders with both precision and efficiency. In this blog post, we'll delve into the "loop until" feature, contrasting it with standard loops and illustrating its advantages through a practical example. The goal is to demonstrate how "loop until" can significantly enhance the efficiency and precision of your trading algorithms.

What is "Loop Until"?

The "loop until" construct in NightShark allows you to run a loop until a specific condition is met. This provides you with greater control over your trading algorithms, enabling you to execute or terminate actions based on real-time conditions.

Why Use "Loop Until"?

  1. Precision: "Loop until" allows you to specify exact conditions under which the loop should terminate, making your algorithm more precise.
  2. Resource Optimization: By running loops only until necessary, you can make your algorithm more efficient and resource-friendly.
  3. Flexibility: The conditions for the loop can be dynamic, allowing your algorithm to adapt to real-time market changes.

A Practical Example

Using a Standard Loop

Here's a simple example using a standard loop to monitor a specific area and make trading decisions:

loop {
  Click(point.a)
  Sleep 1000
  loop {
    read_areas()
    if (toNumber(area[1]) > 20 || toNumber(area[1]) < -10)
      break
  }
  Click(point.b)
  Sleep 5000
}

In this example, the inner loop reads areas continuously and breaks when the condition toNumber(area[1]) > 20 || toNumber(area[1]) < -10 is met.

Using "Loop Until"

Now, let's see how the same logic can be implemented using "loop until":

loop {
  Click(point.a)
  Sleep 1000
  loop {
    read_areas()
  } until (toNumber(area[1]) > 20 || toNumber(area[1]) < -10)
  Click(point.b)
  Sleep 5000
}

With "loop until," the inner loop becomes more concise and easier to read. It will continue to read areas using read_areas() until the condition is met, at which point it will exit the loop and proceed to the next action.

Conclusion

The "loop until" construct in NightShark is a powerful tool for crafting precise and effective algo-trading strategies. By allowing you to specify the exact conditions under which a loop should run or terminate, it provides you with greater control and flexibility. The practical example demonstrates how "loop until" can make your code more concise, readable, and efficient. With "loop until," you're not just automating your trades; you're fine-tuning them for maximum effectiveness.

Happy Trading!