Log in to see Cloud of Tags

Wealth-Lab Wiki

Intraday | Building blocks of Intraday trading strategies

RSS

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 Datebar.Hour * 100 + Datebar.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 ) ) BuyAtMarket( bar+1 ); } } }


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 = OpenfirstBarToday;


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: CloselastBarYesterday;


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++) { if (Bars.IntradayBarNumber(bar) == 0) highestHighToday = Highbar; else if (Highbar > highestHighToday) highestHighToday = Highbar; }

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 { BuyAtMarket( bar+1 ); 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):

if( Bars.IsIntraday ) { 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.

See also: Community.Rules > Position-Based > One entry per day

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) { //code your exit rules here } else { //code your entry rules here } } /* One entry per day Option 2: as an entry condition */ for(int bar = 20; bar < Bars.Count; bar++) { Position p = LastPosition; if (IsLastPositionActive) { //code your exit rules here } else if (p == null || !p.Active && p.EntryBar < (bar - Bars.IntradayBarNumber(bar))) { //code your entry rules here } } } }

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.