change following code so it calculate weekly timeframe instead of 4h: import pandas as…
- User
change following code so it calculate weekly timeframe instead of 4h:
import pandas as pd import os from concurrent.futures import ThreadPoolExecutor, as_completed
def create_4h(filename, csv_path, out_directory_path): try: # Load the dataset df = pd.read_csv(csv_path, parse_dates=['Date'])
# Ensure the 'Date' column is the index df.set_index('Date', inplace=True) # Resample data to 4-hour periods # 'ohlc' computes the open price of the first record, # the high price of the highest record, the low price of the lowest record, # and the close price of the final record within each group df_resampled = df.resample('4h').agg({ 'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last', 'volume_crypto': 'sum', 'volume_base': 'sum', 'tradecount': 'sum' }) # Reset index to add the Date column back df_resampled.reset_index(inplace=True) output_path = os.path.join(out_directory_path, f"4h_{filename}") # Save the 4-hour timeframe data to a new CSV df_resampled.to_csv(output_path, index=False) print(f"success {output_path}") except Exception as e: print(f"Failed to process {csv_path} due to {e}")def process_files(directory, out_directory_path): with ThreadPoolExecutor() as executor: futures = [] for filename in os.listdir(directory): if filename.endswith('.csv'): csv_path = os.path.join(directory, filename) print(f"Processing {filename}…") futures.append(executor.submit(create_4h, filename, csv_path, out_directory_path))
for future in as_completed(futures): pass # Future results or exceptions can be processed here if neededdef main(): # Replace the path with the directory containing your CSV files directory_path = r"..\downloaded_files\Binance\00_rename_columns_sort\1h_spot" out_directory_path = r"..\downloaded_files\Binance\00_rename_columns_sort\4h_spot" process_files(directory_path, out_directory_path)
main()
- Assistant