View Full Version : LSMA with hi and low
Leo123
11-17-2011, 09:33 AM
Hi
I have been using E signal before and I got a LSMA indicator with Hi and low, I have found the code below but I donīt know how to add the code to a new indicator, please help me out! I donīt think these codes are everything needed, but maybe only the "standard" text is missing. I would appreciate if someone can make it complete and as a zip (or rar file) so it is easy to import into NT
Thanks
Michael
Sweden
NinjaTrader_RyanM
11-17-2011, 09:49 AM
Hello Michael,
Welcome to the NinjaTrader forums. You will not be able to simply copy - paste esignal code into a NinjaScript file. To convert you would need some familiarity with both platforms, and then convert the logic into NinjaScript. Hopefully a community member can help assist with this. You may also consider one of our 3rd party NinjaScript consultants. Many offer translation services from one platform to another.
http://www.ninjatrader.com/partners#NinjaScript-Consultants
Leo123
11-17-2011, 10:15 AM
The code below is for ninjatrader, not esignal, I hope someone could make it complete and as an zip file.
Michael
LEAST SQUARE MOVING AVERAGE:
Description: Calculates the Least Square Moving Average (LSMA)
off the high and also off the low to create a channel.
Version Control:
*/
function preMain()
{
setPriceStudy(true);
setStudyTitle("LSMA Channel");
setCursorLabelName("LSMA Hi",0);
setCursorLabelName("LSMA Lo",1);
setDefaultBarThickness(2,0);
setDefaultBarThickness(2,1);
var fp1 = new FunctionParameter("HiLength", FunctionParameter.NUMBER);
fp1.setName("Length of LSMA(High)");
fp1.setDefault(25);
var fp2 = new FunctionParameter("LoLength", FunctionParameter.NUMBER);
fp2.setName("Length of LSMA(Low)");
fp2.setDefault(25);
var fp3 = new FunctionParameter("HiOffset", FunctionParameter.NUMBER);
fp3.setName("Offset of LSMA(High)");
fp3.setDefault(0);
var fp4 = new FunctionParameter("LoOffset", FunctionParameter.NUMBER);
fp4.setName("Offset of LSMA(Low)");
fp4.setDefault(0);
var fp5 = new FunctionParameter("HiColor", FunctionParameter.COLOR);
fp5.setName("Color of LSMA(High)");
fp5.setDefault(Color.blue);
var fp6 = new FunctionParameter("LoColor", FunctionParameter.COLOR);
fp6.setName("Color of LSMA(Low)");
fp6.setDefault(Color.red);
}
var aHigh = null;
var aLow = null;
var aHiLSMA = null;
var aLoLSMA = null;
var vInit = false;
var LSMA_Array = new Array();
function main(HiLength,LoLength,HiOffset,LoOffset,HiColor,L oColor)
{
if (vInit == false) {
aHigh = new Array(HiLength);
aLow = new Array(LoLength);
aHiLSMA = new Array(HiLength);
aLoLSMA = new Array(LoLength);
setDefaultBarFgColor(HiColor,0);
setDefaultBarFgColor(LoColor,1);
preMain();
vInit = true;
}
vHigh = high();
vLow = low();
if (vHigh == null) return;
if (vLow == null) return;
if (getBarState() == BARSTATE_NEWBAR) {
if (aHigh[HiLength-1] != null) aHigh.pop();
aHigh.unshift(vHigh);
if (aLow[LoLength-1] != null) aLow.pop();
aLow.unshift(vLow);
}
aHigh[0] = vHigh;
aLow[0] = vLow;
if (aHigh[HiLength-1] == null || aLow[LoLength-1] == null) return;
// get LSMA values and assign to arrays
var vHiLSMA = getLSMA(aHigh, HiLength);
var vLoLSMA = getLSMA(aLow, LoLength);
if (getBarState() == BARSTATE_NEWBAR) {
if (aHiLSMA[HiLength-1] != null) aHiLSMA.pop();
aHiLSMA.unshift(vHiLSMA);
if (aLoLSMA[LoLength-1] != null) aLoLSMA.pop();
aLoLSMA.unshift(vLoLSMA);
}
aHiLSMA[0] = vHiLSMA;
aLoLSMA[0] = vLoLSMA;
return new Array (aHiLSMA[HiOffset], aLoLSMA[LoOffset]);
}
// ------------------------------------
// Get a LSMA value
function getLSMA(aPrice, nLength) {
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;
for (i = 0; i < nLength; ++i) {
SumY += aPrice[i];
Sum1 += i * aPrice[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);
return LinearRegValue;
}
lefevreed
11-17-2011, 10:41 AM
Michael,
LSMA is a bit of a misnomer, it is just the LinReg indicator in NT.
Leo123
11-17-2011, 11:09 AM
Michael,
LSMA is a bit of a misnomer, it is just the LinReg indicator in NT.
I want LSMA with high and low setting, You canīt do that in LinReg or how do you change to high and low, I want with 25 settings on both?
Michael
Leo123
11-17-2011, 01:07 PM
Maybe itīs called LSMA channel?
M
lefevreed
11-17-2011, 01:47 PM
Michael,
Here's a quick core indicator to get you started. It can be modified to meet your needs, but you can see what needs to be done. Off the top of my head, you may want to figure out what you want for an Offset. I included a variable for it, but it's not used in the code itself, that will have to be added.
Let me know if you have any questions about it.
Leo123
11-17-2011, 02:09 PM
Thank you , I will try it.... :)
M
Leo123
11-22-2011, 07:41 AM
Thank you lefevreed, it worked perfect! :)
You donīt have an MACD crossover indicator with dots in the graph?
M