ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  CSV File + visual basic 6
Posted by pratik on Jan-05-2010 12:14
im trying to plot a Candlestick chart using vb6 and a csv file

i searched before posting but did not get the example, i am attaching my csv,

can someone give me demo code to plot a candlestick graph using a csv file

this is how my csv file looks

Ticker,Date,Open,High,Low,Close,Volume
ABB,11/05/2009,712.35,765.05,712.35,751.5,459168
ABB,11/06/2009,703,772,703,743.85,252781
ABB,11/09/2009,755,760,745.1,752.1,278756
ABB,11/10/2009,750,759.85,738.2,741.75,271004
ABB,11/11/2009,758.65,773.8,740.15,764.7,398692
ABB,11/12/2009,765,776.05,755.1,768.55,397719
ABB,11/13/2009,770.1,778.25,761,763.15,164925
ABB,11/16/2009,769,786.8,768.35,774.45,316676
ABB,11/17/2009,775,779.85,756.25,761.4,265629
ABB,11/18/2009,764,774.9,757.35,772.6,232931
ABB,11/19/2009,761.35,772.6,753,761.25,129207
ABB,11/20/2009,755,772,743.1,767.95,265908
ABB,11/23/2009,762,778,762,768.05,123646
ABB,11/24/2009,774.65,776,758,760.75,152623
ABB,11/25/2009,750.1,769.9,750.1,753.1,433066
ABB,11/26/2009,754,759.85,743,750.4,286672
ABB,11/27/2009,710.55,749.65,710.55,737.25,229252
ABB,11/30/2009,758.95,758.95,738,741.05,135250
ABB,12/01/2009,749.4,749.4,737,743.7,312783
ABB,12/02/2009,746,746,735,736.95,251337
ABB,12/03/2009,742,742.85,730,734.5,202456
ABB,12/04/2009,731.95,739.7,728.05,735.35,207927
ABB,12/07/2009,738.85,744.9,731.4,734.95,550940
ABB,12/08/2009,788,788,737,748.4,196875
ABB,12/09/2009,745,753.85,737.9,739.6,126071
ABB,12/10/2009,744.65,744.65,735.35,738.05,160422
ABB,12/11/2009,744.5,759.9,742,750.9,457938
ABB,12/14/2009,755,764,751,753.9,455669
ABB,12/15/2009,759.65,763.3,746.95,752.25,308019
ABB,12/16/2009,749.65,760.5,748,758.45,188408
ABB,12/17/2009,758.85,765,753.3,758.85,244711
ABB,12/18/2009,759.65,765.1,752.2,762.7,382519

  Re: CSV File + visual basic 6
Posted by Peter Kwan on Jan-06-2010 01:22
Hi pratik,

For how to read a CSV file using VB6, there should be a lot of sample code in the Internet. (This is a general VB6 question, and is not specific to ChartDirector.) You may consider to search the web to find the code you need. It may be easier to find the code this way.

Anyway, I have written some short code for you. It just reads the data from the text file into arrays. You can use use the arrays to plot the chart. For example, you can use the arrays as inputs to the "Candlestick Chart" sample code to replace the hard coded arrays there.


    Dim fnum, whole_file, lines, one_line

    fnum = FreeFile
    Open "aaa.txt" For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)
    Close fnum

    lines = Split(whole_file, vbLf)
    If Trim(lines(UBound(lines))) = "" Then ReDim Preserve lines(UBound(lines) - 1)

    ReDim labels(UBound(lines) - 1)
    ReDim openData(UBound(labels))
    ReDim highData(UBound(labels))
    ReDim lowData(UBound(labels))
    ReDim closeData(UBound(labels))

    Dim i As Integer
    For i = 0 To UBound(labels)
        one_line = Split(lines(i + 1), ",")
        labels(i) = one_line(1)
        openData(i) = one_line(2)
        highData(i) = one_line(3)
        lowData(i) = one_line(4)
        closeData(i) = one_line(5)
    Next


Hope this can help.

Regards
Peter Kwan

  Re: CSV File + visual basic 6
Posted by pratikos on Jan-17-2011 22:01
Hello,

When i put your code, this message appears :" Error 32768 Error converting argument 1
to type class DoubleArray" ? I'm lost (a little).

Here is "my" code :


Option Explicit
'Number of charts produced in this demo module
Public Function getNoOfCharts() As Integer
    getNoOfCharts = 1
End Function

'Main code for creating chart.
'Note: the argument img is unused because this demo only has 1 chart
Public Sub createChart(viewer As Object, img As String)

    Dim cd As New ChartDirector.API


    fnum = FreeFile
    Open "aaa.txt" For Input As fnum
    whole_file = Input$(LOF(fnum), #fnum)
    Close fnum

    lines = Split(whole_file, vbLf)
    If Trim(lines(UBound(lines))) = "" Then ReDim Preserve lines(UBound(lines) - 1)

    ReDim labels(UBound(lines) - 1)
    ReDim openData(UBound(labels))
    ReDim highData(UBound(labels))
    ReDim lowData(UBound(labels))
    ReDim closeData(UBound(labels))

    Dim i As Integer
    For i = 0 To UBound(labels)
        one_line = Split(lines(i + 1), ",")
        labels(i) = one_line(1)
        openData(i) = one_line(2)
        highData(i) = one_line(3)
        lowData(i) = one_line(4)
        closeData(i) = one_line(5)
    Next


    ' Create a XYChart object of size 600 x 350 pixels
    Dim c As XYChart
    Set c = cd.XYChart(600, 350)

    ' Set the plotarea at (50, 25) and of size 500 x 250 pixels. Enable both the
    ' horizontal and vertical grids by setting their colors to grey (0xc0c0c0)
    Call c.setPlotArea(50, 25, 500, 250).setGridColor(&HC0C0C0, &HC0C0C0)

    ' Add a title to the chart
    Call c.addTitle("Universal Stock Index on Jan 2001")

    ' Add a custom text at (50, 25) (the upper left corner of the plotarea). Use 12
    ' pts Arial Bold/blue (4040c0) as the font.
    Call c.addText(50, 25, "(c) Global XYZ ABC Company", "arialbd.ttf", 12, _
        &H4040C0)

    ' Add a title to the x axis
    Call c.xAxis().setTitle("Jan 2001")

    ' Set the labels on the x axis. Rotate the labels by 45 degrees.
    Call c.xAxis().setLabels(labels).setFontAngle(45)

    ' Add a title to the y axis
    Call c.yAxis().setTitle("Universal Stock Index")

    ' Draw the y axis on the right hand side of the plot area
    Call c.setYAxisOnRight(True)

    ' Reserve 10% margin at the top and bottom of the plot area during auto-scaling.
    ' This is to leave space for the high and low data labels.
    Call c.yAxis().setAutoScale(0.1, 0.1)

    ' Add a CandleStick layer to the chart using green (00ff00) for up candles and
    ' red (ff0000) for down candles
    Dim layer As CandleStickLayer
    Set layer = c.addCandleStickLayer(highData, lowData, openData, closeData, _
        &HFF00, &HFF0000)

    ' Set the line width to 2 pixels
    Call layer.setLineWidth(2)

    ' Output the chart
    Set viewer.Picture = c.makePicture()

    'include tool tip for the chart
    viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
        "title='{xLabel} Jan 2001 High:{high} Open:{open} Close:{close} Low:{low}'")

End Sub


The code error is at this part :

   Set layer = c.addCandleStickLayer(highData, lowData, openData, closeData, _
        &HFF00, &HFF0000)

Can you explain me et solve (or help me !) ? I would like to understand not to disturb you
everytime.

Thanks

  Re: CSV File + visual basic 6
Posted by pratikos on Jan-17-2011 22:59
The trouble seems the file's format.
If I change "," by ";" and "." by "," it works very well.

Thanks