Reputation: 794
I am following Michael Halls-Moore Algorithmic Trading book and having some problems with the code. When I paste the code into python I get a load of errors.
Am I missing something here because its exactly the same as what is written in the book?
from __future__ import print_function
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn
def hurst(ts):
"""Returns the Hurst Exponent of the time series vector ts"""
# Create the range of lag values
lags = range(2, 100)
# Calculate the array of the variances of the lagged differences
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# Use a linear fit to estimate the Hurst Exponent
poly = polyfit(log(lags), log(tau), 1)
# Return the Hurst exponent from the polyfit output
return poly[0]*2.0
# Create a Geometric Brownian Motion, Mean-Reverting and Trending Series
gbm = log(cumsum(randn(100000))+1000)
mr = log(randn(100000)+1000)
tr = log(cumsum(randn(100000)+1)+1000)
# Output the Hurst Exponent for each of the above series
# and the price of Amazon (the Adjusted Close price) for
# the ADF test given above in the article
print("Hurst(GBM): %s" % hurst(gbm))
print("Hurst(MR): %s" % hurst(mr))
print("Hurst(TR): %s" % hurst(tr))
# Assuming you have run the above code to obtain 'amzn'!
print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))
The errors below
james@ubuntu:~$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>>
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract
>>> from numpy.random import randn
>>>
>>>
>>> def hurst(ts):
... """Returns the Hurst Exponent of the time series vector ts"""
... # Create the range of lag values
...
>>> lags = range(2, 100)
File "<stdin>", line 1
lags = range(2, 100)
^
IndentationError: unexpected indent
>>>
>>> # Calculate the array of the variances of the lagged differences
...
>>> tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
File "<stdin>", line 1
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
^
IndentationError: unexpected indent
>>>
>>> # Use a linear fit to estimate the Hurst Exponent
...
>>> poly = polyfit(log(lags), log(tau), 1)
File "<stdin>", line 1
poly = polyfit(log(lags), log(tau), 1)
^
IndentationError: unexpected indent
>>>
>>> # Return the Hurst exponent from the polyfit output
...
>>> return poly[0]*2.0
File "<stdin>", line 1
return poly[0]*2.0
^
IndentationError: unexpected indent
>>>
>>> # Create a Gometric Brownian Motion, Mean-Reverting and Trending Series
... gbm = log(cumsum(randn(100000))+1000)
>>> mr = log(randn(100000)+1000)
>>> tr = log(cumsum(randn(100000)+1)+1000)
>>>
>>> # Output the Hurst Exponent for each of the above series
... # and the price of Amazon (the Adjusted Close price) for
... # the ADF test given above in the article
... print("Hurst(GBM): %s" % hurst(gbm))
Hurst(GBM): None
>>> print("Hurst(MR): %s" % hurst(mr))
Hurst(MR): None
>>> print("Hurst(TR): %s" % hurst(tr))
Hurst(TR): None
>>>
>>> # Assuming you have run the above code to obtain 'amzn'!
... print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))
Hurst(AMZN): None
Upvotes: -1
Views: 339
Reputation: 397
Looks like you're pasting the code into a python interactive window. When you use the interactive window to write a block of indented code (like when defining a function or starting a for loop), pressing enter twice ends the code block section (this is why you get the error after a blank line when the code is supposed to be in an indented code block). In your code, you can either remove all the blank lines (except for blank lines at the end of a block of code; those are necessary to end the code block) and paste it into an interactive window, or you can copy the code into a text file, save the file with .py file extension, and run the script from the command prompt/powershell/terminal using the command python your_script.py
.
Alternatively, use a python IDE (instead of interactive window or from the command line) like pycharm (https://www.jetbrains.com/pycharm/).
Upvotes: 2