Formula Wizard Guide

ICE Data Services -

Sample EFS Routines


eSignal Formula Script (EFS), which is an extended version of JavaScript 1.5, offers amazing flexibility, allowing you to write sophisticated indicators using core JavaScript functionality such as for loops, Math object, Date object and much more.  Additionally, you can develop formulas for drawing lines and text on charts that can contain URL links. The following is a list of common EFS routines used in formula development.

Sample EFS Routines

1 Setting the minimum/maximum of a study
2 Setting the study title (appears in upper left corner or pane)
3 Setting the cursor label
4 Turning off the cursor label (hiding the cursor label)
5 Setting the plot type
6 Setting the histogram base (when plotting a histogram)
7 Setting the default color, style and thickness of the line being plotted
8 Setting the color, style and thickness of a specific bar being plotted
9 Adding horizontal lines (aka bands) (e.g., Stochastic Bands @ 20 and 80, RSI Bands @ 30 and 70)
10 Controlling the color of the price bars
11 Plotting data with the price bars (Examples: Keltner Bands, Moving Average, Bollinger Bands)
12 Plotting multiple values from one formula
13 Setting the color, style, thickness to and cursor label for a formula that plots multiple values
14 Other tools and debugging



Setting the Minimum/Maximum of a Study
The formula engine will automatically determine the minimum and maximum data values of your study and scale the study as needed. If you want to override the automatic scaling, use setStudyMin and setStudyMax. These must be called in preMain.

Example (for an RSI):

var rsi = new RSIStudy(14);
function preMain() {
  setStudyMin(0);
  setStudyMax(100);
}

function main() {
  return rsi.getValue(RSIStudy.RSI);
}

Setting the Study Title (appears in upper left corner or pane)

Example:

var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Setting the Cursor Label

Example:

var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);
	setCursorLabelName(“RSI”);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Turning Off the Cursor Label (hiding the cursor label)

Example:

 var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);
	setShowCursorLabel(false);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Back To Top

Setting the Plot Type

Example:

 var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);
	setCursorLabelName(“RSI”);
	setPlotType(PLOTTYPE_HISTOGRAM);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Setting the Histogram Base (when plotting a histogram)

Example:

var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);
	setCursorLabelName(“RSI”);
	setPlotType(PLOTTYPE_HISTOGRAM);
	setHistogramBase(50);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Setting the Default Color, Style and Thickness of the Line Being Plotted

Example:

var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);

	setDefaultBarFgColor(Color.black);
	setDefaultBarThickness(1);
	setDefaultBarStyle(PS_DOT);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Setting the Color, Style and Thickness of a Specific Bar Being Plotted

Example:

 var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);

	setDefaultBarFgColor(Color.black);
	setDefaultBarThickness(1);
	setDefaultBarStyle(PS_DOT);
}

function main(){
	var v = rsi.getValue(RSIStudy.RSI);
	if (v == null)
		return;

	if (v > 70)	{
		setBarFgColor(Color.red);
		setBarThickness(2);
		setBarStyle(PS_SOLID);
	}
	else if (v < 30){
		setBarFgColor(Color.green);
		setBarThickness(2);
		setBarStyle(PS_SOLID);
	}
	return v;
}

Back To Top

Adding Horizontal Lines (aka Bands) (e.g., Stochastic Bands @ 20 and 80, RSI Bands @ 30 and 70)

Example:

var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);

	addBand(30, PS_SOLID, 1, Color.black);
	addBand(70, PS_SOLID, 1, Color.black);
}

function main(){
	return rsi.getValue(RSIStudy.RSI);
}

Controlling the Color of the Price Bars

Example:

 var rsi = new RSIStudy(14);

function preMain(){
	setStudyMin(0);
	setStudyMax(100);
	setStudyTitle(“RSI Study”);

	setDefaultBarFgColor(Color.black);
	setDefaultBarThickness(1);
	setDefaultBarStyle(PS_DOT);

	setColorPriceBars(true);
	setDefaultPriceBarColor(Color.black);
}

function main(){
	var v = rsi.getValue(RSIStudy.RSI);
	if (v == null)
		return;

	if (v > 70)	{
		setPriceBarColor(Color.red);
	}
	else if (v < 30){
		setPriceBarColor(Color.green);
	}
	return v;
}

Plotting Data with the Price Bars (Examples: Keltner Bands, Moving Average, Bollinger Bands)

Example:

var bb = new BollingerStudy(20, "Close", 2.0);

function preMain(){
	setPriceStudy(true);
}
function main(){
	var vBasis = study.getValue(BollingerStudy.BASIS);

	return vBasis;
}

Back To Top

Plotting Multiple Values from One Formula
To plot multiple lines from your formula (e.g., Stochastic %K and %D, Keltner Bands, Bollinger Bands, MACD) return an array of values.

Example:

var bb = new BollingerStudy(20, "Close", 2.0);

function preMain(){
	setPriceStudy(true);
}

function main(){
	var vUpper = study.getValue(BollingerStudy.UPPER);
	var vBasis = study.getValue(BollingerStudy.BASIS);
	var vLower = study.getValue(BollingerStudy.LOWER);

	return new Array(vUpper, vBasis, vLower);
}

Setting the Color, Style, Thickness and Cursor Label for a Formula That Plots Multiple Values

Example:

var bb = new BollingerStudy(20, "Close", 2.0);

function preMain(){
	setPriceStudy(true);

	// 0,1, and 2 are determined by the order the
	// Parameters are returned in main.
	// In main, the values returned in the array
	// are (in this order:)
	// 0=vUpper
	// 1=vBasis
	// 2=vLower
	//
	setCursorLabelName(“BBUpper”, 0);
	setCursorLabelName(“BBBasis”, 1);
	setCursorLabelName(“BBLower”, 2);

	// upper
	setDefaultBarColor(Color.red, 0);
	// basis
	setDefaultBarColor(Color.blue, 1);
	// lower
	setDefaultBarColor(Color.red, 2);

}

function main(){
	var vUpper = study.getValue(BollingerStudy.UPPER);
	var vBasis = study.getValue(BollingerStudy.BASIS);
	var vLower = study.getValue(BollingerStudy.LOWER);

	return new Array(vUpper, vBasis, vLower);
}

Outputting Text for Debugging Purposes
You can write text to the “Formula Output Window”. This option is available on the “View” menu. Using “debugPrint” and “debugPrintln”, you can send information to this window.

Example:

function main(){
	debugPrintln(“Symbol: “+getSymbol());
	debugPrintln(“Interval: “+getInterval());
	debugPrintln(“CurrentBar: “+getCurrentBarIndex());
}

Back To Top

What Are the Different Bar Styles?

  • PS_SOLID
  • PS_DASH
  • PS_DOT
  • PS_DASHDOT
  • PS_DASHDOTDOT

What Are the Built-In Color Values?

  • Builtin Color Value Coressponding RGB Value
  • Color:white Color RGB(0xFF, 0xFF, 0xFF)
  • Color:black Color RGB(0x00, 0x00, 0x00)
  • Color:darkgrey Color RGB(0x40, 0x40, 0x40)
  • Color:grey Color RGB(0x80, 0x80, 0x80)
  • Color:lightgrey Color RGB(0xC0, 0xC0, 0xC0)
  • Color.navy Color RGB(0x00, 0x00, 0x80)
  • Color:blue Color RGB(0x00, 0x00, 0xFF)
  • Color:aqua Color RGB(0x00, 0x00, 0xDD)
  • Color:cyan Color RGB(0x00, 0xFF, 0xFF)
  • Color:teal Color RGB(0x40, 0x80, 0x80)
  • Color:darkgreen Color RGB(0x00, 0x40, 0x00)
  • Color:green Color RGB(0x00, 0x80, 0x00)
  • Color:lime Color RGB(0x00, 0xFF, 0x00)
  • Color:olive Color RGB(0x66, 0x66, 0x00)
  • Color:khaki Color RGB(0xCC, 0xCC, 0x00)
  • Color:brown Color RGB(0x80, 0x40, 0x40)
  • Color:purple Color RGB(0x99, 0x66, 0xCC)
  • Color:red Color RGB(0xFF, 0x00, 0x00)
  • Color:magenta Color RGB(0xFF, 0x00, 0xFF)
  • Color:maroon Color RGB(0x80, 0x00, 0x00)
  • Color:fushcia Color RGB(0xFF, 0x00, 0xFF)
  • Color:yellow Color RGB(0xFF, 0xFF, 0x00)
  • Color:lightyellow Color RGB(0xFF, 0xFF, 0x40)
  • Color:paleyellow Color RGB(0xFF, 0xFF, 0x80)

How Can I Create My Own Color?
Use Color.RGB(rValue, gValue, bValue). rValue, gValue , and bValue must be a value between 0 and 255 (or 0 and 0xFF).

Example:
Color.RGB(128, 128, 128);

What Are the Different Plot Types?

  • PLOTTYPE_LINE (default)
  • PLOTTYPE_DOT
  • PLOTTYPE_SQUAREWAVE
  • PLOTTYPE_HISTOGRAM
  • PLOTTYPE_FLATLINES
  • PLOTTYPE_INSTANTCOLORLINE