This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-11-29 15:46:11

ScipioBellorum
Scratcher
Registered: 2007-05-15
Posts: 94

Stock Market Data?

I'm not sure that this is the right place to post this but...

A while ago someone posted that they were working on a method of importing live data from the stock markets into Scratch. I remember reading the post and being particularly interested as I've been working on a virtual stock market program in Scratch.

The problem is, I now can't find that post despite many forum searches. If I recall correctly the author was a fairly well known user, possibly Paddle2See or Jens.

Does anyone have any idea what I'm talking about or am I going mad...?

SB


http://img393.imageshack.us/img393/339/logory8.png
                      The word processor for Scratch

Offline

 

#2 2008-11-30 13:10:20

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Stock Market Data?

You're not going crazy. There was a leaked beta of something called NetScratch a while back from a member of the Scratch Team's blog that would import data from the Scratch website, but I don't remember anything about stocks. It never worked online, and the beta seems to be lost forever (I swear they remotely deleted it from my flash drive  wink  ).


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#3 2008-11-30 13:44:29

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Stock Market Data?

ScipioBellorum wrote:

I'm not sure that this is the right place to post this but...

A while ago someone posted that they were working on a method of importing live data from the stock markets into Scratch. I remember reading the post and being particularly interested as I've been working on a virtual stock market program in Scratch.

The problem is, I now can't find that post despite many forum searches. If I recall correctly the author was a fairly well known user, possibly Paddle2See or Jens.

Does anyone have any idea what I'm talking about or am I going mad...?

SB

I was working on a Python script that would use Scratch's remote sensors to import stock market data.  I got it working for one stock but kind of lost interest as it was simpler to just use Python to work with the data directly and skip the Scratch part.   Here is the Python script I was working with, if you want to take it further

import urllib
import re
from array import array
import socket
import time

HOST = '127.0.0.1'      # IP address of the remote host (in this case, the local machine)
                        # to connect to Scratch on a different computer, just put in its IP address
PORT = 42001            # The same port as used by the server

# sendScratchCommand(cmd)
# this function packages a message according the Scratch Networking Protocol
# and then sends it off
def sendScratchCommand(cmd):
        n = len(cmd)
        a = array('c')
        a.append(chr((n >> 24) & 0xFF))
        a.append(chr((n >> 16) & 0xFF))
        a.append(chr((n >>  8) & 0xFF))
        a.append(chr(n & 0xFF))
        scratchSock.send(a.tostring() + cmd)

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    m = re.search('class="pr".*?>(.*?)<', content)
    if m:
        quote = m.group(1)
    else:
        quote = 'no quote available for: ' + symbol
    return quote

# Make a connection to Scratch

print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("connected!")

# Get value - report it if it has changed

OldQuote = 0

while 1:
    Quote = get_quote('nyse:ge')

    TimeStamp = time.asctime()
   
    if Quote <> OldQuote:
        sendScratchCommand('sensor-update StockPrice ' + str(Quote))
        sendScratchCommand('sensor-update TimeStamp ' + '\"' + TimeStamp + '\"')
        sendScratchCommand('broadcast "QuoteRefresh"')
        print TimeStamp, Quote
        OldQuote = Quote
    else:
        print TimeStamp
       
    time.sleep(10)
   
scratchSock.close()


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#4 2008-11-30 14:59:29

Ricardo-san
Scratcher
Registered: 2008-11-20
Posts: 27

Re: Stock Market Data?

Paddle2See wrote:

ScipioBellorum wrote:

I'm not sure that this is the right place to post this but...

A while ago someone posted that they were working on a method of importing live data from the stock markets into Scratch. I remember reading the post and being particularly interested as I've been working on a virtual stock market program in Scratch.

The problem is, I now can't find that post despite many forum searches. If I recall correctly the author was a fairly well known user, possibly Paddle2See or Jens.

Does anyone have any idea what I'm talking about or am I going mad...?

SB

I was working on a Python script that would use Scratch's remote sensors to import stock market data.  I got it working for one stock but kind of lost interest as it was simpler to just use Python to work with the data directly and skip the Scratch part.   Here is the Python script I was working with, if you want to take it further

import urllib
import re
from array import array
import socket
import time

HOST = '127.0.0.1'      # IP address of the remote host (in this case, the local machine)
                        # to connect to Scratch on a different computer, just put in its IP address
PORT = 42001            # The same port as used by the server

# sendScratchCommand(cmd)
# this function packages a message according the Scratch Networking Protocol
# and then sends it off
def sendScratchCommand(cmd):
        n = len(cmd)
        a = array('c')
        a.append(chr((n >> 24) & 0xFF))
        a.append(chr((n >> 16) & 0xFF))
        a.append(chr((n >>  8) & 0xFF))
        a.append(chr(n & 0xFF))
        scratchSock.send(a.tostring() + cmd)

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    m = re.search('class="pr".*?>(.*?)<', content)
    if m:
        quote = m.group(1)
    else:
        quote = 'no quote available for: ' + symbol
    return quote

# Make a connection to Scratch

print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("connected!")

# Get value - report it if it has changed

OldQuote = 0

while 1:
    Quote = get_quote('nyse:ge')

    TimeStamp = time.asctime()
   
    if Quote <> OldQuote:
        sendScratchCommand('sensor-update StockPrice ' + str(Quote))
        sendScratchCommand('sensor-update TimeStamp ' + '\"' + TimeStamp + '\"')
        sendScratchCommand('broadcast "QuoteRefresh"')
        print TimeStamp, Quote
        OldQuote = Quote
    else:
        print TimeStamp
       
    time.sleep(10)
   
scratchSock.close()

Wow.

Offline

 

#5 2008-11-30 17:38:36

ScipioBellorum
Scratcher
Registered: 2007-05-15
Posts: 94

Re: Stock Market Data?

Paddle2See wrote:

I was working on a Python script that would use Scratch's remote sensors to import stock market data.  I got it working for one stock but kind of lost interest as it was simpler to just use Python to work with the data directly and skip the Scratch part.   Here is the Python script I was working with, if you want to take it further

[etc.]

I thought it was you. I'm sorry to hear that you're no longer working on it as it could be very useful if coupled with my existing project. Unfortunately, I have no experience what so ever with Python or anything similar but I will take a look non the less.

Thanks for your help,

SB


http://img393.imageshack.us/img393/339/logory8.png
                      The word processor for Scratch

Offline

 

#6 2008-12-05 15:22:58

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Stock Market Data?

My bad! Apparently there is still a NetScratch link floating around!
http://web.media.mit.edu/~tstern/stuff/netscratch/NetScratch.zip


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

Board footer