A Timer Helper and CQG's Set/Reset Function

We often receive questions on how to limit trading activity to certain times of day. One little extra line of code can accomplish this quite easily.

In CQG there is a very efficient way to create a variable inside a trading system, custom study, or condition. With "NAME:=" you create the variable and then end the expression with ";". For example, the local time is 11:10 and we calculate this expression:

TIMER:= LocalHour(@) * 100 + LocalMinute(@);

The local hour, 11, multiplied by 100, equals 1100. The local minute, 10, added to the result is 1110, which is a four-digit time without any separators.

Normally I use "LocalHour(@)" and "LocalMinute(@)" but you have to keep in mind that this will change the results of a study or trading system if it runs in a different time zone. You can use "Hour(@)" and "Minute(@)" instead, which is the CQG Line Time, independent of the time zone.

To limit a trading system to start a position only between 10:30 and 14:00, add the timer and add the limitations to the signal portion of the trading system:

Alternatively, we can exit a position at a certain time of day like this:

This method can also be used to reference a certain value at a certain time; for example, the high and low of the eight o'clock thirty-minute bar.

TIMER:= LocalHour(@) * 100 + LocalMinute(@);

High(@) WHEN TIMER = 800

This can even be accomplished across time frames (i.e., If you want to know the high and low from the eight o'clock thirty-minute bar but your trading system should run on a five-minute bar). This is often referred to as the opening range in Market Profile®* theory.

TIMER:= LocalHour(@) * 100 + LocalMinute(@);

(High(@) WHEN TIMER = 800),30

There are many examples where this timer function could be very useful.

Another very helpful tool inside CQG's formula toolbox is the Set/Reset function. With Set/Reset you can use two totally independent conditions to enable and disable trading in contrast to normal filters, which usually use a single function.

For example, normal filters might allow long trades only when the low is greater than the moving average x. As long as we are above the moving average, the trading system trades, when we are below, it stops trading. What if we want to create a simple filter that allows long trades when the low crosses above the moving average, but it stops long trading when the Average Directional Movement Index (ADX) tops out? We don't want to wait until the moving average is crossed again to disable the long trades.

"SetRst(@,@)" is like a light switch. You substitute the first "@" with the condition that switches on the function and substitute the second "@" with the function that switches it off again.

For example:

SetRst( Low(@) XABOVE MAx1(@,Sim,16), ADX(@,10) > 40 AND ADX(@,10) TURNSDOWN )

The study returns the value "one" or "true" after the first condition and goes back to "zero" after the second.


Learn more from CQG HTML Help