Welcome back for the next lesson in our way to become fluent in Multicharts, Easylanguage and PowerLanguage. If you haven’t read the previous lessons yet, I would suggest to start here. A new lesson will build up on the previous lessons and starting at the beginning will ensure you have a solid foundation. Lesson 02 showed how you can easily calculate a simple moving average and plot it on the chart. We used a “for loop” to sum the values over the previous bars that should compose the average. Today you will learn another type of loop and how to use the editor to print information to the output bar.
In the first lesson we took a look at the main window within the PowerLanguage editor. When you open the editor it will probably show three different parts. If it looks very different on your end chances are that you changed the appearance under “View”. Make sure that the “Output Bar” is checked as we will use this during the current lesson.
Switch to the “Output” tab in the bottom window. This is where the PL editor will display information coming from print statements within your code. We will take a look at how this works now. Create a new indicator with a name of your choice. Again I am using a name that is easy for me to find within the editor and that displays the purpose of the indicator. We want the print statement only to appear one time in this demonstration. Easylanguage offers a convenient reserved word for code pieces you want to be executed once only.
Applying an indicator with this statement only will produce this result in the Output tab. Removing the reserved word “once” would make the indicator print once for every historical bar on the chart and once with every incoming realtime tick.
In the previous session the “for loop” came in handy to allow for a flexible moving average calculation. We could achieve the exact same result with another type of loop offered by EasyLanguage. You might ask why you’d need another type, but each type has it’s strength and sometimes one is better suited than the other. The type we want to familiarize ourselves with today is the “while loop”. It will repeat a code statement while a condition to be evaluated is true. An example for a while loop could look like this:
From the code you can see that the while loop will be executed until “ii” reaches a value of ten. When ii = 9 the statements within the loop will be executed for the last time and ii will be incremented to ten. When the code jumps back to the beginning and checks for the condition ii < 10 this now is no longer true as ii = 10 and therefore the loop will not be executed again.
As ii starts at 0 and the statements within the loop get executed while ii < 10 we should expect ten print statements in our Output tab.
If you recall the “for loop” automatically increments (or decrements) the counter variable and stops when the ending value for the counter is reached. The while loop is different here as you have to take care of the counter variable via code yourself. If you forget taking care of the counter variable or the condition that is evaluated in the first line of the loop you create a so called “infinite loop”. Infinite because you have a loop that will never be exited and therefore the code calculation would never be finished. This is a sure way to crash Multicharts so make sure to always add a check to prevent that. Let’s take a look at how an infinite loop would look like and how you can add some code to prevent it from crashing your program. The next three examples have in common that their evaluation condition will never be true and create an infinite loop. Therefore I would not recommend executing this code in Multicharts unless you are keen on making it crash. When you work in Tradestation the software by default has a protection against infinite loops enabled. This means it won’t crash, but raise a warning message and stop the code calculation.
Let’s take a closer look at each example and see why it’s resulting in an infinite loop. The first loop will be executed as long as “ii” is lower than ten. It starts with “ii” being zero as it’s reset before the loops begins. With every loop cycle “ii” is decremented i.e. every cycle subtracts one from the value of “ii”. After the first cycle “ii” now is -1, -2 after the next cycle and so on. As you can see this would never reach a value of +10.
The second example will be executed so long while “kk” is equal to zero. As the value for “kk” is not changed during the loop it will stay at zero and thus the cycle will not be stopped. The last example uses a boolean (true/false) expression to check if the loop should stop or continue being executed. As long as “ThisCondition” remains true it will continue to cycle through once it’s entered. In this example “ThisCondition” is never set to false within the loop code, so the result is another infinite loop and a crash of Multicharts.
To prevent a loop from resulting in a Multicharts crash you can either make sure that the ev