Gondola Language

The Gondola Language is a language for analysing stock market data. The language allows you to do a variety of tasks from listing stocks in tables that only match your criteria to creating automated buy/sell paper trade rules. The language is closely modelled after the C programming language, so if you know that language, then you should find Gondola easy and familliar to work with.

Whenever you enter a Gondola expression there are two implicit variables that are always set: the current date and the current stock. For example if you are displaying a Table of stock quotes, you can execute an equation for each stock. If you entered this line:

avg(close, 30)

It would display, for each stock, the average day close value over the last 30 days starting from today. Here the current date would be set to the most recent day you have a quote for and the current stock would be set to the current stock.

You can also enter equations when performing Paper Trading. If you entered the following as a buy rule:

avg(close, 15) > avg(close, 30, -1)

It would only buy the stock when the average day close over the last 15 days was higher than the average day close of over the last 30 days, where the 30 day average would be calculated starting from the previous day and working backwards. So here the current date would be set to whatever date the trade was to be analysed for.

The Gondola language is very type strict. What this means is that each value has a given type, whether it is an integer, real or boolean. This means that the numbers 12 and 12.0 are different, and 1 and true are different. If you get a type mismatch error, you've probably entered an integer number (e.g. 12) instead of a real number (e.g. 12.0).

Gondola Operators

The Gondola language supports the following boolean operators: and, or and not. For example:

It also supports basic arithmetic: +, -, * and /. For example:

Note that Gondola expects the binary operands (that is the above +, 0, * and /) to be grouped in pairs. Therefore, if you have an expression like A + B +C, it must be grouped into two pairs, using paranethesis. In the example above, that would either (A + B) + C, or A + (B + C)

The type of the returned value is determined by the operands, with floating point types having precedence. The type determinations changed as of version 0.72. Prior to this version, the type of an expression was determined by the type of the first operand. The following examples explain the behaviour:

And finally it also supports the relational operators: ==, >, >=, <, <= and !=. For example:

Gondola Comments

You can add comments to your rule definitions which are ignored by Venice. Comments are enclosed by pairs of "/*" and "*/" strings.

For example in the rule:

/* Buy if the close is higher than yesterdays close */ close > lag(close, -1).

the comment is "Buy if the close is higher than yesterdays close" and will be ignored. Unlike the C language, comments can be nested.

Gondola Variables

The Gondola language has full support for variables, which allow you to store, retrieve and manipulate values. When defining a new variable you need to specify whether the variable can change or is constant, the variable type, the name of the variable and optionally the initial value of the variable.

Examples:

Gondola User Defined Functions

In addition to tbe built in functions listed below, the Gondola langauage supports functions written by the user. Defining a function is similar to defining a variable - the return type and the type of any parameters must be specified. Additionally, the keyword "function" is appended after the type definition.

For Example:

int function factorialFunction(int n)

defines a function returning an integer value, and takes one integer parameter "n". The body of the function has the same properties as a for or while loop in that it is the last line in the expression which is the return value. Note that the type of the return value must match the function definition. A warning on returning from functions: Gondola does not have a line separator (e.g. ";") and you may have unexpected results when you try to write a function like this:

int function returnSomeInteger() {
float tmpFloat = returnSomeFloat()
-returnSomeInt2()
}

This will return a type mismatch because the last line actually is: float tmpFloat = returnSomeFloat() - returnSomeInt2() .

Variables can be defined inside functions. However, their scope will persist when the function call ends, because Gondola is not stack based at this time. Similarly, declared parameters remain in scope as well. However, variables which are declared in an included rule will not be in scope. See the following section "Including External Rules" for more details.

Including External Rules

You can nest rules within another using the "include" mechanism:

include "RULENAME"

Where RULENAME is the name of the rule. Note that the name must be encased in quotes, otherwise Venice will attempt to resolve RULENAME as a variable.

Any variables which are defined in the included rule will not be available, However, user defined functions in included rules are available.

Gondola Functions

Absolute Value

The absoluate value function returns the absolute value of the given value. The absolute value is the positive value of a number. For example the absolute value of -12 is 12, and the absolute value of 12 is 12.

abs(VALUE)

Where VALUE is the initial value.

Example:

abs(-28.0)

Returns the absolute value of -28.0 which is 28.0.

Alert

The alert function displays a message on the screen. The purpose of this function is to aid rule debugging and always returns 0.0. Note: When this function is evaluated, it will pause execution! Also note that the alert will display for every date in the range. So if you have 30 days data in the range, you may end up with 20 alert messages appearing, all of which have to be dismissed before execution can continue.

alert(STRING, [ argument2 ], [ argument3 ], [ argument4 ])

Where STRING is an expression or a string, and the optional arguments argument2 - argument4 are also expressions or strings. The value of the optional parameters is appended to the output message.

Example:

float mesg1 = alert("Days held = ", held, "stock capital = ", stockcapital)

Displays the following message: "Days held = held, stock capital = stockcaptial", with the values for held and stockcapital displayed respectively. The variable mesg1 will contain the value 0.0.

Average

The average function averages a series of stock quotes.

avg(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to average. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

avg(open, 15, -1)

Returns the average of the day open value for the current stock for the last 15 days, ending with yesterday.

Bollinger Bands

The bollinger bands are: bol_upper=avg+2sd, bol_lower=avg-2sd .

bol_lower(QUOTE, DAYS[, START_OFFSET])

bol_upper(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to average. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

bol_upper(close, 26, -1)

Returns the bollinger band value of the day close value for the current stock for the last 26 days, ending with yesterday.

The expression bol_lower(close, 26, 0) is the same as bol_lower(close, 26) similarly for open, high and low.

The expression bol_upper(close, 26, 0) is the same as bol_upper(close, 26) similarly for open, high and low.

ceil

The ceil function takes a floating point number and returns an integer that is not less than the given argument.

Example:

ceil(-3.14159) returns -4.

Correlation

The correlation function returns the correlation between two stock quotes.

corr(SYMBOL, QUOTE, DAYS[, START_OFFSET])

Where SYMBOL is the stock symbol. Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to correlate. Where START_OFFSET is the most recent date to correlate, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

corr("CBA", close, 30, -1)

Returns the correlation of the day close values of this stock and CBA over 30 days ending in yesterday.

Cosine

The cosine function returns the cosine of the given value.

cos(VALUE)

Where VALUE can be any float or integer.

Example:

cos(0)

Returns 1.0

cos(3.141592653589793/2)

Returns 0.0

cos(3.141592653589793)

Returns -1.0

cos((3*3.141592653589793)/2)

Returns 0.0

cos(2*3.141592653589793)

Returns 1.0

Day

The day function returns the current day of the month.

day()

Example:

day()

Returns the current day, which will be 31, if it is the 31st.

Day of Week

The day of week function returns the current day of the week.

dayofweek()

Example:

dayofweek()

Returns the current day of the week, which will be 1, if it is a Sunday.

Day of Year

The day of year function returns the current day of the year.

dayofyear()

Example:

dayofyear()

Returns the current day of the year, which might be 365, if it is New Years Eve.

Exponential

The exp function returns the exponential of the given value.

exp(VALUE)

Where VALUE is any float or integer.

Example:

exp(0)

Returns 1.0

Exponential Moving Average

The exponential moving average function averages a series of stock quotes according to the following equation: EMA(current) = EMA(previous) + SMOOTHING_CONSTANT * (QUOTE - EMA(previous).

ema(QUOTE, DAYS[, START_OFFSET][, SMOOTHING_CONSTANT])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to average. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc. Where SMOOTHING_CONSTANT is the smoothing constant.

Example:

ema(close, 26, 0.1, -1)

Returns the average of the day close value for the current stock for the last 26 days, ending with yesterday.

The expression ema(close, 26, 0, 0.1) is the same as ema(close, 26) similarly for open, high and low.

The expression ema(close, 26, 0, 0.2) is the same as ema(close, 26, 0.2) similarly for open, high and low.

The expression ema(close, 26, -1, 0.1) is the same as ema(close, 26, -1) similarly for open, high and low.

Floor

The floor function takes a floating point number and returns an integer that is not greater than the given argument.

Example:

floor(-3.14159) returns -3.

For

The for function is a looping function that allows you to loop over an expression of code. Typically the loop is tied to a variable, so you need to set the initial value of the variable, a condition where the loop will terminate, and how the variable should change after each loop.

for(INITIAL; CONDITION; LOOP) { COMMAND }

The function will execute the INITIAL expression, then execute the COMMAND expression, and then execute the LOOP expression. It will then execute the CONDITION expression. If the CONDITION expression was FALSE then the function will return. Otherwise it will run the COMMAND expression, then the LOOP expression, then check the CONDITION expression, etc.

Example:

int b = 0

for(int i = 0; i < 10; i = i + 1) {
  b = b + i
}

The above code will sum the numbers 0, 1, 2, ... 9 and store the result in the b variable.

If

The if function allows a selection of which code to be executed.

if(VALUE) { TRUE } else { FALSE }

If the value of the VALUE expression is true, then the TRUE expression will be executed, otherwise the FALSE expression will be.

Example:

if(lag(open, 0) > lag(open, -1)) {
   lag(open, 0)
}
else {
   lag(open, -1)
}

Returns the greater of today and yesterday's day open values.

Lag

The lag function returns a stock quote.

lag(QUOTE[, OFFSET])

Where QUOTE is open, close, low, high or volume. Where OFFSET is the date to retrieve the stock quote, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

lag(high, -5)

Returns the day high value of the current stock, 5 days previous.

The expression lag(close, 0) is the same as lag(close) and they can both be abbreviated to close; similarly for open, high and low.

Logarithm

The log function returns the natural logarithm of the given value.

log(VALUE)

Where VALUE is any positive float or integer.

Example:

log(1)

Returns 0.0

log(exp(1)))

Returns 1.0

Minimum

The minimum function finds the minimum of a series of stock quotes.

min(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to search. Where START_OFFSET is the most recent date to search, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

min(volume, 15, -1)

Returns the minimum volume of trade for the current stock for the last 15 days, ending with yesterday.

Maximum

The maximum function finds the maximum of a series of stock quotes.

max(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to search. Where START_OFFSET is the most recent date to search, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

max(volume, 15, -1)

Returns the maximum volume of trade for the current stock for the last 15 days, ending with yesterday.

Moving Average Convergence Divergence

The MACD is: MACD = 26 days EMA - 12 days EMA.

macd(QUOTE[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

macd(close, -1)

Returns the macd value of the day close value for the current stock ending with yesterday.

The expression macd(close, 0) is the same as macd(close) similarly for open, high and low.

The expression macd(close, 0) is the same as ema(close,26,0,0.1)-ema(close,12,0,0.1) similarly for open, high and low.

Momentum

The momentum is: momentum(now)=quote(now)-quote(period deleyed).

momentum(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to delay. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

momentum(close, 26, -1)

Returns the momentum value of the day close value for the current stock for the last 26 days, ending with yesterday.

The expression momentum(close, 26, 0) is the same as momentum(close, 26) similarly for open, high and low.

Month

The month function returns the current month.

month()

Example:

month()

Returns the current month, which will be 8, if it is August.

On Balance Volume

The OBV is the sum of volumes in the period, counted as positive if close is greater than open or as negative if open is greater then close.

obv(DAYS[, START_OFFSET[, INITIAL_VALUE]])

Where DAYS is the number of days to count over. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc. Where INITIAL_VALUE is the initial value which counting from

Example:

obv(200, -1, 50000)

Returns the obv value counted over the last 200 days, ending with yesterday, starting with value 50000.

The expression obv(200) is the same as obv(200, 0, 50000).

Percent

The percent function returns the given percent of the given value.

percent(VALUE, PERCENT)

Where VALUE is the initial value and PERCENT is the ratio to return.

Example:

percent(200, 10)

Returns 10% of 200 which is 20.

Random

The random function returns a positive floating point random number between 0.0 and 1.0. This function can be used to test expressions to see if following a rule produces better results than just choosing randomly. Note that the analyser (Genetic Programming etc) functions do not generate this function.

random(seed)

Returns a random number between 0.0 and 1.0. Seed is optional

Relative Strength Index

This function calculates the Relative Strength Index (RSI) of the current stock.

rsi([PERIOD[, START_OFFSET]])

Where PERIOD is the period to apply the RSI. Where START_OFFSET is the most recent date to calculate, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

rsi()

Returns the RSI of the current stock.

Sine

The sine function returns the sine of the given value.

sin(VALUE)

Where VALUE can be any float or integer.

Example:

sin(0)

Returns 0.0

sin(3.141592653589793/2)

Returns 1.0

sin(3.141592653589793)

Returns 0.0

sin((3*3.141592653589793)/2)

Returns -1.0

sin(2*3.141592653589793)

Returns 0.0

Square Root

The square root function returns the square root of the given value.

sqrt(VALUE)

Where VALUE is the initial value.

Example:

sqrt(144)

Returns the square root of 144 which is 12.

Standard Deviation

sd(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to average. Where START_OFFSET is the most recent date to average, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

sd(close, 26, -1)

Returns the standard deviation value of the day close value for the current stock for the last 26 days, ending with yesterday.

The expression sd(close, 26, 0) is the same as sd(close, 26) similarly for open, high and low.

Sum

The sum function sums a series of stock quotes.

sum(QUOTE, DAYS[, START_OFFSET])

Where QUOTE is open, close, low, high or volume. Where DAYS is the number of days to sum. Where START_OFFSET is the most recent date to sum, 0 means the current trading date, -1 means the previous trading date, etc.

Example:

sum(open, 15, -1)

Returns the sum of the day open value for the current stock for the last 15 days, ending with yesterday.

While

The while function is a looping function that allows you to loop over an expression of code. The loop contains an expression that will be executed until a specific condition is not met.

while(CONDITION) { COMMAND }

The function will execute the COMMAND expression until the CONDITION is not met. If the CONDITION is never met, the loop will not be entered.

Example:

int sum = 1

while(sum < 100) {
  sum = sum + 1
}

The above code will keep increment the value of sum until it is equal to 100.

Year

The year function returns the current year.

year()

Example:

year()

Returns the current year, which will be 2004, if it is 2004.