Hi Alexis,
Would you help me optimize an efs code which paints bars depending on Linear Regression Bands (High/Low) direction.
The code works fine, but the application is very slow (even on Daily charts) and CPU utilization almost 100%...
efslib:
LSMA_High
LSMA_Low
Study
Thanks in advance!
Would you help me optimize an efs code which paints bars depending on Linear Regression Bands (High/Low) direction.
The code works fine, but the application is very slow (even on Daily charts) and CPU utilization almost 100%...
efslib:
LSMA_High
PHP Code:
function preMain()
{
setPriceStudy(true);
setStudyTitle("LSMA_High");
setCursorLabelName("LSMA_High");
setDefaultBarThickness(2);
setDefaultBarFgColor(Color.cyan,3);
}
var vPrice = null;
var vInit = false;
var vHLC3 = 0;
function main(nLength,nOffset,nBar)
{
if (nLength == null) nLength = 55;
if (nOffset == null) nOffset = 0;
if (nBar == null) nBar = 0;
if (vInit == false) {
vPrice = new Array(nLength);
vInit = true;
}
vClose = close(nBar);
vHigh = high(nBar);
vLow = low(nBar);
if (vClose == null) return;
if (vHigh == null) return;
if (vLow == null) return;
if (getBarState() == BARSTATE_NEWBAR) {
vPrice.pop();
vPrice.unshift(vHLC3);
}
vHLC3 = vHigh;
vPrice[0] = vHLC3;
if (vPrice[nLength-1] == null) return;
var Num1 = 0.0;
var Num2 = 0.0;
var SumBars = nLength * (nLength - 1) * 0.5;
var SumSqrBars = (nLength - 1) * nLength * (2 * nLength - 1) / 6;
var SumY = 0.0;
var Sum1 = 0.0;
var Sum2 = 0.0;
var Slope = 0.0;
var Intercept = 0.0;
var i = 0;
for (i = 0; i < nLength; i++)
{
if(getCurrentBarIndex() > -5 && getCurrentBarIndex() != 0) debugPrintln(vPrice[i]);
SumY += vPrice[i];
Sum1 += i * vPrice[i];
}
Sum2 = SumBars * SumY;
Num1 = nLength * Sum1 - Sum2;
Num2 = SumBars * SumBars - nLength * SumSqrBars;
if (Num2 != 0) Slope = Num1 / Num2;
Intercept = (SumY - Slope * SumBars) / nLength;
var LinearRegValue = Intercept + Slope * (nLength - 1 - nOffset);
return LinearRegValue;
}
PHP Code:
function preMain()
{
setPriceStudy(true);
setStudyTitle("LSMA_Low");
setCursorLabelName("LSMA_Low");
setDefaultBarThickness(2);
setDefaultBarFgColor(Color.cyan,3);
}
var vPrice = null;
var vInit = false;
var vHLC3 = 0;
function main(nLength,nOffset,nBar)
{
if (nLength == null) nLength = 55;
if (nOffset == null) nOffset = 0;
if (nBar == null) nBar = 0;
if (vInit == false) {
vPrice = new Array(nLength);
vInit = true;
}
vClose = close(nBar);
vHigh = high(nBar);
vLow = low(nBar);
if (vClose == null) return;
if (vHigh == null) return;
if (vLow == null) return;
if (getBarState() == BARSTATE_NEWBAR) {
vPrice.pop();
vPrice.unshift(vHLC3);
}
vHLC3 = vLow;
vPrice[0] = vHLC3;
if (vPrice[nLength-1] == null) return;
var Num1 = 0.0;
var Num2 = 0.0;
var SumBars = nLength * (nLength - 1) * 0.5;
var SumSqrBars = (nLength - 1) * nLength * (2 * nLength - 1) / 6;
var SumY = 0.0;
var Sum1 = 0.0;
var Sum2 = 0.0;
var Slope = 0.0;
var Intercept = 0.0;
var i = 0;
for (i = 0; i < nLength; i++)
{
if(getCurrentBarIndex() > -5 && getCurrentBarIndex() != 0) debugPrintln(vPrice[i]);
SumY += vPrice[i];
Sum1 += i * vPrice[i];
}
Sum2 = SumBars * SumY;
Num1 = nLength * Sum1 - Sum2;
Num2 = SumBars * SumBars - nLength * SumSqrBars;
if (Num2 != 0) Slope = Num1 / Num2;
Intercept = (SumY - Slope * SumBars) / nLength;
var LinearRegValue = Intercept + Slope * (nLength - 1 - nOffset);
return LinearRegValue;
}
PHP Code:
var fpArray = new Array();
var vLastAlert = -1;
function preMain() {
setPriceStudy(true);
setStudyTitle("LinReg Paint Bars");
setCursorLabelName("LinReg Paint Bars");
setShowCursorLabel(false);
setShowTitleParameters(false);
setColorPriceBars(true);
var x=0;
fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(55);
}
}
function main(Length,inputLength, inputOffset) {
setPriceBarColor(Color.RGB(255,170,0));
if(inputLength == null)
inputLength = Length;
if(inputOffset == null)
inputOffset = 0;
var vNewH, vOldH, vNewL, vOldL;
vNewH = call("/downloads/LSMA_High.efs", inputLength, inputOffset, 0);
vOldH = call("/downloads/LSMA_High.efs", inputLength, inputOffset, -1);
vNewL = call("/downloads/LSMA_Low.efs", inputLength, inputOffset, 0);
vOldL = call("/downloads/LSMA_Low.efs", inputLength, inputOffset, -1);
if(vNewH > vOldH && vNewL > vOldL) onAction1();
if(vNewH < vOldH && vNewL < vOldL) onAction2();
return null;
}
function onAction1() {
setPriceBarColor(Color.lime);
vLastAlert = 1;
}
function onAction2() {
setPriceBarColor(Color.red);
vLastAlert = 2;
}
Comment