Syntax
public static bool TomorrowIsLastTradingDayOfWeek(this WealthScript obj, int bar)
public bool TomorrowIsLastTradingDayOfWeek(int bar)
Parameter Description
Description
TomorrowIsLastTradingDayOfWeek returns
true if the
next trading session (with respect to
bar) is the last for the week, otherwise
false. You can use this function to determine if the next trading day is the last for the week in order to be alerted to exit positions before the weekend. The function accounts for weekends, U.S. market holidays, and past closures.
The function is useful primarily for EOD strategies (Daily scale) but should also work for Minute, Second, or Tick scales too.
See also:
GetRemainingTradingDays,
DateOfNextTradingDay
Example
The example is a duplicate of the Trading System Lab from the Nov. 2012 issue of
Active Trader Magazine. It uses TomorrowIsLastTradingDayOfWeek to be alerted to exit the Position at the market close on the last trading day of the week, usually Friday.
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 WeeklyRotation : WealthScript
{
StrategyParameter _chaseThem;
StrategyParameter _profitPct;
public WeeklyRotation()
{
_chaseThem = CreateParameter("Chase ?", 0, 0, 1, 1);
_profitPct = CreateParameter("Target", 4, 2, 6, 0.5);
}
protected override void Execute()
{
int rsiPeriod = 20;
int chase = _chaseThem.ValueInt == 1 ? 1 : -1;
double tgtFactor = 1 + _profitPct.Value/100d;
/* if _chaseThem is 1, then the most overbought stocks are purchased */
for(int bar = rsiPeriod * 3; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
Position p = LastPosition;
if (p.MFEAsOfBarPercent(bar) >= _profitPct.Value)
SellAtMarket(bar + 1, p, "Profit");
else
if (this.TomorrowIsLastTradingDayOfWeek(bar))
SellAtClose(bar + 1, p, "Friday");
}
else
{
if (this.DateOfNextTradingDay(bar).DayOfWeek < Datebar.DayOfWeek)
{
double pri = chase * RSI.Series(Close, rsiPeriod)bar;
Position p = BuyAtMarket(bar + 1, pri.ToString("0.000"));
if (p != null)
p.Priority = chase * RSI.Series(Close, rsiPeriod)bar;
}
}
}
}
}
}
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, 2012.10 or later***/
namespace WealthLab.Strategies
{
public class WeeklyRotation : WealthScript
{
StrategyParameter _chaseThem;
StrategyParameter _profitPct;
public WeeklyRotation()
{
_chaseThem = CreateParameter("Chase ?", 0, 0, 1, 1);
_profitPct = CreateParameter("Target", 4, 2, 6, 0.5);
}
protected override void Execute()
{
DateTimeFunctions dtf = new DateTimeFunctions(this);
int rsiPeriod = 20;
int chase = _chaseThem.ValueInt == 1 ? 1 : -1;
double tgtFactor = 1 + _profitPct.Value/100d;
/* if _chaseThem is 1, then the most overbought stocks are purchased */
for(int bar = rsiPeriod * 3; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
Position p = LastPosition;
if (p.MFEAsOfBarPercent(bar) >= _profitPct.Value)
SellAtMarket(bar + 1, p, "Profit");
else
if (dtf.TomorrowIsLastTradingDayOfWeek(bar))
SellAtClose(bar + 1, p, "Friday");
}
else
{
if (dtf.DateOfNextTradingDay(bar).DayOfWeek < Datebar.DayOfWeek)
{
double pri = chase * RSI.Series(Close, rsiPeriod)bar;
Position p = BuyAtMarket(bar + 1, pri.ToString("0.000"));
if (p != null)
p.Priority = chase * RSI.Series(Close, rsiPeriod)bar;
}
}
}
}
}
}