LinearRegLine

double LinearRegLine(DataSeries series, int bar1, int bar2, double predict);

Allows you to perform ad-hoc linear regression analysis on the specified DataSeries. Specify the Start and End bars (bar1 and bar2) for which to calculate the regression line. Then, specify the bar, predict, for which you want to predict a value. This could be a bar that extends into the future.


Example

protected override void Execute(){
int StBar = 0; int EndBar = 0; 
            double Diff = 0; double MaxDiff = 0;

            for(int bar = 20; bar < Bars.Count; bar++)
            {
                if (IsLastPositionActive)
                {
                    /* Exit after N days */
                    
                    Position p = LastPosition;
                    if ( bar+1 - p.EntryBar >= 20 )
                        SellAtMarket( bar+1, p, "Timed" ); 
                }
                else
                {
                    BuyAtLimit( bar+1, Close[bar]*0.95 );
                }
            }

            /* Highlight the regression channel of winning trades */

            foreach( Position p in Positions )
            {
                if( p.NetProfit > 0 )
                {
                    StBar = p.EntryBar;
                    EndBar = p.ExitBar;
                    if( EndBar == -1 )
                        EndBar = Bars.Count - 1;
                    double StPnt = LinearRegLine( Close, StBar, EndBar, (double)StBar );
                    double EnPnt = LinearRegLine( Close, StBar, EndBar, (double)EndBar );
                    MaxDiff = 0;
                    
                    for(int bar = StBar; bar <= EndBar; bar++)
                    {
                        Diff = Math.Abs( Close[bar] - LinearRegLine( Close, StBar, EndBar, (double)bar ) );                        
                        MaxDiff = Math.Max( Diff, MaxDiff );
                    }
                    
                    double[] rectangle = { StBar, StPnt - MaxDiff, StBar, StPnt + MaxDiff, EndBar, EnPnt + MaxDiff, EndBar, EnPnt - MaxDiff };
                    
                    DrawPolygon( PricePane, Color.FromArgb( 80, Color.LightGreen ), Color.FromArgb( 80, Color.LightGreen ),
                        LineStyle.Solid, 1, true, rectangle );
                }
            }
        }
    }
}
BuyAtMarket

LineExtendX

double LineExtendX(double x1, double y1, double x2, double y2, double y);

Extends the line specified by the x1, y1 and x2, y2 parameters, solving for x using the specified y parameter.

Remarks

 


Example

protected override void Execute(){
    // Determine middle bar between last 2 peaks 
    int bar = Bars.Count-1;
    double rev = 5;
                
    int bar1 = (int)PeakBar.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price1 = Peak.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    int bar2 = (int)PeakBar.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price2 = Peak.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price3 = ( price1 + price2 ) / 2;                        
    int bar3 = (int) LineExtendX( bar1, price1, bar2, price2, price3 );

    SetBarColor( bar3, Color.Red );
    if( ( bar2 > -1 ) & ( bar1 > -1 ) )
        DrawLine( PricePane, bar1, price1, bar2, price2, Color.Blue, WealthLab.LineStyle.Solid, 1 );
}
BuyAtMarket

LineExtendY

double LineExtendY(double x1, double y1, double x2, double y2, double x);

Extends the line specified by the x1, y1 and x2, y2 parameters, solving for y using the specified x parameter.

Remarks

 


Example

protected override void Execute(){
    // Extend recent resistance line to most current bar
    
    int bar = Bars.Count-1;
    double rev = 5;
                
    int bar1 = (int)PeakBar.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price1 = Peak.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    int bar2 = (int)PeakBar.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price2 = Peak.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price3 = LineExtendY( bar1, price1, bar2, price2, bar );
                
    DrawLine( PricePane, bar1, price1, bar2, price2, Color.Blue, WealthLab.LineStyle.Solid, 1 );
    DrawLine( PricePane, bar2, price2, bar, price3, Color.Red, WealthLab.LineStyle.Solid, 1 );
}
BuyAtMarket

LineExtendYLog

double LineExtendYLog(double x1, double y1, double x2, double y2, double x);

Extends the line specified by the x1, y1 and x2, y2 parameters, solving for y using the specified x parameter.  A logarithmic y-scale axis is assumed.


Example

protected override void Execute(){
    // Extend recent resistance line to most current bar
    
    int bar = Bars.Count-1;
    double rev = 5;
                
    int bar1 = (int)PeakBar.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price1 = Peak.Value( bar, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    int bar2 = (int)PeakBar.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price2 = Peak.Value( bar1, High, rev, WealthLab.Indicators.PeakTroughMode.Percent );
    double price3 = LineExtendYLog( bar1, price1, bar2, price2, bar );
                
    DrawLine( PricePane, bar1, price1, bar2, price2, Color.Blue, WealthLab.LineStyle.Solid, 1 );
    DrawLine( PricePane, bar2, price2, bar, price3, Color.Red, WealthLab.LineStyle.Solid, 1 );
}