Welcome to How To Build a Formula Subroutine
Overview
Each formula has the structure
SubroutineName [Equal] formula [Semicolon]
or typed out
SubroutineName=formula;
SubroutineName - Identifies the subroutine and can be any text that is not one of the reserved names used to make a formula. Once the name is defined it can be used in 1) any of the subroutines that follow it , 2) it can be attached to a chart line, or 3) if can be attached to a signal condition.
Formulas - This can have any combination of five different parts: a value (input), data, a function, an operator, and a comparison (conditions).
Value - the actual number used in a calculation, like the length of an EMA. Values can be come from two areas 1) you can use the number itself i.e. “EMA(C,10)” for a 10 bar EMA or 2) you can define an input such as “Length =10” then use the input name in the function i.e. (EMA(C, Length)”. The advantage of an input is it can be changed in the program while it is running.
Data - This equals the data that is going to used as a starting point for calculations. It can come from a data object such as Open, High, Low, or Close or it can be the result of a function call.
Function - This executes a high level calculation like a moving average (SMA). Almost all functions have the structure Function(data, value). Functions always return a data object.
Operator - This object manipulate two data objects and consist of operators such as add, subtract, multiply, etc.
Comparison - This object produces conditions which are either 1 (true) or 0 (false). These are used mostly for buy and sell signal formulas.
Examples:
MA1 = EMA(C,10) ; Calculates a 10 bar ema of the Close price
MidPrice = (H + C) /2 ; Add the High to the Low price and divide by two.
Flat = 100 ; Plot a line across the chart at value 100.
Data
ATR
Returns the Average True Range for the current bar.
A
Returns the Average Price which is (High + Low + Close) / 3 for the current bar.
C
Returns the current bar's close price.
YC
Returns yesterdays close price.
H
Returns the current bar's high price.
YH
Returns yesterdays high price.
L
Returns the current bar's low price.
YL
Returns yesterdays low price.
N
Returns the negative change of the current bar. The negative change subtracts the current close from the prior bar close and records it if it is negative. If the change on this bar is positive a zero is returned.
O
Returns the current bar's open price.
P
Returns the positive change of the current bar. The positive change subtracts the current close from the prior bar close and records it if it is positive. If the change on this bar is negative a zero is returned.
R
Returns the range of the current bar which is equal to (High - Low).
STOCK(Symbol)
Returns the current bar's close price for the symbol.
V
Returns the current bar's volume.
MIMAFAN
Returns a data item where the value of each bar is the fractal number of the MIMA Fan. (Only available for teams with PhiCube)
EMAFAN
Returns a data item where the value of each bar is the fractal number of the Exponential Moving Average Fan.
RAINBOW
Returns a data item where the value of each bar is the highest complete rainbow. Positive for up, negative for down. (Only available for teams with PhiCube.)
All data items except STOCK can have a number appended to them to designate a value x bars ago. For example, C5 will get the close price 5 bars ago. R10 will get the bar range 10 bars ago. C - C1 will subtract the close one bar in the past from the current bar's close.
Functions
ABS(data)
Returns the Absolute Value of the data item.
/*…*/
Comment - All comments will be ignored during formula parsing.
EMA(data, value)
Returns the exponential moving average from the data item for length of "value".
MAXH(data, value)
Returns the maximum high from the data item over the last "value" of bars.
MEANDEV(data, value)
Returns the Mean Deviation from the data item over the last "value" of bars.
MIMA(data, value)
Returns the MIMA from the data item for length of value (Only available for PhiCube teams).
MINL(data, value)
Returns the minimum low from the data item over the last "value" of bars.
PAST(data, value)
Get the value of the data item "value" number of bars in the past.
ROC(data, value)
Returns the percent change in price from the current bar vs the bar "value" number of bars ago (Rate of Change).
SAN(data, value)
Returns the Santo from the data item for length of value (Only available for PhiCube teams).
SAR(data)
Returns the Parabolic SAR For the data item.
SHL(data, value)
Shift the data item 'value" number of bars in the future to the current bar.
SHR(data, value)
Shift the data item 'value" number of bars ago to the current bar. This is identical to PAST.
SIGNAL(buycondition, sellcondition)
If the buy condition = 1, set the current bar to 1. If the sell condition = 1, set the current bar to -1. Otherwise set the current bar to 0.
SMA(data, value)
Returns the standard moving average from the data item for length of "value".
SMIMA(data, value)
Returns the MIMA using standard moving averages from the data item for length of value (Only available for PhiCube teams).
SQRT(data)
Returns the square root of the data item.
STDDEV(data, value, units)
Returns the standard deviation of the data item over the "value" number of bars using "units" of standard deviation.
STOC(data, value)
Returns the stochastics value over the last "value" of bars.
SUM(data, value)
Adds all the values in the last "value" of bars and places the result in the current bar.
TERM(buycondition, sellcondition)
Returns the number of bars since the last signal change in the current bar, positive if last signal was a buy, negative if a sell.
TRUN(data)
Remove decimals from the data item without rounding (truncate).
WSUM(data, value)
Returns the weighted sum for the data item over the last 'value" of bars.
Operators
+
data + data
Add two data items together. (Add)
-
data - data
Subtract the second data item from the first. (subtract)
*
data * data
Multiply two data items together. (Multiple)
/
data / data
Divide the first data item by the second. (Divide)
***
(condition) *** (data)
If the condition before the operator is 1, add the value of the data after the operator to a cumulative line, if it is 0, subtract it. (Cumulative Boolean)
CUMN
(condition) CUMN (data)
If the condition before the operator is 1, subtract the value of the data after the operator to a cumulative line, if it is 0, do nothing. (Cumulative Negative)
CUMP
(condition) CUMP (data)
If the condition before the operator is 1, add the value of the
data after the operator to a cumulative line, if it is 0, do nothing. (Cumulative positive)
%
data1 % data2
The value in data2 will be added to a series when the value in data1 is above zero and will be subtracted when the value in data1 is below zero. (Cumulative)
/RS
data1 / data2
Divide data1 by data2 and express the result as a percent. (Divide Relative Strength)
Comparisons
All comparisons return a condition
AND
condition AND condition
If both conditions equal 1 return 1 else return 0.
CO
data CO data
Return a 1 if the first data value is greater than the second data value and prior bar is the opposite otherwise output a 0. (Cross Over)
CU
data CU data
Return a 1 if the first data value is less than the second data value and prior bar is the opposite otherwise output a 0. (Cross Under)
>
data > data
Return a 1 if data1 is greater than data2, otherwise return a 0. (Greater)
>=
data >= data
Return a 1 if data1 is greater or equal to data2, otherwise return a 0. (Greater or Equal)
<
data < data
Return a 1 if data1 is less than data2, otherwise return a 0. (Less)
<=
data <= data
Return a 1 if data1 is less than or equal to data2, otherwise return a 0. (Less or Equal)
==
data == data
Return a 1 if data1 is equal to data2, otherwise return a 0. (Equal)
<>
data <> data
Return a 1 if data1 is not equal to data2, otherwise return a 0. (Not Equal)
OR
condition OR condition
If either condition equals 1 return 1 else return 0.
IF ELSE THEN
IF (condition) (function1) else (function2) THEN
If the condition equals 1 execute function1. If the condition equals 0 execute function2.