Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:05:03+00:00 2026-05-23T07:05:03+00:00

I am very new to Python and want to build a black box stock

  • 0

I am very new to Python and want to build a black box stock trading program that finds various correlations between stock’s rates of return and gives me a response such as buy, sell, hold, etc. I found a neat little easy to use Python module for retrieving stock data called ystockquote that pulls information from Yahoo! Finance. The module can be found at http://www.goldb.org/ystockquote.html.

One of its abilities is to output historical prices for a stock in the form ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']. I can give it a date range to do this and it gives me a nested list containing a single list with the above information this for each day.

My question is how to organize each of these separate data points (Date, Open, High, Low, etc.) into a structure that I can call upon later in my script and sort. I need this process to be easy to automate. What sorts of algorithms or data structures might I find useful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:05:04+00:00Added an answer on May 23, 2026 at 7:05 am

    You might be looking for a dictionary structure rather than a list:

    >>> prices = dict()
    >>> prices['2011-01-02'] = {'Open':20.00, 'High':30.00, 'Low':10.00, 'Close':21.00, 'Volume':14.00, 'Adj Clos':120}
    >>> prices['2010-11-09'] = {'Open':22.00, 'High':50.00, 'Low':20.00, 'Close':42.00, 'Volume':10.00, 'Adj Clos':666}
    >>> prices
    {'2011-01-02': {'Volume': 14.0, 'Adj Clos': 120, 'High': 30.0, 'Low': 10.0, 'Close': 21.0, 'Open': 20.0}, '2010-11-09': {'Volume': 10.0, 'Adj Clos': 666, 'High': 50.0, 'Low': 20.0, 'Close': 42.0, 'Open': 22.0}}
    

    Here I’ve nested a dictionary within each entry of the main “prices” dictionary. The first level of the dictionary takes the date as its key, and maps to a dictionary containing the price information for that date.

    >>> prices['2011-01-02']
    {'Volume': 14.0, 'Adj Clos': 120, 'High': 30.0, 'Low': 10.0, 'Close': 21.0, 'Open': 20.0}
    

    The second level of the dictionary uses the attribute names as keys, and maps to the attribute values themselves.

    >>> prices['2010-11-09']['Open']
    22.0
    >>> prices['2010-11-09']['Close']
    42.0
    

    It seems that, for the get_historical_prices function you refer to, each day is output as an entry of the form [Date, Open, High, Low, Close, Volume, Adj_Clos]. If you want to construct a dictionary for a list of these entries, you’re gonna need to do three things:

    First, you’ll need to index each entry to separate out the Date from the other elements, since that is what you’ll be using to index the first dimension of your dict. You can get the first element with entry[0] and the remaining elements with entry[1:].

    >>> entry = ['2011-01-02', 20.00, 30.00, 10.00, 21.00, 14.00, 120]
    >>> date = entry[0]
    >>> date
    '2011-01-02'
    >>> values = entry[1:]
    >>> values
    [20.0, 30.0, 10.0, 21.0, 14.0, 120]
    

    Second since you want to associate each of the other elements with a specific key, you should make a list of those keys in the same order as the data elements are given to you. Using the zip() function you can combine two lists p and q, taking the ith element from each and making zip(p,q)[i] == (p[i], q[i]). In such a way you create a list of (key, value) pairs that you can pass to a dictionary constructor:

    >>> keys = ['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
    >>> pairs = zip(keys, entry[1:])
    >>> pairs
    [('Open', 20.0), ('High', 30.0), ('Low', 10.0), ('Close', 21.0), ('Volume', 14.0), ('Adj Clos', 120)]
    

    Finally you want to construct your dictionary, and index it into its appropriate date in the overall history:

    >>> stockdict = dict(pairs)
    >>> stockdict
    {'Volume': 14.0, 'Adj Clos': 120, 'High': 30.0, 'Low': 10.0, 'Close': 21.0, 'Open': 20.0}
    >>> histodict = dict()
    >>> histodict[date] = stockdict
    

    You can iterate through your nested history list to construct your dictionary in two ways, the first is using a traditional for loop:

    keys = ['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
    histodict = dict()
    for item in history:
        date = item[0]
        values = item[1:]
        histodict[date] = dict(zip(keys, values))
    

    Or if you want to play around with a slightly more advanced Python technique, try a nested dict generator statement:

    keys = ['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
    histodict = dict((item[0], dict(zip(keys, item[1:]))) for item in history)
    

    That last one’s a doozy if you’re new to programming, but I encourage you to read up in that link; remember, when programming in Python, Google is your friend. I hope I’ve given you sufficient keywords and ideas here to get started learning, and I’ll leave the rest up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Python and trying to write a program that parses some
I'll start by saying that I am very, very new to Python. I used
I am very new to python and I want to match a pattern which
I very new to Python, and fairly new to regex. (I have no Perl
I am very new to python and have no idea what I'm doing but
I'm very new to Python development, and am having a problem with one of
I'm very new to Python and just crawling my way through it to accomplish
I am very new to Python, and trying to figure out how to create
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I am very new to Python. I need to run diff.py(a python in built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.