An infinite loop is a loop that runs forever (Stellman & Greene, 2010) and such an error is common (Sempf, Spar, & Davis, 2010). Previously, we discussed infinite loops in PowerLanguage with examples. But what to do when an infinite loop occurs?

In this article:

Dealing with infinite PowerLanguage loops in MultiCharts

There are three steps in fixing infinite PowerLanguage loops:

  1. Recognise an infinite loop,
  2. Terminate the affected MultiCharts processes,
  3. Locate the programming error and fix it.

These are discussed in turn below.

Step 1: Recognise an infinite loop in MultiCharts PowerLanguage

When an infinite loop occurs, the trading strategy or indicator keeps displaying its ‘Calculating…’ message:

A MultiCharts script stuck in calculating

This message also displays when the script calculates correctly. But if it displays longer than a few seconds (depending on your computer speed), there is an error with the script and MultiCharts has become stuck.

Step 2: Terminate the affected MultiCharts processes

With MultiCharts stuck in ‘Calculating…’ the CPU usage of MultiCharts.exe (or MultiCharts64.exe if you use the 64-bit version) is significantly increased and the program becomes unresponsive. The same can happen to the PowerLanguage Editor (PLEditor.exe), for example when the Print() keyword is used inside the erroneous loop.

To recover from this, each of the non-responding MultiCharts processes need to be manually terminated. To do that, bring up the Windows Task Manager (Ctrl - Shift - Esc) and go to the ‘Processes’ tab. Select the troublesome processes and click on ‘End Process’:

Terminating MultiCharts processes with the Task Manager

After waiting a moment for the processes to end, restart the PowerLanguage Editor for the next step.

Step 3: Fix the programming error

Now we need to locate the error in the script or in any functions used by the script. While an infinite recalculation can also be caused by executing the recalculate keyword unconditionally (MultiCharts Wiki, 2012), a more common cause is a loop error.

Infinite loop errors can be caused by three things (Wikipedia, 2014):

  • No exit condition,
  • Having an exit condition that can never be met,
  • Or having a statement that causes the loop to (partially) start over.

These are discussed in turn below.

Infinite loop cause 1: no exit condition

A faulty loop without an exit condition is easy to spot in PowerLanguage:


Variables:
    y(0);

y = 0;

while true begin 

    Print("y = ", y);
    y = y + 1;

end;

This while loop has no true/false expression that becomes false at some point. Instead, it uses the true keyword as a substitute. And since true will always be true, this loop will never end.

Infinite loop cause 2: having an exit condition that can never be met

An exit condition that is never met can be harder to spot, like in the following repeat-until loop:


Variables:
    x(0);

x = 0;

repeat

    condition2 = Close[x] < Close[x-1];
    Print("Close lower than previous close? ", condition2);
    x = x + 1;

until condition1;

This loop continues as long as the condition1 true/false expression is false. In the loop two closing prices are compared and the result of this evaluation is assigned to the condition2 variable. But this is another variable than the condition1 variable used as the loop’s exit condition.

Because conditionN variables are false by default, the condition1 variable in this example will remain forever false and so the loop will not stop. This small error (using a ‘2’ instead of a ‘1’) also shows that it helps to have clear, descriptive variable names.

Infinite loop cause 3: having the loop start over

Another cause of infinite loops is when the loop is partially start over again:


Variables:
    z(0);

for z = 0 to 10 begin 

    Print("z = ", z);

    if (Close[z] < Open[z]) then
        z = 0;

end;

This for-to loop runs from 0 to 10 with the z variable automatically increased with one at the end of each loop cycle. Inside the loop, an if statement compares a bar’s open and close price. When that expression is true, the if statement erroneously gives z a value of 0. That causes the loop to start over.

Summary

Infinite loops cause the script to remain in ‘Calculating…’ mode and make the MultiCharts program unresponsive. These errors can happen with all PowerLanguage loops, and can be caused by a missing exit condition, an exit condition that can never be met, or having a loop start over.


References

MultiCharts Wiki (2012, August 12). Recalculate. Retrieved on October 23, 2014, from http://www.multicharts.com/trading-software/index.php/Recalculate

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.

Wikipedia (2014, August 27). Infinite loop. Retrieved on October 23, 2014, from https://en.wikipedia.org/w/index.php?title=Infinite_loop&oldid=623054860