Calculating Daily Log Returns from Wikifolio CSV Data with Python

38 views Asked by At

I'm in need of some assistance with creating a Python code to calculate the daily log returns from historical data extracted from a CSV document obtained from the webpage wikifolio (e.g., https://www.wikifolio.com/de/de/w/wf00200775). I have limited experience in coding and would greatly appreciate any help or guidance you can provide.

I would like to calculate the daily log returns based on the 'Close' values in this dataset. However, I'm encountering some challenges in handling missing or invalid values, as well as potential formatting differences.

If anyone has experience with parsing CSV data, handling missing values, and calculating log returns, I would be grateful for any code examples or insights you could share. Additionally, if you have any tips or best practices for working with financial data in Python, they would be highly appreciated.

Thank you in advance for your support!

Tried this and it did not work:

import pandas as pd
import numpy as np

### Read the data from the CSV file into a pandas DataFrame

data = pd.read_csv('your_file.csv', delimiter=';')

### Convert the 'Begin date' column to datetime type
data['Begin date'] = pd.to_datetime(data['Begin date'])

#### Sort the DataFrame by the 'Begin date' column in ascending order

data.sort_values('Begin date', inplace=True)

### Convert numeric columns to float type
numeric_columns = ['Open', 'Close', 'High', 'Low']
data[numeric_columns] = data[numeric_columns].apply(lambda x: x.str.replace(',', '.')).astype(float)

### Calculate the log return
data['LogReturn'] = np.log(data['Close'] / data['Close'].shift(1))

### Print the DataFrame with log returns
print(data)
0

There are 0 answers