Simple question - stock market simulation

C

cptn.spoon

I'm trying to create an incredibly simple stock market simulator to be
used in a childrens classroom and I'm wondering whether someone can
point me in the right direction.

I basically want to be able to have a stock struct as follows:

StockName = "Test"
StockPriceList = (12,13,12,10,10,8,10)
StockRisk = 0.15
StockQty = 2000

And then have a StockMarket struct that can contain 1 to many Stocks.
I then want to be able to iterate through the stocks to perform
operations as such:

for Stock in StockMarket:

I'm not asking for tips on the program itself, I just can't figure out
how to get the data structures in place. Would I use Classes or would
I use dictionaries? Am I completely off the mark with this?

Apologies for such a rudimentary question...I'm very new to all this.
 
P

Paul Rubin

cptn.spoon said:
I'm not asking for tips on the program itself, I just can't figure out
how to get the data structures in place. Would I use Classes or would
I use dictionaries? Am I completely off the mark with this?

Typically you would use a class for the multi-fielded data structure;
a dictionary is intended more for lookup tables where you don't know
ahead of time what the keys will be. Also, you'd typically use a list
[1,2,3] rather than a tuple (1,2,3) to hold the price list.
 
C

cptn.spoon

cptn.spoon said:
I'm not asking for tips on the program itself, I just can't figure out
how to get the data structures in place. Would I use Classes or would
I use dictionaries? Am I completely off the mark with this?

Typically you would use a class for the multi-fielded data structure;
a dictionary is intended more for lookup tables where you don't know
ahead of time what the keys will be.  Also, you'd typically use a list
[1,2,3] rather than a tuple (1,2,3) to hold the price list.

Thanks Paul! I thought this might be the case. So how would I get the
StockMarket class instance to contain many Stock class instances and
then be able to iterate through them? I'm guessing the basic structure
would be as follows...but I think I'm wrong:

class StockMarket:
pass

class Stock:
def __init__(self, stockname, stockpricelist[], stockrisk,
stockqty):
self.stockname = stockname
self.stockpricelist[] = stockpricelist[]
self.stockrisk = stockrisk
self.stockqty = stockqty

Can you possibly point me in the right direction?

Thanks
Phil
 
P

Paul Rubin

cptn.spoon said:
def __init__(self, stockname, stockpricelist[], stockrisk,
stockqty): ...

You would say just stockpricelist rather than stockpricelist[], but
other than that your class def looks good.
 
T

Terry Reedy

cptn.spoon said:
cptn.spoon said:
I'm not asking for tips on the program itself, I just can't figure out
how to get the data structures in place. Would I use Classes or would
I use dictionaries? Am I completely off the mark with this?
Typically you would use a class for the multi-fielded data structure;
a dictionary is intended more for lookup tables where you don't know
ahead of time what the keys will be. Also, you'd typically use a list
[1,2,3] rather than a tuple (1,2,3) to hold the price list.

Thanks Paul! I thought this might be the case. So how would I get the
StockMarket class instance to contain many Stock class instances and
then be able to iterate through them? I'm guessing the basic structure
would be as follows...but I think I'm wrong:

class StockMarket:
pass

I would start with using a list for StockMarket.
class Stock:
def __init__(self, stockname, stockpricelist[], stockrisk,
stockqty):
self.stockname = stockname
self.stockpricelist[] = stockpricelist[]
self.stockrisk = stockrisk
self.stockqty = stockqty

I would remove the redundant 'stock' from all the parameter and
attribute names. (And call 'qty' 'quantity'.)

You will iterating like so:

for stock in StockMarket:
print(stock.name, stock.quantity,.... or whatever, and the iteration
variable makes clear that a stock is a Stock.

tjr
 
H

Hendrik van Rooyen

Thanks Paul! I thought this might be the case. So how would I get the
StockMarket class instance to contain many Stock class instances and
then be able to iterate through them? I'm guessing the basic structure
would be as follows...but I think I'm wrong:

class StockMarket:
pass

No.
At this level, just use a list of instances of your Stock class.

- Hendrik
 
A

Anthra Norell

cptn.spoon said:
I'm trying to create an incredibly simple stock market simulator to be
used in a childrens classroom and I'm wondering whether someone can
point me in the right direction.

I basically want to be able to have a stock struct as follows:

StockName = "Test"
StockPriceList = (12,13,12,10,10,8,10)
StockRisk = 0.15
StockQty = 2000

And then have a StockMarket struct that can contain 1 to many Stocks.
I then want to be able to iterate through the stocks to perform
operations as such:

for Stock in StockMarket:

I'm not asking for tips on the program itself, I just can't figure out
how to get the data structures in place. Would I use Classes or would
I use dictionaries? Am I completely off the mark with this?

Apologies for such a rudimentary question...I'm very new to all this.
If the subject is investment performance, surely your best bet is to
hand out piggy banks.

Frederic
 
C

cptn.spoon

If the subject is investment performance, surely your best bet is to
hand out piggy banks.

Frederic

Thank you all for your advice. It's been most helpful!

Frederic: Haha, it's more to encourage behaviour and risk management
but I agree with your sentiment!
 
C

cptn.spoon

No.
At this level, just use a list of instances of your Stock class.

- Hendrik

How do I get a list of instances of a particular class? Is there a way
to do this dynamically?

Also, what would be the way of dynamically creating an instance of a
class based on user input (ie a user wants to create a new instance of
the Stock class via shell input)?

I'm not sure if I have the wrong mindset here.

Thanks for any help
~Phil
 
R

Robert Kern

How do I get a list of instances of a particular class? Is there a way
to do this dynamically?

You *can*, but I highly recommend that you don't. Instead, just keep your own
list of instances. When you make a new instance, just append it to the list (see
below).

all_stocks = []
Also, what would be the way of dynamically creating an instance of a
class based on user input (ie a user wants to create a new instance of
the Stock class via shell input)?

Define an __init__ method on your class to initialize it from given values. Use
raw_input() to query the user for information. Convert the text input to
whatever objects your class needs (e.g. if the user entered "10" on the prompt,
x=raw_input() will return the string '10', so you would do int(x) to get the
integer 10). Now, instantiate your class with the arguments:

the_stock = Stock(name, risk, initial_price)

And append it to your list of stocks.

all_stocks.append(the_stock)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
C

cptn.spoon

How do I get a list of instances of a particular class? Is there a way
to do this dynamically?

You *can*, but I highly recommend that you don't. Instead, just keep your own
list of instances. When you make a new instance, just append it to the list (see
below).

   all_stocks = []
Also, what would be the way of dynamically creating an instance of a
class based on user input (ie a user wants to create a new instance of
the Stock class via shell input)?

Define an __init__ method on your class to initialize it from given values. Use
raw_input() to query the user for information. Convert the text input to
whatever objects your class needs (e.g. if the user entered "10" on the prompt,
x=raw_input() will return the string '10', so you would do int(x) to get the
integer 10). Now, instantiate your class with the arguments:

   the_stock = Stock(name, risk, initial_price)

And append it to your list of stocks.

   all_stocks.append(the_stock)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

That's exactly what I was after. Thanks Robert!
 
H

Hendrik van Rooyen

How do I get a list of instances of a particular class? Is there a way
to do this dynamically?

Yes there is. First make an empty list:

stock_market = []

Now when you create the instance:

stuff = GetStuffFromTheUserIfNecessary()
stock_market.append(Stock(stuff))

This leaves the new instance in the last position in the list.

When you have done that as many times as you wanted,
to create the stocks in the market, you can do:

for stock in stock_market:
DoThingsWithThisOneStock(stock)
Also, what would be the way of dynamically creating an instance of a
class based on user input (ie a user wants to create a new instance of
the Stock class via shell input)?

This means your program must be looking for that input.
Either look at thread and threading, or write a simple
loop that gets input, appends a stock, gets more input
if required, does something with all the stocks, and
either loops or terminates. Or some variation of that
like first building the market and then manipulating it.
I'm not sure if I have the wrong mindset here.

I honestly cannot tell if your mindset is right or
wrong - I suppose it depends on your definitions
of the two terms.
:)
- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top