Bars Since

Modified on 2013/05/11 09:48 by Eugene — Categorized as: Community Components

Syntax

public static int BarsSince(this int bar, bool condition )

public int BarsSince(int bar, bool condition )


Parameter Description

barBar
condition Any boolean condition

Description

In MetaStock formula language, BarsSince(Expression) is used to return the number of bars elapsed since an expression was encountered. Here is its WealthScript analogue returning the bar on which the expression became true. Consequently, to arrive at BarSince one must subtract that saved bar from current bar. The code sample below illustrates the idea.

Example

The following example demonstrates how, using the BarsSince function, is possible to create a data series that holds the number of bars since the high price has broke through the highest high of the past 40 bars:

Example using C# extension methods:

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;

namespace WealthLab.Strategies { public class BarsSinceDemo : WealthScript { protected override void Execute() { DataSeries barsSince = new DataSeries(Bars,"Bars Since Breakout(40)"); int b1 = 0; int markbar = 0;

// Building BarsSinceBreakout series for(int bar = 41; bar < Bars.Count; bar++) { if( bar.BarsSince( ( Highbar > Highest.Series(High,40)bar-1 ) ) == 0 ) markbar = bar; barsSincebar = bar - markbar; }

PlotSeries( PricePane, Highest.Series(High,40), Color.Blue, LineStyle.Dashed, 2 ); SetBarColors( Color.Silver,Color.Silver ); ChartPane bscPane = CreatePane(30,true,true); PlotSeries(bscPane, barsSince, Color.Blue, LineStyle.Histogram, 3 ); } } }


Legacy syntax example:

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components; // BarsSince here /*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/

namespace WealthLab.Strategies { public class BarsSinceDemo : WealthScript { protected override void Execute() { Calculate c = new Calculate(this); // pass an instance of the WealthScript object DataSeries barsSince = new DataSeries(Bars,"Bars Since Breakout(40)"); int b1 = 0; int markbar = 0;

// Building BarsSinceBreakout series for(int bar = 41; bar < Bars.Count; bar++) { if( c.BarsSince( bar, ( Highbar > Highest.Series(High,40)bar-1 ) ) == 0 ) markbar = bar; barsSincebar = bar - markbar; }

PlotSeries( PricePane, Highest.Series(High,40), Color.Blue, LineStyle.Dashed, 2 ); SetBarColors( Color.Silver,Color.Silver ); ChartPane bscPane = CreatePane(30,true,true); PlotSeries(bscPane, barsSince, Color.Blue, LineStyle.Histogram, 3 ); } } }


The BarsSinceBreakout series created

The BarsSinceBreakout series created