import pandas as pd import numpy as np from binance.client import Client from…
- User
import pandas as pd import numpy as np from binance.client import Client from binance.exceptions import BinanceAPIException import time import logging import traceback from concurrent.futures import ThreadPoolExecutor
Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(name)
class PriceActionTradingBot: def init(self, api_key, api_secret, symbol, interval, lookback, quantity): self.client = Client(api_key, api_secret) self.symbol = symbol self.interval = interval self.lookback = lookback self.quantity = quantity self.df = None self.position = None
def get_historical_data(self): try: bars = self.client.get_historical_klines(self.symbol, self.interval, self.lookback + " ago UTC") self.df = pd.DataFrame(bars, 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']) self.df['timestamp'] = pd.to_datetime(self.df['timestamp'], unit='ms') for col in ['open', 'high', 'low', 'close', 'volume']: self.df[col] = self.df[col].astype(float) self.df.set_index('timestamp', inplace=True) except BinanceAPIException as e: logger.error(f"Error fetching historical data: {e}") def price_action_analysis(self, df): try: recent_df = df.iloc[-100:] # Increased lookback for more robust analysis recent_df.dropna(inplace=True) if len(recent_df) < 20: return None highs = recent_df['high'].values lows = recent_df['low'].values closes = recent_df['close'].values # Trend Analysis short_term_trend = np.polyfit(range(20), closes[-20:], 1)[0] medium_term_trend = np.polyfit(range(50), closes[-50:], 1)[0] long_term_trend = np.polyfit(range(100), closes[-100:], 1)[0] # Support and Resistance support = np.min(lows[-20:]) resistance = np.max(highs[-20:]) # Volatility volatility = np.std(closes[-20:]) # Price Action Patterns bullish_engulfing = (closes[-1] > opens[-1]) and (closes[-2] < opens[-2]) and (closes[-1] > opens[-2]) and (closes[-1] - opens[-1] > opens[-2] - closes[-2]) bearish_engulfing = (closes[-1] < opens[-1]) and (closes[-2] > opens[-2]) and (closes[-1] < opens[-2]) and (opens[-1] - closes[-1] > closes[-2] - opens[-2]) pin_bar_bullish = (highs[-1] - max(opens[-1], closes[-1])) > 2 * abs(opens[-1] - closes[-1]) and (min(opens[-1], closes[-1]) - lows[-1]) < abs(opens[-1] - closes[-1]) pin_bar_bearish = (max(opens[-1], closes[-1]) - lows[-1]) > 2 * abs(opens[-1] - closes[-1]) and (highs[-1] - min(opens[-1], closes[-1])) < abs(opens[-1] - closes[-1]) # Inside bar inside_bar = highs[-1] < highs[-2] and lows[-1] > lows[-2] # Decision making if short_term_trend > 0 and medium_term_trend > 0 and long_term_trend > 0: if closes[-1] > resistance or bullish_engulfing or pin_bar_bullish: signal = 'STRONG_BUY' elif inside_bar: signal = 'POTENTIAL_BREAKOUT' else: signal = 'WEAK_BUY' elif short_term_trend < 0 and medium_term_trend < 0 and long_term_trend < 0: if closes[-1] < support or bearish_engulfing or pin_bar_bearish: signal = 'STRONG_SELL' elif inside_bar: signal = 'POTENTIAL_BREAKDOWN' else: signal = 'WEAK_SELL' else: signal = 'NO_SIGNAL' last_row = recent_df.iloc[-1].copy() last_row['Signal'] = signal last_row['Volatility'] = volatility last_row['Support'] = support last_row['Resistance'] = resistance return last_row.to_frame().T except Exception as e: logger.error(f"Error in price action analysis: {e}\n{traceback.format_exc()}") return None def advanced_price_action_analysis(self, df): try: recent_df = df.iloc[-200:] # Extended lookback for more comprehensive analysis recent_df.dropna(inplace=True) if len(recent_df) < 50: return None highs = recent_df['high'].values lows = recent_df['low'].values closes = recent_df['close'].values opens = recent_df['open'].values # Swing High and Swing Low detection swing_high = np.where((highs[1:-1] > highs[:-2]) & (highs[1:-1] > highs[2:]))[0] + 1 swing_low = np.where((lows[1:-1] < lows[:-2]) & (lows[1:-1] < lows[2:]))[0] + 1 # Higher Highs and Lower Lows higher_highs = len(swing_high) >= 2 and highs[swing_high[-1]] > highs[swing_high[-2]] lower_lows = len(swing_low) >= 2 and lows[swing_low[-1]] < lows[swing_low[-2]] # Fibonacci Retracement if len(swing_high) > 0 and len(swing_low) > 0: fib_high = highs[swing_high[-1]] fib_low = lows[swing_low[-1]] fib_diff = fib_high - fib_low fib_levels = [fib_low + l * fib_diff for l in [0.236, 0.382, 0.5, 0.618, 0.786]] current_price = closes[-1] fib_support = max([l for l in fib_levels if l < current_price], default=None) fib_resistance = min([l for l in fib_levels if l > current_price], default=None) else: fib_support = fib_resistance = None # Volume Analysis volume = recent_df['volume'].values avg_volume = np.mean(volume[-20:]) volume_trend = 'UP' if volume[-1] > avg_volume else 'DOWN' # Momentum momentum = closes[-1] - closes[-20] # Decision making if higher_highs and momentum > 0: if closes[-1] > fib_resistance and volume_trend == 'UP': signal = 'STRONG_BUY' elif closes[-1] > fib_resistance: signal = 'BUY' else: signal = 'WEAK_BUY' elif lower_lows and momentum < 0: if closes[-1] < fib_support and volume_trend == 'UP': signal = 'STRONG_SELL' elif closes[-1] < fib_support: signal = 'SELL' else: signal = 'WEAK_SELL' else: signal = 'NO_SIGNAL' last_row = recent_df.iloc[-1].copy() last_row['Signal'] = signal last_row['Volume_Trend'] = volume_trend last_row['Momentum'] = momentum last_row['Fib_Support'] = fib_support last_row['Fib_Resistance'] = fib_resistance return last_row.to_frame().T except Exception as e: logger.error(f"Error in advanced price action analysis: {e}\n{traceback.format_exc()}") return None def execute_trade(self, side, quantity): try: if side == 'BUY': order = self.client.create_order(symbol=self.symbol, side=Client.SIDE_BUY, type=Client.ORDER_TYPE_MARKET, quantity=quantity) else: order = self.client.create_order(symbol=self.symbol, side=Client.SIDE_SELL, type=Client.ORDER_TYPE_MARKET, quantity=quantity) logger.info(f"Order executed: {order}") except Exception as e: logger.error(f"An error occurred while executing trade: {e}") def run_bot(self): while True: try: self.get_historical_data() with ThreadPoolExecutor(max_workers=2) as executor: future_pa = executor.submit(self.price_action_analysis, self.df) future_apa = executor.submit(self.advanced_price_action_analysis, self.df) pa_result = future_pa.result() apa_result = future_apa.result() if pa_result is not None and apa_result is not None: pa_signal = pa_result.iloc[-1]['Signal'] apa_signal = apa_result.iloc[-1]['Signal'] if pa_signal in ['STRONG_BUY', 'BUY'] and apa_signal in ['STRONG_BUY', 'BUY']: self.execute_trade('BUY', self.quantity) elif pa_signal in ['STRONG_SELL', 'SELL'] and apa_signal in ['STRONG_SELL', 'SELL']: self.execute_trade('SELL', self.quantity) time.sleep(60) # Wait for 1 minute before next iteration except Exception as e: logger.error(f"Error in main loop: {e}\n{traceback.format_exc()}") time.sleep(60) # Wait for 1 minute before retrying - Assistant
Estimating the accuracy of a trading bot's signals is inherently difficult without testing the bot in real market conditions over a significant period. However, I can provide some insights based on the features and strategies implemented in your bot:
-
Signal Types and Features:
-
- User
What can I do to increase the accuracy to 75-85%
- Assistant