{"id":2866,"date":"2025-07-04T21:31:03","date_gmt":"2025-07-04T19:31:03","guid":{"rendered":"https:\/\/store.market-trader.de\/?page_id=2866"},"modified":"2025-07-04T21:41:50","modified_gmt":"2025-07-04T19:41:50","slug":"selbst-programmiert-expert-advisor-moving-average-crossover","status":"publish","type":"page","link":"https:\/\/ea-marketplace.com\/de\/selbst-programmiert-expert-advisor-moving-average-crossover\/","title":{"rendered":"Schritt-f\u00fcr-Schritt: Eigenen EA in MQL5 schreiben"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Selbst-Programmiert Expert Advisor \u201eMoving Average Crossover\u201c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Einf\u00fchrung<\/h3>\n\n\n\n<p>Dieser Expert Advisor (EA) <strong>\u201eMA_Cross\u201c<\/strong> f\u00fcr MetaTrader 5 automatisiert das Erkennen und Handeln eines Trendfolge-Setups auf Basis zweier exponentieller gleitender Durchschnitte (EMA) und der Average True Range (ATR). <br><br>Er veranschaulicht, wie man in MQL5:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Indikatoren<\/strong> erzeugt und ausliest<\/li>\n\n\n\n<li><strong>Signale<\/strong> (Crossover und Volatilit\u00e4tsfilter) erkennt<\/li>\n\n\n\n<li><strong>Risikomanagement<\/strong> per Stop-Loss, Take-Profit und Lot-Berechnung umsetzt<\/li>\n\n\n\n<li><strong>Orders<\/strong> platziert und \u00fcberwacht<\/li>\n\n\n\n<li><strong>Trailing-Stop<\/strong> dynamisch anpasst<\/li>\n<\/ol>\n\n\n\n<p>Ziel ist es, zu zeigen, wie man in MQL5 sauber mit Indikator-Handles arbeitet und einen kompletten, lauff\u00e4higen EA erstellt.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u00dcbersicht des Codes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Bibliotheken und Handelsobjekt<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Trade\\Trade.mqh&gt;   \/\/ CTrade-Klasse f\u00fcr Orderfunktionen einbinden\nCTrade trade;               \/\/ Handelsobjekt zum Senden und Anpassen von Orders\n<\/code><\/pre>\n\n\n\n<p><em>Trade.mqh<\/em> stellt die Klasse <strong>CTrade<\/strong> zur Verf\u00fcgung, mit der Marktorders (Buy\/Sell) sowie Anpassungen an bestehenden Positionen komfortabel umgesetzt werden k\u00f6nnen.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Eingabeparameter<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>input int    FastEMAPeriod   = 10;            \/\/ Periode f\u00fcr schnellen EMA\ninput int    SlowEMAPeriod   = 50;            \/\/ Periode f\u00fcr langsamen EMA\ninput int    ATRPeriod       = 14;            \/\/ Periode f\u00fcr ATR\ninput double RiskPercent     = 1.0;           \/\/ Max. Risiko pro Trade in % des Kontos\ninput double RewardRiskRatio = 2.0;           \/\/ Verh\u00e4ltnis TP zu SL (z.B. 1:2)\ninput bool   UseTrailingStop = true;          \/\/ Trailing-Stop aktivieren?\ninput string CommentText     = \"EMA_Cross_ATR\";\/\/ Kommentar f\u00fcr alle Orders\n<\/code><\/pre>\n\n\n\n<p>Diese <strong>Inputs<\/strong> erlauben es, alle wichtigen Parameter (EMA-Perioden, ATR, Risiko, R-R-Ratio, Trailing-Stop) direkt im Tester oder in den EA-Eigenschaften anzupassen.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Globale Handles f\u00fcr Indikatoren<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int fastHandle;    \/\/ Handle f\u00fcr schnellen EMA (FastEMAPeriod)\nint slowHandle;    \/\/ Handle f\u00fcr langsamen EMA (SlowEMAPeriod)\nint atrHandle;     \/\/ Handle f\u00fcr ATR (ATRPeriod)\n<\/code><\/pre>\n\n\n\n<p>Indikatoren werden in MQL5 \u00fcber <strong>Handles<\/strong> angelegt. Diese Integer-Variablen speichern die Referenzen und werden sp\u00e4ter zum Auslesen der Werte via <code>CopyBuffer()<\/code> benutzt.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">OnInit()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int OnInit()\n  {\n   \/\/ EMA-Handles erzeugen\n   fastHandle = iMA(_Symbol, PERIOD_CURRENT, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);\n   slowHandle = iMA(_Symbol, PERIOD_CURRENT, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);\n   \/\/ ATR-Handle erzeugen\n   atrHandle  = iATR(_Symbol, PERIOD_CURRENT, ATRPeriod);\n\n   \/\/ Pr\u00fcfen, ob alle Handles g\u00fcltig sind\n   if(fastHandle == INVALID_HANDLE\n      || slowHandle == INVALID_HANDLE\n      || atrHandle  == INVALID_HANDLE)\n     {\n      Print(\"Fehler beim Erstellen der Indikator-Handles\");\n      return(INIT_FAILED);\n     }\n\n   return(INIT_SUCCEEDED);\n  }\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>iMA(&#8230;)<\/strong> und <strong>iATR(&#8230;)<\/strong> liefern die Handles.<\/li>\n\n\n\n<li>Bei <strong>INVALID_HANDLE<\/strong> schl\u00e4gt die Initialisierung fehl.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">CalculateLot()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>double CalculateLot(bool isBuy, double stopLossPrice)\n  {\n   \/\/ Entry-Preis (Ask bei Buy, Bid bei Sell)\n   double entryPrice = isBuy\n                       ? SymbolInfoDouble(_Symbol, SYMBOL_ASK)\n                       : SymbolInfoDouble(_Symbol, SYMBOL_BID);\n   \/\/ Abstand Entry \u2194 Stop-Loss in Punkten\n   double distance   = MathAbs(entryPrice - stopLossPrice);\n   \/\/ Betrag, den wir maximal riskieren d\u00fcrfen\n   double riskAmount = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent \/ 100.0;\n   \/\/ Tick-Wert und Tick-Gr\u00f6\u00dfe f\u00fcr das Symbol\n   double tickValue  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);\n   double tickSize   = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);\n   double pointValue = tickValue \/ tickSize;\n   \/\/ Roh-Lot-Berechnung: Risiko \/ (Abstand \u00d7 Punktwert)\n   double lots       = riskAmount \/ (distance * pointValue);\n   \/\/ Auf erlaubte Volumen-Schritte runden\n   double step       = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);\n   double minLot     = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);\n   double maxLot     = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);\n   lots = MathFloor(lots \/ step) * step;\n   if(lots &lt; minLot) lots = minLot;\n   if(lots &gt; maxLot) lots = maxLot;\n   return(lots);\n  }\n<\/code><\/pre>\n\n\n\n<p>Diese Funktion errechnet die <strong>Lot-Size<\/strong> so, dass beim Erreichen des Stop-Loss der maximale Risiko-Betrag nicht \u00fcberschritten wird.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">OnTick()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void OnTick()\n  {\n   \/\/ 1) Aktuelle Bid-\/Ask-Preise\n   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);\n   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);\n\n   \/\/ 2) Puffer f\u00fcr die letzten 2 Werte jedes Indikators\n   double fastBuf&#91;2], slowBuf&#91;2], atrBuf&#91;2];\n\n   \/\/ 3) Indikator-Werte kopieren (aktueller = Index 0, vorheriger = Index 1)\n   if(CopyBuffer(fastHandle, 0, 0, 2, fastBuf) &lt;= 0 ||\n      CopyBuffer(slowHandle, 0, 0, 2, slowBuf) &lt;= 0 ||\n      CopyBuffer(atrHandle,  0, 0, 2, atrBuf)  &lt;= 0)\n     {\n      Print(\"Fehler beim Kopieren der Indikator-Daten\");\n      return;\n     }\n\n   \/\/ 4) Zuordnung der Pufferdaten\n   double fastEMA    = fastBuf&#91;0], fastEMAold = fastBuf&#91;1];\n   double slowEMA    = slowBuf&#91;0], slowEMAold = slowBuf&#91;1];\n   double atrNow     = atrBuf&#91;0],  atrOld     = atrBuf&#91;1];\n\n   \/\/ 5) Pr\u00fcfen, ob bereits eine Position offen ist\n   bool positionExists = PositionSelect(_Symbol);\n\n   \/\/ \u2014 Einstieg nur, wenn keine Position offen \u2014\n   if(!positionExists)\n     {\n      \/\/ Long-Signal: EMA10 kreuzt EMA50 nach oben + ATR steigend\n      bool longSignal  = (fastEMAold &lt; slowEMAold)\n                         &amp;&amp; (fastEMA &gt; slowEMA)\n                         &amp;&amp; (atrNow &gt; atrOld);\n      \/\/ Short-Signal: EMA10 kreuzt EMA50 nach unten\n      bool shortSignal = (fastEMAold &gt; slowEMAold)\n                         &amp;&amp; (fastEMA &lt; slowEMA);\n\n      \/\/ Long-Entry\n      if(longSignal)\n        {\n         double sl  = ask - 1.5 * atrNow;\n         double tp  = ask + RewardRiskRatio * (ask - sl);\n         double lot = CalculateLot(true, sl);\n         if(trade.Buy(lot, NULL, ask, sl, tp, CommentText))\n            Print(\"Long er\u00f6ffnet bei \", ask, \" SL=\", sl, \" TP=\", tp);\n        }\n      \/\/ Short-Entry\n      else if(shortSignal)\n        {\n         double sl  = bid + 1.5 * atrNow;\n         double tp  = bid - RewardRiskRatio * (sl - bid);\n         double lot = CalculateLot(false, sl);\n         if(trade.Sell(lot, NULL, bid, sl, tp, CommentText))\n            Print(\"Short er\u00f6ffnet bei \", bid, \" SL=\", sl, \" TP=\", tp);\n        }\n     }\n   \/\/ \u2014 Trailing-Stop, falls Position offen und aktiviert \u2014\n   else if(UseTrailingStop)\n     {\n      ulong ticket = PositionGetInteger(POSITION_TICKET);\n      ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);\n      double newSL = (type == POSITION_TYPE_BUY)\n                     ? bid - atrNow\n                     : ask + atrNow;\n      trade.PositionModify(ticket, newSL, 0);\n     }\n  }\n<\/code><\/pre>\n\n\n\n<p>In <strong>OnTick()<\/strong> werden die Indikatoren ausgewertet, Signale erkannt, Orders gesendet und bei Bedarf der Stop-Loss dynamisch nachgezogen.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">OnDeinit()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void OnDeinit(const int reason)\n  {\n   if(fastHandle != INVALID_HANDLE) IndicatorRelease(fastHandle);\n   if(slowHandle != INVALID_HANDLE) IndicatorRelease(slowHandle);\n   if(atrHandle  != INVALID_HANDLE) IndicatorRelease(atrHandle);\n  }\n<\/code><\/pre>\n\n\n\n<p>Beim Entfernen des EAs werden alle <strong>Indikator-Handles freigegeben<\/strong>, um Speicherlecks und andere Probleme zu vermeiden.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Erkl\u00e4rung der wichtigsten Schritte<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Handle-Erzeugung und Werte-Auslesung<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>iMA() \/ iATR()<\/strong> liefern in <code>OnInit()<\/code> jeweils einen <strong>Handle<\/strong>, eine numerische Referenz auf den jeweiligen Indikator.<\/li>\n\n\n\n<li>In <strong>OnTick()<\/strong> ruft <code>CopyBuffer(handle, 0, shift, count, buffer)<\/code> die aktuellen und vorherigen Werte in ein lokales Array.<\/li>\n\n\n\n<li>Vorteil: Effizienter als wiederholte Direktaufrufe und erm\u00f6glicht den Zugriff auf historische Balken.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Crossover-Erkennung<\/strong><br><code>bool longSignal = (fastEMAold &lt; slowEMAold) &amp;&amp; (fastEMA &gt; slowEMA) &amp;&amp; (atrNow &gt; atrOld); bool shortSignal = (fastEMAold &gt; slowEMAold) &amp;&amp; (fastEMA &lt; slowEMA);<\/code>\n<ul class=\"wp-block-list\">\n<li><strong>fastEMAold<\/strong> und <strong>slowEMAold<\/strong> sind die EMA-Werte des vorherigen Balkens, <strong>fastEMA<\/strong> und <strong>slowEMA<\/strong> jene des aktuellen.<\/li>\n\n\n\n<li>Ein <strong>Long-Signal<\/strong> entsteht, wenn der schnelle EMA den langsamen EMA von unten nach oben kreuzt <strong>und<\/strong> die ATR zeigt steigende Volatilit\u00e4t.<\/li>\n\n\n\n<li>Ein <strong>Short-Signal<\/strong> erfolgt beim Kreuzen von oben nach unten, ohne ATR-Filter.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Risikomanagement &amp; Lot-Berechnung<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Stop-Loss<\/strong> wird auf <strong>1,5 \u00d7 ATR<\/strong> festgelegt:\n<ul class=\"wp-block-list\">\n<li>Long: SL = Ask \u2013 1.5 \u00d7 ATR<\/li>\n\n\n\n<li>Short: SL = Bid + 1.5 \u00d7 ATR<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Take-Profit<\/strong> so, dass das Risiko-Ertrags-Verh\u00e4ltnis 1:2 betr\u00e4gt:\n<ul class=\"wp-block-list\">\n<li>TP-Abstand = 2 \u00d7 SL-Abstand<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>In <code>CalculateLot()<\/code> wird ermittelt, wie viele Lots so platziert werden k\u00f6nnen, dass beim Erreichen des Stop-Loss maximal <strong>RiskPercent\u2009%<\/strong> des Kontoguthabens verloren gehen.<\/li>\n\n\n\n<li>Dabei flie\u00dfen <code>SymbolInfoDouble(..., SYMBOL_TRADE_TICK_VALUE)<\/code> und <code>SYMBOL_TRADE_TICK_SIZE<\/code> mit ein, um den Geldwert pro Punkt korrekt zu ber\u00fccksichtigen.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Order-Platzierung<\/strong>\n<ul class=\"wp-block-list\">\n<li>Mit <code>trade.Buy(lot, NULL, price, sl, tp, CommentText)<\/code> bzw.<br><code>trade.Sell(...)<\/code> wird eine Marktorder mit definiertem SL und TP ausgel\u00f6st.<\/li>\n\n\n\n<li><code>CTrade<\/code> k\u00fcmmert sich intern um Fehlerbehandlung, Slippage und R\u00fcckgabe des Order-Tickets.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Trailing-Stop (optional)<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sobald eine Position offen ist und <code>UseTrailingStop == true<\/code>, verschiebt der EA bei jedem Tick den Stop-Loss auf <strong>1 \u00d7 ATR<\/strong> hinter den aktuellen Marktpreis:\n<ul class=\"wp-block-list\">\n<li>Long: neuer SL = Bid \u2013 ATR<\/li>\n\n\n\n<li>Short: neuer SL = Ask + ATR<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Erm\u00f6glicht eine <strong>dynamische Absicherung<\/strong> bereits erzielter Gewinne.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Aufr\u00e4umen in OnDeinit()<\/strong>\n<ul class=\"wp-block-list\">\n<li>Alle erzeugten Indikator-Handles (<code>fastHandle<\/code>, <code>slowHandle<\/code>, <code>atrHandle<\/code>) werden mit <code>IndicatorRelease(handle)<\/code> wieder freigegeben, um Ressourcen korrekt zu bereinigen.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Durch diese klar strukturierten Schritte lernst du, wie man in MQL5:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Indikatoren performant anlegt und ausliest<\/li>\n\n\n\n<li>Signale pr\u00e4zise detektiert<\/li>\n\n\n\n<li>Verantwortungsvolles Risikomanagement implementiert<\/li>\n\n\n\n<li>Marktorders professionell verwaltet<\/li>\n\n\n\n<li>Dynamische Stop-Loss-Strategien umsetzt<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Zusammenfassung<\/h2>\n\n\n\n<p>Dieser EA demonstriert einen vollst\u00e4ndigen Workflow in MQL5:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Saubere Trennung<\/strong> zwischen Initialisierung (<code>OnInit<\/code>), Hauptlogik (<code>OnTick<\/code>) und Aufr\u00e4umen (<code>OnDeinit<\/code>).<\/li>\n\n\n\n<li><strong>Handle-basierte<\/strong> Indikatornutzung mit <code>CopyBuffer()<\/code>.<\/li>\n\n\n\n<li><strong>Einfache Trendfolge-Strategie<\/strong> auf Basis zweier EMAs plus Volatilit\u00e4tsfilter.<\/li>\n\n\n\n<li><strong>Sorgf\u00e4ltiges Risikomanagement<\/strong> mittels Stop-Loss, Take-Profit und dynamischer Lot-Berechnung.<\/li>\n\n\n\n<li><strong>Optionale Automatisierung<\/strong> eines Trailing-Stops.<\/li>\n<\/ul>\n\n\n\n<p>Mit diesem kommentierten Beispiel erh\u00e4ltst du eine solide Grundlage, um eigene Strategien in MQL5 zu entwickeln und zu verstehen, wie man automatisierte Handelssysteme professionell umsetzt. Viel Erfolg bei deinen n\u00e4chsten Schritten in der MQL5-Programmierung!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Quellcode MA_Cross.mq5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/+------------------------------------------------------------------+\n\/\/|                                                   MA_Cross.mq5   |\n\/\/|        EMA Crossover + ATR-Filter f\u00fcr MetaTrader 5 (MQL5)        |\n\/\/+------------------------------------------------------------------+\n#include &lt;Trade\\Trade.mqh&gt;                                  \/\/ CTrade-Klasse f\u00fcr Orderfunktionen einbinden\nCTrade trade;                                              \/\/ Handelsobjekt\n\n\/\/\u2014\u2014 Eingabeparameter \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\ninput int    FastEMAPeriod    = 10;                       \/\/ Periode f\u00fcr schnellen EMA\ninput int    SlowEMAPeriod    = 50;                       \/\/ Periode f\u00fcr langsamen EMA\ninput int    ATRPeriod        = 14;                       \/\/ Periode f\u00fcr ATR\ninput double RiskPercent      = 1.0;                      \/\/ Max. Risiko pro Trade in % (z.B. 1 = 1 %)\ninput double RewardRiskRatio  = 2.0;                      \/\/ Risiko-Ertrag Verh\u00e4ltnis (z.B. 1:2)\ninput bool   UseTrailingStop  = true;                     \/\/ Trailing-Stop aktivieren?\ninput string CommentText      = \"EMA_Cross_ATR\";          \/\/ Kommentartext f\u00fcr Orders\n\n\/\/\u2014\u2014 Globale Handles f\u00fcr Indikatoren \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\nint fastHandle;                                           \/\/ Handle f\u00fcr schnellen EMA\nint slowHandle;                                           \/\/ Handle f\u00fcr langsamen EMA\nint atrHandle;                                            \/\/ Handle f\u00fcr ATR\n\n\/\/+------------------------------------------------------------------+\n\/\/| OnInit: Initialisierung beim Laden des EAs                      |\n\/\/+------------------------------------------------------------------+\nint OnInit()\n  {\n   \/\/ EMA-Handles mit Symbol, Timeframe, Periode, Shift, Methode, Preisquelle erzeugen\n   fastHandle = iMA(_Symbol, PERIOD_CURRENT, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);\n   slowHandle = iMA(_Symbol, PERIOD_CURRENT, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);\n   \/\/ ATR-Handle mit Symbol, Timeframe, Periode erzeugen\n   atrHandle  = iATR(_Symbol, PERIOD_CURRENT, ATRPeriod);\n\n   \/\/ Pr\u00fcfen, ob Handles g\u00fcltig sind\n   if(fastHandle==INVALID_HANDLE || slowHandle==INVALID_HANDLE || atrHandle==INVALID_HANDLE)\n     {\n      Print(\"Fehler beim Erstellen der Indikator-Handles\");\n      return(INIT_FAILED);                               \/\/ Init abbrechen bei Fehler\n     }\n   return(INIT_SUCCEEDED);                                \/\/ Alles ok\n  }\n\n\/\/+------------------------------------------------------------------+\n\/\/| CalculateLot: Berechnet Lotgr\u00f6\u00dfe basierend auf SL-Abstand       |\n\/\/+------------------------------------------------------------------+\ndouble CalculateLot(bool isBuy, double stopLossPrice)\n  {\n   \/\/ 1) Entry-Preis je nach Buy\/Sell ermitteln\n   double entryPrice = isBuy\n                       ? SymbolInfoDouble(_Symbol, SYMBOL_ASK)\n                       : SymbolInfoDouble(_Symbol, SYMBOL_BID);\n   \/\/ 2) Abstand SL \u2194 Entry\n   double distance   = MathAbs(entryPrice - stopLossPrice);\n   \/\/ 3) Risikobetrag (Konto \u00d7 Risiko%)\n   double riskAmount = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent \/ 100.0;\n   \/\/ 4) Punktwert f\u00fcr Symbol (TickValue\/TickSize)\n   double tickValue  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);\n   double tickSize   = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);\n   double pointValue = tickValue \/ tickSize;\n   \/\/ 5) Roh-Lot = Risiko \/ (Abstand \u00d7 Punktwert)\n   double lots       = riskAmount \/ (distance * pointValue);\n   \/\/ 6) Auf minimale Lot-Schritte runden\n   double step       = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);\n   double minLot     = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);\n   double maxLot     = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);\n   lots = MathFloor(lots \/ step) * step;\n   if(lots &lt; minLot) lots = minLot;\n   if(lots &gt; maxLot) lots = maxLot;\n   return(lots);                                          \/\/ Fertige Lotgr\u00f6\u00dfe zur\u00fcckgeben\n  }\n\n\/\/+------------------------------------------------------------------+\n\/\/| OnTick: Hauptlogik bei jedem Tick                               |\n\/\/+------------------------------------------------------------------+\nvoid OnTick()\n  {\n   \/\/ 1) Aktuelle Bid\/Ask-Preise holen\n   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);\n   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);\n\n   \/\/ 2) Puffer f\u00fcr aktuelle und vorige Indikator-Werte\n   double fastBuf&#91;2], slowBuf&#91;2], atrBuf&#91;2];\n   \/\/ 3) Daten aus den Handles kopieren\n   if(CopyBuffer(fastHandle, 0, 0, 2, fastBuf) &lt;= 0 ||\n      CopyBuffer(slowHandle, 0, 0, 2, slowBuf) &lt;= 0 ||\n      CopyBuffer(atrHandle,  0, 0, 2, atrBuf)  &lt;= 0)\n     {\n      Print(\"Fehler beim Kopieren der Indikator-Daten\");\n      return;                                             \/\/ Abbruch bei Fehler\n     }\n\n   \/\/ 4) Werte zuordnen: &#91;0] = aktuell, &#91;1] = vorher\n   double fastEMA    = fastBuf&#91;0];\n   double fastEMAold = fastBuf&#91;1];\n   double slowEMA    = slowBuf&#91;0];\n   double slowEMAold = slowBuf&#91;1];\n   double atrNow     = atrBuf&#91;0];\n   double atrOld     = atrBuf&#91;1];\n\n   \/\/ 5) Pr\u00fcfen, ob aktuell eine Position offen ist\n   bool positionExists = PositionSelect(_Symbol);\n\n   \/\/ \u2014\u2014 Einstieg wenn keine Position offen \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n   if(!positionExists)\n     {\n      \/\/ Long, wenn EMA-Cross up + ATR steigend\n      bool longSignal  = (fastEMAold &lt; slowEMAold) &amp;&amp; (fastEMA &gt; slowEMA) &amp;&amp; (atrNow &gt; atrOld);\n      \/\/ Short, wenn EMA-Cross down\n      bool shortSignal = (fastEMAold &gt; slowEMAold) &amp;&amp; (fastEMA &lt; slowEMA);\n\n      if(longSignal)  \/\/ Long-Einstieg\n        {\n         double sl  = ask - 1.5 * atrNow;                                         \/\/ SL = Entry \u20131.5\u00d7ATR\n         double tp  = ask + RewardRiskRatio * (ask - sl);                        \/\/ TP = Entry +2\u00d7Risiko\n         double lot = CalculateLot(true, sl);                                    \/\/ Lot berechnen\n         if(trade.Buy(lot, NULL, ask, sl, tp, CommentText))                      \/\/ Buy-Order senden\n            Print(\"Long er\u00f6ffnet bei \", ask, \" SL=\", sl, \" TP=\", tp);            \n        }\n      else if(shortSignal) \/\/ Short-Einstieg\n        {\n         double sl  = bid + 1.5 * atrNow;                                         \/\/ SL = Entry +1.5\u00d7ATR\n         double tp  = bid - RewardRiskRatio * (sl - bid);                        \/\/ TP = Entry \u20132\u00d7Risiko\n         double lot = CalculateLot(false, sl);                                   \/\/ Lot berechnen\n         if(trade.Sell(lot, NULL, bid, sl, tp, CommentText))                     \/\/ Sell-Order senden\n            Print(\"Short er\u00f6ffnet bei \", bid, \" SL=\", sl, \" TP=\", tp);\n        }\n     }\n   \/\/ \u2014\u2014 Trailing-Stop wenn Position offen \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n   else if(UseTrailingStop)\n     {\n      ulong ticket = PositionGetInteger(POSITION_TICKET);                        \/\/ Ticket der Position\n      ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);\n      double newSL;\n      if(type == POSITION_TYPE_BUY)                                             \/\/ Bei Long\n         newSL = bid - atrNow;                                                   \/\/ SL = Bid \u20131\u00d7ATR\n      else                                                                       \/\/ Bei Short\n         newSL = ask + atrNow;                                                   \/\/ SL = Ask +1\u00d7ATR\n\n      trade.PositionModify(ticket, newSL, 0);                                   \/\/ SL anpassen\n     }\n  }\n\n\/\/+------------------------------------------------------------------+\n\/\/| OnDeinit: Handles freigeben                                      |\n\/\/+------------------------------------------------------------------+\nvoid OnDeinit(const int reason)\n  {\n   if(fastHandle != INVALID_HANDLE)  IndicatorRelease(fastHandle);             \/\/ Fast EMA freigeben\n   if(slowHandle != INVALID_HANDLE)  IndicatorRelease(slowHandle);             \/\/ Slow EMA freigeben\n   if(atrHandle  != INVALID_HANDLE)  IndicatorRelease(atrHandle);              \/\/ ATR freigeben\n  }\n\/\/+------------------------------------------------------------------+\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Selbst-Programmiert Expert Advisor \u201eMoving Average Crossover\u201c Einf\u00fchrung Dieser Expert Advisor (EA) \u201eMA_Cross\u201c f\u00fcr MetaTrader 5 automatisiert das Erkennen und Handeln [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-2866","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/pages\/2866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/comments?post=2866"}],"version-history":[{"count":2,"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/pages\/2866\/revisions"}],"predecessor-version":[{"id":2870,"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/pages\/2866\/revisions\/2870"}],"wp:attachment":[{"href":"https:\/\/ea-marketplace.com\/de\/wp-json\/wp\/v2\/media?parent=2866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}