Stock Price Alert

Waiting for a stock price to rise to a certain value to sell it? or waiting for the price to drop to a certain value to buy it? But don’t want to be checking it every day? Here’s a little automation that sends an alert to email inbox.

This script will send you an email when stock price is outside the defined thresholds.

To set it up running every day, use windows Task Scheduler or CRON for linux and mac machines.

tip: on windows set the filename to .rbw (means window mode script), so that no console appears when the scheduled task runs. Beware that if there is an error you wont see it also.


class Stock
  require 'net/http'
    def self.fetch(*symbols)
        Hash[*(symbols.collect { |symbol| 
        	[symbol, Hash[*(Net::HTTP.get('','/d?f=nl1&s='+symbol).chop.split(',').unshift("Name").insert(2,"Price"))]];
# puts Stock::fetch("MSFT").inspect 

class Mail
  require 'time'
  require 'net/smtp'
  def self.send(mail)
    msg = "Subject: #{mail[:subject]}\n\n#{mail[:body]}"
    smtp = '', 587
    smtp.start('', mail[:from], mail[:password], :login) do
      smtp.send_message(msg, mail[:from], mail[:to])

stock = Stock::fetch("MSFT")["MSFT"]["Price"].to_f
Mail::send({from:     "", 
            to:       "", 
            password: "*********", 
            subject:  "MSFT stock",
            body:     stock}) if stock <= 26 or stock > 28

Update July 2013

And now in Clojure:

code - web

July 2013 - Tools: Clojure

comments powered by Disqus