Page History: TradingDaysBetweenDates
Compare Page Revisions
Page Revision: 2013/05/11 14:39
Syntax
public static int TradingDaysBetweenDates(this WealthScript obj, DateTime dt1, DateTime dt2)
public int TradingDaysBetweenDates(DateTime dt1, DateTime dt2)
Parameter Description
dt1 | First date |
dt2 | Second date |
Description
TradingDaysBetweenDates is a function created by Robert Sucher that calculates the number of trading days between two dates.
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(Datebar, 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(Datebar, 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);
}
}
}
}
}
}