Files
Place-Order-Trading-Bot/Position_Control_Patch.py
T

107 lines
4.2 KiB
Python

"""
QUICK FIX: Position Control Patch
Füge diese Funktionen zu deinem bestehenden TradingBot_V1.4_Complete.ipynb hinzu
"""
def check_existing_positions_simple(symbol="XAUUSD", strategy_name="TradingBot_V1.4_Complete"):
"""
Einfache Position-Überprüfung für bestehende Notebooks
"""
try:
positions = mt.positions_get(symbol=symbol)
if positions is None:
return False, 0
# Zähle Positionen mit unserem Strategy-Namen
strategy_positions = 0
for pos in positions:
if strategy_name in pos.comment:
strategy_positions += 1
return strategy_positions > 0, strategy_positions
except:
return False, 0
def execute_trade_v2_with_simple_position_control(
symbol="XAUUSD",
atr_mult=1.5,
base_confidence=70,
max_risk_per_trade=0.01,
risk_filter=True,
min_atr=0.0010,
use_pullback_entry=True,
debug=True
):
"""
QUICK FIX: Füge diese Funktion zu deinem bestehenden Notebook hinzu
Ersetzt deine bestehende execute_trade_v2() Funktion
"""
# WICHTIG: Position-Check ZUERST!
has_position, position_count = check_existing_positions_simple(symbol, strategy_name)
if has_position:
if debug:
print(f"🛑 TRADE BLOCKIERT: {position_count} Position(en) bereits aktiv für {symbol}")
# Zeige bestehende Positionen
positions = mt.positions_get(symbol=symbol)
if positions:
for pos in positions:
if strategy_name in pos.comment:
profit_emoji = "🟢" if pos.profit >= 0 else "🔴"
print(f" Aktive Position: {'BUY' if pos.type == 0 else 'SELL'} {pos.volume} @ {pos.price_open} | Profit: {profit_emoji} {pos.profit:.2f}")
return None
print(f"✅ Position-Check OK: Keine aktiven Positionen für {symbol}")
# AB HIER: Deine bestehende execute_trade_v2 Logik
# (Kopiere deine bestehende Funktion hier hin, aber mit Position-Check am Anfang)
# Vereinfachte Version für Demonstration:
signal_info = extended_top_down_v2(symbol)
if signal_info is None:
print("❌ Signal-Analyse fehlgeschlagen")
return None
entry_signal = signal_info["entry_signal"]
confidence = signal_info["confidence"]
adaptive_threshold = signal_info["adaptive_threshold"]
if entry_signal == 0:
print(f"⏸️ No entry signal (Confidence: {confidence}%, Threshold: {adaptive_threshold}%)")
return None
if confidence < adaptive_threshold:
print(f"⏸️ Confidence too low: {confidence}% < {adaptive_threshold}%")
return None
# FINAL POSITION CHECK vor Order (Sicherheit)
final_check, _ = check_existing_positions_simple(symbol, strategy_name)
if final_check:
print(f"🛑 LAST-MINUTE BLOCK: Position wurde zwischen Checks eröffnet!")
return None
# Hier würde deine normale Trading-Logik kommen
print(f"🚀 WOULD EXECUTE TRADE: {'LONG' if entry_signal == 1 else 'SHORT'}")
print(f"Confidence: {confidence}% | Quality: {signal_info['signal_quality']}")
# Für echte Ausführung: Uncomment die nächste Zeile und füge deine market_order Logik hinzu
# return market_order(symbol, volume, order_type, stoploss, take_profit)
return "DEMO_ORDER_RESULT" # Placeholder für Demo
print("✅ Simple Position Control Patch ready")
# ANLEITUNG FÜR DEIN BESTEHENDES NOTEBOOK:
print("\\n📋 ANLEITUNG für dein bestehendes Notebook:")
print("1. Kopiere check_existing_positions_simple() in eine neue Zelle")
print("2. Ersetze deine execute_trade_v2() durch execute_trade_v2_with_simple_position_control()")
print("3. Oder füge am Anfang deiner bestehenden execute_trade_v2() den Position-Check hinzu:")
print("\\n # Position-Check am Anfang der Funktion:")
print(" has_position, position_count = check_existing_positions_simple(symbol, strategy_name)")
print(" if has_position:")
print(" print(f'🛑 TRADE BLOCKIERT: {position_count} Position(en) aktiv')")
print(" return None")
print("\\n4. Fertig! Dein Bot handelt jetzt maximal 1x gleichzeitig.")