# Building blocks

Below you'll find some helpful code snippets and patterns applicable when creating trading systems dealing with intraday data.

## Function to easily determine and compare time of day

```public class MyStrategy : WealthScript
{
public int GetTime(int bar)
{
return Date[bar].Hour * 100 + Date[bar].Minute;
}

protected override void Execute()
{
// Specifically, I use a strategy that buys stocks only in the time period between 9:55 am and 13:00.		for(int bar = 1; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
// exit rule here...
}
else if( ( GetTime(bar) >= 0955 ) & ( GetTime(bar) <= 1300 ) )
}
}
}```

## Identify the Open price of the day when working with intraday data

Here's a shortcut to find the first bar of the current day from within a Bars.Count loop -

```int firstBarToday = bar - Bars.IntradayBarNumber(bar);// So the Opening price of the session is then:
double openToday = Open[firstBarToday];```

You can also identify yesterday's close price just the same:

```int firstBarToday = bar - Bars.IntradayBarNumber(bar);//So it follows that the previous bar is the last bar of the prior day.
int lastBarYesterday = firstBarToday - 1;//So, yesterday's close is:
Close[lastBarYesterday];```

## Find highest high for the current day

Here's a shortcut to find the highest High for the current day in an intraday chart:

```// in the loop
double highestHighToday = Highest.Value(bar, High, Bars.IntradayBarNumber(bar) + 1);```

Here's more speed-wise solution:

```double highestHighToday = -1;
for(int bar = 20; bar < Bars.Count; bar++)
{
highestHighToday = High[bar];
else
if (High[bar] > highestHighToday) highestHighToday = High[bar];
}```

## Exiting after N days in an intraday strategy

```using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
protected override void Execute()
{
// counter of days
int cnt = 0;

for(int bar = 1; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
Position p = LastPosition;					// loop backwards from current bar to last position's bar of entry
for( int Bar = bar; Bar >= p.EntryBar; Bar-- )
{
// when day changes, increment counter
if( Bars.IntradayBarNumber( Bar ) == 0 )
{
cnt++;
break;
}
}

// when counter reaches 3, sell
if( cnt >= 3 )
ExitAtMarket( bar+1, LastPosition );
}
else
{
cnt = 0;
}
}
}
}
}```

## Access/plot 52-week high on an intraday chart

Here's a slick way to determine 52 week high from intraday data. Assumes 9:30 AM to 4 PM trading session (6 1/2 hours):

```
{
int bi = Bars.BarInterval;
DataSeries high52week = Highest.Series( High, (int)Math.Truncate( 5 * (60/bi)*6.5 * 52 ) );
PlotSeries( PricePane, high52week, Color.Black, WealthLab.LineStyle.Solid, 1 );
}
```

## Design pattern: One entry per day per symbol

Use this technique to trade a symbol only once per day. Note! Use only one of the options shown below.

```public class MyStrategy : WealthScript
{
protected override void Execute()
{
/* One entry per day Option 1: at top of loop */
for(int bar = 20; bar < Bars.Count; bar++)
{
Position p = LastPosition;
if (p != null && !p.Active && p.EntryBar > (bar - Bars.IntradayBarNumber(bar)))
continue;

if (IsLastPositionActive)
{
}
else
{
}

}

/* One entry per day Option 2: as an entry condition */
for(int bar = 20; bar < Bars.Count; bar++)
{
Position p = LastPosition;

if (IsLastPositionActive)
{