Log in to see Cloud of Tags

Wealth-Lab Wiki

GetRemainingTradingDays

RSS

Syntax

public static int GetRemainingTradingDays(this DateTime DateFrom)

public static int GetRemainingTradingDays(DateTime DateFrom)


Parameter Description

DateFromFrom which date

Description

This function returns the approximate number of remaining trading days in a month. For example, it is useful with Strategies that enter X market days before the end of the month and exit on Y market day of the following month.

Note that even after handling weekends properly, you're still left with business (banking/trading) holiday schedule that varies from year to year. Careful accounting for these events takes a good deal of coding. You could go two ways about it:

  1. Devise/adapt a math formula for each such holiday and process it in run-time, or
  2. Use something like this Holiday WebService in your code. Ideally, you would cache the request results somewhere in Wealth-Lab's global memory, disk file or local database.

Example

Strategy below buys 6 market days before the end of the month and sells 2nd market day of the following month:

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 EOM : WealthScript { private StrategyParameter paramNth; private StrategyParameter paramDaysR; public EOM() { paramNth = CreateParameter("Nth", 2, 1, 4, 1); paramDaysR = CreateParameter("Remaining Days", 6, 2, 8, 1); } protected override void Execute() { int remaining = 0; int newMonth = -1; int days = paramDaysR.ValueInt; int Nth = paramNth.ValueInt; for(int bar = Math.Max( days, Nth ); bar < Bars.Count; bar++) { // Month has changed if( Datebar.Month != Datebar-1.Month ) newMonth = bar; // Remaining days are returned by this static method remaining = Datebar.GetRemainingTradingDays(); if (IsLastPositionActive) { if( ( newMonth > 0 ) & ( bar == newMonth + Nth - 1 ) ) SellAtMarket( bar, LastPosition ); } else { if( remaining <= days ) BuyAtMarket( bar, remaining.ToString() ); } } } } }


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 EOM : WealthScript { private StrategyParameter paramNth; private StrategyParameter paramDaysR; public EOM() { paramNth = CreateParameter("Nth", 2, 1, 4, 1); paramDaysR = CreateParameter("Remaining Days", 6, 2, 8, 1); } protected override void Execute() { int remaining = 0; int newMonth = -1; int days = paramDaysR.ValueInt; int Nth = paramNth.ValueInt; for(int bar = Math.Max( days, Nth ); bar < Bars.Count; bar++) { // Month has changed if( Datebar.Month != Datebar-1.Month ) newMonth = bar; // Remaining days are returned by this static method remaining = DateTimeFunctions.GetRemainingTradingDays( Datebar ); if (IsLastPositionActive) { if( ( newMonth > 0 ) & ( bar == newMonth + Nth - 1 ) ) SellAtMarket( bar, LastPosition ); } else { if( remaining <= days ) BuyAtMarket( bar, remaining.ToString() ); } } } } }

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.