mirror of
https://github.com/quachtinh113/main-fx.git
synced 2026-08-02 13:27:43 +00:00
28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
import pandas as pd
|
|
import numpy as np
|
|
|
|
def clean_and_prepare_data(df, symbol_name):
|
|
# 1. Loại bỏ nến trùng lặp
|
|
df = df[~df.index.duplicated(keep='first')]
|
|
|
|
# 2. Đảm bảo nến logic (OHLC)
|
|
bad_candles = df[(df['high'] < df['low']) | (df['high'] < df['open']) | (df['high'] < df['close'])]
|
|
if not bad_candles.empty:
|
|
print(f"Cảnh báo: Phát hiện {len(bad_candles)} nến lỗi tại {symbol_name}")
|
|
# Có thể xóa hoặc sửa tùy bạn, ở đây ta chọn xóa nến lỗi cực nặng
|
|
df = df.drop(bad_candles.index)
|
|
|
|
# 3. Tạo khung thời gian đầy đủ (không ngắt quãng)
|
|
# Giả sử khung M15, ta tạo range từ đầu đến cuối
|
|
full_range = pd.date_range(start=df.index.min(), end=df.index.max(), freq='15min')
|
|
df = df.reindex(full_range)
|
|
|
|
# 4. Điền nến thiếu (ffill cho giá, 0 cho volume)
|
|
df[['open', 'high', 'low', 'close']] = df[['open', 'high', 'low', 'close']].ffill()
|
|
df['tick_volume'] = df['tick_volume'].fillna(0)
|
|
|
|
# 5. Loại bỏ ngày cuối tuần (Forex nghỉ)
|
|
# Thứ 7 (5) và Chủ Nhật (6)
|
|
df = df[df.index.dayofweek < 5]
|
|
|
|
return df |