Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to find what is causing the unboundlocalerror 'crumb' and invalid syntax error?
#1
I am running a test for cointegration using numpy and pandas library. The codeis written in Python 2 while I am trying to execute the code in Python 3. I am getting 'crum' and invalid syntax errors see below code for detailed input/output. The complete code is available in the link at the end of the post.

This is the code:

import numpy as np
    import pandas as pd

    import statsmodels
    from statsmodels.tsa.stattools import coint
    # just set the seed for the random number generator
    np.random.seed(107)

    import matplotlib.pyplot as plt
Generate a fake security X and model it’s daily returns by drawing from a normal distribution. Then perform a cumulative sum to get the value of X on each day.

# Generate daily returns
    Xreturns = np.random.normal(0, 1, 100) 
    # sum them and shift all the prices up
    X = pd.Series(np.cumsum(
    Xreturns), name='X') + 50
    X.plot(figsize=(15,7))
    plt.show()
Generate Y which has deep economic link to X, so price of Y should vary pretty similarly as X.

noise = np.random.normal(0, 1, 100)
    Y = X + 5 + noise
    Y.name = 'Y'
    pd.concat([X, Y], axis=1).plot(figsize=(15,7))
    plt.show()
Plot the ratio between the two:

(Y/X).plot(figsize=(15,7)) 
    plt.axhline((Y/X).mean(), color='red', linestyle='--') 
    plt.xlabel('Time')
    plt.legend(['Price Ratio', 'Mean'])
    plt.show()
# compute the p-value of the cointegration test
    # will inform us as to whether the ratio between the 2 timeseries is stationary
    # around its mean
    score, pvalue, _ = coint(X,Y)
    print (pvalue)
ret1 = np.random.normal(1, 1, 100)
    ret2 = np.random.normal(2, 1, 100)
s1 = pd.Series( np.cumsum(ret1), name='X')
    s2 = pd.Series( np.cumsum(ret2), name='Y')
 pd.concat([s1, s2], axis=1 ).plot(figsize=(15,7))
    plt.show()
    print 'Correlation: ' + str(X_diverging.corr(Y_diverging))
    score, pvalue, _ = coint(X_diverging,Y_diverging)
    print 'Cointegration test p-value: ' + str(pvalue)
Error Message:
File "<ipython-input-9-f34a88d80c6a>", line 9
print 'Correlation: ' + str(X_diverging.corr(Y_diverging))
SyntaxError: invalid syntax

Y2 = pd.Series(np.random.normal(0, 1, 800), name='Y2') + 20
    Y3 = Y2.copy()
Y3[0:100] = 30
    Y3[100:200] = 10
    Y3[200:300] = 30
    Y3[300:400] = 10
    Y3[400:500] = 30
    Y3[500:600] = 10
    Y3[600:700] = 30
    Y3[700:800] = 10
    Y2.plot(figsize=(15,7))
    Y3.plot()
    plt.ylim([0, 40])
    plt.show()
    # correlation is nearly zero
    print 'Correlation: ' + str(Y2.corr(Y3))
    score, pvalue, _ = coint(Y2,Y3)
    print 'Cointegration test p-value: ' + str(pvalue)
Error message:File "<ipython-input-11-63dc43af8155>", line 14
print 'Correlation: ' + str(Y2.corr(Y3))
SyntaxError: invalid syntax

def find_cointegrated_pairs(data):
    n = data.shape[1]
    score_matrix = np.zeros((n, n))
    pvalue_matrix = np.ones((n, n))
    keys = data.keys()
    pairs = []
    for i in range(n):
        for j in range(i+1, n):
            S1 = data[keys[i]]
            S2 = data[keys[j]]
            result = coint(S1, S2)
            score = result[0]
            pvalue = result[1]
            score_matrix[i, j] = score
            pvalue_matrix[i, j] = pvalue
            if pvalue < 0.02:
                pairs.append((keys[i], keys[j]))
     return score_matrix, pvalue_matrix, pairs
pip install auquan-toolbox and execute following code snippet:

from backtester.dataSource.yahoo_data_source import YahooStockDataSource
     from datetime import datetime
     startDateStr = '2007/12/01'
     endDateStr = '2017/12/01'
     cachedFolderName = 'yahooData/'
     dataSetId = 'testPairsTrading'
     instrumentIds = ['SPY','AAPL','ADBE','SYMC','EBAY','MSFT','QCOM',
                 'HPQ','JNPR','AMD','IBM']
     ds = YahooStockDataSource(cachedFolderName=cachedFolderName,
                            dataSetId=dataSetId,
                            instrumentIds=instrumentIds,
                            startDateStr=startDateStr,
                            endDateStr=endDateStr,
                            event='history')
     data = ds.getBookDataByFeature()['Adj Close']
     data.head(3)
Error message:File "C:\ProgramData\Anaconda3\lib\site-packages\backtester\dataSource\data_source_utils.py", line 25, in getCookieForYahoo
return cookie, crumb # return a tuple of crumb and cookie

UnboundLocalError: local variable 'crumb' referenced before assignment

Complete code and description.


Any help is much appreciated. Thank you.
Quote
#2
The syntax errors are because python 3 print statements should be contained in brakets
print('something')

The code line return cookie, crumb # return a tuple of crumb and cookie shown in the UnboundLocalError does not appear anywhere in the code shown.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find subsequent error relationships between diagnostic events vanny 1 218 Apr-04-2020, 04:19 PM
Last Post: Larz60+
  SyntaxError: invalid syntax Truman 3 413 Mar-10-2020, 03:16 PM
Last Post: Truman
  Keras + Matplotlib causing crash spearced 3 973 Feb-06-2020, 04:54 PM
Last Post: zljt3216
  I need help fixing a syntax error! chenqin348 5 665 Dec-27-2019, 12:07 PM
Last Post: Larz60+
  Error : (FileNotFoundError(2, 'The system cannot find the file specified', None, 2, Shipika 3 662 Nov-25-2019, 02:39 PM
Last Post: buran
  list comprehension invalid syntax mcgrim 1 1,059 Jun-12-2019, 08:28 PM
Last Post: Yoriz
  Can you help me with this error? IndexError: invalid index of a 0-dim tensor. DerBerliner 1 1,558 Feb-28-2019, 05:47 PM
Last Post: Larz60+
  %matplotlib inline , invalid syntax yamoon 1 8,037 Jul-12-2018, 07:22 AM
Last Post: volcano63
  Misterious invalid syntax Galedon 3 1,036 Mar-30-2018, 03:49 PM
Last Post: Galedon

Forum Jump:


Users browsing this thread: 1 Guest(s)