Mıhı

 avatar
neron
plain_text
5 months ago
8.2 kB
3
Indexable
import ccxt
import numpy as np
import pandas as pd
from pandas import DataFrame
import pandas_ta as ta
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_score
from sklearn.utils import resample
import joblib
import os
from datetime import datetime, timedelta, timezone 
import time
import logging
from collections import deque
import random

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Binance borsasına bağlanın
exchange = ccxt.binanceusdm()
binance = ccxt.binance({
    'enableRateLimit': True,
})
symbols = ['BNB/USDT']
timeframe = '5m'
limit = 500
ma_period = 10

# Tahmin sonuçlarını saklamak için bir liste
predictions_history = []
correct_predictions = 0
total_predictions = 0
learning_rate = 0.1

# Deneyim Havuzu (Experience Replay) için
experience_replay_buffer = deque(maxlen=1000)  # Deneyim havuzunun boyutu
batch_size = 128  # Rastgele örnekleme yapılacak örnek sayısı

def remaining_time(clock, timeframe):  # bekleme süresinin hesaplanmasında kullanılır.
    if timeframe == "5m":
        wait_time = (300 - (clock.minute % 5 * 60 + clock.second))
    elif timeframe == "15m":
        wait_time = (900 - (clock.minute % 15 * 60 + clock.second))
    elif timeframe == "30m":
        wait_time = (1800 - (clock.minute % 30 * 60 + clock.second))
    elif timeframe == "1h":
        wait_time = (3600 - (clock.minute * 60 + clock.second))
    elif timeframe == "2h":
        wait_time = (7200 - (clock.hour % 2 * 3600 + clock.minute * 60 + clock.second))
    elif timeframe == "4h":
        wait_time = (14400 - (clock.hour % 4 * 3600 + clock.minute * 60 + clock.second))
    elif timeframe == "1d":
        wait_time = (86400 - (clock.hour * 3600 + clock.minute * 60 + clock.second))  # Günlük için
    return wait_time

# Oyun teorisi tabanlı özellikleri hesaplama fonksiyonu
def calculate_game_theory_features(df):
    df['price_change'] = df['close'].diff()
    df['up_moves'] = np.where(df['price_change'] > 0, 1, 0)
    df['down_moves'] = np.where(df['price_change'] < 0, 1, 0)
    
    df['total_up_moves'] = df['up_moves'].cumsum()
    df['total_down_moves'] = df['down_moves'].cumsum()
    
    df['volume_change'] = df['volume'].diff().fillna(0)
    df['volume_up'] = np.where(df['volume_change'] > 0, 1, 0)
    df['volume_down'] = np.where(df['volume_change'] < 0, 1, 0)
    
    df['total_volume_up'] = df['volume_up'].cumsum()
    df['total_volume_down'] = df['volume_down'].cumsum()
    
    df = df.dropna()  # NaN değerleri olan satırları kaldır
    return df

def fetch_and_update_data(symbol, timeframe, limit):
    filename = f"{symbol.replace('/', '_')}_{timeframe}.csv"
    filepath = os.path.join(os.getcwd(), filename)

    df = pd.DataFrame()  # df'i başlangıçta boş bir DataFrame olarak tanımlayalım
    last_timestamp_ms = None

    if os.path.exists(filepath):
        try:
            df = pd.read_csv(filepath)
            if not df.empty:
                last_timestamp = df['timestamp'].max()
                try:
                    last_timestamp_ms = int(pd.to_datetime(last_timestamp).timestamp() * 1000)
                except ValueError:
                    last_timestamp_ms = int(last_timestamp)
        except Exception as e:
            logging.error(f"{filepath} okunurken hata oluştu: {e}")
            df = pd.DataFrame()  # Eğer hata varsa, df'i yine boş bırak

    ohlcv_data = exchange.fetch_ohlcv(symbol, timeframe, since=last_timestamp_ms) 

    try:
        print(f"Çekilen veri sayısı: {len(ohlcv_data)}")
    except Exception as e:
        print(f"Veri çekme hatası: {e}")
        return df

    if len(ohlcv_data) > 0:
        new_df = pd.DataFrame(ohlcv_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
        new_df = new_df[:] if not df.empty else new_df  # Eğer df zaten veri içeriyorsa son satırı kaldır

        df = pd.concat([df, new_df], ignore_index=True) if not df.empty else new_df

        df['symbol'] = symbol
        
        # timestamp değerlerini datetime'a dönüştürme ve formatlama
        def safe_to_datetime(t):
            if isinstance(t, str):
                return pd.to_datetime(t)
            elif isinstance(t, (int, float)) and t > 1e10:
                unit = 'ms' if t > 1e12 else 's'
                return pd.to_datetime(t, unit=unit, utc=True).tz_convert('US/Eastern')
            else:
                raise ValueError(f"Unexpected timestamp format: {t}")

        df['timestamp'] = df['timestamp'].apply(safe_to_datetime)
        df['timestamp'] = df['timestamp'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S') if pd.notna(x) else None)

        # Gösterge hesaplamaları
        df['rsi'] = ta.rsi(df['close'], length=14)
        bbands = ta.bbands(df['close'], length=20, std=2)
        for col in bbands.columns:
            df[col] = bbands[col]
        df['bollinger_flag'] = np.where(df['close'] > df['BBU_20_2.0'], 1, np.where(df['close'] < df['BBL_20_2.0'], -1, 0))
        
        stoch = ta.stoch(df['high'], df['low'], df['close'], k=14, d=3)
        df['stoch_k'] = stoch['STOCHk_14_3_3']
        df['stoch_d'] = stoch['STOCHd_14_3_3']
        df['stochastic_flag'] = np.where(df['stoch_k'] < 20, -1, np.where(df['stoch_k'] > 80, 1, 0))
        
        macd = ta.macd(df['close'])
        for col in macd.columns:
            df[col] = macd[col]
        df['macd_signal'] = np.where((df['MACD_12_26_9'].shift(1) < df['MACDs_12_26_9'].shift(1)) & 
                                      (df['MACD_12_26_9'] > df['MACDs_12_26_9']), 1, 
                                      np.where((df['MACD_12_26_9'].shift(1) > df['MACDs_12_26_9'].shift(1)) & 
                                               (df['MACD_12_26_9'] < df['MACDs_12_26_9']), -1, 0))

        df.to_csv(filepath, index=False)

    return df

def train_and_evaluate_model(df):
    global correct_predictions, total_predictions, experience_replay_buffer

    # Öznitelik ve hedef değişkenleri
    X = df.drop(['timestamp', 'symbol', 'close'], axis=1)
    y = np.where(df['close'].shift(-1) > df['close'], 1, 0)[:-1]
    X = X[:-1]

    # Deneyim havuzuna ekleme
    for i in range(len(X)):
        experience_replay_buffer.append((X.iloc[i], y[i]))

    # Rastgele örnekleme (Experience Replay)
    if len(experience_replay_buffer) >= batch_size:
        batch = random.sample(experience_replay_buffer, batch_size)
        X_batch, y_batch = zip(*batch)
        X_batch = pd.DataFrame(X_batch)
        y_batch = np.array(y_batch)

        # Veriyi eğitim ve test olarak ayırın
        X_train, X_test, y_train, y_test = train_test_split(X_batch, y_batch, test_size=0.2, random_state=42)

        # Aşırı örnekleme
        X_train, y_train = resample(X_train, y_train, replace=True, n_samples=len(X_train), random_state=42)

        # Model oluşturma
        model = GradientBoostingClassifier(learning_rate=learning_rate, n_estimators=100, max_depth=3, random_state=42)
        model.fit(X_train, y_train)

        # Modelin test verisi üzerindeki tahminleri
        y_pred = model.predict(X_test)

        f1 = f1_score(y_test, y_pred)
        predictions_history.append(y_pred)

        # Modelin sonuçları
        correct_predictions += np.sum(y_pred == y_test)
        total_predictions += len(y_test)

        # Tahminin kaydedilmesi
        joblib.dump(model, 'trading_model.pkl')
        logging.info(f"Model başarıyla kaydedildi. F1 Skoru: {f1:.2f}")

while True:
    # Geçerli zaman
    current_time = datetime.now(timezone.utc).astimezone()

    # Bekleme süresi hesapla
    wait_time = remaining_time(current_time, timeframe)
    logging.info(f"Kalan süre: {wait_time} saniye")

    # Veriyi güncelle
    for symbol in symbols:
        df = fetch_and_update_data(symbol, timeframe, limit)

        # Veriler yeterliyse eğit ve değerlendir
        if len(df) >= 100:  # 100'den fazla veri noktası varsa
            train_and_evaluate_model(df)

    # Belirli bir süre bekle
    time.sleep(wait_time)  # Burada belirlenen bekleme süresince uykuya geçilir
Editor is loading...
Leave a Comment