GetRemainingTradingDays

Modified on 2013/05/11 12:53 by Eugene — Categorized as: Community Components

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( Date[bar].Month != Date[bar-1].Month ) newMonth = bar; // Remaining days are returned by this static method remaining = Date[bar].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( Date[bar].Month != Date[bar-1].Month ) newMonth = bar; // Remaining days are returned by this static method remaining = DateTimeFunctions.GetRemainingTradingDays( Date[bar] ); if (IsLastPositionActive) { if( ( newMonth > 0 ) & ( bar == newMonth + Nth - 1 ) ) SellAtMarket( bar, LastPosition ); } else { if( remaining <= days ) BuyAtMarket( bar, remaining.ToString() ); } } } } }