Is there a way to make the LSMA green when it rises and red when it falls? I've tried a couple of things, but my best results changed the color one bar too late.
/*************************
Copyright © eSignal, 2003
**************************
Description: Calculates the Least Square Moving Average (LSMA)
based on theories of "Woodie".
MA Length = 25
MA Offset = 0
MA Source = HLC/3
*/
function preMain()
{
setPriceStudy(true);
setStudyTitle("Woodies LSMA");
setCursorLabelName("LSMA");
setDefaultBarThickness(2);
}
var vPrice = null;
var vInit = false;
function main(nLength,nOffset)
{
if (nLength == null) nLength = 25;
if (nOffset == null) nOffset = 0;
if (nBar == null) nBar = 0;
if (vInit == false) {
vPrice = new Array(nLength);
vInit = true;
}
vClose = close();
vHigh = high();
vLow = low();
if (vClose == null) return;
if (vHigh == null) return;
if (vLow == null) return;
if (getBarState() == BARSTATE_NEWBAR) {
vPrice.pop();
vPrice.unshift(vHLC3);
}
vHLC3 = (vClose + vHigh + vLow) / 3;
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;
}
/*************************
Copyright © eSignal, 2003
**************************
Description: Calculates the Least Square Moving Average (LSMA)
based on theories of "Woodie".
MA Length = 25
MA Offset = 0
MA Source = HLC/3
*/
function preMain()
{
setPriceStudy(true);
setStudyTitle("Woodies LSMA");
setCursorLabelName("LSMA");
setDefaultBarThickness(2);
}
var vPrice = null;
var vInit = false;
function main(nLength,nOffset)
{
if (nLength == null) nLength = 25;
if (nOffset == null) nOffset = 0;
if (nBar == null) nBar = 0;
if (vInit == false) {
vPrice = new Array(nLength);
vInit = true;
}
vClose = close();
vHigh = high();
vLow = low();
if (vClose == null) return;
if (vHigh == null) return;
if (vLow == null) return;
if (getBarState() == BARSTATE_NEWBAR) {
vPrice.pop();
vPrice.unshift(vHLC3);
}
vHLC3 = (vClose + vHigh + vLow) / 3;
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;
}
Comment