Was ist falsch?

 

Hello guys, i am not very good with mql5, but with the help of chatgpt i tried to transform a Supertrend Script from pinescript to mql5.

This is my Code:

//+------------------------------------------------------------------+

//|                                                      SuperTrend.mq5|

//|                        Copyright 2024, KivancOzbilgic           |

//|                        https://www.mql5.com/en/codebase          |

//+------------------------------------------------------------------+

#property strict



input int ATR_Periods = 10;             // ATR Periods

input double ATR_Multiplier = 3.0;      // ATR Multiplier

input bool Change_ATR_Method = true;    // Change ATR Calculation Method

input bool Show_Signals = false;        // Show Buy/Sell Signals

input bool Highlighting = true;         // Highlighter On/Off

input bool Bar_Coloring = true;         // Bar Coloring On/Off

input double Lots = 0.1;                // Lot Size

input int SL_Pips = 100;                // Stop Loss in Pips

input int TP_Pips = 200;                // Take Profit in Pips



double Ask, Bid;

double atr[];

double up[], dn[];

int trend[];

bool buySignal, sellSignal;

double High[];

double Low[];

double Close[];

datetime Time[];  // Deklaration des Time-Arrays



//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   Print("SuperTrend EA initialized.");



   ArraySetAsSeries(atr, true);

   ArraySetAsSeries(up, true);

   ArraySetAsSeries(dn, true);

   ArraySetAsSeries(trend, true);

   ArraySetAsSeries(High, true);

   ArraySetAsSeries(Low, true);

   ArraySetAsSeries(Close, true);

   ArraySetAsSeries(Time, true);  // Initialisieren als Serie



   return INIT_SUCCEEDED;

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   Print("SuperTrend EA deinitialized. Reason: ", reason);

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

   static datetime lastTradeTime = 0;



   // Get the latest market data

   Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);



   // Calculate the SuperTrend values

   CalculateSuperTrend();



   // Check for buy signal

   if (buySignal && Time[0] != lastTradeTime)

     {

      double lotSize = Lots;

      double sl = Ask - SL_Pips * _Point;

      double tp = Ask + TP_Pips * _Point;

      MqlTradeRequest request;

      MqlTradeResult result;

      ZeroMemory(request);

      ZeroMemory(result);

      request.action = TRADE_ACTION_DEAL;

      request.symbol = _Symbol;

      request.volume = lotSize;

      request.type = ORDER_TYPE_BUY;

      request.price = Ask;

      request.sl = sl;

      request.tp = tp;

      request.deviation = 3;

      request.magic = 0;

      request.comment = "SuperTrend Buy";

      if (OrderSend(request, result))

        {

         Print("Buy order sent successfully.");

         lastTradeTime = TimeCurrent();  // Verwendung von TimeCurrent() statt Time[0]

        }

      else

        {

         Print("Failed to send buy order. Error: ", GetLastError());

        }

     }



   // Check for sell signal

   if (sellSignal && Time[0] != lastTradeTime)

     {

      double lotSize = Lots;

      double sl = Bid + SL_Pips * _Point;

      double tp = Bid - TP_Pips * _Point;

      MqlTradeRequest request;

      MqlTradeResult result;

      ZeroMemory(request);

      ZeroMemory(result);

      request.action = TRADE_ACTION_DEAL;

      request.symbol = _Symbol;

      request.volume = lotSize;

      request.type = ORDER_TYPE_SELL;

      request.price = Bid;

      request.sl = sl;

      request.tp = tp;

      request.deviation = 3;

      request.magic = 0;

      request.comment = "SuperTrend Sell";

      if (OrderSend(request, result))

        {

         Print("Sell order sent successfully.");

         lastTradeTime = TimeCurrent();  // Verwendung von TimeCurrent() statt Time[0]

        }

      else

        {

         Print("Failed to send sell order. Error: ", GetLastError());

        }

     }

  }

//+------------------------------------------------------------------+

//| Calculate SuperTrend function                                    |

//+------------------------------------------------------------------+

void CalculateSuperTrend()

  {

   int bars = iBars(_Symbol, _Period);

   if (bars < ATR_Periods) 

     {

      Print("Not enough bars to calculate SuperTrend. Bars: ", bars);

      return;

     }



   ArrayResize(atr, bars);

   ArrayResize(up, bars);

   ArrayResize(dn, bars);

   ArrayResize(trend, bars);



   for (int i = bars - 1; i >= 0; i--)

     {

      double src = (High[i] + Low[i]) / 2.0;

      double atrValue;

      if (Change_ATR_Method)

        {

         atrValue = iATR(_Symbol, _Period, ATR_Periods);  // Korrigierte iATR-Funktion

        }

      else

        {

         int atr2Handle = iMA(_Symbol, _Period, ATR_Periods, 0, MODE_SMA, PRICE_MEDIAN);

         double atr2Buffer[];

         ArraySetAsSeries(atr2Buffer, true);

         CopyBuffer(atr2Handle, 0, 0, bars, atr2Buffer);

         atrValue = atr2Buffer[i];

        }

      

      atr[i] = atrValue;

      up[i] = src - (ATR_Multiplier * atr[i]);

      if (i < bars - 1) {

          up[i] = (Close[i + 1] > up[i + 1]) ? MathMax(up[i], up[i + 1]) : up[i];

      }

      dn[i] = src + (ATR_Multiplier * atr[i]);

      if (i < bars - 1) {

          dn[i] = (Close[i + 1] < dn[i + 1]) ? MathMin(dn[i], dn[i + 1]) : dn[i];

      }

      if (i < bars - 1) {

          trend[i] = trend[i + 1];

      }

      trend[i] = (trend[i] == -1 && Close[i] > dn[i + 1]) ? 1 : (trend[i] == 1 && Close[i] < up[i + 1]) ? -1 : trend[i];

     }



   if (bars > 1) {

       buySignal = (trend[0] == 1 && trend[1] == -1);

       sellSignal = (trend[0] == -1 && trend[1] == 1);

   } else {

       buySignal = false;

       sellSignal = false;

   }

   

   Print("SuperTrend calculated: buySignal=", buySignal, ", sellSignal=", sellSignal);

  }

//+------------------------------------------------------------------+

The Expert Advisor should place orders for me but when used on a chart it keeps disappearing and sometimes i get an error : Array out of range. Could someone help me with this?
Dateien:
 

Seufz, ChatGPT :( Aber

  1. Wir können hier deutsch sprechen ;)
  2. Auch vor ChtGPT sollte man wissen, wie ein MQL5-Prigramm aussehen soll,
    Dazu z.B.
        https://www.mql5.com/en/articles/496
        https://www.mql5.com/en/articles/100
        https://www.mql5.com/en/articles/599
        https://www.mql5.com/en/articles/232

  3. Es gibt Fehler bei der Kompilierung und zur Laufzeit - wo ist er denn bei Dir?
  4. Es gibt fast nichts, was nicht schon für MT5 programmiert wurde!
    Daher wäre suchen besser als ChatGPT:
    Supertrend in der Codebase: https://www.mql5.com/en/search#!keyword=Supertrend&module=mql5_module_codebase
    Oder über Google nach: site:mql5.com Supertrend
    Die funktionieren!
Quick Start: Short Guide for Beginners
Quick Start: Short Guide for Beginners
  • www.mql5.com
Hello dear reader! In this article, I will try to explain and show you how you can easily and quickly get the hang of the principles of creating Expert Advisors, working with indicators, etc. It is beginner-oriented and will not feature any difficult or abstruse examples.