Intraday Support Functions

Modified on 2020/01/28 07:13 by Administrator — Categorized as: Community Components

Syntax

public static int GetTime(this WealthScript obj, int bar) public static int GetTime(this Bars bars, int bar) // For use when using a non-synchronized Bars object

public int GetTime(int bar) public int GetTime(Bars bars, int bar) // For use when using a non-synchronized Bars object


Parameter Description

barBar number
barsBars object

GetTime returns 24-hour time as an integer. Example: 4pm = 1600


Syntax

public static int AddIntegerTime(this int t, int minutes)

public int AddIntegerTime( int minutes )


Parameter Description

tInteger time e.g. 1600
minutesMinutes to add, e.g. 5. To subtract, pass a negative integer.

AddIntegerTime adds minutes to an integer time (HHnn) and returns a HHnn time.

Example:
// Legacy Syntax
Utility.AddIntegerTime(1600, -5) returns 1555

// Extension method syntax
int itime = 1600;
itime.AddIntegerTime(-5) returns 1555
itime.AddIntegerTime(120) returns 1800


Syntax

public static int FirstBarofDay(this WealthScript obj, int startDay)

public int FirstBarofDay( int startDay )


Parameter Description

startDayDay number

FirstBarofDay returns the chart bar number of the first bar of the startDay. Example: Pass 2 to get the first bar of the second day loaded in the current chart.


Syntax

public static int DaysInPosition(this Position p, int bar)

public int DaysInPosition(int bar)


Parameter Description

barBar number
pPosition object

DaysInPosition returns how many trading days have passed since establishing a position using intraday data.

Example:
// Extension method syntax
Position p = LastPosition;
int days = p.DaysInPosition(bar);


Description

Intraday support functions and methods created by Robert Sucher. They are often required for intraday strategies.

GetTime returns 24-hour time as an integer. Example: 4pm = 1600
AddIntegerTime adds minutes to an integer time (HHnn) and returns a HHnn time. Example 1600 - 5 = 1555
FirstBarofDay returns the chart bar number of the first bar of the startDay. Example: Pass 2 to get the first bar of the second day
DaysInPosition returns how many days have passed since establishing a position using intraday data.


Example

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 MOCStrategy : WealthScript { /* Sliders * minTradeBars are the min number of bars before the end of day that an entry is allowed */ private StrategyParameter todayCloseTime; private StrategyParameter minTradeBars; public MOCStrategy() { todayCloseTime = CreateParameter("Close Today", 1600, 1300, 1600, 300); minTradeBars = CreateParameter("Min Trade Bars", 5, 2, 10, 1); } protected override void Execute() { int todayClose = todayCloseTime.ValueInt; int minBarsInTrade = minTradeBars.ValueInt; int lastChartBar = Bars.Count - 1; int nextToLastBarTime = todayClose.AddIntegerTime( -Bars.BarInterval ); for(int bar = this.FirstBarofDay( 2 ); bar < Bars.Count - 1; bar++) { if( !IsLastPositionActive ) { if ( bar < lastChartBar - minBarsInTrade ) { BuyAtLimit(bar + 1, Bars.Lowbar * 0.995); } } else { Position p = LastPosition; if( (bar == lastChartBar) && (this.GetTime(bar) >= nextToLastBarTime) ) { SetBarColor( bar, Color.Orange); ExitAtMarket( bar + 1, p, "Exit at open of last bar"); } else if ( (bar != lastChartBar) && Bars.IsLastBarOfDay(bar + 1) ) { SetBarColor( bar, Color.Orange); ExitAtMarket( bar + 1, p, "Exit at open of last bar"); } else { // other exit logic here } } } } } }


Legacy syntax example:

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

namespace WealthLab.Strategies { public class MOCStrategy : WealthScript { /* Sliders * minTradeBars are the min number of bars before the end of day that an entry is allowed */ private StrategyParameter todayCloseTime; private StrategyParameter minTradeBars; public MOCStrategy() { todayCloseTime = CreateParameter("Close Today", 1600, 1300, 1600, 300); minTradeBars = CreateParameter("Min Trade Bars", 5, 2, 10, 1); } protected override void Execute() { // Intraday support functions here: Utility u = new Utility( this ); int todayClose = todayCloseTime.ValueInt; int minBarsInTrade = minTradeBars.ValueInt; int lastChartBar = Bars.Count - 1; int nextToLastBarTime = u.AddIntegerTime( todayClose, -Bars.BarInterval ); for(int bar = u.FirstBarofDay( 2 ); bar < Bars.Count - 1; bar++) { if( !IsLastPositionActive ) { if ( bar < lastChartBar - minBarsInTrade ) { BuyAtLimit(bar + 1, Bars.Lowbar * 0.995); } } else { Position p = LastPosition; if( (bar == lastChartBar) && (u.GetTime(bar) >= nextToLastBarTime) ) { SetBarColor( bar, Color.Orange); ExitAtMarket( bar + 1, p, "Exit at open of last bar"); } else if ( (bar != lastChartBar) && Bars.IsLastBarOfDay(bar + 1) ) { SetBarColor( bar, Color.Orange); ExitAtMarket( bar + 1, p, "Exit at open of last bar"); } else { // other exit logic here } } } } } }