none
How to Plot an Array of Values as a Candlestick Chart

    Question

  • I would like to plot a two-dimensional array of double values as a candlestick chart using Visual Basic 2010 or later. The array contains four columns. I am using a For...Next loop to work my way through the lines of the array. Each candlestick will consist of an X value, which is the ArrayLine value of the loop, and four Y values, which are the double values of each of the four columns. What code do I put in the For...Next loop? Thanks.

            Chart1.Series.Add("Price")
            Chart1.Series("Price").ChartType = DataVisualization.Charting.SeriesChartType.Candlestick

            For ArrayLine As Integer = 0 To 10
                    Chart1.Series("Price").Points ?????
            Next
    Friday, March 24, 2017 6:48 PM

Answers

  • Here is an example.  You just have to put the prices in the order I show however they are structured. Add the date if you want etc.

    'candlestick stock chart example 
    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form5
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim rand As New Random(Now.Millisecond)
            Dim t As Single = 50
            Dim stocks(20, 4) As Double
    
            'fill array with random data
            For i = 1 To 10
                t += rand.Next(-2, 3)
                If t > 90 Then t = 90
                If t < 30 Then t = 30
                stocks(i, 0) = t + rand.Next(-0, 4)
                stocks(i, 1) = t + rand.Next(-4, 0)
                stocks(i, 2) = t + rand.Next(-3, 4)
                stocks(i, 3) = t + rand.Next(-2, 1)
            Next
    
            Chart1.ChartAreas.Clear()
            Chart1.ChartAreas.Add("Chart 1")
            With Chart1.ChartAreas(0)
                .AxisX.MajorGrid.Interval = 5
                .AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot
                .AxisX.Minimum = 0
                .AxisY.Minimum = 30
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("Series 1")
            Chart1.Series(0)("PriceUpColor") = "Green"
            Chart1.Series(0)("PriceDownColor") = "Red"
    
            With Chart1.Series(0)
                .Color = Color.Blue
                .ChartType = DataVisualization.Charting.SeriesChartType.Candlestick
                .IsVisibleInLegend = False
            End With
    
            For i = 0 To 10
                'Date, High, Low, Open, Close
                Chart1.Series(0).Points.AddXY(i, stocks(i, 0), stocks(i, 1), stocks(i, 2), stocks(i, 3))
            Next
    
        End Sub
    End Class

    • Proposed as answer by IronRazerz Friday, March 24, 2017 11:33 PM
    • Marked as answer by Wisconsin lmnop Saturday, March 25, 2017 1:04 AM
    Friday, March 24, 2017 10:58 PM

All replies

  • Here is an example.  You just have to put the prices in the order I show however they are structured. Add the date if you want etc.

    'candlestick stock chart example 
    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form5
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim rand As New Random(Now.Millisecond)
            Dim t As Single = 50
            Dim stocks(20, 4) As Double
    
            'fill array with random data
            For i = 1 To 10
                t += rand.Next(-2, 3)
                If t > 90 Then t = 90
                If t < 30 Then t = 30
                stocks(i, 0) = t + rand.Next(-0, 4)
                stocks(i, 1) = t + rand.Next(-4, 0)
                stocks(i, 2) = t + rand.Next(-3, 4)
                stocks(i, 3) = t + rand.Next(-2, 1)
            Next
    
            Chart1.ChartAreas.Clear()
            Chart1.ChartAreas.Add("Chart 1")
            With Chart1.ChartAreas(0)
                .AxisX.MajorGrid.Interval = 5
                .AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot
                .AxisX.Minimum = 0
                .AxisY.Minimum = 30
            End With
    
            Chart1.Series.Clear()
            Chart1.Series.Add("Series 1")
            Chart1.Series(0)("PriceUpColor") = "Green"
            Chart1.Series(0)("PriceDownColor") = "Red"
    
            With Chart1.Series(0)
                .Color = Color.Blue
                .ChartType = DataVisualization.Charting.SeriesChartType.Candlestick
                .IsVisibleInLegend = False
            End With
    
            For i = 0 To 10
                'Date, High, Low, Open, Close
                Chart1.Series(0).Points.AddXY(i, stocks(i, 0), stocks(i, 1), stocks(i, 2), stocks(i, 3))
            Next
    
        End Sub
    End Class

    • Proposed as answer by IronRazerz Friday, March 24, 2017 11:33 PM
    • Marked as answer by Wisconsin lmnop Saturday, March 25, 2017 1:04 AM
    Friday, March 24, 2017 10:58 PM
  • Thank you! Thank you! Thank you!
    Saturday, March 25, 2017 12:35 AM
  • Thank you! Thank you! Thank you!

     If Tom's post has answered your question,  please remember to mark his post as the answer.  Thanks.  8)

     


    If you say it can`t be done then i`ll try it

    Saturday, March 25, 2017 12:50 AM