2016-08-03 67 views
0

我跟隨邁克爾·霍爾斯 - 摩爾算法交易書,並且遇到了一些代碼問題。當我把代碼粘貼到python中時,我得到了一些錯誤。邁克爾·霍爾斯 - 摩爾算法交易的錯誤

我在這裏錯過了些什麼,因爲它與書中寫的完全一樣嗎?

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'])) 

下面

[email protected]:~$ 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 
+2

這是一個縮進錯誤,所以...檢查您的縮進。使用空格而不是標籤,絕對不是標籤和空格。每個縮進級別必須是相同的空格數量。 –

+1

縮進時是否有混合製表符和空格的機會?編輯:@ Two-BitAlchemist擊敗了我。 :) –

+0

它實際上在堆棧跟蹤中說4次IndentationError。檢查你的標籤與空格。 – lonewaft

回答

2

的錯誤看起來像你將代碼粘貼到一個Python交互窗口。當您使用交互式窗口編寫一段縮進代碼時(例如定義函數或啓動for循環時),按兩次enter鍵結束代碼塊部分(這就是爲什麼當代碼是空行後出現錯誤應該是在一個縮進代碼塊)。在你的代碼中,你可以刪除所有的空白行(除了代碼塊末尾的空白行;這些都是結束代碼塊所必需的),並將其粘貼到交互式窗口中,或者可以將代碼複製到一個文本文件,以.py文件擴展名保存該文件,然後使用命令python your_script.py從命令提示符/ powershell/terminal運行該腳本。

或者,使用python IDE(而不是交互式窗口或從命令行)像pycharm(https://www.jetbrains.com/pycharm/)。

+0

取出*全部*空白行不完全正確。每個縮進部分末尾需要一個空行。我發現'exec r'''''''是一種避免這種問題的有效方法,只要不使用粘貼代碼用於文檔字符串的相同的三引號類型即可。 – user2357112

+0

這是真的;我會編輯我的回覆。感謝您指出了這一點! – crld

+1

如果這是問題,那麼使用iPython的魔術'%paste'而不是CTRL-V或同等的。 –