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

RE: [amibroker] Re: Another question about how to use LinearReg()



PureBytes Links

Trading Reference Links

Steve and jnz,

 

Here’s some code that NW Trader provided.   I converted it to a function.  The first portion of the code is the function.  The second portion is the chart section that calls it. 

 

It looks bad below, so copy it to AB’s code editor and the tables should be correct.   Also, watch out for line wraps.   

 

Regards,

 

Dan.

 

 

function PlotLinearRegression(CurSymbolPrice, PlotLRBool, PlotLRSD1Bool, PlotLRSD2Bool,

                                                                     DaysBack, Shift, LRSTitleUpColor, LRSTitleDownColor, LRColor, LRStyle,

                                                                     LRSDPeriods, LRSDColor, LRSDStyle,

                              LRSDPeriods2, LRSDColor2, LRSDStyle2,

                                                                     SmoothPds

                                                                     )

       {

       //P                                      = ParamField("Price field",-1);

 

       //  =============================== Math Formula =============================================================

             

       x = Cum(1);

       lastx = LastValue( x ) - shift;

       aa = LastValue( Ref(LinRegIntercept( CurSymbolPrice , Daysback), -shift) );

       bb = LastValue( Ref(LinRegSlope( CurSymbolPrice , Daysback ), -shift) );

       y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );

      

       LRSlope                            = LinRegSlope( CurSymbolPrice , Daysback) ;

       SlopeAngle                 = bb;

 

       //==================Plot the Linear Regression Line ==========================================================

       //LRColor = ParamColor("LR Color", colorCycle );

       //LRStyle = ParamStyle("LR Style");

 

       LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );

 

       Plot( LRLine , "LinReg", LRCOLOR, LRSTyle + styleNoLabel ); //  styleDots );

//     Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE|styleLine + styleNoRescale + styleNoTitle + styleNoLabel ); //  styleDots );

       // ==========================  Plot 1st SD Channel ===============================================================

 

       LRSDPeriods = Param("Standard Deviation", 1.5, 0, 6, 0.1);

       SD = LRSDPeriods/2;

 

       width = LastValue( Ref(SD * StDev(CurSymbolPrice , Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET 

       SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;

       SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

 

       //LRSDColor = ParamColor("SD Color", colorCycle );

       //LRSDStyle = ParamStyle("SD Style");

 

       Plot( SDU , "Upper Lin Reg", LRSDColor, LRSDStyle + styleNoRescale + styleNoTitle + styleNoLabel );

       Plot( SDL , "Lower Lin Reg", LRSDColor, LRSDStyle + styleNoRescalestyleNoTitle + styleNoLabel);

      

 

       //  ==========================  Plot 2d SD Channel ===============================================================

       ATRRaw = ATR(Daysback);

       ATRMultiplier = Param("ATR Multiplier", 1, .1, 10, .1, .1);

       //Make this a single value to

       ATRWeighted = ATRRaw * ATRMultiplier;

      

       //LRSDPeriods2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);

       SD2 = LRSDPeriods2/2;

      

       //width2 = ATRWeighted * LastValue( Ref(SD2*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET 

       width2 = LastValue( Ref(SD2*StDev(CurSymbolPrice , Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET 

       SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;

       SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

      

       //LRSDColor2 = ParamColor("2 SD Color", colorCycle );

       //LRSDStyle2 = ParamStyle("2 SD Style");

      

       Plot( SDU2 , "Upper Lin Reg", LRSDColor2, LRSDStyle2  + styleNoRescale + styleNoTitle + styleNoLabel);

       Plot( SDL2 , "Lower Lin Reg", LRSDColor2, LRSDStyle2  + styleNoRescale + styleNoTitle + styleNoLabel );

       // ============================ End Indicator Code ==============================================================

 

       return SlopeAngle;

 

       }

 

 

_SECTION_BEGIN("Linear Regression");

//  Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below

//  Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support

//      Designed for use with AB 4.63 beta and above, using drag and drop feature. 

//  Permits plotting a linear regression line of any price field available on the chart for a period determined by the user. 

//     2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line.

//            A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.   

 

CurDatabase = GetDatabaseName();

if (CurDatabase == "InteractiveBrokers")

       {

       DefaultLRPeriod      =      400;

       MinLRPeriod                =      10;

       MaxLRPeriod          =  1400;

       IncrementLRPeriod    =      10;

       }

else

       {

       DefaultLRPeriod      =      21;

       MinLRPeriod                =      1;

       MaxLRPeriod          =  200;

       IncrementLRPeriod    =      1;

       }

 

PlotLRSD1Bool              = ParamToggle("Plot LR Std Dev Narrow?", "No|Yes", 1);

PlotLRSD2Bool              = ParamToggle("Plot Std Dev Wide?", "No|Yes", 1);

 

Daysback                   = Param("Period for Liner Regression Line",DefaultLRPeriod,MinLRPeriod,MaxLRPeriod ,IncrementLRPeriod);

shift                      = Param("Look back period",0,0,240,1);

LRSTitleUpColor      = ParamColor("LR Slope Title Up Color", colorLime );

LRSTitleDownColor = ParamColor("LR Slope Title Down Color", colorRed );

 

LRColor                           = ParamColor("LR Color", colorCycle );

LRStyle                           = ParamStyle("LR Style");

 

LRSDPeriods          = Param("LR Std Dev Wide", 1.5, 0, 6, 0.1);

LRSDColor                  = ParamColor("SD Color", colorCycle );

LRSDStyle                  = ParamStyle("SD Style");

      

LRSDPeriods2         = Param("Std Dev Narrow", 2.0, 0, 6, 0.1);

LRSDColor2                 = ParamColor("2 SD Color", colorCycle );

LRSDStyle2                 = ParamStyle("2 SD Style");

 

 

//Init

CurSymbolPrice       =      0;

 

if (   PlotLRBool

       AND PlotSymbolBool

       AND CurSymbolType != "NewHiLow"

       AND CurSymbolType != "DMF"

       AND CurSymbolType != "AD"

   )  

       {

       //First get the Current Symbol price that we are using

       if (   CurSymbolType == "Price"

              OR CurSymbolType == "Index"

              OR CurSymbolType == "MutualFund")

              {

              CurSymbolPrice = C;

              }

             

       if (CurSymbolType == "DMF")

              {

              CurSymbolPrice       = C    ;

              CurSymbolPrice       = EMA(CurSymbolPrice , SmoothPds);

              }

       else if (CurSymbolType == "NewHiLow")

              {

              CurSymbolPrice =     0      ;

              }

       else if (CurSymbolType == "AD")

              {

              AdvancingCount             =      C;

              DecliningCount             =      O;

              AdvancingVolume      =      H;

              DecliningVolume      =      L;

 

              if (ADVolumeBool)    //PlotVolume        

                     {

                     ADAdvancing   =      AdvancingVolume;

                     ADDeclining   =      DecliningVolume;

                     }

              else

                     {

                     ADAdvancing   =      AdvancingCount;

                     ADDeclining   =      DecliningCount;

                     }

 

 

              CurSymbolPrice = ADAdvancing - ADDeclining;

              }

//Title = NumToStr(DecliningCount);

       LRSlopeAngle = PlotLinearRegression(CurSymbolPrice, PlotLRBool, PlotLRSD1Bool, PlotLRSD2Bool,

                                                                     DaysBack, Shift, LRSTitleUpColor, LRSTitleDownColor, LRColor, LRStyle,

                                                                     LRSDPeriods, LRSDColor, LRSDStyle,

                              LRSDPeriods2, LRSDColor2, LRSDStyle2,

                                                                     SmoothPds);

             

       }

 

 

 

_SECTION_END();

 


From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of Steve Dugas
Sent: Saturday, February 04, 2006 12:11 PM
To: amibroker@xxxxxxxxxxxxxxx
Subject: Re: [amibroker] Re: Another question about how to use LinearReg()

 

OK...Could you show me how you would do this?  Draw the line I mean...

Steve

----- Original Message -----
From: "Fred" <ftonetti@xxxxxxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Saturday, February 04, 2006 2:49 PM
Subject: [amibroker] Re: Another question about how to use LinearReg()


> It is ALSO for drawing straight lines around which one can plot among
> other things such as parallel lines at +/- n Standard Deviations ...
>
> --- In amibroker@xxxxxxxxxxxxxxx, "Steve Dugas" <sjdugas@xxx> wrote:
>>
>> Hi,
>>
>> LinearReg is not for drawing straight lines - it is built by
> plotting the
>> endpoints of a LinReg line as it shifts forward through time. Use
> it like a
>> moving average - it hugs the price ( or whatever ) very tightly.
>>
>> Steve
>>
>> ----- Original Message -----
>> From: "jnz88" <jnz88@xxx>
>> To: <amibroker@xxxxxxxxxxxxxxx>
>> Sent: Saturday, February 04, 2006 11:55 AM
>> Subject: [amibroker] Another question about how to use LinearReg()
>>
>>
>> > There is a library item in AFL libreary showing how to draw
> linear reg
>> > line without using LinearReg() function.
>> >
>> > Could anybody show me how to use LinearReg() to do the same
> thing. This
>> > could help me to understand further about this function
>> >
>> > thanks,
>> >
>> >
>> >
>> >
>> >
>> >
>> > Please note that this group is for discussion between users only.
>> >
>> > To get support from AmiBroker please send an e-mail directly to
>> > SUPPORT {at} amibroker.com
>> >
>> > For other support material please check also:
>> > http://www.amibroker.com/support.html
>> >
>> >
>> > Yahoo! Groups Links
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>
>
>
>
>
>
>
> Please note that this group is for discussion between users only.
>
> To get support from AmiBroker please send an e-mail directly to
> SUPPORT {at} amibroker.com
>
> For other support material please check also:
> http://www.amibroker.com/support.html
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





SPONSORED LINKS
Investment management software Real estate investment software Investment property software
Software support Real estate investment analysis software Investment software


YAHOO! GROUPS LINKS