Real Stock Price Chart with Moving Averages (Source Code)

In a previous project we draw real stock price charts by reading data from files. We also draw stock moving averages in this project. Download historical stock prices for GoogleApple, and SPY and draw 20-day and 50-day moving averages for the last 1000 trading days.

20-day and 50-day moving averages for AAPL
20-day and 50-day moving averages for GOOGL
20-day and 50-day moving averages for SPY

Source Code:

import turtle

with open('googl.us.txt','r') as fin:
    lines = fin.readlines()
n = min(len(lines)-1,1000)
lines = lines[-n:]
prices = [None]*n
mva1 = [None]*n
mva2 = [None]*n
p1 = 20
p2 = 50

for i in range(1,n+1):
    row = lines[i-1].split(',')
    prices[i-1] = float(row[4])
    mva1[i-1] = sum(prices[max(0,i-p1):i])/min(p1,i)
    mva2[i-1] = sum(prices[max(0,i-p2):i])/min(p2,i)
minprice, maxprice = min(prices),max(prices)

screen = turtle.Screen()
screen.title('Stock Price and Moving Averages From File - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,minprice*0.9,n,maxprice*1.1)

turtle.speed(0)
turtle.hideturtle()
turtle.color('gray')
turtle.up()
turtle.goto(0,prices[0])
turtle.down()
tm1 = turtle.Turtle()
tm2 = turtle.Turtle()
tm1.hideturtle()
tm2.hideturtle()
tm1.color('red')
tm2.color('blue')
tm1.up()
tm1.goto(0,mva1[0])
tm1.down()
tm2.up()
tm2.goto(0,mva2[0])
tm2.down()
for i in range(1,n):
    turtle.goto(i,prices[i])
    tm1.goto(i,mva1[i])
    tm2.goto(i,mva2[i])

Real Stock Price Chart (GOOG, AAPL, SPY) with Source Code

Download historical stock prices for Google, Apple, and SPY. Read the file and draw the stock charts for these companies. The following figures show the stock prices for these companies:

Stock Price Chart of GOOG from 2004 to 2016
Stock Price Chart of AAPL from 1984 to 2016
Stock Price Chart of SPY from 2005 to 2016

Source Code:

import turtle

with open('spy.us.txt','r') as fin:
    lines = fin.readlines()
n = len(lines)-1
prices = [None]*n
for i in range(1,len(lines)):
    row = lines[i].split(',')
    prices[i-1] = float(row[4])
minprice, maxprice = min(prices),max(prices)

screen = turtle.Screen()
screen.title('Stock Price From File - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,minprice*0.9,n,maxprice*1.1)
turtle.speed(0)
turtle.hideturtle()
turtle.up()
turtle.goto(0,prices[0])
turtle.down()
for i in range(1,n):
    turtle.goto(i,prices[i])

Drawing Sierpinski Carpet with Chaos Game (with Solution)

You can draw a Sierpinski Carpet with Chaos Game too. Read the first Chaos Game project first to understand what Chaos Game is.

In this project, you start with a square. The moving point will toward a randomly chosen corner of the square as well as the center of the four edges of the square. Therefore, there are total eight points that the moving point will move toward. In addition, it will only 1/3 of the way towards the target.

Sierpinski Carpet with Chaos Game

Solution:

import turtle
import random
import math

screen = turtle.Screen()
screen.title('Sierpinski Carpet with Chaos Game - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-200,-200,200,200)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

m=4
angle = math.pi/4
V = []
for i in range(m):
    p = (400*math.cos(angle),400*math.sin(angle))
    V.append(p)
    angle += math.pi*2/m

for v in V:
    turtle.goto(v)
    turtle.dot('blue')

n = 100000 # number of points to draw
p = V[0] # start from the first vertex
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'red')
    r = random.randrange(len(V)) # pick a random vertex
    if random.randint(0,1) == 0: # randomly decide to use edge or vertex
        q = V[r] # vertex
    else:
        q = ((V[r][0]+V[(r+1)%len(V)][0])/2,(V[r][1]+V[(r+1)%len(V)][1])/2) # go to mid point between two vertices   
    p = ((q[0]+p[0])/3,(q[1]+p[1])/3) # go 1/3 towards target   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Firing, Accelerating, and Rotating Spaceship

Continuing from previous Accelerating and Rotating Spaceship project, make the spaceship fire the bullets. Each bullet have limited range and there should be some time gap between the firings so that spaceship won’t destroy asteroids too easily in the future game. You may need to use list structure in Python to store the bullets.

Game of Snake with Python Turtle

Develop a Game of Snake with Python Turtle with multiple difficulty levels.

You may need to use features or libraries: List, Random, Keyboard Event, Timer Event, Colorsys.

Game of Snake with Python Turtle