Announcement
Collapse
No announcement yet.
Heiken Ashi Alert
Collapse
X
-
Can you please send me the .efs for the latest revision of the formula?
Thanks, Bob
PS I do not want the PHP, but just the .efs so I can do save target as and put it in my formulas folder.
Leave a comment:
-
Thank you so much for your help Alex! The script is working great now.
Leave a comment:
-
Charley
You have two issues.
The first one is that any code following a return statement is not executed. This is explained in the section regarding the return statement in the Beginner Tutorial 2: Commonly Used Core JavaScript which you can find in the Help Guides and Tutorials folder of the EFS KnowledgeBase. In essence that code you added is simply ignored.
Once you resolve that and run the script you will however get an error. That is because when implementing the ref() function you also need to use some counter to ensure it returns valid results (rather than null). For more on this see both articles in the EFS KnowledgeBase related to the ref() function as they provide a specific explanation and example. It seems to me that in the case of your script you should be at least on the third oldest bar before calling the ref() function
Once you resolve also this the script will work correctly
Alex
Originally posted by charley View PostThank you for your help Alex. I have never used the ref() function before. I added the lines following return retArray; as my latest attempt, and have tried several different variables and still cannot return the previous values. I cannot figure out what I am missing.
PHP Code:/*****************************************************************
Provided By : eSignal. (c) Copyright 2003
EFS Formula : Heikin-Ashi
Version 2.0
9/13/2004
Notes:
* Non-price study
* Draws most recent 200 bars
* Formula Parameters
- Bullish Color Default: Green
- Bearish Color Default: Red
- Smoothing Default: None [None, T3, KAMA]
* 2.0 Added T3 and KAMA smoothing options per
[url]http://warrants.bnpparibas.com/de/doc/WZ_0804.pdf[/url] (page 16)
*****************************************************************/
function preMain() {
setStudyTitle("Heikin-Ashi w/ Smoothing ");
setCursorLabelName("HA-High", 0);
setCursorLabelName("HA-Low", 1);
setCursorLabelName("HA-Open", 2);
setCursorLabelName("HA-Close", 3);
setDefaultBarFgColor(Color.black, 0);
setDefaultBarFgColor(Color.black, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.black, 3);
setPlotType(PLOTTYPE_DOT, 0);
setPlotType(PLOTTYPE_DOT, 1);
setPlotType(PLOTTYPE_DOT, 2);
setPlotType(PLOTTYPE_DOT, 3);
setDefaultBarThickness(0, 0);
setDefaultBarThickness(0, 1);
setDefaultBarThickness(0, 2);
setDefaultBarThickness(0, 3);
setShowTitleParameters(false);
var fp1 = new FunctionParameter("cBull", FunctionParameter.COLOR);
fp1.setName("Bullish Candles");
fp1.setDefault(Color.green);
var fp2 = new FunctionParameter("cBear", FunctionParameter.COLOR);
fp2.setName("Bearish Candles");
fp2.setDefault(Color.red);
var fp3 = new FunctionParameter("sSmooth", FunctionParameter.STRING);
fp3.setName("Smoothing");
fp3.addOption("None");
fp3.addOption("T3");
fp3.addOption("KAMA");
fp3.setDefault("T3");
var fp4 = new FunctionParameter("nT3Periods", FunctionParameter.NUMBER);
fp4.setName("T3 Periods");
fp4.setLowerLimit(1);
fp4.setDefault(3);
var fp5 = new FunctionParameter("vFactor", FunctionParameter.NUMBER);
fp5.setName("T3 V Factor");
fp5.setLowerLimit(0);
fp5.setDefault(0.5);
var fp6 = new FunctionParameter("nKAMAPeriods", FunctionParameter.NUMBER);
fp6.setName("KAMA Periods");
fp6.setLowerLimit(2);
fp6.setDefault(2);
}
var haClose = null;
var haOpen = null;
var haClose1 = null;
var haOpen1 = null;
var iCntr = 0;
// T3 vars
var b = null; // V Factor
var b2 = null;
var b3 = null;
var c1 = null;
var c2 = null;
var c3 = null;
var c4 = null;
var f1 = null;
var f2 = null;
var e1 = null;
var e2 = null;
var e3 = null;
var e4 = null;
var e5 = null;
var e6 = null;
var e1_1 = 0;
var e2_1 = 0;
var e3_1 = 0;
var e4_1 = 0;
var e5_1 = 0;
var e6_1 = 0;
var Price = null;
var bInit = false;
// KAMA vars
var nBarCounter = 0;
var aAMA = null;
var aFPArray = new Array();
var bInitialized = false;
var bPrimed = false;
function main(cBull, cBear, sSmooth, nT3Periods, vFactor, nKAMAPeriods) {
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
if ((haClose == null || haOpen == null) && close(-1) != null) {
haClose = close(-1);
haOpen = open(-1);
}
haClose1 = haClose;
haOpen1 = haOpen;
iCntr += 1;
if (iCntr > 200) iCntr = 0;
}
if (haClose1 == null || haOpen1 == null) return;
haOpen = (haOpen1 + haClose1) / 2;
haClose = (open() + high() + low() + close()) / 4;
if (sSmooth == "T3") {
setStudyTitle("Heikin-Ashi w/ " + nT3Periods + "-Period T3 Smoothing ");
haClose = T3Avg(nT3Periods, vFactor, haClose);
} else if (sSmooth == "KAMA") {
setStudyTitle("Heikin-Ashi w/ " + nKAMAPeriods + "-Period KAMA Smoothing ");
haClose = KAMA(nKAMAPeriods, haClose);
} else {
setStudyTitle("Heikin-Ashi w/out Smoothing ");
}
if (haClose == null) return;
var haHigh = Math.max(high(), haOpen, haClose);
var haLow = Math.min(low(), haOpen, haClose);
//candlesticks
var vColor = Color.black;
if (haClose > haOpen) vColor = cBull;
if (haClose < haOpen) vColor = cBear;
setBarFgColor(vColor, 2);
setBarFgColor(vColor, 3);
drawLineRelative(0, haHigh, 0, haLow, PS_SOLID, 1, Color.black, "Shadow"+iCntr);
drawLineRelative(0, haOpen, 0, haClose, PS_SOLID, 3, vColor, "Body"+iCntr);
var retArray = new Array(4);
retArray[0] = haHigh.toFixed(2)*1;
retArray[1] = haLow.toFixed(2)*1;
retArray[2] = haOpen.toFixed(2)*1;
retArray[3] = haClose.toFixed(2)*1;
return retArray;
var myRef = ref(-1);
var Open1 = myRef[2];
var Close1 = myRef[3];
if ((Close1 > Open1) && (haClose < haOpen)) {
Alert.playSound("chimes.wav");
}
if ((Close1 < Open1) && (haClose > haOpen)) {
Alert.playSound("chimes.wav");
}
}
//*** T3 ****
function T3Avg(nPeriods, vFactor, vPrice) {
if (bInit == false) {
b = vFactor;
b2 = b * b;
b3 = b * b * b;
c1 = -b3;
c2 = 3 * b2 + 3 * b3;
c3 = -6 * b2 - 3 * b - 3 * b3;
c4 = 1 + 3 * b + b3 + 3 * b2;
f1 = 2 / (nPeriods + 1);
f2 = 1 - f1;
bInit = true;
}
if (getBarState() == BARSTATE_NEWBAR) {
e1_1 = e1;
e2_1 = e2;
e3_1 = e3;
e4_1 = e4;
e5_1 = e5;
e6_1 = e6;
}
e1 = f1 * vPrice + f2 * e1_1;
e2 = f1 * e1 + f2 * e2_1;
e3 = f1 * e2 + f2 * e3_1;
e4 = f1 * e3 + f2 * e4_1;
e5 = f1 * e4 + f2 * e5_1;
e6 = f1 * e5 + f2 * e6_1;
var T3Average = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;
return T3Average;
}
//*** END of T3 Functions ****
//*** KAMA ****
function KAMA(Period, vClose) {
var x;
var nNoise;
var nSignal;
var nSmooth;
if (bInitialized == false) {
Period = Math.round(Period);
aAMA = new Array(Period);
for ( x=0; x<Period; x++ ) {
aAMA[x] = 0.0;
}
bInitialized = true;
}
if ( getBarState() == BARSTATE_NEWBAR ) {
aAMA.pop();
aAMA.unshift(0);
nBarCounter++;
}
if (nBarCounter<Period) return null;
if (bPrimed == false) {
aAMA[0] = (open(0)+close(0)) / 2 + ( ( (close(0)-open(0)) / (high(0)-low(0)) ) * Math.abs( (close(0)-open(0)) / 2) );
bPrimed = true;
return aAMA[0];
} else {
aAMA[0] = vClose;
}
nSignal = Math.abs( close(0) - close(-Period) );
x=0;
nNoise = 0;
while(x<Period) {
nNoise += Math.abs( close(-x)-close(-(x+1)) );
x++;
}
if ( nNoise==0 ) nNoise = 1.0;//added by ACM
nSmooth = Math.pow( ( nSignal/nNoise ) * ( 0.6667 - 0.0645 ) + 0.0645, 2 );
aAMA[0] = aAMA[1] + nSmooth * ( close(0) - aAMA[1] );
return aAMA[0];
}
//*** END of KAMA ****
Leave a comment:
-
Thank you for your help Alex. I have never used the ref() function before. I added the lines following return retArray; as my latest attempt, and have tried several different variables and still cannot return the previous values. I cannot figure out what I am missing.
PHP Code:/*****************************************************************
Provided By : eSignal. (c) Copyright 2003
EFS Formula : Heikin-Ashi
Version 2.0
9/13/2004
Notes:
* Non-price study
* Draws most recent 200 bars
* Formula Parameters
- Bullish Color Default: Green
- Bearish Color Default: Red
- Smoothing Default: None [None, T3, KAMA]
* 2.0 Added T3 and KAMA smoothing options per
[url]http://warrants.bnpparibas.com/de/doc/WZ_0804.pdf[/url] (page 16)
*****************************************************************/
function preMain() {
setStudyTitle("Heikin-Ashi w/ Smoothing ");
setCursorLabelName("HA-High", 0);
setCursorLabelName("HA-Low", 1);
setCursorLabelName("HA-Open", 2);
setCursorLabelName("HA-Close", 3);
setDefaultBarFgColor(Color.black, 0);
setDefaultBarFgColor(Color.black, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.black, 3);
setPlotType(PLOTTYPE_DOT, 0);
setPlotType(PLOTTYPE_DOT, 1);
setPlotType(PLOTTYPE_DOT, 2);
setPlotType(PLOTTYPE_DOT, 3);
setDefaultBarThickness(0, 0);
setDefaultBarThickness(0, 1);
setDefaultBarThickness(0, 2);
setDefaultBarThickness(0, 3);
setShowTitleParameters(false);
var fp1 = new FunctionParameter("cBull", FunctionParameter.COLOR);
fp1.setName("Bullish Candles");
fp1.setDefault(Color.green);
var fp2 = new FunctionParameter("cBear", FunctionParameter.COLOR);
fp2.setName("Bearish Candles");
fp2.setDefault(Color.red);
var fp3 = new FunctionParameter("sSmooth", FunctionParameter.STRING);
fp3.setName("Smoothing");
fp3.addOption("None");
fp3.addOption("T3");
fp3.addOption("KAMA");
fp3.setDefault("T3");
var fp4 = new FunctionParameter("nT3Periods", FunctionParameter.NUMBER);
fp4.setName("T3 Periods");
fp4.setLowerLimit(1);
fp4.setDefault(3);
var fp5 = new FunctionParameter("vFactor", FunctionParameter.NUMBER);
fp5.setName("T3 V Factor");
fp5.setLowerLimit(0);
fp5.setDefault(0.5);
var fp6 = new FunctionParameter("nKAMAPeriods", FunctionParameter.NUMBER);
fp6.setName("KAMA Periods");
fp6.setLowerLimit(2);
fp6.setDefault(2);
}
var haClose = null;
var haOpen = null;
var haClose1 = null;
var haOpen1 = null;
var iCntr = 0;
// T3 vars
var b = null; // V Factor
var b2 = null;
var b3 = null;
var c1 = null;
var c2 = null;
var c3 = null;
var c4 = null;
var f1 = null;
var f2 = null;
var e1 = null;
var e2 = null;
var e3 = null;
var e4 = null;
var e5 = null;
var e6 = null;
var e1_1 = 0;
var e2_1 = 0;
var e3_1 = 0;
var e4_1 = 0;
var e5_1 = 0;
var e6_1 = 0;
var Price = null;
var bInit = false;
// KAMA vars
var nBarCounter = 0;
var aAMA = null;
var aFPArray = new Array();
var bInitialized = false;
var bPrimed = false;
function main(cBull, cBear, sSmooth, nT3Periods, vFactor, nKAMAPeriods) {
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
if ((haClose == null || haOpen == null) && close(-1) != null) {
haClose = close(-1);
haOpen = open(-1);
}
haClose1 = haClose;
haOpen1 = haOpen;
iCntr += 1;
if (iCntr > 200) iCntr = 0;
}
if (haClose1 == null || haOpen1 == null) return;
haOpen = (haOpen1 + haClose1) / 2;
haClose = (open() + high() + low() + close()) / 4;
if (sSmooth == "T3") {
setStudyTitle("Heikin-Ashi w/ " + nT3Periods + "-Period T3 Smoothing ");
haClose = T3Avg(nT3Periods, vFactor, haClose);
} else if (sSmooth == "KAMA") {
setStudyTitle("Heikin-Ashi w/ " + nKAMAPeriods + "-Period KAMA Smoothing ");
haClose = KAMA(nKAMAPeriods, haClose);
} else {
setStudyTitle("Heikin-Ashi w/out Smoothing ");
}
if (haClose == null) return;
var haHigh = Math.max(high(), haOpen, haClose);
var haLow = Math.min(low(), haOpen, haClose);
//candlesticks
var vColor = Color.black;
if (haClose > haOpen) vColor = cBull;
if (haClose < haOpen) vColor = cBear;
setBarFgColor(vColor, 2);
setBarFgColor(vColor, 3);
drawLineRelative(0, haHigh, 0, haLow, PS_SOLID, 1, Color.black, "Shadow"+iCntr);
drawLineRelative(0, haOpen, 0, haClose, PS_SOLID, 3, vColor, "Body"+iCntr);
var retArray = new Array(4);
retArray[0] = haHigh.toFixed(2)*1;
retArray[1] = haLow.toFixed(2)*1;
retArray[2] = haOpen.toFixed(2)*1;
retArray[3] = haClose.toFixed(2)*1;
return retArray;
var myRef = ref(-1);
var Open1 = myRef[2];
var Close1 = myRef[3];
if ((Close1 > Open1) && (haClose < haOpen)) {
Alert.playSound("chimes.wav");
}
if ((Close1 < Open1) && (haClose > haOpen)) {
Alert.playSound("chimes.wav");
}
}
//*** T3 ****
function T3Avg(nPeriods, vFactor, vPrice) {
if (bInit == false) {
b = vFactor;
b2 = b * b;
b3 = b * b * b;
c1 = -b3;
c2 = 3 * b2 + 3 * b3;
c3 = -6 * b2 - 3 * b - 3 * b3;
c4 = 1 + 3 * b + b3 + 3 * b2;
f1 = 2 / (nPeriods + 1);
f2 = 1 - f1;
bInit = true;
}
if (getBarState() == BARSTATE_NEWBAR) {
e1_1 = e1;
e2_1 = e2;
e3_1 = e3;
e4_1 = e4;
e5_1 = e5;
e6_1 = e6;
}
e1 = f1 * vPrice + f2 * e1_1;
e2 = f1 * e1 + f2 * e2_1;
e3 = f1 * e2 + f2 * e3_1;
e4 = f1 * e3 + f2 * e4_1;
e5 = f1 * e4 + f2 * e5_1;
e6 = f1 * e5 + f2 * e6_1;
var T3Average = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;
return T3Average;
}
//*** END of T3 Functions ****
//*** KAMA ****
function KAMA(Period, vClose) {
var x;
var nNoise;
var nSignal;
var nSmooth;
if (bInitialized == false) {
Period = Math.round(Period);
aAMA = new Array(Period);
for ( x=0; x<Period; x++ ) {
aAMA[x] = 0.0;
}
bInitialized = true;
}
if ( getBarState() == BARSTATE_NEWBAR ) {
aAMA.pop();
aAMA.unshift(0);
nBarCounter++;
}
if (nBarCounter<Period) return null;
if (bPrimed == false) {
aAMA[0] = (open(0)+close(0)) / 2 + ( ( (close(0)-open(0)) / (high(0)-low(0)) ) * Math.abs( (close(0)-open(0)) / 2) );
bPrimed = true;
return aAMA[0];
} else {
aAMA[0] = vClose;
}
nSignal = Math.abs( close(0) - close(-Period) );
x=0;
nNoise = 0;
while(x<Period) {
nNoise += Math.abs( close(-x)-close(-(x+1)) );
x++;
}
if ( nNoise==0 ) nNoise = 1.0;//added by ACM
nSmooth = Math.pow( ( nSignal/nNoise ) * ( 0.6667 - 0.0645 ) + 0.0645, 2 );
aAMA[0] = aAMA[1] + nSmooth * ( close(0) - aAMA[1] );
return aAMA[0];
}
//*** END of KAMA ****
Leave a comment:
-
Charley
Given that those values are returned by the script the easiest way is to use the ref() function. See the EFS KnowledgeBase for the description and syntax of the function
There are also other ways of storing and retrieving the prior values of custom variables such as using arrays, or creating additional global variables to hold those values and updating them at each new bar. Most of these topics have been covered at length before (even recently for that matter) so you may want to search the forums
Alex
Originally posted by charley View PostGood morning guys,
I am using this old Heiken Ashi efs and want to set an alert that triggers when the candles change from green to red and vice versa. I understand how to set the alert, but have been unable to extract the previous value of haOpen and haClose to compare against the current values. Any help would be appreciated.
Thank you, Charley
PHP Code:/*****************************************************************
Provided By : eSignal. (c) Copyright 2003
EFS Formula : Heikin-Ashi
Version 2.0
9/13/2004
Notes:
* Non-price study
* Draws most recent 200 bars
* Formula Parameters
- Bullish Color Default: Green
- Bearish Color Default: Red
- Smoothing Default: None [None, T3, KAMA]
* 2.0 Added T3 and KAMA smoothing options per
http://warrants.bnpparibas.com/de/doc/WZ_0804.pdf (page 16)
*****************************************************************/
function preMain() {
setStudyTitle("Heikin-Ashi w/ Smoothing ");
setCursorLabelName("HA-High", 0);
setCursorLabelName("HA-Low", 1);
setCursorLabelName("HA-Open", 2);
setCursorLabelName("HA-Close", 3);
setDefaultBarFgColor(Color.black, 0);
setDefaultBarFgColor(Color.black, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.black, 3);
setPlotType(PLOTTYPE_DOT, 0);
setPlotType(PLOTTYPE_DOT, 1);
setPlotType(PLOTTYPE_DOT, 2);
setPlotType(PLOTTYPE_DOT, 3);
setDefaultBarThickness(0, 0);
setDefaultBarThickness(0, 1);
setDefaultBarThickness(0, 2);
setDefaultBarThickness(0, 3);
setShowTitleParameters(false);
var fp1 = new FunctionParameter("cBull", FunctionParameter.COLOR);
fp1.setName("Bullish Candles");
fp1.setDefault(Color.green);
var fp2 = new FunctionParameter("cBear", FunctionParameter.COLOR);
fp2.setName("Bearish Candles");
fp2.setDefault(Color.red);
var fp3 = new FunctionParameter("sSmooth", FunctionParameter.STRING);
fp3.setName("Smoothing");
fp3.addOption("None");
fp3.addOption("T3");
fp3.addOption("KAMA");
fp3.setDefault("T3");
var fp4 = new FunctionParameter("nT3Periods", FunctionParameter.NUMBER);
fp4.setName("T3 Periods");
fp4.setLowerLimit(1);
fp4.setDefault(3);
var fp5 = new FunctionParameter("vFactor", FunctionParameter.NUMBER);
fp5.setName("T3 V Factor");
fp5.setLowerLimit(0);
fp5.setDefault(0.5);
var fp6 = new FunctionParameter("nKAMAPeriods", FunctionParameter.NUMBER);
fp6.setName("KAMA Periods");
fp6.setLowerLimit(2);
fp6.setDefault(2);
}
var haClose = null;
var haOpen = null;
var haClose1 = null;
var haOpen1 = null;
var iCntr = 0;
// T3 vars
var b = null; // V Factor
var b2 = null;
var b3 = null;
var c1 = null;
var c2 = null;
var c3 = null;
var c4 = null;
var f1 = null;
var f2 = null;
var e1 = null;
var e2 = null;
var e3 = null;
var e4 = null;
var e5 = null;
var e6 = null;
var e1_1 = 0;
var e2_1 = 0;
var e3_1 = 0;
var e4_1 = 0;
var e5_1 = 0;
var e6_1 = 0;
var Price = null;
var bInit = false;
// KAMA vars
var nBarCounter = 0;
var aAMA = null;
var aFPArray = new Array();
var bInitialized = false;
var bPrimed = false;
function main(cBull, cBear, sSmooth, nT3Periods, vFactor, nKAMAPeriods) {
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
if ((haClose == null || haOpen == null) && close(-1) != null) {
haClose = close(-1);
haOpen = open(-1);
}
haClose1 = haClose;
haOpen1 = haOpen;
iCntr += 1;
if (iCntr > 200) iCntr = 0;
}
if (haClose1 == null || haOpen1 == null) return;
haOpen = (haOpen1 + haClose1) / 2;
haClose = (open() + high() + low() + close()) / 4;
if (sSmooth == "T3") {
setStudyTitle("Heikin-Ashi w/ " + nT3Periods + "-Period T3 Smoothing ");
haClose = T3Avg(nT3Periods, vFactor, haClose);
} else if (sSmooth == "KAMA") {
setStudyTitle("Heikin-Ashi w/ " + nKAMAPeriods + "-Period KAMA Smoothing ");
haClose = KAMA(nKAMAPeriods, haClose);
} else {
setStudyTitle("Heikin-Ashi w/out Smoothing ");
}
if (haClose == null) return;
var haHigh = Math.max(high(), haOpen, haClose);
var haLow = Math.min(low(), haOpen, haClose);
//candlesticks
var vColor = Color.black;
if (haClose > haOpen) vColor = cBull;
if (haClose < haOpen) vColor = cBear;
setBarFgColor(vColor, 2);
setBarFgColor(vColor, 3);
drawLineRelative(0, haHigh, 0, haLow, PS_SOLID, 1, Color.black, "Shadow"+iCntr);
drawLineRelative(0, haOpen, 0, haClose, PS_SOLID, 3, vColor, "Body"+iCntr);
var retArray = new Array(4);
retArray[0] = haHigh.toFixed(2)*1;
retArray[1] = haLow.toFixed(2)*1;
retArray[2] = haOpen.toFixed(2)*1;
retArray[3] = haClose.toFixed(2)*1;
return retArray;
}
//*** T3 ****
function T3Avg(nPeriods, vFactor, vPrice) {
if (bInit == false) {
b = vFactor;
b2 = b * b;
b3 = b * b * b;
c1 = -b3;
c2 = 3 * b2 + 3 * b3;
c3 = -6 * b2 - 3 * b - 3 * b3;
c4 = 1 + 3 * b + b3 + 3 * b2;
f1 = 2 / (nPeriods + 1);
f2 = 1 - f1;
bInit = true;
}
if (getBarState() == BARSTATE_NEWBAR) {
e1_1 = e1;
e2_1 = e2;
e3_1 = e3;
e4_1 = e4;
e5_1 = e5;
e6_1 = e6;
}
e1 = f1 * vPrice + f2 * e1_1;
e2 = f1 * e1 + f2 * e2_1;
e3 = f1 * e2 + f2 * e3_1;
e4 = f1 * e3 + f2 * e4_1;
e5 = f1 * e4 + f2 * e5_1;
e6 = f1 * e5 + f2 * e6_1;
var T3Average = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;
return T3Average;
}
//*** END of T3 Functions ****
//*** KAMA ****
function KAMA(Period, vClose) {
var x;
var nNoise;
var nSignal;
var nSmooth;
if (bInitialized == false) {
Period = Math.round(Period);
aAMA = new Array(Period);
for ( x=0; x<Period; x++ ) {
aAMA[x] = 0.0;
}
bInitialized = true;
}
if ( getBarState() == BARSTATE_NEWBAR ) {
aAMA.pop();
aAMA.unshift(0);
nBarCounter++;
}
if (nBarCounter<Period) return null;
if (bPrimed == false) {
aAMA[0] = (open(0)+close(0)) / 2 + ( ( (close(0)-open(0)) / (high(0)-low(0)) ) * Math.abs( (close(0)-open(0)) / 2) );
bPrimed = true;
return aAMA[0];
} else {
aAMA[0] = vClose;
}
nSignal = Math.abs( close(0) - close(-Period) );
x=0;
nNoise = 0;
while(x<Period) {
nNoise += Math.abs( close(-x)-close(-(x+1)) );
x++;
}
if ( nNoise==0 ) nNoise = 1.0;//added by ACM
nSmooth = Math.pow( ( nSignal/nNoise ) * ( 0.6667 - 0.0645 ) + 0.0645, 2 );
aAMA[0] = aAMA[1] + nSmooth * ( close(0) - aAMA[1] );
return aAMA[0];
}
//*** END of KAMA ****
Leave a comment:
-
Heiken Ashi Alert
Good morning guys,
I am using this old Heiken Ashi efs and want to set an alert that triggers when the candles change from green to red and vice versa. I understand how to set the alert, but have been unable to extract the previous value of haOpen and haClose to compare against the current values. Any help would be appreciated.
Thank you, Charley
PHP Code:/*****************************************************************
Provided By : eSignal. (c) Copyright 2003
EFS Formula : Heikin-Ashi
Version 2.0
9/13/2004
Notes:
* Non-price study
* Draws most recent 200 bars
* Formula Parameters
- Bullish Color Default: Green
- Bearish Color Default: Red
- Smoothing Default: None [None, T3, KAMA]
* 2.0 Added T3 and KAMA smoothing options per
http://warrants.bnpparibas.com/de/doc/WZ_0804.pdf (page 16)
*****************************************************************/
function preMain() {
setStudyTitle("Heikin-Ashi w/ Smoothing ");
setCursorLabelName("HA-High", 0);
setCursorLabelName("HA-Low", 1);
setCursorLabelName("HA-Open", 2);
setCursorLabelName("HA-Close", 3);
setDefaultBarFgColor(Color.black, 0);
setDefaultBarFgColor(Color.black, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.black, 3);
setPlotType(PLOTTYPE_DOT, 0);
setPlotType(PLOTTYPE_DOT, 1);
setPlotType(PLOTTYPE_DOT, 2);
setPlotType(PLOTTYPE_DOT, 3);
setDefaultBarThickness(0, 0);
setDefaultBarThickness(0, 1);
setDefaultBarThickness(0, 2);
setDefaultBarThickness(0, 3);
setShowTitleParameters(false);
var fp1 = new FunctionParameter("cBull", FunctionParameter.COLOR);
fp1.setName("Bullish Candles");
fp1.setDefault(Color.green);
var fp2 = new FunctionParameter("cBear", FunctionParameter.COLOR);
fp2.setName("Bearish Candles");
fp2.setDefault(Color.red);
var fp3 = new FunctionParameter("sSmooth", FunctionParameter.STRING);
fp3.setName("Smoothing");
fp3.addOption("None");
fp3.addOption("T3");
fp3.addOption("KAMA");
fp3.setDefault("T3");
var fp4 = new FunctionParameter("nT3Periods", FunctionParameter.NUMBER);
fp4.setName("T3 Periods");
fp4.setLowerLimit(1);
fp4.setDefault(3);
var fp5 = new FunctionParameter("vFactor", FunctionParameter.NUMBER);
fp5.setName("T3 V Factor");
fp5.setLowerLimit(0);
fp5.setDefault(0.5);
var fp6 = new FunctionParameter("nKAMAPeriods", FunctionParameter.NUMBER);
fp6.setName("KAMA Periods");
fp6.setLowerLimit(2);
fp6.setDefault(2);
}
var haClose = null;
var haOpen = null;
var haClose1 = null;
var haOpen1 = null;
var iCntr = 0;
// T3 vars
var b = null; // V Factor
var b2 = null;
var b3 = null;
var c1 = null;
var c2 = null;
var c3 = null;
var c4 = null;
var f1 = null;
var f2 = null;
var e1 = null;
var e2 = null;
var e3 = null;
var e4 = null;
var e5 = null;
var e6 = null;
var e1_1 = 0;
var e2_1 = 0;
var e3_1 = 0;
var e4_1 = 0;
var e5_1 = 0;
var e6_1 = 0;
var Price = null;
var bInit = false;
// KAMA vars
var nBarCounter = 0;
var aAMA = null;
var aFPArray = new Array();
var bInitialized = false;
var bPrimed = false;
function main(cBull, cBear, sSmooth, nT3Periods, vFactor, nKAMAPeriods) {
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
if ((haClose == null || haOpen == null) && close(-1) != null) {
haClose = close(-1);
haOpen = open(-1);
}
haClose1 = haClose;
haOpen1 = haOpen;
iCntr += 1;
if (iCntr > 200) iCntr = 0;
}
if (haClose1 == null || haOpen1 == null) return;
haOpen = (haOpen1 + haClose1) / 2;
haClose = (open() + high() + low() + close()) / 4;
if (sSmooth == "T3") {
setStudyTitle("Heikin-Ashi w/ " + nT3Periods + "-Period T3 Smoothing ");
haClose = T3Avg(nT3Periods, vFactor, haClose);
} else if (sSmooth == "KAMA") {
setStudyTitle("Heikin-Ashi w/ " + nKAMAPeriods + "-Period KAMA Smoothing ");
haClose = KAMA(nKAMAPeriods, haClose);
} else {
setStudyTitle("Heikin-Ashi w/out Smoothing ");
}
if (haClose == null) return;
var haHigh = Math.max(high(), haOpen, haClose);
var haLow = Math.min(low(), haOpen, haClose);
//candlesticks
var vColor = Color.black;
if (haClose > haOpen) vColor = cBull;
if (haClose < haOpen) vColor = cBear;
setBarFgColor(vColor, 2);
setBarFgColor(vColor, 3);
drawLineRelative(0, haHigh, 0, haLow, PS_SOLID, 1, Color.black, "Shadow"+iCntr);
drawLineRelative(0, haOpen, 0, haClose, PS_SOLID, 3, vColor, "Body"+iCntr);
var retArray = new Array(4);
retArray[0] = haHigh.toFixed(2)*1;
retArray[1] = haLow.toFixed(2)*1;
retArray[2] = haOpen.toFixed(2)*1;
retArray[3] = haClose.toFixed(2)*1;
return retArray;
}
//*** T3 ****
function T3Avg(nPeriods, vFactor, vPrice) {
if (bInit == false) {
b = vFactor;
b2 = b * b;
b3 = b * b * b;
c1 = -b3;
c2 = 3 * b2 + 3 * b3;
c3 = -6 * b2 - 3 * b - 3 * b3;
c4 = 1 + 3 * b + b3 + 3 * b2;
f1 = 2 / (nPeriods + 1);
f2 = 1 - f1;
bInit = true;
}
if (getBarState() == BARSTATE_NEWBAR) {
e1_1 = e1;
e2_1 = e2;
e3_1 = e3;
e4_1 = e4;
e5_1 = e5;
e6_1 = e6;
}
e1 = f1 * vPrice + f2 * e1_1;
e2 = f1 * e1 + f2 * e2_1;
e3 = f1 * e2 + f2 * e3_1;
e4 = f1 * e3 + f2 * e4_1;
e5 = f1 * e4 + f2 * e5_1;
e6 = f1 * e5 + f2 * e6_1;
var T3Average = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3;
return T3Average;
}
//*** END of T3 Functions ****
//*** KAMA ****
function KAMA(Period, vClose) {
var x;
var nNoise;
var nSignal;
var nSmooth;
if (bInitialized == false) {
Period = Math.round(Period);
aAMA = new Array(Period);
for ( x=0; x<Period; x++ ) {
aAMA[x] = 0.0;
}
bInitialized = true;
}
if ( getBarState() == BARSTATE_NEWBAR ) {
aAMA.pop();
aAMA.unshift(0);
nBarCounter++;
}
if (nBarCounter<Period) return null;
if (bPrimed == false) {
aAMA[0] = (open(0)+close(0)) / 2 + ( ( (close(0)-open(0)) / (high(0)-low(0)) ) * Math.abs( (close(0)-open(0)) / 2) );
bPrimed = true;
return aAMA[0];
} else {
aAMA[0] = vClose;
}
nSignal = Math.abs( close(0) - close(-Period) );
x=0;
nNoise = 0;
while(x<Period) {
nNoise += Math.abs( close(-x)-close(-(x+1)) );
x++;
}
if ( nNoise==0 ) nNoise = 1.0;//added by ACM
nSmooth = Math.pow( ( nSignal/nNoise ) * ( 0.6667 - 0.0645 ) + 0.0645, 2 );
aAMA[0] = aAMA[1] + nSmooth * ( close(0) - aAMA[1] );
return aAMA[0];
}
//*** END of KAMA ****
Tags: None
Leave a comment: