DateOfNextTradingDay

Modified on 2014/10/23 10:56 by Administrator — Categorized as: Community Components

Syntax

public static DateTime DateOfNextTradingDay(this WealthScript obj, int bar)
public static DateTime DateOfNextTradingDay(this WealthScript obj, DateTime dt)

public DateTime DateOfNextTradingDay(int bar) public DateTime DateOfNextTradingDay(DateTime dt)

Parameter Description

barCurrent bar number
dtCurrent DateTime

Description

DateOfNextTradingDay returns the DateTime of the next trading day with respect to the bar number or date passed to the function. You can use this function to determine if the next trading day (U.S. Stock Market only) is a specific DayOfWeek (like DayOfWeek.Friday). This information can be used to trigger an Alert for a trading action on the next day, i.e., bar + 1. DateOfNextTradingDay accounts for weekends and U.S. market holidays.

The function is useful primarily for EOD strategies (Daily scale) but should also work for Minute, Second, or Tick scales too.

See also: GetRemainingTradingDays, TomorrowIsLastTradingDayOfWeek

Example

In this example, the strategy simply buys on Thursdays and sells on the first trading session of the following week, usually Monday.

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 MyStrategy : WealthScript { protected override void Execute() { for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { if (IsLastPositionActive) { Position p = LastPosition; if (this.DateOfNextTradingDay(bar).DayOfWeek < Date[bar].DayOfWeek) SellAtMarket(bar + 1, p, "It's next week!"); } else { if (this.DateOfNextTradingDay(bar).DayOfWeek == DayOfWeek.Thursday) { BuyAtMarket(bar + 1, "Buy Thurs Morning"); } } } } } }

Legacy syntax example:


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

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { DateTimeFunctions dtf = new DateTimeFunctions(this); for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { if (IsLastPositionActive) { Position p = LastPosition; if (dtf.DateOfNextTradingDay(bar).DayOfWeek < Date[bar].DayOfWeek) SellAtMarket(bar + 1, p, "It's next week!"); } else { if (dtf.DateOfNextTradingDay(bar).DayOfWeek == DayOfWeek.Thursday) { BuyAtMarket(bar + 1, "Buy Thurs Morning"); } } } } } }