import numpy as np import pandas as pd import talib from binance.client import Client from binance.enums import * import time # Binance API anahtarlarınızı buraya ekleyin API_KEY = 'your_api_key' API_SECRET = 'your_api_secret' client = Client(API_KEY, API_SECRET) client.FUTURES_API_URL = 'https://testnet.binancefuture.com/fapi/v1/' # Testnet URL def fetch_klines(symbol, interval, lookback): klines = client.get_klines(symbol=symbol, interval=interval, limit=lookback) df = pd.DataFrame(klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore']) df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') df.set_index('timestamp', inplace=True) df = df.astype(float) return df def calculate_support_resistance(df): high = df['high'].max() low = df['low'].min() return high, low def calculate_fibonacci_levels(df): high, low = calculate_support_resistance(df) diff = high - low levels = { '0.0': high, '23.6': high - 0.236 * diff, '38.2': high - 0.382 * diff, '50.0': high - 0.5 * diff, '61.8': high - 0.618 * diff, '100.0': low } return levels def calculate_trend_direction(df): df['SMA_20'] = talib.SMA(df['close'], timeperiod=20) df['SMA_50'] = talib.SMA(df['close'], timeperiod=50) if df['SMA_20'].iloc[-1] > df['SMA_50'].iloc[-1]: return "uptrend" elif df['SMA_20'].iloc[-1] < df['SMA_50'].iloc[-1]: return "downtrend" else: return "sideways" def check_breakout(df, levels): last_close = df['close'].iloc[-1] if last_close > levels['50.0']: return "breakout_up" elif last_close < levels['50.0']: return "breakout_down" else: return "no_breakout" def check_buying_power(df): df['volume_ma'] = df['volume'].rolling(window=20).mean() last_volume = df['volume'].iloc[-1] average_volume = df['volume_ma'].iloc[-1] if last_volume > 1.5 * average_volume: return "strong_buying" elif last_volume < 0.5 * average_volume: return "weak_buying" else: return "normal" def get_current_position(symbol): positions = client.futures_position_information(symbol=symbol) for position in positions: if float(position['positionAmt']) != 0: return position return None def close_position(symbol, position): side = SIDE_SELL if float(position['positionAmt']) > 0 else SIDE_BUY quantity = abs(float(position['positionAmt'])) client.futures_create_order( symbol=symbol, side=side, type=ORDER_TYPE_MARKET, quantity=quantity ) def place_order(symbol, side, quantity, price): order = client.futures_create_order( symbol=symbol, side=side, type=ORDER_TYPE_LIMIT, quantity=quantity, price=price, timeInForce=TIME_IN_FORCE_GTC ) return order def strategy(symbol): df = fetch_klines(symbol, '15m', 100) levels = calculate_fibonacci_levels(df) trend_direction = calculate_trend_direction(df) breakout_status = check_breakout(df, levels) buying_power = check_buying_power(df) last_close = df['close'].iloc[-1] current_position = get_current_position(symbol) if current_position: if trend_direction == "uptrend" and breakout_status == "breakout_up" and buying_power == "strong_buying": if float(current_position['positionAmt']) <= 0: print("Pozisyon kapatılıyor ve uzun pozisyon açılıyor") close_position(symbol, current_position) place_order(symbol, SIDE_BUY, 1, last_close) # 1 kontrat örnek miktar elif trend_direction == "downtrend" and breakout_status == "breakout_down" and buying_power == "strong_buying": if float(current_position['positionAmt']) >= 0: print("Pozisyon kapatılıyor ve kısa pozisyon açılıyor") close_position(symbol, current_position) place_order(symbol, SIDE_SELL, 1, last_close) # 1 kontrat örnek miktar else: if trend_direction == "uptrend" and breakout_status == "breakout_up" and buying_power == "strong_buying": print("Uzun pozisyon açılıyor") place_order(symbol, SIDE_BUY, 1, last_close) # 1 kontrat örnek miktar elif trend_direction == "downtrend" and breakout_status == "breakout_down" and buying_power == "strong_buying": print("Kısa pozisyon açılıyor") place_order(symbol, SIDE_SELL, 1, last_close) # 1 kontrat örnek miktar if __name__ == "__main__": symbol = 'BTCUSDT' # Testnet üzerinde kullanılacak sembol while True: strategy(symbol) time.sleep(900) # 15 dakika (900 saniye) bekle