One to Ten (Source Code)

Draw from a single point to ten points that fall into a single line. Also draw dots on the end points.

One to Ten

Source Code:

import turtle

turtle.setup(1000,1000)
turtle.title('One to Ten - PythonTurtle.Academy')
turtle.speed(0)
turtle.hideturtle()

turtle.color('green')

turtle.up()
turtle.goto(0,250)
turtle.dot('red')
for x in range(-450,460,100):
    turtle.up()
    turtle.goto(0,250)
    turtle.down()
    turtle.goto(x,-250)
    turtle.dot('blue')

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])

Stock Price Random Walk with Moving Averages

Moving averages can smooth out the stock prices and reduce the randomness. Based on the previous projects (stock price random walk with candlestick chart, and trending stock price random walk with candlestick chart), add 10 day and 25 day moving averages to the charts.

Stock Price Random Walk with Moving Averages
Trending Stock Price Random Walk with Moving Averages
Trending Stock Price Random Walk with Moving Averages

Trending Stock Price Random Walk with Candlestick Chart

In a previous project, we draw a trending stock price random walk chart. Candlestick chart is a more common way of representing stock price chart. Modify the code for the previous project to draw stock price random walk with Candlestick Chart.

Trending Stock Price Random Walk with Candlestick Chart
Trending Stock Price Random Walk with Candlestick Chart