Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

i’m trying to run a python code which i found on the net…

  1. User

    i’m trying to run a python code which i found on the net (https://levelup.gitconnected.com/cryptocurrency-portfolio-optimization-in-python-46b5bd6d6f81) with spyder, but it gives error. this error: Traceback (most recent call last):

    File C:\Programs\Anaconda\lib\site-packages\requests\models.py:971 in json return complexjson.loads(self.text, **kwargs)

    File C:\Programs\Anaconda\lib\json_init_.py:346 in loads return _default_decoder.decode(s)

    File C:\Programs\Anaconda\lib\json\decoder.py:337 in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())

    File C:\Programs\Anaconda\lib\json\decoder.py:355 in raw_decode raise JSONDecodeError(“Expecting value”, s, err.value) from None

    JSONDecodeError: Expecting value

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):

    File C:\Programs\Anaconda\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals)

    File c:\users\sepanta.spyder-py3\untitled0.py:14 resp = resp.json()

    File C:\Programs\Anaconda\lib\site-packages\requests\models.py:975 in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

    JSONDecodeError: Expecting value. this is the code i was trying to run:import pandas as pd import requests import time from pypfopt import risk_models from pypfopt.risk_models import CovarianceShrinkage from pypfopt import expected_returns from pypfopt.efficient_frontier import EfficientFrontier

    get the starting date in unix timestamp

    date_time = ‘2020-01-01 00:00:00’ pattern = ‘%Y-%m-%d %H:%M:%S’ epoch = int(time.mktime(time.strptime(date_time, pattern)))

    get all the cryptocurrencies in USD

    resp = requests.get(‘https://api.kraken.com/0/public/AssetPairs’) resp = resp.json()

    Keep all the cryptos over USD

    dollar_pairs = [] for pair in resp[‘result’]: if pair.endswith(‘USD’): dollar_pairs.append(pair)

    get all the unixtimestamps

    tmp_url = f’https://api.kraken.com/0/public/OHLC?pair=XXBTZUSD&since={epoch}&interval=1440’.format(epoch) resp = requests.get(tmp_url) dummy_df = pd.DataFrame(resp.json()[‘result’][‘XXBTZUSD’])[[0]]

    create a pandas data frame and add the closing prices of each asset

    df = pd.DataFrame(index=dummy_df[0]) for pair in dollar_pairs: tmp_url = f’https://api.kraken.com/0/public/OHLC?pair={pair}&since={epoch}&interval=1440’.format(pair,epoch) resp = requests.get(tmp_url) tmp_df = pd.DataFrame(resp.json()[‘result’][pair])[[0,4]] tmp_df.columns = [‘unixtimestap’, pair] tmp_df[pair] = tmp_df[pair].astype(float) df = pd.concat([df,tmp_df.set_index(‘unixtimestap’)], axis=1)

    remove the last row since it is not a full day

    df = df[:-1].copy()

    drop the columns with a least one NA

    df.dropna(axis=‘columns’, inplace=True)

    remove the other fiat currencies liker EURO and the stable coins like Tether and DAI

    df.drop(columns=[‘USDCUSD’, ‘USDTZUSD’, ‘DAIUSD’, ‘ZGBPZUSD’,‘ZEURZUSD’, ‘PAXGUSD’], axis=1, inplace=True)

    calculate the mean and variance

    mu = expected_returns.ema_historical_return(df, frequency=365) Sigma = risk_models.exp_cov(df, frequency=365) sharpe_weights = ef.max_sharpe() (ef.clean_weights()) ef.portfolio_performance(verbose=True) ef = EfficientFrontier(mu, Sigma) min_vol_weights = ef.min_volatility() (ef.clean_weights()) ef.portfolio_performance(verbose=True) with open(“AB.json”, encoding=‘utf-8’, errors=‘ignore’) as json_data: data = json.load(json_data, strict=False)

  2. Assistant

    The error you are encountering is a JSONDecodeError, indicating that the code is unable to parse the JSON response from the API correctly. Here are a few possible reasons for this error:

    1. Internet connection: Ensure that you have a stable internet connection, as the code relies on making API requests.

    2. API response: The code uses the Kraken API to fetch data. Make sure that the API is functioning correctly and returns the expected response. You can test this by opening the API URL in a web browser.

    3. API rate limits: Some APIs have rate limits, meaning that you can only make a certain number of requests per minute or hour. Check if Kraken has any rate limits and make sure you are not exceeding them.

    4. JSON formatting: The error message suggests that the JSON response may not have the expected format. You can print or inspect the response (resp.text) to verify if the JSON is correctly returned from the API.

    Make sure to debug and investigate each step of the code to determine the exact cause of the error.

  3. User

    let's supposes something's wrong with the api. can i download the relevant data from it's address manually and edit the code in a way in which it gets the data from my hard drive, not the internet?

  4. Assistant

    Yes, it is possible to download the relevant data from the API manually and modify the code to read the data from your hard drive instead of making API requests. Here's how you can do it:

    1. First, you need to download the data from the API manually. Open the API URL in a web browser and save the response as a JSON file on your hard drive. Make sure to save it in the appropriate format and location.

    2. Update the code to read the data from the locally saved JSON file instead of making API requests. Replace the API request lines with code to read the JSON file.

    Here's an example of how you can modify the code to read the data from a locally saved JSON file:

    import pandas as pd
    import json
    
    # Specify the path to the locally saved JSON file
    json_file_path = 'path/to/your/saved/json/file.json'
    
    # Read the data from the JSON file
    with open(json_file_path, 'r') as json_file:
        data = json.load(json_file)
    
    # Process the data as required
    # ...
    

    Replace 'path/to/your/saved/json/file.json' with the actual path to your locally saved JSON file.

    By doing this, the code will read the data from the JSON file on your hard drive instead of making API requests.

  5. User

    i'm a bit confused. please rewrite the whole code.

  6. Assistant

    Certainly! Here's the modified code that reads the data from a locally saved JSON file:

    import pandas as pd
    import json
    
    # Specify the path to the locally saved JSON file
    json_file_path = 'path/to/your/saved/json/file.json'
    
    # Read the data from the JSON file
    with open(json_file_path, 'r') as json_file:
        data = json.load(json_file)
    
    # Process the data as required
    # Replace the relevant sections of the code with the following:
    
    # Example: Accessing data from the JSON
    example_data = data['some_key']
    
    # Continue with the rest of the code, replacing the API request sections appropriately
    # ...
    

    Replace 'path/to/your/saved/json/file.json' with the actual path to your locally saved JSON file.

    Please note that you will need to properly extract the relevant data from the data variable according to the structure of the JSON file you have. Replace 'some_key' with the appropriate key(s) to access the necessary data.

    This way, the code will read the data from the JSON file on your hard drive, allowing you to bypass the API and work with the locally saved data instead.

  7. User

    this was the code right? import pandas as pd import requests import time from pypfopt import risk_models from pypfopt.risk_models import CovarianceShrinkage from pypfopt import expected_returns from pypfopt.efficient_frontier import EfficientFrontier

    get the starting date in unix timestamp

    date_time = ‘2020-01-01 00:00:00’ pattern = ‘%Y-%m-%d %H:%M:%S’ epoch = int(time.mktime(time.strptime(date_time, pattern)))

    get all the cryptocurrencies in USD

    resp = requests.get(‘https://api.kraken.com/0/public/AssetPairs’) resp = resp.json()

    Keep all the cryptos over USD

    dollar_pairs = [] for pair in resp[‘result’]: if pair.endswith(‘USD’): dollar_pairs.append(pair)

    get all the unixtimestamps

    tmp_url = f’https://api.kraken.com/0/public/OHLC?pair=XXBTZUSD&since={epoch}&interval=1440’.format(epoch) resp = requests.get(tmp_url) dummy_df = pd.DataFrame(resp.json()[‘result’][‘XXBTZUSD’])[[0]]

    create a pandas data frame and add the closing prices of each asset

    df = pd.DataFrame(index=dummy_df[0]) for pair in dollar_pairs: tmp_url = f’https://api.kraken.com/0/public/OHLC?pair={pair}&since={epoch}&interval=1440’.format(pair,epoch) resp = requests.get(tmp_url) tmp_df = pd.DataFrame(resp.json()[‘result’][pair])[[0,4]] tmp_df.columns = [‘unixtimestap’, pair] tmp_df[pair] = tmp_df[pair].astype(float) df = pd.concat([df,tmp_df.set_index(‘unixtimestap’)], axis=1)

    remove the last row since it is not a full day

    df = df[:-1].copy()

    drop the columns with a least one NA

    df.dropna(axis=‘columns’, inplace=True)

    remove the other fiat currencies liker EURO and the stable coins like Tether and DAI

    df.drop(columns=[‘USDCUSD’, ‘USDTZUSD’, ‘DAIUSD’, ‘ZGBPZUSD’,‘ZEURZUSD’, ‘PAXGUSD’], axis=1, inplace=True)

    calculate the mean and variance

    mu = expected_returns.ema_historical_return(df, frequency=365) Sigma = risk_models.exp_cov(df, frequency=365) sharpe_weights = ef.max_sharpe() (ef.clean_weights()) ef.portfolio_performance(verbose=True) ef = EfficientFrontier(mu, Sigma) min_vol_weights = ef.min_volatility() (ef.clean_weights()) ef.portfolio_performance(verbose=True) with open(“AB.json”, encoding=‘utf-8’, errors=‘ignore’) as json_data: data = json.load(json_data, strict=False) rewrite it completely, making the necessary fixes.

  8. Assistant

    Certainly! Here's the modified code that incorporates the necessary fixes, including reading data from a locally saved JSON file instead of making API requests:

    import pandas as pd
    import json
    from pypfopt import risk_models
    from pypfopt import expected_returns
    from pypfopt.efficient_frontier import EfficientFrontier
    
    # Specify the path to the locally saved JSON file
    json_file_path = 'path/to/your/saved/json/file.json'
    
    # Read the data from the JSON file
    with open(json_file_path, 'r') as json_file:
        data = json.load(json_file)
    
    # Process the data as required
    # ...
    # Replace the relevant sections of the code with the following:
    
    # Example: Accessing data from the JSON
    example_data = data['some_key']
    
    # Removed the API request sections, starting from:
    # resp = requests.get('https://api.kraken.com/0/public/AssetPairs')
    # ...
    # up until:
    # resp = requests.get(tmp_url)
    # ...
    
    # Calculate the mean and variance using the locally saved data
    df = pd.DataFrame(data)  # Assuming the data from the JSON can be directly used as a DataFrame
    
    # Remove the last row since it is not a full day
    df = df[:-1].copy()
    
    # Drop the columns with at least one NA
    df.dropna(axis=1, inplace=True)
    
    # Remove the other fiat currencies like EURO and the stable coins like Tether and DAI
    df.drop(columns=['USDCUSD', 'USDTZUSD', 'DAIUSD', 'ZGBPZUSD', 'ZEURZUSD', 'PAXGUSD'], inplace=True)
    
    # Calculate the mean and variance
    mu = expected_returns.ema_historical_return(df, frequency=365)
    Sigma = risk_models.exp_cov(df, frequency=365)
    
    # Create the efficient frontier
    ef = EfficientFrontier(mu, Sigma)
    
    # Optimize for maximum Sharpe ratio
    sharpe_weights = ef.max_sharpe()
    ef.set_weights(sharpe_weights)
    ef.portfolio_performance(verbose=True)
    
    # Optimize for minimum volatility
    min_vol_weights = ef.min_volatility()
    ef.set_weights(min_vol_weights)
    ef.portfolio_performance(verbose=True)
    

    Replace 'path/to/your/saved/json/file.json' with the actual path to your locally saved JSON file.

  9. User

    what should i replace some key with?

  10. Assistant

    In the code snippet I provided, 'some_key' is a placeholder representing a key present in the JSON data that you need to access for further processing.

    To determine the specific key you need to replace 'some_key' with, you should examine the structure of the JSON data you have saved. The JSON data is typically organized in a hierarchical structure consisting of key-value pairs.

    For example, if your JSON data looks like this:

    {
       "results": {
          "stock": {
             "symbol": "ABC",
             "price": 100.50
          }
       }
    }
    

    And you want to access the price of the stock, you would replace 'some_key' with 'results', 'stock', and 'price' step by step to access the desired value:

    example_data = data['results']['stock']['price']
    

    Inspect the structure of your JSON data and identify the relevant key(s) you need to access in order to retrieve the necessary information for further processing. Replace 'some_key' with the appropriate key(s) in the code snippet to extract the desired data.

  11. User

    i don't know . what key was used in the original code?

  12. Assistant

    In the original code, the JSON response is assigned to the variable resp. Since I don't have access to the actual JSON response provided by the API, I cannot determine the specific key used to access the relevant data in the original code.

    However, I can provide a general approach for accessing data from a JSON response based on the code you shared.

    For example, let's assume the JSON response has the following structure:

    {
      "result": {
        "XXBTZUSD": {
          "data": [
            [timestamp_1, closing_price_1],
            [timestamp_2, closing_price_2],
            ...
          ]
        },
        "XETHZUSD": {
          "data": [
            [timestamp_1, closing_price_1],
            [timestamp_2, closing_price_2],
            ...
          ]
        },
        ...
      }
    }
    

    To access the closing prices for different cryptocurrency pairs, you can modify the code as follows:

    import pandas as pd
    import json
    
    # Specify the path to the locally saved JSON file
    json_file_path = 'path/to/your/saved/json/file.json'
    
    # Read the data from the JSON file
    with open(json_file_path, 'r') as json_file:
        data = json.load(json_file)
    
    # Accessing the closing prices for each cryptocurrency pair
    df = pd.DataFrame()
    for pair, pair_data in data['result'].items():
        closing_prices = []
        for datapoint in pair_data['data']:
            timestamp, closing_price = datapoint
            closing_prices.append((timestamp, closing_price))
        tmp_df = pd.DataFrame(closing_prices, columns=['unixtimestamp', pair])
        df = pd.concat([df, tmp_df.set_index('unixtimestamp')], axis=1)
    
    # Drop the last row since it is not a full day
    df = df[:-1].copy()
    
    # Remove columns with at least one NA
    df.dropna(axis='columns', inplace=True)
    
    # Process the dataframe as required
    # ...
    

    In this code, we assume that the relevant data can be accessed using the keys 'result', 'XXBTZUSD', 'XETHZUSD', and so on. Modify the code according to the structure of your JSON response and the specific data you need to extract.