Power Law Discovery

User Story

I want to Discover a Power Law in the closing Stock Prices Log Returns

Business Value

The Business Value of this User Story comes from the fact that our Hedge Fund operates a long term trading strategy. We don’t want to trade too often, because of involved costs. Let’s say that we would prefer to buy and sell once a month based on a significant correction. The issue is to determine an appropriate signal given that we want to initiate a transaction every 1 out of about 20 days. Approximately 5% probability with other words.

from matplotlib.finance import quotes_historical_yahoo

from datetime import date

import numpy

import sys

import matplotlib.pyplot

#1. Get close prices.

today = date.today()

start = (today.year - 1, today.month, today.day)

quotes = quotes_historical_yahoo('AAPL', start, today)

close =  numpy.array([q[4] for q in quotes])

#2. Get positive log returns.

logreturns = numpy.diff(numpy.log(close))

pos = logreturns[logreturns > 0]

#3. Get frequencies of returns.

counts, rets = numpy.histogram(pos)

rets = rets[:-1] + (rets[1] - rets[0])/2

freqs = 1.0/counts

freqs =  numpy.log(freqs)

#4. Fit the frequencies and returns to a line.

p = numpy.polyfit(rets,freqs, 1)

#5. Plot the results.

matplotlib.pyplot.plot(rets, freqs, 'o')

matplotlib.pyplot.plot(rets, p[0] * rets + p[1])

matplotlib.pyplot.show()