For labeling your cryptocurrency data based on the criteria you provided, you will first need to ensure your dataset is structured appropriately for time series analysis and that you have the 'Close' prices available since we'll base our computation on them. We will use pandas, a powerful data manipulation library in Python, to achieve this.
First, ensure you have pandas installed. If not, you can install it via pip:
pip install pandas
Then, you can use the following Python code to label your data:
import pandas as pd
# Assuming `df` is your DataFrame and it has a 'Close' column with the closing prices of a cryptocurrency
# Ensure your DataFrame is sorted by date ascending (earliest to latest)
# Calculate the percentage change in closing price for the next two days
df['Pct_change_next_2_days'] = df['Close'].pct_change(periods=2).shift(-2) * 100
# Define a function to apply the labeling based on the criteria
def label_data(row):
if -3 <= row['Pct_change_next_2_days'] <= 3:
return 0
elif row['Pct_change_next_2_days'] < -3:
return 1
elif row['Pct_change_next_2_days'] > 3:
return 2
# Apply the function to create a new 'Label' column
df['Label'] = df.apply(label_data, axis=1)
# The last two rows will have NaN in 'Pct_change_next_2_days' as their future data is not available
# You might want to handle or remove these
df.dropna(subset=['Label'], inplace=True)
# Your dataframe now has a new column 'Label' with the criteria specified applied
print(df[['Close', 'Pct_change_next_2_days', 'Label']])
Please ensure that you replace df with the actual name of your DataFrame. This script does the following:
-
It calculates the percentage change in the closing price for the next 2 days and stores this in a new column. The periods=2 parameter in pct_change() does this calculation considering the next 2 days, and shift(-2) ensures alignment with the current day for comparison. This introduces NaN values for the last two rows because they don't have a "next 2 days" reference, which we then handle by dropping.
-
Defines a function label_data that categorizes the percentage change into 3 distinct labels as per your conditions.
-
Applies this function across the DataFrame, row-wise (axis=1), to generate a new column 'Label' with the corresponding labels.
-
Finally, it prints a portion of your DataFrame showing the 'Close' price, the percentage change compared to the next 2 days, and the assigned label.
Be aware that cryptocurrencies are traded 24/7, so "next 2 days" doesn't account for weekends or holidays as it would with traditional stock market data, but it proceeds to the next 48 hours of data directly.