Static Variables in RT Systems

Fact: Without the use of Static Variables you cannot develop an automated trading system.

AmiBroker executes your AFL programs one line at the time, top to bottom. Unlike most conventional programming languages, it doesn’t ever hang around and wait for events unless you specifically want to do this (not recommended). In addition, AFL never re-uses values assigned to variables during the previous pass through the code, i.e. all variables are re-initialized at each chart refresh. This means that unless you use Static Variables you can never assign a value to a variable and retain its value over any length of time or pass it from one refresh to the next. RT Trading requires you to save many variables, such as Signals, OrderIDs, Position and Order status, and Timing.

In the case of transient signals, such as when the price briefly crosses your limit price, a buy signal is generated, but it will disappear on the next pass through the code if the RT condition no longer exists. When this happens, an order is transmitted, but you have no record that this ever happened. Then, if the same event takes place again a few seconds later, you’ll get another buy signal and another order goes out. With these multiple orders being sent to the TWS, you’ll be broke before you know what hit you.

This is where Static Variables offer the solution you need: they maintain values of variables and arrays until you change them or until you shut down AmiBroker. They give you a means to carry forward values from one execution to the next. This is the key mechanism used to prevent multiple orders from going out; there is no other way to do this. Of course, you could always check your position size, but, due to various delays, your code could execute several times before your position change was even acknowledged. It is a risk not worth taking and simply wouldn’t work.

Without the use of Static Variables, you cannot develop an RT Automated Trading system. Reading up on them in the users’ manual and understanding how they work and are implemented in AFL will prove to be your best investment in time.

Edited by Al Venosa.

1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 3.67 out of 5)
Loading...Loading...

Comments are closed.