Situation
You want to know how to randomly generate colours and, for example, how to apply these colours on price bars.

Programming example

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

namespace PowerLanguage.Indicator
{
    [UpdateOnEveryTick(false)]
    [SameAsSymbol(true)]
    public class Example_ColourBarsRandomly : IndicatorObject
    {
        private IPlotObject plotHigh, plotLow, plotOpen, plotClose;
        private Random rnd = new Random();

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

        protected override void Create()
        {
            plotHigh = AddPlot(new PlotAttributes("High", 
                EPlotShapes.BarHigh, Color.Yellow));
            plotLow = AddPlot(new PlotAttributes("Low", 
                EPlotShapes.BarLow, Color.Yellow));
            plotOpen = AddPlot(new PlotAttributes("Open", 
                EPlotShapes.LeftTick, Color.Yellow));
            plotClose = AddPlot(new PlotAttributes("Close", 
                EPlotShapes.RightTick, Color.Yellow));
        }

        protected override void CalcBar()
        {
            Color barColour = GenerateRandomColour(rnd);

            // Set the colour of all plots
            plotOpen.Set(Bars.Open[0], barColour);
            plotHigh.Set(Bars.High[0], barColour);
            plotLow.Set(Bars.Low[0], barColour);
            plotClose.Set(Bars.Close[0], barColour);
        }

        // GenerateRandomColour; returns a random Color
        private Color GenerateRandomColour(Random rndNmbr)
        {
            return Color.FromArgb(
                rndNmbr.Next(0, 256),   // Red component value
                rndNmbr.Next(0, 256),   // Green component value
                rndNmbr.Next(0, 256));  // Blue component value
        }
    }
}

Output of the example

Applied to a chart, the bars look similar like:

Example of random bar colours in MultiCharts .NET

Generating random colours in C#

See the article ‘how to colour a price bar in MultiCharts .NET?’ for the basics of colouring price bars. This article will focus on generating random colours.

Before we can generate random numbers (and use those numbers to generate the colours), we first need to declare an object of the class Random (line 13):

private Random rnd = new Random();

While the random class is not truly random ("C# in Depth - Random Numbers", n.d.; Liberty & MacDonald, 2009), these pseudo-random numbers are more than random enough for our goal.

In the CalcBar() override method, we call the GenerateRandomColour() method and store the returned colour in the variable barColour. This colour is then used to paint the price bars:

protected override void CalcBar()
{
    Color barColour = GenerateRandomColour(rnd);

    // Set the colour of all plots
    plotOpen.Set(Bars.Open[0], barColour);
    plotHigh.Set(Bars.High[0], barColour);
    plotLow.Set(Bars.Low[0], barColour);
    plotClose.Set(Bars.Close[0], barColour);
}

Random colour method in MultiCharts .NET

The GenerateRandomColour() method is implemented as follows:

// GenerateRandomColour; returns a random Color
private Color GenerateRandomColour(Random rndNmbr)
{
    return Color.FromArgb(
        rndNmbr.Next(0, 256),   // Red component value
        rndNmbr.Next(0, 256),   // Green component value
        rndNmbr.Next(0, 256));  // Blue component value
}

The parameter of this method requires that an instance of the Random class is passed as an argument into this method. Why did we not just create the method without any parameters, like this?

// Generate a random colour; no parameters version
private Color GenerateRandomColour()
{
    Random rnd = new Random();

    return Color.FromArgb(
        rnd.Next(0, 255),
        rnd.Next(0, 255),
        rnd.Next(0, 255));
}

Every time you create a new instance of ‘Random’ with the parameterless constructor (i.e. Random rnd = new Random();), the current date and time is used as a starting point for generating the random numbers. But if you create new instances of Random quickly after another, the current date and time might not have changed yet, which means you will not get different ‘random’ numbers ("C# in Depth - Random Numbers", n.d.).

To prevent that from happening, we create one instance of Random (line 13 of the example) and pass this instance into the method.

With the Color.FromArgb method a colour can be generated depending on the red, green, and blue components, which all three range from 0 to 255 (the RGB colour model). To get an idea which red, green, and blue value corresponds to which colour, see for example this list of colours.

To generate a random number for each of these three primary colours, we call the Next() method of the Random class. The overloaded version that we use here accepts two parameters: the minimal value and the maximum value, and returns a random integer that falls in this range.

And with the random numbers for red, green, and blue, the GenerateRandomColour() method returns our random colour.

References

C# in Depth: Random Numbers. (n.d.). Retrieved November 10, 2013, from http://csharpindepth.com/Articles/Chapter12/Random.aspx

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