[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RT] Explain How the LRS Works



PureBytes Links

Trading Reference Links

Could someone please explain to me exactly what the LRS does, and
how
it does it?  Thank you.

Here is the indicator:

{LRS}
Inputs: Len(10);
Plot1(LinearRegSlope(close,Len),"LRS");

Here is the Function it references:
             

static CurrentBar as Long
static UB, LB as Long
static arHigh() as Double
static arClose() as Double
static arLow() as Double

''''''''''''''''''

function FillArs as Double
  ReDim arHigh(UB)
  ReDim arClose(UB)     
  ReDim arLow(UB)
  
  For CurrentBar = LB To UB
    arHigh(CurrentBar) = twData(CurrentBar).dHigh
    arClose(CurrentBar) = twData(CurrentBar).dClose
    arLow(CurrentBar) = twData(CurrentBar).dLow
  Next CurrentBar

end function
                         

''''''''''''''''''                         

                         

function dOpen(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    dOpen = twData(CurrentBar-BarsBack).dOpen
  else 
    dOpen = 0
  end if
end function

''''''''''''''''''''''''''

function dClose(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    dClose = twData(CurrentBar-BarsBack).dClose
  else 
    dClose = 0
  end if
end function

''''''''''''''''''''''''''

function dPrice(Price() as Double, BarsBack as Double) as Double
  if CurrentBar-BarsBack >= LB then
    dPrice = Price(CurrentBar-BarsBack)
  else
    dPrice = 0
  end if
end function
                         

''''''''''''''''''                         


function dHigh(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    dHigh = twData(CurrentBar-BarsBack).dHigh
  else 
    dHigh = 0
  end if
end function

''''''''''''''''''''''''''

function lVolume(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    lVolume = twData(CurrentBar-BarsBack).lVolume
  else 
    lVolume = 0
  end if
end function

''''''''''''''''''''''''''

function dLow(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    dLow = twData(CurrentBar-BarsBack).dLow
  else 
    dLow = 0
  end if
end function

''''''''''''''''''''''''''

function dtDate(BarsBack as Long) as Double
  if CurrentBar-BarsBack >= LB then
    dtDate = twData(CurrentBar-BarsBack).dtDate
  else 
    dtDate = 0
  end if
end function

''''''''''''''''''''''''''

function dRange(BarsBack as Long) as Double 
  if CurrentBar-BarsBack >= LB then
    dRange = twData(CurrentBar-BarsBack).dHigh -
twData(CurrentBar-BarsBack).dLow
  else 
    dRange = 0
  end if
end function

''''''''''''''''''''''''''

function Summation(ar() as Double, Length as Double) as Double
  dim res as double
  res = 0
  if CurrentBar-Length+1 >= LB then 
    dim i as long
    for i = CurrentBar to CurrentBar-Length+1 step 
-1
      Res = Res + ar(i)
    next
i            

  end if
  Summation =
Res                         

end
function                      

                

''''''''''''''''''''''''
         
function LinearRegSlope(Price() as Double, Length as Double, ByRef
arLRS() as Double) as Double
  redim arLRS(UB)
  dim X as Double
  dim Num1, Num2, SumBars, SumSqrBars, SumY, Sum1, Sum2 as
Double
  
  for CurrentBar = LB to UB
    if Length = 0 then 
      arLRS(CurrentBar) = 0
    else
      if CurrentBar = 1 then 
        SumBars =
Length*(Length-1)/2
           
SumSqrBars = (Length-1)*Length*(2*Length-1)/6
      end if
      Sum1 = 0
      for X = 0 to Length-1 
        Sum1 = Sum1 + X*dPrice(Price,
X)
      next
X                        

      SumY = Summation(Price, Length)
      Sum2 = SumBars * SumY
      Num1 = Length*Sum1 - Sum2
      Num2 = SumBars*SumBars -
Length*SumSqrBars
      if Num2 <> 0 then arLRS(CurrentBar)
= Num1 / Num2 else arLRS(CurrentBar) = 0
    end if
  next CurrentBar
end function  

''''''''''''''''''''''''

Function Main As Integer
  UB = UBound(twData)
  LB = LBound(twData)
  dim Ret as Double
  Ret = FillArs

  dim Plot1() as tiData
  redim Plot1(UB)
  dim Plot2() as tiData
  redim Plot2(UB)

  dim arLRS() as Double
  Ret = LinearRegSlope(arClose, Length, arLRS)
  
  for CurrentBar = LB to UB
    Plot1(CurrentBar).lValid = 1
    Plot1(CurrentBar).dtDate =
twData(CurrentBar).dtDate
    Plot1(CurrentBar).lColor = twColor
    Plot1(CurrentBar).dValue = arLRS(CurrentBar)
    Plot2(CurrentBar).lValid = 1
    Plot2(CurrentBar).dtDate =
twData(CurrentBar).dtDate
    Plot2(CurrentBar).lColor = twColor
    Plot2(CurrentBar).dValue = 0
  next
CurrentBar                    

  
  plotindicator twPanelID, "RegSlope " &
Plot1(UB).dValue , twstyleline, Plot1
  plotindicator twPanelID, "Zero line " &
Plot2(UB).dValue , twstyleline, Plot2

  Main = 0
end function






Yahoo! Groups Sponsor





















To unsubscribe from this group, send an email to:
realtraders-unsubscribe@xxxxxxxxxxxxxxx





Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.