CQG’s sophisticated analytics offerings allow a tremendous amount of flexibility for creating and testing custom formulas and trade systems.
This section systematically walks you through the process, detailing:

formula components, in Formula Basics

creating five formula types using Formula Builder and its toolbox, in QFormulas, Conditions and User Values, Custom Studies, and Trade Systems.

definitions of CQGprovided formula components, in Functions and Custom Studies

how to use formulas in our analytics tools, in Alerts, Signal Evaluator, Market Scan, and Trade System Optimizer as well as backtesting
This section systematically walks you through the process, detailing:

formula components, in Formula Basics

creating four formula types using Formula Builder and its toolbox, in QFormulas, Conditions and User Values, and Custom Studies.

definitions of CQGprovided formula components, in Functions and Custom Studies

how to use formulas in our analytics tools
CQG’s recorded webinars and YouTube videos can also help.
If you have a particularly complex question, submit an Advanced Features Support Request. CQG’s product specialists are some of the best in the business.
Formula Basics
Five types of formulas exist:
QFormulas
Conditions
User Values
Custom Studies
Trade Systems
For our purposes, a formula can be either:
a mathematical calculation, e.g. addition or division to reach a numerical value;
a logical calculation, e.g. greater than or AND to reach a true/false value; or
a parameter (in the case of user values).
Formulas are applied to bars on charts and return either a value or an indication of a true condition.
1.Formula Components
Formulas are made up some combination of:
symbols
bar values
mathematical, relational, logical, and movement operators
offsets
functions
variables
parameters
comments
studies
other custom formulas
Consider these the building blocks of formulas.
Formulas are created on Formula Builder. Components can be typed manually, or you can use the toolbox to help build formulas.
Formulas include either a specific symbol or the symbol placeholders (@).
Symbols must be entered in capital letters. Lowercase symbols display the inverse contract value. The system automatically defaults to all capital letters.
When the placeholder is used, the symbol is inherited from the application the formula is being used in.
For example:
This QFormula uses specific symbols: SPREAD(CLEM2*CLEN+CLEQ,,,1:2:1)
This user value uses the placeholder: (High(@) + Low(@) + Close(@))/3
You can also use question marks:
?

Offset to most active contract

??

Offset to most active contract, includes cash/spot

In Formula Builder syntax preferences, symbols are included in the Identifier group.
Bar values in formulas
Comments
Bar values represent the outputs of chart typestopic=Chart Types. For example:
Bar: Last
Bar: TrueRange
Bar: CurrentBestAsk
CVB: Open
HeikinAshi: Mid
Spread Bar: HLC3
TFlow: BestBidPrice
TFlow: DomBidVol1
Within formulas, bar values are formatted like this:
Last(@)[1]
TrueRange(@)
CurrentBestAsk(@)
CVBXOpen(@,10)
HAMid(@)
SBHLC3(@,5X)
TFBestBidPrice(@,TFlowSimpleAggregation,1)
TFDomBidVol1(@,TFlowSimpleAggregation,1)
Bar parameters are also indicated. For example, (@,TFlowSimpleAggregation,1). These parameters can be changed.
Bar values also include three studies: Bar, CVB, and TFlow External Data.
Note about Current Values: Current values (such as CurrentBestAsk, CurrentBestBid, and CurrentTickVol) were designed especially for building trade systems. As input variables in trade systems, the current bar values recalculate on every tick within the trade system regardless of recalculation settings (Setup > Chart Preferences > Recalc). Regular bar values follow the recalculation mode setting.
Bar values have no special syntax highlighting.
Operators in formulas
Operators create relationships between formula components. You can type operators in a formula, or you can insert them using the Toolbox.
In Formula Builder syntax preferences, mathematical and relational operators, commas, and question marks are included in the Operator group. Logical and movement operators are in the Keyword group.
Order of Operations
Mathematical (numerical value)
Operator

Meaning

Example

+

addition

Close(@) + High(@)



subtraction

MA(@,Exp,15)MA(@,Exp,60)

*

multiplication

2.5 * Close(@)

/

division

Range(@)/2

Relational (true/false)
Operator

Meaning

Example

>

greater than

Range(@) > .987

=

equals

Hour(@) = BHour AND Minute(@) = BMinute

<=

less than or equal to

Close(CUS10Y)  Close(CUS02Y) <= Threshold

<>

not equal

IF(LO <> LO[1], LOHI, IF(HI<>HI[1],HILO, none))

>=

greater than or equal to

ADX(@,Period) >= Threshold

Logical (true/false)
To illustrate these operators as clearly as possible, the variables A and B have been added.
Operator

Meaning

Example

A AND B

True when both A and B are true

High(@) < High(@)[1] AND Low(@) > Low(@)[1]

A OR B

True when either A is true or B is true

Low(@) < Low(@)[1] OR High(@) > High(@)[1]

NOT B

Exclude B

NOT Close@ > Open@[1]

A WHEN B

If B is true, then A is true

High@[1] WHEN RSI(@,5) XBELOW 75

IF(A, B,C,)

This operator creates:
 one IF statement A
 one THEN statement B, in the case A is true
 one THEN statement C, in the case A is false
So,
If A = T, then B
If A = F, then C

IF(Low(@) = LoLevel(@,10,0), 0, none)

You can type these operators in a formula, or you can use the Toolbox.
Movement (true/false)
Formula

True When…

Example

A XABOVE B

A crosses above B

RSI(@,21) XABOVE 25,

A XBELOW B

A crosses below B

RSI(@,21) XBELOW 75,

A GOINGUP

A is greater than previous A

MAx1(DD,Sim,5) GOINGUP

A GOINGDN

A is less than previous A

MAx1(DD,Sim,5) GOINGDOWN

A GOINGUP B

A has been greater than the previous A for B consecutive times

Close(@)GOINGUP 3

A GOINGDN B

A has been less than the previous A for B consecutive times

Close(@) GOINGDOWN 3

A TURNSUP

A is greater than the last A, and the last A is less than two As ago

ADX(@,10) TURNSUP

A TURNSDN

A is less than the last A, and the last A is greater than two As ago

ADX(@,10) TURNSDOWN

You can type these operators in a formula, or you can use the Toolbox.
Offsets in formulas
Offsets identify which bar to apply the formula to.
An offset is indicated by using brackets [ ] around the offset value.
The offset value can be negative (for a bar in the past), positive (for a bar in the future), or zero (for the current bar). It is not necessary to indicate the current bar, as it is the default.
For example: High(@)[2] = high of two bars ago
Functions in formulas
Functions represent the outputs of predefined functions. For example:
Bars Since
DOM Bid Volume
Maximum Since
Net Change
Spread
Standard Deviation
Yield
Within formulas, functions are formatted like this:
BarsSince(@,1,400)
DomBidVol(@,1,Single,1.0_secs)
MaxSince(@,none)
NC(@)
SPREAD(@)
STDDEV(@,8)
Yield(@)
Function parameters are also indicated. For example, (@,1,Single,1.0_secs). These parameters can be changed.
Variables in formulas
A variable is a temporary value used to simplify complex, nested formulas.
A colon (:) is placed after the variable and a semicolon (;) is placed at the end of the expression.
For example:
x:=MA(2, Sim, 7);
y:=close(@)[5];
z:=x+y;
So, z = MA(2, Sim, 7) + close(@)[5]
Variables do not apply across formulas, as user values do. So, x could mean three different things in three different formulas.
Parameters in formulas
Conditions, User Values, Custom Studies, and Trade System can include parameters.
For example: B.cqg.HVolLOw
In
HVOL(@,Percent,HVPeriod,AnnFactor) < (1+Percent) * LoLevel (HVOL(@,Percent,HVPeriod,AnnFactor),Lookback,0)
HVPeriod, AnnFactor, Lookback, and Percent are parameters.
In Formula Builder syntax preferences, parameters are included in the Identifier group.
Comments in formulas
Comments are set off by /* and */.
There are two types of comments: those automatically generated by the system and those you add yourself.
For example, typing
SPREAD(
automatically generates
/* SPREAD(linear expression, [calculation mode], [tick size], [trade ratio], [BAT filter], [rollover], [rounding mode])
Examples:
SPREAD(42*HOECLE, L1, 0.01, 1:2, T:BA, 1)
SPREAD(EPENQ, L1, 0.01, 1:1.33, T:BA, 1, MATH)
SPREAD(EP/ENQ) */
SPREAD(@)
You can turn off autogenerated comments in Formula Builder preferences.
Studies in formulas
Studies represent the outputs of predefined studiestopic=Studies. For example:
Moving Average
Moving Average Top Envelope
Momentum
Momentum Oversold
RSI Divergence Down
Filtered Bid Volume
Within formulas, studies are formatted like this:
MA(@,Sim,21)
MATE(@,Sim,21)
Mom(@,10)
Mom_OS(@,10)
RsDivDn(@,1,5,25,75,25,Sim,1,9)
VolFiltBid(@,0)
Study parameters are also indicated. For example, (@,1,5,25,75,25,Sim,1,9). These parameters can be changed.
Custom formulas in formulas
Formulas can be components of other formulas.
When you add a condition to a formula, prefix it with B, like this:
B.Condition(@)
When you add a user value to a formula, prefix it with V, like this:
V.UserValue(@)
When you add a study to a formula, format it like this:
STUDY.CURVE^(@)
CustomStudy.Average^(@)
Share with your friends: 