4.0 KiB
title | date | draft | tags | |||
---|---|---|---|---|---|---|
Getting Into Day Trading: Simple Moving Average | 2017-10-28T18:13:45-04:00 | false |
|
We've all heard the get rich quick schemes right? That someone somewhere has some plan to game the stock market to make billions of dollars and you'll never have to work again if you have followed their plan. This may not be one of those schemes (full disclosure), but I am a current "day trader." My portfolio kinda sucks. If I'm being honest, that is because I never ever watch it. How can I make sure that I am not losing tons of money daily without watching my portfolio?
Programming, that's how. I want to build a learning model that can do two things for me.
- Figure out when to buy and sell stock that I have and
- try to ascertain what a good next pick would be for an investment.
Now, I'm starting from square 0 here, with no knowledge of the stock market and no knowledge of how to attack this problem. I've done a little bit of research, and seems like a good place to start is with simple moving averages, so I'll start there.
What is a simple moving average? Investopedia says that a simple moving average is "an arithmetic moving average calculated by adding the closing price of a security for a number of time periods and then dividing this total by the number of time periods." Sounds like a pretty simple average to me, so lets get started.
I went ahead and grabbed a dataset of stocks off of Kaggle, fired up the Julia programming language, and got to work. The first step in my journey here is to read the data into memory to start playing with it. Let's do this.
julia> Pkg.add("DataFrames")
# Julia doing things
Now that I've installed DataFrames, I can get to work writing some Julia code.
using DataFrames;
data = readtable("~/prices.csv")
This reads our data into a data table using the DataFrames, the likes of which look like this
julia> head(data)
6×7 DataFrames.DataFrame
│ Row │ date │ symbol │ open │ close │ low │ high │ volume │
├─────┼───────────────────────┼────────┼────────┼────────┼────────┼────────┼──────────┤
│ 1 │ "2016-01-05 00:00:00" │ "WLTW" │ 123.43 │ 125.84 │ 122.31 │ 126.25 │ 2.1636e6 │
│ 2 │ "2016-01-06 00:00:00" │ "WLTW" │ 125.24 │ 119.98 │ 119.94 │ 125.54 │ 2.3864e6 │
│ 3 │ "2016-01-07 00:00:00" │ "WLTW" │ 116.38 │ 114.95 │ 114.93 │ 119.74 │ 2.4895e6 │
│ 4 │ "2016-01-08 00:00:00" │ "WLTW" │ 115.48 │ 116.62 │ 113.5 │ 117.44 │ 2.0063e6 │
│ 5 │ "2016-01-11 00:00:00" │ "WLTW" │ 117.01 │ 114.97 │ 114.09 │ 117.33 │ 1.4086e6 │
│ 6 │ "2016-01-12 00:00:00" │ "WLTW" │ 115.51 │ 115.55 │ 114.5 │ 116.06 │ 1.098e6 │
Now we need to grab the data for a given security and analyze it to calulate the moving average. How about WLTW? Top of the list seems like a good pick here, so lets calculate the moving average for the close price over rows 1-5. This will be the moving average for WLTW over 5 time periods.
# Get all rows for WLTW
wltw = data[data[:symbol] .== "WLTW", :]
# Grab the closing cost for the first 5 rows and calculate an average
moving_avg = sum(wltw[1:5, :close])/5
@printf "Moving average for WLTW, 5 days: %f" moving_avg
Upon executing this code in Julia your moving average should output as 118.472.
Next post, I will be trying to get moving average for WLTW across several different time periods. Also, I'll try to use those moving averages as inputs to learning algorithms to try to detect patterns that can be used to predict the rise and/or fall of the WLTW security at least. Thanks for reading!