2008 Aug: Premiere Stochastic Oscillator

ICE Data Services -

PremiereStochastic.efs  
EFSLibrary - Discussion Board  

File Name: PremiereStochastic.efs

Description:
The following studies are based on the August 2008 article, Premier Stochastic Oscillator, by Lee Leibfarth.

Formula Parameters:

PremiereStochastic.efs

  • Stoch Length: 8
  • Period: 25
  • Line1: .9
  • Line2: .2

PSO_Strategy.efs

  • Stoch Length: 8
  • Period: 25
  • Line1: .9
  • Line2: .2
  • Profit Target %: 3
  • Stop Loss %: 1.5

Notes:
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
PremiereStochastic.efs
PSO_Strategy.efs



EFS Code:

/*********************************
Provided By: 
    eSignal (Copyright ) eSignal), a division of Interactive Data Corporation. 2008. 
    All rights reserved. This sample eSignal Formula Script (EFS) is for educational
    purposes only and may be modified and saved under a new file name. eSignal is not
    responsible for the functionality once modified. eSignal reserves the right to modify
    and overwrite this EFS file with each new release. Description: Premier Stochastic Oscillator by Lee Leibfarth
    
Version: 1.0 6/11/2008
Notes:
    * August 2008 Issue of Stocks and Commodities Magazine
    * Study requires version 10.1 or later.
**********************************/
function preMain(){
	setStudyTitle("Premiere Stochastic Oscillator");
	setCursorLabelName("PSO", 0);
	setShowCursorLabel(false, 1);
	setShowTitleParameters(false);
	setPlotType(PLOTTYPE_LINE, 0);
	setPlotType(PLOTTYPE_HISTOGRAM, 1);
	setDefaultBarFgColor(Color.black, 0);
	setDefaultBarFgColor(Color.blue, 1);
	setStudyMax(1.1);
	setStudyMin(-1.1);
	var fp1 = new FunctionParameter("StochLength", FunctionParameter.NUMBER);
	fp1.setName("Stoch Length");
	fp1.setLowerLimit(1);
	fp1.setDefault(8);
	var fp2 = new FunctionParameter("Period", FunctionParameter.NUMBER);
	fp2.setName("Period");
	fp2.setLowerLimit(1);
	fp2.setDefault(25);
	var fp3 = new FunctionParameter("Line1", FunctionParameter.NUMBER);
	fp3.setName("Line1");
	fp3.setLowerLimit(0);
	fp3.setDefault(.9);
	var fp4 = new FunctionParameter("Line2", FunctionParameter.NUMBER);
	fp4.setName("Line2");
	fp4.setLowerLimit(0);
	fp4.setDefault(.2);
}
var bVersion = null;
var bInit = false;
var xPremiere = null;

function main(StochLength, Period, Line1, Line2){
	if (bVersion == null) bVersion = verify();
	if (bVersion == false) return;
	if (bInit == false)	{
		xPremiere = efsInternal("calcPremiere", StochLength, Period);
		addBand(Line1, PS_SOLID, 1, Color.blue, "Line1");
		addBand(Line2, PS_SOLID, 1, Color.blue, "Line2");
		addBand(Line1 * (-1), PS_SOLID, 1, Color.red, "Line3");
		addBand(Line2 * (-1), PS_SOLID, 1, Color.red, "Line4");
		addBand(0, PS_SOLID, 1, Color.black, "0");
		bInit = true;
	}
	var nPremiere = xPremiere.getValue(0);
	if (nPremiere == null) return;
	if (nPremiere >= 0) setBarFgColor(Color.blue, 1);
	else setBarFgColor(Color.red, 1);
	return new Array(nPremiere, nPremiere);
}
var xSmoothStoch = null;

function calcPremiere(_stochlength, _period){
	if (_period < 0) var Length = 1;
	else var Length = Math.sqrt(_period);
	if (xSmoothStoch == null) xSmoothStoch = ema(Length, ema(Length, efsInternal("normStoch", _stochlength)));
	var nSmoothStoch = xSmoothStoch.getValue(0);
	if (nSmoothStoch == null) return;
	return (Math.exp(1 * nSmoothStoch) - 1) / (Math.exp(1 * nSmoothStoch) + 1);
}
var xStochK = null;

function normStoch(_stochlength){
	if (xStochK == null) xStochK = stochK(_stochlength, 1, 1) var nStochK = xStochK.getValue(0);
	if (nStochK == null) return;
	return (.1 * (nStochK - 50));
}

function verify(){
	var b = false;
	if (getBuildNumber() < 999){
		drawTextAbsolute(5, 35, "This study requires version 10.1 or later.", Color.white, Color.blue, Text.RELATIVETOBOTTOM | Text.RELATIVETOLEFT | Text.BOLD | Text.LEFT, null, 13, "error");
		drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", Color.white, Color.blue, Text.RELATIVETOBOTTOM | Text.RELATIVETOLEFT | Text.BOLD | Text.LEFT, null, 13, "upgrade");
		return b;
	}
	else{
		b = true;
	}
	return b;
}

 

/*********************************
Provided By: 
    eSignal (Copyright ) eSignal), a division of Interactive Data Corporation. 2008. 
    All rights reserved. This sample eSignal Formula Script (EFS) is for educational
    purposes only and may be modified and saved under a new file name. eSignal is not
    responsible for the functionality once modified. eSignal reserves the right to modify
    and overwrite this EFS file with each new release. Description: Premier Stochastic Oscillator by Lee Leibfarth
    
Version: 1.0 6/11/2008
Notes:
    * August 2008 Issue of Stocks and Commodities Magazine
    * Study requires version 10.1 or later.* Study is back testing compatible.
**********************************/
function preMain(){
	setStudyTitle("PSO Strategy");
	setCursorLabelName("PSO", 0);
	setShowTitleParameters(false);
	setDefaultFont("Ariel", 11);
	setColorPriceBars(true);
	setDefaultPriceBarColor(Color.lightgrey);
	setStudyMax(1.1);
	setStudyMin(-1.1);
	var fp1 = new FunctionParameter("StochLength", FunctionParameter.NUMBER);
	fp1.setName("Stoch Length");
	fp1.setLowerLimit(1);
	fp1.setDefault(8);
	var fp2 = new FunctionParameter("Period", FunctionParameter.NUMBER);
	fp2.setName("Period");
	fp2.setLowerLimit(1);
	fp2.setDefault(25);
	var fp3 = new FunctionParameter("Line1", FunctionParameter.NUMBER);
	fp3.setName("Line1");
	fp3.setLowerLimit(0);
	fp3.setDefault(.9);
	var fp4 = new FunctionParameter("Line2", FunctionParameter.NUMBER);
	fp4.setName("Line2");
	fp4.setLowerLimit(0);
	fp4.setDefault(.2);
	var fp5 = new FunctionParameter("Profit", FunctionParameter.NUMBER);
	fp5.setName("Profit Target %");
	fp5.setLowerLimit(0);
	fp5.setDefault(3);
	var fp6 = new FunctionParameter("Stop", FunctionParameter.NUMBER);
	fp6.setName("Stop Loss %");
	fp6.setLowerLimit(0);
	fp6.setDefault(1.5);
}
var bVersion = null;
var bInit = false;
var bBT = true;
var xPremiere = null;
var TargetPrice = 0;
var StopPrice = 0;
var vPosition = 0; // 1=long, 0=flat, -1=short
function main(StochLength, Period, Line1, Line2, Profit, Stop){
	if (bVersion == null) bVersion = verify();
	if (bVersion == false) return;
	if (bInit == false)	{
		xPremiere = efsInternal("calcPremiere", StochLength, Period);
		addBand(Line1, PS_SOLID, 1, Color.blue, "Line1");
		addBand(Line2, PS_SOLID, 1, Color.blue, "Line2");
		addBand(Line1 * (-1), PS_SOLID, 1, Color.red, "Line3");
		addBand(Line2 * (-1), PS_SOLID, 1, Color.red, "Line4");
		addBand(0, PS_SOLID, 1, Color.black, "0");
		bInit = true;
	}
	var nPremiere_0 = xPremiere.getValue(0);
	var nPremiere_1 = xPremiere.getValue(-1);
	var nPremiere_2 = xPremiere.getValue(-2);
	if (nPremiere_2 == null) return;
	if (getCurrentBarIndex() >= -1) bBT = false;
	if (vPosition == 1)	{ // Long Exit 
		if (high(0) >= TargetPrice)	{
			if (bBT) Strategy.doSell("Long Target", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, Math.max(open(0), TargetPrice));
			drawShape(Shape.DOWNARROW, TopRow2, Color.green, "LongTarget" + rawtime(0));
			drawText("LT", TopRow1, Color.green, Text.CENTER, "LT" + rawtime(0));
			vPosition = 0;
		}
		if (low(0) <= StopPrice){
			if (bBT) Strategy.doSell("Long Stop", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, Math.min(open(0), StopPrice));
			drawShape(Shape.DOWNARROW, TopRow2, Color.red, "LongStop" + rawtime(0));
			drawText("LS", TopRow1, Color.red, Text.CENTER, "LS" + rawtime(0));
			vPosition = 0;
		}
	}
	if (vPosition == -1) { // Short Exit 
		if (low(0) <= TargetPrice){
			if (bBT) Strategy.doCover("Short Target", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, Math.min(open(0), TargetPrice));
			drawShape(Shape.UPARROW, BottomRow2, Color.green, "ShortTarget" + rawtime(0));
			drawText("ST", BottomRow1, Color.green, Text.CENTER, "ST" + rawtime(0));
			vPosition = 0;
		}
		if (high(0) >= StopPrice){
			if (bBT) Strategy.doCover("Short Stop", Strategy.STOP, Strategy.THISBAR, Strategy.ALL, Math.max(open(0), StopPrice));
			drawShape(Shape.UPARROW, BottomRow2, Color.red, "ShortStop" + rawtime(0));
			drawText("SS", BottomRow1, Color.red, Text.CENTER, "SS" + rawtime(0));
			vPosition = 0;
		}
	}
	if (getBarState() == BARSTATE_NEWBAR){ // Long Entry 
		if (vPosition != 1 && nPremiere_2 > Line1 && nPremiere_1 < line1 || npremiere_2 > Line2 && nPremiere_1 < line2) != "-1" && = "" (bbt) = ""
		drawshape(shape.uparrow, bottomrow2, color.blue, = "drawshape(shape.uparrow,bottomrow2,color.blue,"
			longentry = "longentry" + rawtime(0)); = ""
		drawtext( = "drawtext("
			le = "le", bottomrow1, color.blue, text.center, = ",bottomrow1,color.blue,text.center,"
			entry = "entry", strategy.market, strategy.thisbar, strategy.default); = ""
		if = ""
		if (vposition = ""
			npremiere_2 = "npremiere_2" > < line1 * (-1) && npremiere_1 = ""
			short = ""
			stopprice = "open(0)*(1-(Stop/100));"
			strategy.dolong( = "strategy.dolong("
				long = ""
				targetprice = "open(0)*(1+(Profit/100));"
				vposition = "1;"
				{ = ""
				} = "" > Line1 * (-1) || nPremiere_2 < line2 * (-1) && npremiere_1 > Line2 * (-1))
			{
				if (bBT) Strategy.doShort("Short Entry", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT);
				drawShape(Shape.DOWNARROW, TopRow2, Color.magenta, "ShortEntry" + rawtime(0));
				drawText("SE", TopRow1, Color.magenta, Text.CENTER, "SE" + rawtime(0));
				TargetPrice = open(0) * (1 - (Profit / 100));
				StopPrice = open(0) * (1 + (Stop / 100));
				vPosition = -1;
			}
		}
		if (nPremiere_0 >= 0) setBarFgColor(Color.blue);
		else setBarFgColor(Color.red);
		if (vPosition == 1) setDefaultPriceBarColor(Color.blue);
		else if (vPosition == -1) setDefaultPriceBarColor(Color.red);
		else setDefaultPriceBarColor(Color.lightgrey);
		return nPremiere_0;
	}
	var xSmoothStoch = null;

	function calcPremiere(_stochlength, _period){
		if (_period < 0) var Length = 1;
		else var Length = Math.sqrt(_period);
		if (xSmoothStoch == null) xSmoothStoch = ema(Length, ema(Length, efsInternal("normStoch", _stochlength)));
		var nSmoothStoch = xSmoothStoch.getValue(0);
		if (nSmoothStoch == null) return;
		return (Math.exp(1 * nSmoothStoch) - 1) / (Math.exp(1 * nSmoothStoch) + 1);
	}
	var xStochK = null;

	function normStoch(_stochlength){
		if (xStochK == null) xStochK = stochK(_stochlength, 1, 1) var nStochK = xStochK.getValue(0);
		if (nStochK == null) return;
		return (.1 * (nStochK - 50));
	}

	function verify(){
		var b = false;
		if (getBuildNumber() < 999){
			drawTextAbsolute(5, 35, "This study requires version 10.1 or later.", Color.white, Color.blue, Text.RELATIVETOBOTTOM | Text.RELATIVETOLEFT | Text.BOLD | Text.LEFT, null, 13, "error");
			drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", Color.white, Color.blue, Text.RELATIVETOBOTTOM | Text.RELATIVETOLEFT | Text.BOLD | Text.LEFT, null, 13, "upgrade");
			return b;
		}
		else{
			b = true;
		}
		return b;
	}