I am currently trying to pull high,low, and closing stock data from Yahoo finance, and graph it using Turtle. I was able to pull the data from the url into a list but I can't figure out how to graph it. I know the dates have to be the x-axis, and the values for high, low, and close data have to the Y-axis. I also know that high, low, and closing data have to be shown on three separate lines. Can anyone point me in the direction of how to do this?

def graphStock(symbol,start,end):

    highList = []
    lowList = []
    closeList= []
    dateList = []


    url01 = 'http://ichart.finance.yahoo.com/table.csv?s=%s&' % symbol + \
          'a=%s&' % str(int(start[4:6]) - 1) + \
          'b=%s&' % str(int(start[6:8])) + \
          'c=%s&' % str(int(start[0:4])) + \
          'd=%s&' % str(int(end[4:6]) - 1) + \
          'e=%s&' % str(int(end[6:8])) + \
          'f=%s&' % str(int(end[0:4])) + \
          'g=d&' + \
          'ignore=.csv'
    days = urllib.request.urlopen(url01).readlines()
    data = [bytes.decode(day).split(',') for day in days]


    for line in data[1:]:
        date = line[0]
        high = line[2]
        low = line[3]
        close = line[4]


        if line in data:
            dateList.append(date)
            highList.append(high)
            lowList.append(low)
            closeList.append(close)

    return graphStock

Recommended Answers

All 6 Replies

  1. Turtle graphic is not for plotting. see. If it is your homework, than forget it.
  2. What is the reason you return the function object instead of the list?
  3. I cannot run your code, because you did not provide a testcase. It is hard to help in that way.
  4. See one simple example.

    import random
    import turtle

    data=[random.randint(1,100) for i in range(20)]

    for i in range(20):
    turtle.goto(i*20,data[i])

    a=input("Press any, to continue")

Well, the problem I am trying to do is :

Write a function that will graph the high,low, closing price for a stock. Your function should accept a beginning date and a end date, along with a ticker symbol for stock.

My professors only advice was to pull the data into a list, and then use turtle to do a line graph.

Not going to lie, I really don't know if I am even doing this problem right.

And the reason I returned the object, is because the point of the function is to return a graph, rather than a list. So I thought returning the list would not do what I am trying to do.

And here's the code with my Testcase.

import urllib.request
import cTurtle

# Functions

def graphStock(symbol,start,end):

    highList = []
    lowList = []
    closeList= []
    dateList = []


    url01 = 'http://ichart.finance.yahoo.com/table.csv?s=%s&' % symbol + \
          'a=%s&' % str(int(start[4:6]) - 1) + \
          'b=%s&' % str(int(start[6:8])) + \
          'c=%s&' % str(int(start[0:4])) + \
          'd=%s&' % str(int(end[4:6]) - 1) + \
          'e=%s&' % str(int(end[6:8])) + \
          'f=%s&' % str(int(end[0:4])) + \
          'g=d&' + \
          'ignore=.csv'
    days = urllib.request.urlopen(url01).readlines()
    data = [bytes.decode(day).split(',') for day in days]


    for line in data[1:]:
        date = line[0]
        high = line[2]
        low = line[3]
        close = line[4]


        if line in data:
            dateList.append(date)
            highList.append(high)
            lowList.append(low)
            closeList.append(close)

    return graphStock

# Main Tests

symb = 'APL'              
sDate = '20130201'
eDate = '20130322'


graphStock(symb,sDate,eDate)

And the reason I returned the object, is because the point of the function is to return a graph, rather than a list. So I thought returning the list would not do what I am trying to do.

You return the function, not a graph.
Do you need a picture, or only a program that draws a graph?

What is the problem with my code? It accepts a list, and draws a graph.

Please read: http://docs.python.org/release/2.7/library/turtle.html
or
http://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle
Depending on your python version.

Watch this, too.

Nothing's wrong with your code, but when I try to use it with my lists it just gives me a straight line.
I wonder if has to do with my data values?

I have looked at the highList. All values are near 33. No wonder that you see a straight line.
You have to shift the horizontal axis and zoom in.

    shift=-33
    zoom=50
    for i in range(len(highList)):
        turtle.goto(i*20,(float(highList[i])+shift)*zoom)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.