Log in to see Cloud of Tags

Wealth-Lab Wiki

TradingDaysBetweenDates

RSS

Syntax

public static int TradingDaysBetweenDates(this WealthScript obj, DateTime dt1, DateTime dt2)

public int TradingDaysBetweenDates(DateTime dt1, DateTime dt2)

Parameter Description

dt1First date
dt2Second date

Description

TradingDaysBetweenDates is a function created by Robert Sucher that calculates the number of trading days between two dates, accounting for weekends and U.S. stock market holidays and past closures. The function returns a positive number of days when the date of the dt2 parameter falls after the date of dt1. Likewise, the function returns a negative number of days when the date of the dt2 parameter is before the date of dt1. (Note! For Community.Components prior to 2014.x, TradingDaysBetweenDates returned 0 for the latter case.)

See also: GetRemainingTradingDays

Example

In this example, TradingDaysBetweenDates calculates the number of trading days for future ex dates. When the ex-dates are found in the chart, we know that bar number directly, so the code uses that. But when the next event(s) occur in the future, it uses TradingDaysBetweenDates.

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() { ClearDebug(); FundamentalItem fi; int barsToNextEvent = -1; for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { // Determine the number of bars until the next event try { fi = GetNextFundamentalItem(bar, Bars.Symbol, "dividend"); barsToNextEvent = fi.Bar - bar; // if fi.Bar isn't on the chart yet calculate the number of trading days if (fi.Bar == -1) { barsToNextEvent = this.TradingDaysBetweenDates(Date[bar], fi.Date); if (bar == Bars.Count - 1) DrawLabel(PricePane, "Calculated trading days to next event: " + barsToNextEvent, Color.Red); } } catch { barsToNextEvent = -1; // no future fundamental item } if (IsLastPositionActive) { Position p = LastPosition; if (barsToNextEvent == 1) // Exit on day before ex-div { SellAtClose(bar, p, "Ex-div"); } else { double Stop = p.EntryPrice * (1 - 15.00 / 100.0d); SellAtStop(bar + 1, p, Stop, "Stop Loss"); } } else { if (barsToNextEvent > - 1 && barsToNextEvent <= 8) { BuyAtMarket(bar + 1); } } } } } }

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); ClearDebug(); FundamentalItem fi; int barsToNextEvent = -1; for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { // Determine the number of bars until the next event try { fi = GetNextFundamentalItem(bar, Bars.Symbol, "dividend"); barsToNextEvent = fi.Bar - bar; // if fi.Bar isn't on the chart yet calculate the number of trading days if (fi.Bar == -1) { barsToNextEvent = dtf.TradingDaysBetweenDates(Date[bar], fi.Date); if (bar == Bars.Count - 1) DrawLabel(PricePane, "Calculated trading days to next event: " + barsToNextEvent, Color.Red); } } catch { barsToNextEvent = -1; // no future fundamental item } if (IsLastPositionActive) { Position p = LastPosition; if (barsToNextEvent == 1) // Exit on day before ex-div { SellAtClose(bar, p, "Ex-div"); } else { double Stop = p.EntryPrice * (1 - 15.00 / 100.0d); SellAtStop(bar + 1, p, Stop, "Stop Loss"); } } else { if (barsToNextEvent > - 1 && barsToNextEvent <= 8) { BuyAtMarket(bar + 1); } } } } } }

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.