Your interactive trading laboratory!
 • 
24 users online

Coded Rotation Model - shifting by day of month

In a coded rotation model, I try to shift on the 20th of each month. For whatever reasons it doesn't works on every month. In June 2020, January 2020, October 2019 and July 2020 the shifting wasn't done. What I'm doing wrong in the code?

public override void PreExecute(DateTime dt, List<BarHistory> participants)
		{
			MarketDetails md = participants[0].Market;
			DateTime nextTradingDay = dt;
			do
			{
				nextTradingDay = nextTradingDay.AddDays(1);
			}
			while (!md.IsTradingDay(nextTradingDay));

			if (nextTradingDay.Day != 20)
			{
				rebalance = false;
				return;
			}
			rebalance = true;

Attachment

Cancel

Responses

Meanwhile, I managed to get around this problem, but I wonder whether there is not an easier way to evaluate a Trading Day?

Meanwhile, I managed to get around this problem, but I wonder whether there is not an easier way to evaluate a Trading Day?

Everyone writes code differently, and this is how I approached the problem …

        public override void PreExecute(DateTime dt, List<BarHistory> participants)
        {
			MarketDetails md = MarketManager.UsaStocks;
			bool rebalance = false;
			if (dt.Day <= 19)
			{
				DateTime next = dt.AddDays(1);
				while(!md.IsTradingDay(next))
					next = next.AddDays(1);
				rebalance = next.Day >= 20;
			}
	        if (rebalance)
				WriteToDebugLog(dt.ToShortDateString());
        }
Everyone writes code differently, and this is how I approached the problem … [CODE] public override void PreExecute(DateTime dt, List<BarHistory> participants) { MarketDetails md = MarketManager.UsaStocks; bool rebalance = false; if (dt.Day <= 19) { DateTime next = dt.AddDays(1); while(!md.IsTradingDay(next)) next = next.AddDays(1); rebalance = next.Day >= 20; } if (rebalance) WriteToDebugLog(dt.ToShortDateString()); } [/CODE]
Forum Tips

Please sign in if you want to participate in our forum.

Our forum uses Markdown syntax to format posts.

To embed code snippets, enclose them in [CODE][/CODE] tags.