{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "4cb5824d", "metadata": {}, "outputs": [], "source": [ "# 📦 Imports\n", "import MetaTrader5 as mt\n", "import pandas as pd\n", "import sqlite3 as db\n", "from datetime import datetime\n", "import time\n", "from scipy.signal import savgol_filter\n", "import pandas_ta as ta\n", "import keyring as kr" ] }, { "cell_type": "code", "execution_count": 3, "id": "1279652c", "metadata": {}, "outputs": [], "source": [ "# 📊 Verbindung zu SQLite\n", "def init_db(db_name=\"trading_log.db\"):\n", " conn = db.connect(db_name)\n", " c = conn.cursor()\n", " c.execute(\"\"\"\n", " CREATE TABLE IF NOT EXISTS trade_log (\n", " timestamp TEXT,\n", " symbol TEXT,\n", " order_type TEXT,\n", " price REAL,\n", " sl REAL,\n", " tp REAL,\n", " volume REAL,\n", " signal INTEGER,\n", " comment TEXT\n", " )\n", " \"\"\")\n", " conn.commit()\n", " return conn" ] }, { "cell_type": "code", "execution_count": 7, "id": "fc1eb959", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 🔐 MT5 Login einmalig initialisieren\n", "mt.initialize()\n", "login = 10800246\n", "server = \"VantageInternational-Demo\"\n", "password = kr.get_password(server, str(login))\n", "mt.login(login, password, server)" ] }, { "cell_type": "code", "execution_count": 8, "id": "840d41c6", "metadata": {}, "outputs": [], "source": [ "# 📥 MT5 Daten abrufen\n", "def get_mt5_data(symbol=\"XAUUSD\", timeframe=mt.TIMEFRAME_M5, n_bars=500):\n", " rates = mt.copy_rates_from_pos(symbol, timeframe, 0, n_bars)\n", " df = pd.DataFrame(rates)\n", " df[\"time\"] = pd.to_datetime(df[\"time\"], unit=\"s\")\n", " return df" ] }, { "cell_type": "code", "execution_count": 9, "id": "48445ef4", "metadata": {}, "outputs": [], "source": [ "df = get_mt5_data()" ] }, { "cell_type": "code", "execution_count": 11, "id": "964f927f", "metadata": {}, "outputs": [], "source": [ "# 🤖 Signale erzeugen\n", "def generate_signal(df):\n", " df[\"ema10\"] = df[\"close\"].ewm(span=10).mean()\n", " df[\"ema30\"] = df[\"close\"].ewm(span=30).mean()\n", " df[\"rsi\"] = ta.rsi(df[\"close\"], length=14)\n", " df[\"trend\"] = savgol_filter(df[\"close\"], 15, 3)\n", " df[\"signal\"] = 0\n", "\n", " for i in range(1, len(df)):\n", " if (\n", " df[\"ema10\"].iloc[i] > df[\"ema30\"].iloc[i]\n", " and df[\"ema10\"].iloc[i - 1] <= df[\"ema30\"].iloc[i - 1]\n", " and df[\"rsi\"].iloc[i] < 70\n", " and df[\"trend\"].iloc[i] > df[\"trend\"].iloc[i - 1]\n", " ):\n", " df.at[i, \"signal\"] = 1\n", " elif (\n", " df[\"ema10\"].iloc[i] < df[\"ema30\"].iloc[i]\n", " and df[\"ema10\"].iloc[i - 1] >= df[\"ema30\"].iloc[i - 1]\n", " and df[\"rsi\"].iloc[i] > 30\n", " and df[\"trend\"].iloc[i] < df[\"trend\"].iloc[i - 1]\n", " ):\n", " df.at[i, \"signal\"] = -1\n", " return df" ] }, { "cell_type": "code", "execution_count": 12, "id": "7494a5da", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
timeopenhighlowclosetick_volumespreadreal_volumeema10ema30rsitrendsignal
02025-07-17 04:30:003340.743341.463337.213338.0412931803338.0400003338.040000NaN3338.2083860
12025-07-17 04:35:003338.033341.033337.723341.039641803339.6845003339.584833NaN3340.3778740
22025-07-17 04:40:003341.033341.483339.963341.269441803340.3178413340.180848NaN3341.8738820
32025-07-17 04:45:003341.273343.453341.273342.4710991803341.0268813340.811593NaN3342.7858600
42025-07-17 04:50:003342.473343.613342.203342.8410701803341.5473783341.273104NaN3343.2032600
..........................................
4952025-07-18 22:45:003349.293349.713348.473348.789281903350.0249623351.23273534.9351153348.4052470
4962025-07-18 22:50:003348.793348.803347.623348.158591903349.6840603351.03384932.2909093348.1866350
4972025-07-18 22:55:003348.163348.533347.653348.2510281903349.4233223350.85424533.1557603348.1533240
4982025-07-18 23:00:003348.243348.883347.723348.065991903349.1754453350.67397232.3112853348.3467440
4992025-07-18 23:05:003348.063349.253347.923348.884501903349.1217283350.55823139.4760213348.8083270
\n", "

500 rows × 13 columns

\n", "
" ], "text/plain": [ " time open high low close tick_volume \\\n", "0 2025-07-17 04:30:00 3340.74 3341.46 3337.21 3338.04 1293 \n", "1 2025-07-17 04:35:00 3338.03 3341.03 3337.72 3341.03 964 \n", "2 2025-07-17 04:40:00 3341.03 3341.48 3339.96 3341.26 944 \n", "3 2025-07-17 04:45:00 3341.27 3343.45 3341.27 3342.47 1099 \n", "4 2025-07-17 04:50:00 3342.47 3343.61 3342.20 3342.84 1070 \n", ".. ... ... ... ... ... ... \n", "495 2025-07-18 22:45:00 3349.29 3349.71 3348.47 3348.78 928 \n", "496 2025-07-18 22:50:00 3348.79 3348.80 3347.62 3348.15 859 \n", "497 2025-07-18 22:55:00 3348.16 3348.53 3347.65 3348.25 1028 \n", "498 2025-07-18 23:00:00 3348.24 3348.88 3347.72 3348.06 599 \n", "499 2025-07-18 23:05:00 3348.06 3349.25 3347.92 3348.88 450 \n", "\n", " spread real_volume ema10 ema30 rsi trend \\\n", "0 18 0 3338.040000 3338.040000 NaN 3338.208386 \n", "1 18 0 3339.684500 3339.584833 NaN 3340.377874 \n", "2 18 0 3340.317841 3340.180848 NaN 3341.873882 \n", "3 18 0 3341.026881 3340.811593 NaN 3342.785860 \n", "4 18 0 3341.547378 3341.273104 NaN 3343.203260 \n", ".. ... ... ... ... ... ... \n", "495 19 0 3350.024962 3351.232735 34.935115 3348.405247 \n", "496 19 0 3349.684060 3351.033849 32.290909 3348.186635 \n", "497 19 0 3349.423322 3350.854245 33.155760 3348.153324 \n", "498 19 0 3349.175445 3350.673972 32.311285 3348.346744 \n", "499 19 0 3349.121728 3350.558231 39.476021 3348.808327 \n", "\n", " signal \n", "0 0 \n", "1 0 \n", "2 0 \n", "3 0 \n", "4 0 \n", ".. ... \n", "495 0 \n", "496 0 \n", "497 0 \n", "498 0 \n", "499 0 \n", "\n", "[500 rows x 13 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "generate_signal(df)" ] }, { "cell_type": "code", "execution_count": 14, "id": "0a8abc07", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.01" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mt.symbol_info('XAUUSD').point" ] }, { "cell_type": "code", "execution_count": null, "id": "64757821", "metadata": {}, "outputs": [], "source": [ "# 📤 Order senden\n", "def send_market_order(symbol, volume, order_type, signal, conn, sl_pips=20, tp_pips=40, magic=1001):\n", " tick = mt.symbol_info_tick(symbol)\n", " price = tick.ask if order_type == mt.ORDER_TYPE_BUY else tick.bid\n", " point = mt.symbol_info(symbol).point\n", "\n", " sl = price - sl_pips * point if order_type == mt.ORDER_TYPE_BUY else price + sl_pips * point\n", " tp = price + tp_pips * point if order_type == mt.ORDER_TYPE_BUY else price - tp_pips * point\n", "\n", " request = {\n", " \"action\": mt.TRADE_ACTION_DEAL,\n", " \"symbol\": symbol,\n", " \"volume\": volume,\n", " \"type\": order_type,\n", " \"price\": price,\n", " \"sl\": round(sl, 5),\n", " \"tp\": round(tp, 5),\n", " \"deviation\": 20,\n", " \"magic\": magic,\n", " \"comment\": \"AutoSignalBot\",\n", " \"type_time\": mt.ORDER_TIME_GTC,\n", " \"type_filling\": mt.ORDER_FILLING_IOC,\n", " }\n", "\n", " result = mt.order_send(request)\n", " print(f\"[TRADE] {symbol} - {'BUY' if order_type==0 else 'SELL'} @ {price} | SL: {sl} | TP: {tp}\")\n", "\n", " # Logging in DB\n", " c = conn.cursor()\n", " c.execute(\"\"\"\n", " INSERT INTO trade_log (timestamp, symbol, order_type, price, sl, tp, volume, signal, comment)\n", " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\n", " \"\"\", (datetime.now(), symbol, \"BUY\" if order_type==0 else \"SELL\", price, sl, tp, volume, signal, \"AutoSignalBot\"))\n", " conn.commit()\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 13, "id": "3bc5f11e", "metadata": {}, "outputs": [], "source": [ "# 🔁 Hauptfunktion\n", "def run_live_trading(symbol=\"XAUUSD\", interval=300, volume=0.1, db_name=\"trading_log.db\"):\n", " conn = init_db(db_name)\n", " last_signal = 0\n", "\n", " print(f\"✅ Starte Auto-Trading für {symbol} – Intervall {interval}s\")\n", " while True:\n", " df = get_mt5_data(symbol)\n", " df = generate_signal(df)\n", " signal = df[\"signal\"].iloc[-1]\n", "\n", " if signal != 0 and signal != last_signal:\n", " order_type = mt.ORDER_TYPE_BUY if signal == 1 else mt.ORDER_TYPE_SELL\n", " send_market_order(symbol, volume, order_type, signal, conn)\n", " last_signal = signal\n", " else:\n", " print(f\"[{datetime.now()}] Kein neues Signal ({signal})\")\n", "\n", " time.sleep(interval)" ] }, { "cell_type": "code", "execution_count": 22, "id": "71acdb0d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Starte Auto-Trading für XAUUSD – Intervall 300s\n", "[2025-07-18 12:11:30.004688] Kein neues Signal (0)\n", "[2025-07-18 12:16:30.083605] Kein neues Signal (0)\n", "[2025-07-18 12:21:30.121793] Kein neues Signal (0)\n", "[2025-07-18 12:26:30.174396] Kein neues Signal (0)\n", "[2025-07-18 12:31:30.216996] Kein neues Signal (0)\n", "[2025-07-18 12:36:30.258849] Kein neues Signal (0)\n", "[2025-07-18 12:41:30.313072] Kein neues Signal (0)\n", "[2025-07-18 12:46:30.356620] Kein neues Signal (0)\n", "[2025-07-18 12:51:30.396600] Kein neues Signal (0)\n", "[2025-07-18 12:56:30.442114] Kein neues Signal (0)\n", "[2025-07-18 13:01:30.481155] Kein neues Signal (0)\n", "[2025-07-18 13:06:30.530619] Kein neues Signal (0)\n", "[2025-07-18 13:11:30.566248] Kein neues Signal (0)\n", "[2025-07-18 13:16:30.606701] Kein neues Signal (0)\n", "[2025-07-18 13:21:30.645462] Kein neues Signal (0)\n", "[2025-07-18 13:26:30.686722] Kein neues Signal (0)\n", "[2025-07-18 13:31:31.130100] Kein neues Signal (0)\n", "[2025-07-18 13:36:31.193825] Kein neues Signal (0)\n", "[2025-07-18 13:41:31.381330] Kein neues Signal (0)\n", "[2025-07-18 13:46:31.434187] Kein neues Signal (0)\n", "[2025-07-18 13:51:31.471695] Kein neues Signal (0)\n", "[2025-07-18 13:56:31.507322] Kein neues Signal (0)\n", "[2025-07-18 14:01:31.550436] Kein neues Signal (0)\n", "[2025-07-18 14:06:31.587884] Kein neues Signal (0)\n", "[2025-07-18 14:11:31.665002] Kein neues Signal (0)\n", "[2025-07-18 14:16:31.704012] Kein neues Signal (0)\n", "[2025-07-18 14:21:31.745292] Kein neues Signal (0)\n", "[2025-07-18 14:26:31.783905] Kein neues Signal (0)\n", "[2025-07-18 14:31:31.822325] Kein neues Signal (0)\n", "[2025-07-18 14:36:31.873271] Kein neues Signal (0)\n", "[2025-07-18 14:41:31.914303] Kein neues Signal (0)\n", "[2025-07-18 14:46:31.951585] Kein neues Signal (0)\n", "[2025-07-18 14:51:32.005271] Kein neues Signal (0)\n", "[2025-07-18 14:56:32.046384] Kein neues Signal (0)\n", "[2025-07-18 15:01:32.083259] Kein neues Signal (0)\n", "[2025-07-18 15:06:32.132835] Kein neues Signal (0)\n", "[2025-07-18 15:11:32.176714] Kein neues Signal (0)\n", "[2025-07-18 15:16:32.221857] Kein neues Signal (0)\n", "[2025-07-18 15:21:32.269876] Kein neues Signal (0)\n", "[2025-07-18 15:26:32.313363] Kein neues Signal (0)\n", "[2025-07-18 15:31:32.356954] Kein neues Signal (0)\n", "[2025-07-18 15:36:32.403653] Kein neues Signal (0)\n", "[2025-07-18 15:41:32.449777] Kein neues Signal (0)\n", "[2025-07-18 15:46:32.489053] Kein neues Signal (0)\n", "[TRADE] XAUUSD - SELL @ 3354.56 | SL: 3354.7599999999998 | TP: 3354.16\n", "[2025-07-18 15:56:32.609729] Kein neues Signal (0)\n", "[2025-07-18 16:01:32.650996] Kein neues Signal (0)\n", "[TRADE] XAUUSD - BUY @ 3358.95 | SL: 3358.75 | TP: 3359.35\n", "[2025-07-18 16:11:32.744972] Kein neues Signal (0)\n", "[2025-07-18 16:16:32.791366] Kein neues Signal (0)\n", "[2025-07-18 16:21:32.829930] Kein neues Signal (0)\n", "[2025-07-18 16:26:32.871934] Kein neues Signal (0)\n", "[2025-07-18 16:31:32.912571] Kein neues Signal (0)\n", "[2025-07-18 16:36:32.954745] Kein neues Signal (0)\n", "[2025-07-18 16:41:32.994281] Kein neues Signal (0)\n", "[2025-07-18 16:46:33.048113] Kein neues Signal (0)\n", "[2025-07-18 16:51:33.086281] Kein neues Signal (0)\n", "[2025-07-18 16:56:33.127117] Kein neues Signal (0)\n", "[2025-07-18 17:01:33.177813] Kein neues Signal (0)\n", "[2025-07-18 17:06:33.219713] Kein neues Signal (0)\n", "[2025-07-18 17:11:33.260138] Kein neues Signal (0)\n", "[2025-07-18 17:16:33.302870] Kein neues Signal (0)\n", "[2025-07-18 17:21:33.346259] Kein neues Signal (0)\n", "[2025-07-18 17:26:33.390947] Kein neues Signal (0)\n", "[2025-07-18 17:31:33.434736] Kein neues Signal (0)\n", "[2025-07-18 17:36:33.476901] Kein neues Signal (0)\n", "[2025-07-18 17:41:33.520435] Kein neues Signal (0)\n", "[2025-07-18 17:46:33.575426] Kein neues Signal (0)\n", "[2025-07-18 17:51:33.622573] Kein neues Signal (0)\n", "[2025-07-18 17:56:33.662664] Kein neues Signal (0)\n", "[2025-07-18 18:01:33.703019] Kein neues Signal (0)\n", "[2025-07-18 18:06:33.748220] Kein neues Signal (0)\n", "[2025-07-18 18:11:33.788812] Kein neues Signal (0)\n", "[2025-07-18 18:16:33.827200] Kein neues Signal (0)\n", "[2025-07-18 18:21:33.880825] Kein neues Signal (0)\n", "[2025-07-18 18:26:33.931042] Kein neues Signal (0)\n", "[2025-07-18 18:31:33.982017] Kein neues Signal (0)\n", "[2025-07-18 18:36:34.021865] Kein neues Signal (1)\n", "[2025-07-18 18:41:34.059505] Kein neues Signal (0)\n", "[2025-07-18 18:46:34.100395] Kein neues Signal (0)\n", "[2025-07-18 18:51:34.148794] Kein neues Signal (0)\n", "[2025-07-18 18:56:34.186791] Kein neues Signal (0)\n", "[2025-07-18 19:01:34.227630] Kein neues Signal (0)\n", "[2025-07-18 19:06:34.272331] Kein neues Signal (0)\n", "[2025-07-18 19:11:34.309122] Kein neues Signal (0)\n", "[2025-07-18 19:16:34.350483] Kein neues Signal (0)\n", "[2025-07-18 19:21:34.391638] Kein neues Signal (0)\n", "[2025-07-18 19:26:34.430235] Kein neues Signal (0)\n", "[2025-07-18 19:31:34.476827] Kein neues Signal (0)\n", "[2025-07-18 19:36:34.516804] Kein neues Signal (0)\n", "[2025-07-18 19:41:34.551478] Kein neues Signal (0)\n", "[2025-07-18 19:46:34.600704] Kein neues Signal (0)\n", "[2025-07-18 19:51:34.657098] Kein neues Signal (0)\n", "[2025-07-18 19:56:34.694871] Kein neues Signal (0)\n", "[2025-07-18 20:01:34.750662] Kein neues Signal (0)\n", "[2025-07-18 20:06:34.786445] Kein neues Signal (0)\n", "[2025-07-18 20:11:34.941510] Kein neues Signal (0)\n", "[2025-07-18 20:16:34.997686] Kein neues Signal (0)\n", "[2025-07-18 20:21:35.041835] Kein neues Signal (0)\n", "[2025-07-18 20:26:35.082677] Kein neues Signal (0)\n", "[2025-07-18 20:31:35.135303] Kein neues Signal (0)\n", "[2025-07-18 20:36:35.174403] Kein neues Signal (0)\n", "[2025-07-18 20:41:35.211876] Kein neues Signal (0)\n", "[2025-07-18 20:46:35.254113] Kein neues Signal (0)\n", "[2025-07-18 20:51:35.297650] Kein neues Signal (0)\n", "[2025-07-18 20:56:35.338598] Kein neues Signal (0)\n", "[2025-07-18 21:01:35.378704] Kein neues Signal (0)\n", "[2025-07-18 21:06:35.421315] Kein neues Signal (0)\n", "[2025-07-18 21:11:35.446675] Kein neues Signal (0)\n", "[2025-07-18 21:16:35.501085] Kein neues Signal (0)\n", "[2025-07-18 21:21:35.547489] Kein neues Signal (0)\n", "[2025-07-18 21:26:35.600292] Kein neues Signal (0)\n", "[2025-07-18 21:31:35.642080] Kein neues Signal (0)\n", "[2025-07-18 21:36:35.681283] Kein neues Signal (0)\n", "[2025-07-18 21:41:35.719258] Kein neues Signal (0)\n", "[2025-07-18 21:46:35.756743] Kein neues Signal (0)\n", "[2025-07-18 21:51:35.802329] Kein neues Signal (0)\n", "[2025-07-18 21:56:35.849136] Kein neues Signal (0)\n", "[2025-07-18 22:01:35.907573] Kein neues Signal (0)\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[22], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# ▶️ Live-Trading starten (z. B. alle 5 Minuten)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m run_live_trading(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mXAUUSD\u001b[39m\u001b[38;5;124m\"\u001b[39m, interval\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m300\u001b[39m, volume\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0.1\u001b[39m)\n", "Cell \u001b[1;32mIn[13], line 19\u001b[0m, in \u001b[0;36mrun_live_trading\u001b[1;34m(symbol, interval, volume, db_name)\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdatetime\u001b[38;5;241m.\u001b[39mnow()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] Kein neues Signal (\u001b[39m\u001b[38;5;132;01m{\u001b[39;00msignal\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 19\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(interval)\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "# ▶️ Live-Trading starten (z. B. alle 5 Minuten)\n", "run_live_trading(\"XAUUSD\", interval=300, volume=0.1)" ] }, { "cell_type": "code", "execution_count": 13, "id": "a7675469", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
timestampsymbolorder_typepricesltpvolumesignalcomment
02025-07-18 16:06:32.701648XAUUSDBUY3358.953358.753359.350.1b'\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00'AutoSignalBot
12025-07-18 15:51:32.545794XAUUSDSELL3354.563354.763354.160.1b'\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff'AutoSignalBot
\n", "
" ], "text/plain": [ " timestamp symbol order_type price sl tp \\\n", "0 2025-07-18 16:06:32.701648 XAUUSD BUY 3358.95 3358.75 3359.35 \n", "1 2025-07-18 15:51:32.545794 XAUUSD SELL 3354.56 3354.76 3354.16 \n", "\n", " volume signal comment \n", "0 0.1 b'\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00' AutoSignalBot \n", "1 0.1 b'\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff' AutoSignalBot " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 🧾 Optional: SQLite-Datenbank anzeigen\n", "conn = db.connect(\"trading_log.db\")\n", "pd.read_sql(\"SELECT * FROM trade_log ORDER BY timestamp DESC LIMIT 10\", conn)" ] }, { "cell_type": "code", "execution_count": null, "id": "b4512a2c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ec808aea", "metadata": {}, "outputs": [], "source": [ "\n", "import numpy as np\n", "\n", "def calculate_fib_levels(df, lookback=20):\n", " swing_high = df['high'].rolling(lookback).max().iloc[-1]\n", " swing_low = df['low'].rolling(lookback).min().iloc[-1]\n", " diff = swing_high - swing_low\n", " levels = {\n", " '0.0': swing_low,\n", " '0.236': swing_high - 0.236 * diff,\n", " '0.382': swing_high - 0.382 * diff,\n", " '0.5': swing_high - 0.5 * diff,\n", " '0.618': swing_high - 0.618 * diff,\n", " '0.786': swing_high - 0.786 * diff,\n", " '1.0': swing_high\n", " }\n", " return levels\n", "\n", "def calculate_atr(df, period=14):\n", " df['H-L'] = df['high'] - df['low']\n", " df['H-C'] = abs(df['high'] - df['close'].shift())\n", " df['L-C'] = abs(df['low'] - df['close'].shift())\n", " df['TR'] = df[['H-L', 'H-C', 'L-C']].max(axis=1)\n", " df['ATR'] = df['TR'].rolling(period).mean()\n", " return df\n" ] }, { "cell_type": "code", "execution_count": null, "id": "09b3f9df", "metadata": {}, "outputs": [], "source": [ "\n", "def send_fib_trade(df, symbol, order_type, volume):\n", " fib = calculate_fib_levels(df)\n", " df = calculate_atr(df)\n", " atr = df['ATR'].iloc[-1]\n", " point = mt.symbol_info(symbol).point\n", " tick = mt.symbol_info_tick(symbol)\n", " price = tick.ask if order_type == mt.ORDER_TYPE_BUY else tick.bid\n", "\n", " sl = price - atr if order_type == mt.ORDER_TYPE_BUY else price + atr\n", " tp = price + 2 * atr if order_type == mt.ORDER_TYPE_BUY else price - 2 * atr\n", "\n", " request = {\n", " \"action\": mt.TRADE_ACTION_DEAL,\n", " \"symbol\": symbol,\n", " \"volume\": volume,\n", " \"type\": order_type,\n", " \"price\": price,\n", " \"sl\": round(sl, 5),\n", " \"tp\": round(tp, 5),\n", " \"deviation\": 20,\n", " \"magic\": 1010,\n", " \"comment\": \"FibATR_Trade\",\n", " \"type_time\": mt.ORDER_TIME_GTC,\n", " \"type_filling\": mt.ORDER_FILLING_IOC,\n", " }\n", " return mt.order_send(request)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8878e31f", "metadata": {}, "outputs": [], "source": [ "\n", "import matplotlib.pyplot as plt\n", "\n", "def plot_fib_levels(df, levels):\n", " plt.figure(figsize=(12, 6))\n", " plt.plot(df['close'], label='Close Price', alpha=0.5)\n", " last_index = df.index[-1]\n", " start_index = df.index[-50] if len(df) >= 50 else df.index[0]\n", "\n", " for level, price in levels.items():\n", " plt.hlines(price, start_index, last_index, label=f'Fibo {level}: {round(price, 2)}', linestyles='--')\n", "\n", " plt.title(\"Fibonacci Retracement Levels\")\n", " plt.xlabel(\"Zeit\")\n", " plt.ylabel(\"Preis\")\n", " plt.legend()\n", " plt.grid(True)\n", " plt.tight_layout()\n", " plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ffa9c58a", "metadata": {}, "outputs": [], "source": [ "\n", "def check_fib_entry(df, fib_levels, tolerance=0.01):\n", " current_price = df['close'].iloc[-1]\n", " level_price = fib_levels['0.382']\n", " diff = abs(current_price - level_price)\n", " fib_range = fib_levels['1.0'] - fib_levels['0.0']\n", " return diff < tolerance * fib_range\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }