<jupyter_start><jupyter_text>Target- for each stock:   - for each date (T):    - calculate the max price change in next 20 trading dates: price_change = (max{close price in T+1 to T+20} - {close price on T})/({close price on T})<jupyter_code>import pandas as pd
import numpy as np
import os
from pathlib import Path
src_file = 'data/min_date_20120301_163stocks.csv'
df = pd.read_csv(src_file, sep='|', compression='bz2')
df.head()
#for each stock_id, get the max close in next 20 trading days
price_col = 'Close'
roll_len=20
new_col = 'next_20day_max'
target_list = []

for stock_id in df['stock_id'].unique():
    df_ = df.loc[df['stock_id']==stock_id, ['stock_id', 'Date', price_col]].copy(deep=True)
    df_.sort_values(by=['Date'], ascending=False, inplace=True)

    df_next20dmax=df_[[price_col]].shift(1).rolling(roll_len).max()
    df_next20dmax.columns=[new_col]
    df_ = df_.merge(df_next20dmax, right_index=True, left_index=True, how='inner')

    df_.dropna(how='any', inplace=True)
    df_['target']= 100*(df_[new_col]-df_[price_col])/df_[price_col]  
    
    target_list.append(df_[['stock_id', 'Date', 'target']])
len(target_list)
df_target = pd.concat(target_list)
df_target.shape
df_target.tail(5)
df.head(5)
df.shape
df = df.merge(df_target, on=['stock_id', 'Date'], how='inner')
df.shape
df.head()
df.isna().sum()
src_file
dest_file = src_file.replace('.csv', '_w_target.csv')
dest_file
df.to_csv(dest_file, sep='|', compression='bz2', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1627751834556371, 'fraction_numerical': 0.02401601067378252, 'mean_word_length': 5.8807339449541285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '587248', 'license': 'MIT', 'n_tokens_mistral': 601, 'n_tokens_neox': 572, 'n_words': 120, 'path': 'pharma/3_define_target.ipynb', 'repo_name': 'xxxxyyyy80008/north_star', 'size': '1499'}