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

Stock Price Random Walk with Candlestick Chart

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

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

Stock Price Random Walk with Positive Mean

In this project, you simulated stock price random walk with mean equal to 0%. What if we increase the mean of Gaussian distribution very slightly to 0.5% and also increase the standard deviation to 5%. You may see something similar to a trending stock prices of successful companies.

Random Walk of Trending Stock Price
Random Walk of Trending Stock Price
Random Walk of Trending Stock Price

Stock Price Random Walk (Source Code)

In a previous project, we simulated a random walk on a 2-D plane. For this project, simulate stock price change with random walk. Start with an initial stock price. At next day, the price change by random percentage with Gaussian (Normal) distribution (random.gauss() function) with mean 0% and standard deviation of 2%. Try to simulate multiple times and see if they look similar to some of stock price charts you have seen before.

Stock Price Random Walk with mean 0% and std 2%
Stock Price Random Walk with mean 0% and std 2%
Stock Price Random Walk with mean 0% and std 2%

Source Code:

import turtle
import random

screen = turtle.Screen()
screen.title('Stock Price Random Walk - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,0,1000,1000)
turtle.speed(0)
turtle.hideturtle()

price=500
turtle.up()
turtle.goto(0,price)
turtle.down()
for i in range(1,1000):
    price *= (1+random.gauss(0,0.02))
    turtle.goto(i,price)

Dodecagon Spiral (Source Code)

In other related projects, we draw a pentagon spiral and a square spiral. Now draw a dodecagon spiral with 12 sides. Also make the grayscale of dodecagons gradually grow lighter as it gets smaller.

Dodecagon Spiral

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Dodecagon Spiral - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def draw_spiral(x,y,r,direction):
    if r < 10: return
    d = direction
    r2 = r*math.cos(math.radians(360/24))/math.cos(math.radians(360/24-alpha))
    dist = r*math.sin(math.radians(360/24))-r2*math.sin(math.radians(360/24-alpha))
    turtle.up()
    px = x + r*math.cos(math.radians(d))
    py = y + r*math.sin(math.radians(d))
    turtle.goto(px,py)
    turtle.color((1-r/900,1-r/900,1-r/900))
    turtle.down()
    d += 360/12
    for _ in range(12):
        px = x + r*math.cos(math.radians(d))
        py = y + r*math.sin(math.radians(d))
        turtle.goto(px,py)
        d += 360/12
    draw_spiral(x,y,r2,direction+alpha)
    
    
alpha = 3
draw_spiral(0,0,900,90)