Situation
You want to know how to use days in conditional expressions so that you can perform certain actions on certain days.

Code example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    public class Example_DaysConditionally : IndicatorObject
    {
        private IPlotObject volumePlot;

        public Example_DaysConditionally(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            volumePlot = AddPlot(new PlotAttributes("", EPlotShapes.Histogram,
                Color.Yellow, Color.Black, 3, EPlotStyle.Solid, true));
        }

        protected override void CalcBar()
        {
            double barVolume = Bars.UpTicks[0] + Bars.DownTicks[0];
            DayOfWeek barDay = Bars.Time[0].DayOfWeek;

            // Colour the plot purple if the bar's weekday is 
            // the same as the current weekday
            if (barDay == DateTime.Now.DayOfWeek)
            {
                volumePlot.Set(barVolume, Color.Purple);
            }

            // Color red when bar's weekday is Monday
            else if (barDay == DayOfWeek.Monday)
            {
                volumePlot.Set(barVolume, Color.Red);
            }

            // Tuesday's are coloured green
            else if (barDay == DayOfWeek.Tuesday)
            {
                volumePlot.Set(barVolume, Color.Green);
            }

            // When the bar's day is greater than or equal
            // to Thursday, colour plot white
            else if (barDay >= DayOfWeek.Thursday)
            {
                volumePlot.Set(barVolume, Color.White);
            }

            // By default, colour yellow
            else
            {
                volumePlot.Set(barVolume, Color.Yellow);
            }
        }
    }
}

Output of the code example

Applied to a chart, the indicator looks like:

Example of working with dates in MultiCharts .NET

MultiCharts .NET programming example

To use a volume plot, we first need to declare a plot object, which is done in line 10:

private IPlotObject volumePlot;

Then in the Create() method the plot is created its style set to histogram (EPlotShapes.Histogram), a default yellow colour with a black background (respectively Color.Yellow and Color.Black), a plot width of 3 (the higher the value, the ticker the plot's line or bar), with a solid line (EPlotStyle.Solid) and with the marker for the last value (the true parameter):

protected override void Create()
{
    volumePlot = AddPlot(new PlotAttributes("", EPlotShapes.Histogram,
        Color.Yellow, Color.Black, 3, EPlotStyle.Solid, true));
}

In the CalcBar() override method we first create two variable types for easy referencing later on in the method:

double barVolume = Bars.UpTicks[0] + Bars.DownTicks[0];
DayOfWeek barDay = Bars.Time[0].DayOfWeek;

The volume of the bar is calculated by the volume occurring at an up tick and the volume traded on a down tick (respectively Bars.UpTicks[0] and Bars.DownTicks[0]). The DayOfWeek is retrieved from the time of the current bar and stored in the barDay variable.

Changing the plot colour in MultiCharts .NET

Then an if-else statement is used to determine which colour the plot should get:

// Colour the plot purple if the bar's weekday is 
// the same as the current weekday
if (barDay == DateTime.Now.DayOfWeek)
{
    volumePlot.Set(barVolume, Color.Purple);
}

// Color red when bar's weekday is Monday
else if (barDay == DayOfWeek.Monday)
{
    volumePlot.Set(barVolume, Color.Red);
}

// Tuesdays are coloured green
else if (barDay == DayOfWeek.Tuesday)
{
    volumePlot.Set(barVolume, Color.Green);
}

// When the bar's day is greater than or equal
// to Thursday, colour plot white
else if (barDay >= DayOfWeek.Thursday)
{
    volumePlot.Set(barVolume, Color.White);
}

// By default, colour yellow
else
{
    volumePlot.Set(barVolume, Color.Yellow);
}

The first if statement evaluates whether or not the day of the current bar is equal to the day of the computer clock. If that is the case, the histogram bar is coloured purple.

The next two conditional statements (lines 32 till 42) check whether or not the bar’s day is equal to Monday or Tuesday and respectively colour the plot red or green if one of these conditional expressions is true.

The last if...else statement evaluates whether or not the day of the bar is greater or equal than a Thursday (lines 44-49).

Since DayOfWeek is an enumeration, and every constant in an enumeration corresponds to a numerical value (Liberty & MacDonald, 2009), we can use relational operators such as greater than and less than on DayOfWeek. In this case, the conditional expression evaluates to true when the bar’s weekday is Thursday, Friday, or Saturday (for more on this, see the article how to trade only on certain days).

Finally, an else statement (lines 51-55) gives the histogram bars a yellow colour for conditions that were not covered (for example, when the day is Wednesday).

That wraps up this example of how to use days conditionally. See the ‘how to colour a price bar’ article for how to colour price bars instead of histogram bars.

References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.