#P_BarsSinceLvl_01

EasyLanguage and other code for TradeStation
User avatar
progster
Posts: 948
Joined: Thu Jul 26, 2007 7:20 pm
Contact:

#P_BarsSinceLvl_01

Postby progster » Tue Feb 03, 2009 5:12 pm

There are many market situations where you might be interested to know the number of bars that have passed since a certain level was seen.

Here is an indicator of mine that let's you know the number of bars since a designated level.

BarsSinceLevel_01.png

Code: Select all

{
   Indicator:   #P_BarsSinceLvl_01

   Plots the # of bars since the designated level was seen.

   Level            Designated level.
   MaxLB            Maximum lookback to find designated level.
   UpColor            Color to use for indicator values that are relatively high over ColorNormLength bars. 
   DnColor            Color to use for indicator values that are relatively low over ColorNormLength bars. 
   GridForegroundColor   Color to use for numbers in RadarScreen cells when gradient coloring is enabled, that is, when both UpColor and DnColor are set to non-negative values. 

   Set either UpColor and/or DnColor to -1 to disable gradient plot coloring.
   When disabled, Plot1 color is determined by settings in indicator properties dialog box. 
   Plot2 (ZeroLine) color always comes from indicator properties dialog box. 

}

Inputs:
   Level(0),
   MaxLB(200),
   SinceAlertLen(10),
   UpColor( Cyan ),
   DnColor( Magenta ),
   GridForegroundColor( Black ) ;

vars:
   ApplicationType( 0 ),
   ColorLevel( 0 ),
   LvlStraddle(False),
   BarsSinceLevel(0),
   ColorNormLength(MaxLB);      //   Number of bars over which to normalize the indicator for gradient coloring.  See also:  comments in function NormGradientColor.

vars:   //   for writing messages to the log
   NameStem("#P_BarsSinceLvl_01"), technique_name(NameStem), ret_val(0), ret_str(""), msg(""), LogDecimals(2) ;   


if CurrentBar = 1 then
   ApplicationType = GetAppInfo( aiApplicationType ) ;

LvlStraddle = (High = Level) or (Low = Level) or ( (High > Level) and (Low < Level) ) ;
   
BarsSinceLevel = MRO(LvlStraddle, MaxLB, 1) ;

plot1( BarsSinceLevel, "BSL" ) ;

{ Gradient coloring }
if UpColor >= 0 and DnColor >= 0 then begin
   ColorLevel = NormGradientColor( BarsSinceLevel, true, ColorNormLength, UpColor, DnColor ) ;

   if ApplicationType = 1 then          //   study is applied to a chart
      SetPlotColor( 1, ColorLevel )
   else if ApplicationType > 1 then       //   study is applied to grid app
      begin
      SetPlotColor( 1, GridForegroundColor ) ;
      SetPlotBGColor( 1, ColorLevel ) ;
   end
   else begin
      RaiseRuntimeError( "ApplicationType apparantly not set.  Check source code." ) ;
   end;

end ;

{ Alert criteria }
if BarsSinceLevel crosses over SinceAlertLen then begin
   msg = "Long time since level" + NumToStr(Level, LogDecimals) ;
   Alert( msg ) ;
end;

Enjoy!
You do not have the required permissions to view the files attached to this post.

Return to “TradeStation Code”

Who is online

Users browsing this forum: No registered users and 1 guest