Monday, February 08, 2021

Simple Moving Average in Google Sheets

The simple moving average, SMA, is one of the most basic financial calculations used in stock analysis. Calculating this in Google Sheets is not as straightforward as you might expect. Several web sites, for example sheetaki.com and nextlevel.finance contain instructions on how to calculate this value. These are good, but they work by pulling historical data onto the spreadsheet and then running calculations over these values. It would be preferable to also have a technique to more directly calculate this value. That is, to have a self contained calculation that doesn't need to output the historical data onto the sheet. This allows a current trailing SMA to be directly entered onto your stock analysis sheets.

The following composition of functions does just that (where A2 holds a ticker and $C$17 holds the number of days SMA you desire):

Breaking this down from the inside out:

  • The WORKDAY function gets the date $C$17 days ago. Note the use of the named range "holidays" to eliminate days the market was not open.
  • The GOOGLEFINANCE function pulls in the data for the closing price in the last $C$17 days the market was open
  • The INDEX function reduces the two dimensional array (date, price) returned by the GOOGLEFINANCE function into a one-dimensional array of closing prices.
  • The AVERAGE function, well yeah it computes the average of those closing prices.

With this value in hand you can easily compute stats such as the current price's relationship to this value. Many trading strategies, such as the Ivy Portfolio, are concerned with such signals.

The Ivy Portfolio: How to Invest Like the Top Endowments and Avoid Bear Markets

Wednesday, January 27, 2021

Google Sheets Stock Scripts

Google Sheets comes with an impressive assortment of stock functions in its GOOGLEFINANCE function. These are great. They get you a long way towards building a custom stock tracker / dashboard. There are some shortcomings, however. For example:

  • Some functions don't work for certain kinds of securites. =GOOGLEFINANCE("VTI", "expenseratio") does not work. Apparently this function does not work with ETFs such as VTI.
  • There is a lot of financial information available that these functions cannot provide. 200 day SMA, insider ownership, etc., etc..

Thankfully Google Sheets comes with the ability to augment these abilities with import functions and custom scripts that pull data from other sources. finviz is a great financial website that contains a wealth of information. A stock lookup on finviz for TSLA shows a table with more than 50 different data points.

The following gist shows how to pull in the 50 day SMA from finviz using built in Google Sheets functions:

Some of the non-obvious key parameters seen in the call are fetch from the 8th table on the page, indexing into row 12 and column 6. The function is a bit large and unwieldy due to the clean ups needed to convert the result into a numeric value provided by the SUBSTITUTE function.

Custom functions written in Google Apps Script provide a much more concise and potentially more flexible way to fetch such information for your sheets. The following functions makes it possible to do the above fetch with a much more concise =FINVIZ("TSLA", 11, 5):

Sunday, August 21, 2016

Launch Chrome by Profile Name (Updated)

Now for my once a decade update... Chrome eliminated white space from the JSON format used to store profile preferences. This broke the simple file parsing of the previous script. This update switches to the jq command-line JSON parser to fix the parsing. Enjoy: Send peer bonus, please :-)

Tuesday, June 11, 2013

Launch Chrome by Profile Name

Chrome allows you to launch it under a given profile if you give it an argument of --profile-directory="Directory Name". The directory names are "Default", "Profile 1", "Profile 2", etc.. Obviously this is not super convenient as you need to know the profile directory that goes with a given profile name. Here is a script that allows you to launch Chrome by profile name instead, followed by some bash code to allow bash auto-completion of profile names. First the gchrome script: and next, the bash completion code. Throw this in .bashrc or somewhere: Note that this relies on a certain layout of the Preferences file json which I have found to hold so far, however, it wouldn't be too surprising to find json that breaks it. This should work for Chrome on most Linux installs. This could probably be adjusted to work on OS X but the config file is located elsewhere. Enjoy.

Sunday, August 14, 2011

Use MySQL's Modulo Operator to Update in Batches

Sometimes you need to roll out your nefarious plan slowly and MySQL's modulo operator can help.  Here we do the first of ten batches:

Saturday, June 04, 2011

Javascript Binary Heap

Saturday morning javascript exercise - a binary heap in array and tree implementations. The tree implementation is faster as expected when you shove a lot of data as it as the array implementation must occasionally re-size the backing array. A demo or visualization would be nice but that takes more time than this Saturday morning allows. Code:



Saturday, May 14, 2011

Emacs Batch Edit Example

Say the following function is in your .emacs file and lets you indent your C source code to your liking:



But you don't want to manually open each file into an Emacs buffer to execute this script. Emacs batch editing to the rescue:



The parameters load up my .emacs file as normal and runs the given eval. Apply a little command line magic and you should be able to exploit Emacs from the command line in a very efficient way.

Monday, January 17, 2011

Monitor Comcast Usage Data

The good folks at Comcast have decided to put a 250GB cap on monthly usage as a direct assault against my beloved Roku.



I decided to put a script together to notify me in case I start approaching the monthly limit. My first thought was that this was a perfect task for mechanize, a Ruby library for interacting with web sites. I put that aside, however, to make an attempt to script the scraping with curl.

Those folks at Comcast are whack. When logging in to the home page, what is sent back is a redirect - no not an HTTP redirect. You are sent back a page that has a form in it with a "cima.ticket" that submits itself in the body onload event. Here is the somewhat functional script to pull the your Comcast bandwidth usage:



I say somewhat functional because it doesn't work every time. It will fail to pull a result every now and then. There is a lot of wonkiness in the way the Comcast login works - and it appears this results in some kind of timing issue. Hopefully I have a chance to investigate a solution for this in the future.

Oh - back to mechanize. I googled for another solution to this problem and found this. It shows off the elegance and ease of use of mechanize, however, it seems to fail intermittently just like my script.

Saturday, January 15, 2011

Emacs Find File in Project Updated Plus defun toggle-test

I've updated my fork of the Emacs Find File in Project with caching and storage of the file list in a map instead of an alist. Both of these should improve the performance with the latter possibly being a bit of an overkill for anything but very large projects.

I also put together a little function to toggle back and forth between class and test case for Java code per the directory layout we use at work. If the corresponding toggle file does not exist you will be prompted to create it:



Emacs rules with its quick customizations.

Saturday, December 18, 2010

pagiborder Chrome Extension Comes Alive!

Please welcome Chrome Extension pagiborder. It temporarily drops a line on the screen indicating the previous page border position when you paginate with the space bar.


I use it - so maybe someone else will find it useful as well.

Thursday, December 09, 2010

Emacs Find File in Project

Note: This is already out of date. I have found, forked, and tweaked a github repo that did most of what I wanted and my version can be found there. Further tweaks may include caching and other speed increases for large projects.

Increasingly for Java projects I've been relying on IntelliJ IDEA, an amazing IDE. There are still times, however, when I need the sharp tool capabilities of Emacs. I've never been completely satisfied with the "find file in project" capabilities I have been able to configure in Emacs (especially after using IntelliJ IDEA) so I set out to fix that. Building on the ideas found in Stuart Halloway's What you can learn from ido.el and the find file in project snippet given on the emacs wiki page for ido.el I can up with the following improved find file in project approach - well actually two approaches as I have created approaches that gather their file lists from either 1) a TAG file or 2) by issuing a shell find command.

The main tweaks provided are:

  • I am limiting the search to file name rather than full path. This makes the flex finding much more effective and much faster when working with large projects
  • I turn on case sensitive searching making capital letter camel case matching work very well
  • Name conflicts are handled by a second disambiguation prompt.

I'm sure there is still plenty of room for improvement here so you can check for updates in my .emacs file. The following indicates the present state of things with hopefully enough comments so that you can follow along:

Sunday, November 28, 2010

Monday, February 01, 2010

jQuery Shell Google Chrome Extension

I've written a handy Google Chrome extension for jQuery developers. The extension allows you to bring up a console window and run Javascript and jQuery commands against the current web page. It is great for learning or experimenting with jQuery. I call the extension the jQuery Shell and it can be found here. The source code can be found here. Here is a screen shot for your viewing pleasure:


Google Chrome has a really nice extension framework.

A couple of other useful Javascript tools (not Chrome extensions) I've discovered on the web lately are http://jsfiddle.net and http://jsbin.com/.

Sunday, January 31, 2010

Truncate all Tables in a MySQL Database Revisited

Since this old post still manages to get a fair number of hits I thought I should fix it up to encourage people to not reveal their MySQL passwords in history logs and process lists by entering it on the command line. The new version uses getopts for parsing command line options. If you pass in "-p", it will prompt you for your database password for each mysql command. If you really want the old bad behavior then "-P password" will do the trick. Enjoy:



It is also possible to put your password in your my.cnf file to have it picked up automatically. You can read more about MySQL password for version 5.4 security here.

Sunday, January 10, 2010

Java enum and Static Variables

I've been moving back into the world of Java after some time with Ruby...surprisingly (to me anyway) Java enums don't have access to static variables within the enum in the enum constructor. For example, this will fail to compile with an illegal forward reference when attempting to use the static constant:



I haven't found a work around for this that I am comfortable with. Defining the static constants elsewhere seems quite ugly.

The same problem occurs if you want to set up a static cache during construction as well. This can be worked around with a static initializer:

Sunday, July 12, 2009

Searching for Wildcard Characters in SQL

Been a while - a quick flush...you may have a need to search for the wildcard characters "_" and "%". When that happens you need to know how to escape them. This has slightly different behavior for different database products. MySQL and SQL Server do work the same way, however, if you stick to using the ESCAPE clause.

Friday, July 03, 2009

No Peeking in the Javascript Deque

Another (previous) take on the Javascript Deque with more of a Crockford creation style preventing messing with the Deque's internals. Create one without using new - "var d = deque();", enjoy:

Wednesday, July 01, 2009

Javascript Deque

Doubly ended queue code is just a whole lot cleaner if you go with dummy head and tail nodes to avoid special cases. I put together this Javascript example: A Javascript array has the methods available (push(), pop(), shift(), unshift()) to be used as a deque but of course being an array you shouldn't be adding and removing from anywhere but the tail end if you need top performance. The following provides for a comparison between my deque and a regular Javascript array for adding and removing items from the head or tail of the data structure.
Items:
array dequeue
head tail

Tuesday, June 23, 2009

JavaScript InfoVis Toolkit

The JavaScript InfoVis Toolkit is another example of a library exploiting Canvas technology. I put together a small demo Rails application that uses the toolkit to provide a dynamic tree view of the Rails application directory structure.

Monday, June 22, 2009

Don't Miss Aloha on Rails!



You don't want to miss the Hawaii Ruby On Rails Conference - Waikiki, Oahu, Hawaii - October 5-6 2009. The speaker list is impressive and still growing. The venue is awesome and is only a short (really short!!!) walk from Waikiki beach. The economic downturn is making flights to Hawaii very reasonable right now.

This conference is being primarily organized by Seth Ladd, a Honolulu based RoR developer and great co-worker of mine. There is an Aloha On Rail Blog featuring the latest information. The latest post features another great co-worker of mine, Honolulu Hacker Kevin English in a clip on why you should come to Aloha on Rails.

Why Aloha on Rails???

  • mingle with a group of very talented developers
  • gain insights on the latest technologies and techniques
  • re-charge your batteries
  • reward yourself!

This is a no brainer - register now!