Unsupported Filling mode and Ordersend 4756 Error

 

hi , i have an EA . This EA should open a trade for example at 17.00 and close next day 9.oo

but in tests i get 2 errors , first is unsupported filling mode and the second is Ordersend 4756 Error

its on every Pair, Gold, DE40 and so on

here is the code

//+------------------------------------------------------------------+
//|                                                   DAX_Trader.mq5  |
//|                        Von [Ihr Name]                            |
//+------------------------------------------------------------------+
input double LotSize = 1.0;            // Handelslotgröße
input double StopLoss = 700;            // Stop-Loss in Punkten
input double TakeProfit = 700;          // Take-Profit in Punkten
input bool EnableSL = true;             // Stop-Loss aktivieren
input bool EnableTP = true;             // Take-Profit aktivieren
input int BuyHour = 17;                 // Stunde für Buy-Trade
input int BuyMinute = 30;               // Minute für Buy-Trade
input int CloseHour = 8;                // Stunde für Schließen
input int CloseMinute = 50;             // Minute für Schließen

bool tradeOpened = false;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    datetime currentTime = TimeCurrent();
    MqlDateTime timeStruct;
    TimeToStruct(currentTime, timeStruct); // Zeit in Struktur umwandeln

    int currentHour = timeStruct.hour;   // Stunde extrahieren
    int currentMinute = timeStruct.min;   // Minute extrahieren

    Print("Aktuelle Zeit: ", currentHour, ":", currentMinute);

    // Prüfen, ob es Zeit ist, einen Trade zu eröffnen
    if (!tradeOpened && currentHour == BuyHour && currentMinute == BuyMinute)
    {
        double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Preis für Kauf
        double sl = EnableSL ? price - StopLoss * _Point : 0; // SL nur setzen, wenn aktiviert
        double tp = EnableTP ? price + TakeProfit * _Point : 0; // TP nur setzen, wenn aktiviert

        // Überprüfen der Lotgröße
        double minLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
        double maxLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);

        // Protokollierung der Werte
        Print("Aktueller Preis: ", price);
        Print("Lotgröße: ", LotSize);
        Print("Mindestlotgröße: ", minLotSize);
        Print("Maximale Lotgröße: ", maxLotSize);
        Print("SL: ", sl);
        Print("TP: ", tp);

        // Überprüfen, ob die Lotgröße gültig ist
        if (LotSize < minLotSize || LotSize > maxLotSize)
        {
            Print("Ungültige Lotgröße: ", LotSize);
            return;
        }

        // Überprüfen von SL und TP
        double slDistance = EnableSL ? MathAbs(price - sl) : 0; // SL-Distanz nur berechnen, wenn aktiviert
        double tpDistance = EnableTP ? MathAbs(price - tp) : 0; // TP-Distanz nur berechnen, wenn aktiviert
        int stopLevel = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

        if ((EnableSL && slDistance < stopLevel * _Point) || 
            (EnableTP && tpDistance < stopLevel * _Point))
        {
            Print("Stop-Loss oder Take-Profit verletzt die Mindestabstände.");
            return;
        }

        // Buy-Trade eröffnen
        MqlTradeRequest request;
        MqlTradeResult result;

        // Setzen der Parameter
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol; // Aktuelles Symbol
        request.volume = LotSize; // Lotgröße
        request.price = price; // Preis für Kauf
        request.sl = EnableSL ? sl : 0; // SL nur setzen, wenn aktiviert
        request.tp = EnableTP ? tp : 0; // TP nur setzen, wenn aktiviert
        request.type = ORDER_TYPE_BUY; // Ordertyp

        // Debugging-Ausgabe
        Print("OrderSend Anfrage: Preis=", request.price, " SL=", request.sl, " TP=", request.tp, " LotSize=", request.volume);

        // Order senden
        if (OrderSend(request, result))
        {
            if (result.retcode != TRADE_RETCODE_DONE)
            {
                Print("Fehler beim Eröffnen des Handels: ", result.retcode);
            }
            else
            {
                tradeOpened = true;
                Print("Trade eröffnet: ", result.order);
            }
        }
        else
        {
            Print("OrderSend fehlgeschlagen: ", GetLastError());
        }
    }
    else if (tradeOpened)
    {
        Print("Ein Trade ist bereits geöffnet.");
    }
    else
    {
        Print("Die Handelszeit stimmt nicht überein.");
    }

    // Prüfen, ob es Zeit ist, den Trade zu schließen
    if (tradeOpened && (currentHour == CloseHour && currentMinute == CloseMinute))
    {
        // Alle offenen Positionen durchlaufen
        for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(_Symbol))
            {
                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    // Trade schließen
                    ulong ticket = PositionGetInteger(POSITION_TICKET);
                    double volume = PositionGetDouble(POSITION_VOLUME);
                    
                    MqlTradeRequest closeRequest;
                    MqlTradeResult closeResult;

                    closeRequest.action = TRADE_ACTION_DEAL;
                    closeRequest.symbol = _Symbol;
                    closeRequest.volume = volume;
                    closeRequest.price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Preis zum Schließen
                    closeRequest.type = ORDER_TYPE_SELL; // Verkaufsauftrag zum Schließen

                    // Position schließen
                    if (OrderSend(closeRequest, closeResult))
                    {
                        tradeOpened = false; // Rücksetzen der Handelsflagge
                        Print("Trade geschlossen: ", closeResult.order);
                    }
                    else
                    {
                        Print("Fehler beim Schließen des Handels: ", closeResult.retcode);
                    }
                }
            }
            else
            {
                Print("Keine offene Position für das Symbol gefunden.");
            }
        }
    }
}
//+------------------------------------------------------------------+
 
You didn't assign request.type_filling

This is specific for the broker. You find the filling type the broker uses in marketwatch...right click the market symbol, click properties, click specifications, and look for Filling

Nobody should be writing EAs this way when we have the trade.mqh library which abstracts a lot of things
 
Conor Mcnamara # : Sie haben request.type_filling nicht zugewiesen

Dies ist Brokerspezifisch. Den vom Broker verwendeten Fülltyp finden Sie in Marketwatch. Klicken Sie mit der rechten Maustaste auf das Marktsymbol, klicken Sie auf Eigenschaften, dann auf Spezifikationen und suchen Sie nach Fülltyp.

Niemand sollte EAs auf diese Weise schreiben, wenn wir die trade.mqh-Bibliothek haben, die viele Dinge abstrahiert

Ich habe jetzt den Fülltyp im Code. Kannst du mal einen Blick darauf werfen? Der Fülltyp ist IOC vom Broker. aber es ist der gleiche Fehler.

OrderSend 4756 und im Journal steht Kaufauftrag Ungültige Anfrage


//+------------------------------------------------------------------+
//|                                                   DAX_Trader.mq5  |
//|                        Von [Ihr Name]                            |
//+------------------------------------------------------------------+
input double LotSize = 1.0;            // Handelslotgröße
input double StopLoss = 700;            // Stop-Loss in Punkten
input double TakeProfit = 700;          // Take-Profit in Punkten
input bool EnableSL = true;             // Stop-Loss aktivieren
input bool EnableTP = true;             // Take-Profit aktivieren
input int BuyHour = 17;                 // Stunde für Buy-Trade
input int BuyMinute = 30;               // Minute für Buy-Trade
input int CloseHour = 8;                // Stunde für Schließen
input int CloseMinute = 50;             // Minute für Schließen

bool tradeOpened = false;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    datetime currentTime = TimeCurrent();
    MqlDateTime timeStruct;
    TimeToStruct(currentTime, timeStruct); // Zeit in Struktur umwandeln

    int currentHour = timeStruct.hour;   // Stunde extrahieren
    int currentMinute = timeStruct.min;   // Minute extrahieren

    Print("Aktuelle Zeit: ", currentHour, ":", currentMinute);

    // Prüfen, ob es Zeit ist, einen Trade zu eröffnen
    if (!tradeOpened && currentHour == BuyHour && currentMinute == BuyMinute)
    {
        double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Preis für Kauf
        double sl = EnableSL ? price - StopLoss * _Point : 0; // SL nur setzen, wenn aktiviert
        double tp = EnableTP ? price + TakeProfit * _Point : 0; // TP nur setzen, wenn aktiviert

        // Überprüfen der Lotgröße
        double minLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
        double maxLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);

        // Protokollierung der Werte
        Print("Aktueller Preis: ", price);
        Print("Lotgröße: ", LotSize);
        Print("Mindestlotgröße: ", minLotSize);
        Print("Maximale Lotgröße: ", maxLotSize);
        Print("SL: ", sl);
        Print("TP: ", tp);

        // Überprüfen, ob die Lotgröße gültig ist
        if (LotSize < minLotSize || LotSize > maxLotSize)
        {
            Print("Ungültige Lotgröße: ", LotSize);
            return;
        }

        // Überprüfen von SL und TP
        double slDistance = EnableSL ? MathAbs(price - sl) : 0; // SL-Distanz nur berechnen, wenn aktiviert
        double tpDistance = EnableTP ? MathAbs(price - tp) : 0; // TP-Distanz nur berechnen, wenn aktiviert
        int stopLevel = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

        if ((EnableSL && slDistance < stopLevel * _Point) || 
            (EnableTP && tpDistance < stopLevel * _Point))
        {
            Print("Stop-Loss oder Take-Profit verletzt die Mindestabstände.");
            return;
        }

        // Buy-Trade eröffnen
        MqlTradeRequest request;
        MqlTradeResult result;

        // Setzen der Parameter
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol; // Aktuelles Symbol
        request.volume = LotSize; // Lotgröße
        request.price = price; // Preis für Kauf
        request.sl = EnableSL ? sl : 0; // SL nur setzen, wenn aktiviert
        request.tp = EnableTP ? tp : 0; // TP nur setzen, wenn aktiviert
        request.type = ORDER_TYPE_BUY; // Ordertyp

        // Setzen des Filling Modes
        request.type_filling = ORDER_FILLING_IOC; // IOC 

        // Debugging-Ausgabe
        Print("OrderSend Anfrage: Preis=", request.price, " SL=", request.sl, " TP=", request.tp, " LotSize=", request.volume);

        // Order senden
        if (OrderSend(request, result))
        {
            if (result.retcode != TRADE_RETCODE_DONE)
            {
                Print("Fehler beim Eröffnen des Handels: ", result.retcode);
            }
            else
            {
                tradeOpened = true;
                Print("Trade eröffnet: ", result.order);
            }
        }
        else
        {
            Print("OrderSend fehlgeschlagen: ", GetLastError());
        }
    }
    else if (tradeOpened)
    {
        Print("Ein Trade ist bereits geöffnet.");
    }
    else
    {
        Print("Die Handelszeit stimmt nicht überein.");
    }

    // Prüfen, ob es Zeit ist, den Trade zu schließen
    if (tradeOpened && (currentHour == CloseHour && currentMinute == CloseMinute))
    {
        // Alle offenen Positionen durchlaufen
        for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(_Symbol))
            {
                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    // Trade schließen
                    ulong ticket = PositionGetInteger(POSITION_TICKET);
                    double volume = PositionGetDouble(POSITION_VOLUME);
                    
                    MqlTradeRequest closeRequest;
                    MqlTradeResult closeResult;

                    closeRequest.action = TRADE_ACTION_DEAL;
                    closeRequest.symbol = _Symbol;
                    closeRequest.volume = volume;
                    closeRequest.price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Preis zum Schließen
                    closeRequest.type = ORDER_TYPE_SELL; // Verkaufsauftrag zum Schließen

                    // Position schließen
                    if (OrderSend(closeRequest, closeResult))
                    {
                        tradeOpened = false; // Rücksetzen der Handelsflagge
                        Print("Trade geschlossen: ", closeResult.order);
                    }
                    else
                    {
                        Print("Fehler beim Schließen des Handels: ", closeResult.retcode);
                    }
                }
            }
            else
            {
                Print("Keine offene Position für das Symbol gefunden.");
            }
        }
    }
}
//+------------------------------------------------------------------+
 
Alexander Manfred Fleck #:

Ich habe jetzt den Fülltyp im Code. Kannst du mal einen Blick darauf werfen? Der Fülltyp ist IOC vom Broker. aber es ist der gleiche Fehler.

OrderSend 4756 und im Journal steht Kaufauftrag Ungültige Anfrage


Hello , the solution is :

MqlTradeRequest request={};
MqlTradeResult result={};
MqlTradeRequest closeRequest={};
MqlTradeResult closeResult={};
 
Lorentzos Roussos #:

Hello , the solution is :

Here is the New Code, but now i got unsupportes Filling Mode.  The Filling Mode is IOC. In MT5 DE40 the Filling Mode is IOC too. 

I dont know what wents wrong

//+------------------------------------------------------------------+
//|                                                   DAX_Trader.mq5  |
//|                        Von [Ihr Name]                            |
//+------------------------------------------------------------------+
input double LotSize = 1.0;            // Handelslotgröße
input double StopLoss = 700;            // Stop-Loss in Punkten
input double TakeProfit = 700;          // Take-Profit in Punkten
input bool EnableSL = true;             // Stop-Loss aktivieren
input bool EnableTP = true;             // Take-Profit aktivieren
input int BuyHour = 17;                 // Stunde für Buy-Trade
input int BuyMinute = 30;               // Minute für Buy-Trade
input int CloseHour = 8;                // Stunde für Schließen
input int CloseMinute = 50;             // Minute für Schließen

bool tradeOpened = false;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    datetime currentTime = TimeCurrent();
    MqlDateTime timeStruct;
    TimeToStruct(currentTime, timeStruct); // Zeit in Struktur umwandeln

    int currentHour = timeStruct.hour;   // Stunde extrahieren
    int currentMinute = timeStruct.min;   // Minute extrahieren

    Print("Aktuelle Zeit: ", currentHour, ":", currentMinute);

    // Prüfen, ob es Zeit ist, einen Trade zu eröffnen
    if (!tradeOpened && currentHour == BuyHour && currentMinute == BuyMinute)
    {
        double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Preis für Kauf
        double sl = EnableSL ? price - StopLoss * _Point : 0; // SL nur setzen, wenn aktiviert
        double tp = EnableTP ? price + TakeProfit * _Point : 0; // TP nur setzen, wenn aktiviert

        // Überprüfen der Lotgröße
        double minLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
        double maxLotSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);

        // Protokollierung der Werte
        Print("Aktueller Preis: ", price);
        Print("Lotgröße: ", LotSize);
        Print("Mindestlotgröße: ", minLotSize);
        Print("Maximale Lotgröße: ", maxLotSize);
        Print("SL: ", sl);
        Print("TP: ", tp);

        // Überprüfen, ob die Lotgröße gültig ist
        if (LotSize < minLotSize || LotSize > maxLotSize)
        {
            Print("Ungültige Lotgröße: ", LotSize);
            return;
        }

        // Überprüfen von SL und TP
        double slDistance = EnableSL ? MathAbs(price - sl) : 0; // SL-Distanz nur berechnen, wenn aktiviert
        double tpDistance = EnableTP ? MathAbs(price - tp) : 0; // TP-Distanz nur berechnen, wenn aktiviert
        int stopLevel = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL);

        if ((EnableSL && slDistance < stopLevel * _Point) || 
            (EnableTP && tpDistance < stopLevel * _Point))
        {
            Print("Stop-Loss oder Take-Profit verletzt die Mindestabstände.");
            return;
        }

        // Buy-Trade eröffnen
        MqlTradeRequest request={};
        MqlTradeResult result={};
        MqlTradeRequest closeRequest={};
        MqlTradeResult closeResult={};

        // Setzen der Parameter
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol; // Aktuelles Symbol
        request.volume = LotSize; // Lotgröße
        request.price = price; // Preis für Kauf
        request.sl = EnableSL ? sl : 0; // SL nur setzen, wenn aktiviert
        request.tp = EnableTP ? tp : 0; // TP nur setzen, wenn aktiviert
        request.type = ORDER_TYPE_BUY; // Ordertyp

        // Setzen des Filling Modes
        request.type_filling = ORDER_FILLING_IOC; // IOC 

        // Debugging-Ausgabe
        Print("OrderSend Anfrage: Preis=", request.price, " SL=", request.sl, " TP=", request.tp, " LotSize=", request.volume);

        // Order senden
        if (OrderSend(request, result))
        {
            if (result.retcode != TRADE_RETCODE_DONE)
            {
                Print("Fehler beim Eröffnen des Handels: ", result.retcode);
            }
            else
            {
                tradeOpened = true;
                Print("Trade eröffnet: ", result.order);
            }
        }
        else
        {
            Print("OrderSend fehlgeschlagen: ", GetLastError());
        }
    }
    else if (tradeOpened)
    {
        Print("Ein Trade ist bereits geöffnet.");
    }
    else
    {
        Print("Die Handelszeit stimmt nicht überein.");
    }

    // Prüfen, ob es Zeit ist, den Trade zu schließen
    if (tradeOpened && (currentHour == CloseHour && currentMinute == CloseMinute))
    {
        // Alle offenen Positionen durchlaufen
        for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(_Symbol))
            {
                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    // Trade schließen
                    ulong ticket = PositionGetInteger(POSITION_TICKET);
                    double volume = PositionGetDouble(POSITION_VOLUME);
                    
                    MqlTradeRequest request={};
                    MqlTradeResult result={};
                    MqlTradeRequest closeRequest={};
                    MqlTradeResult closeResult={};
                    
                    closeRequest.action = TRADE_ACTION_DEAL;
                    closeRequest.symbol = _Symbol;
                    closeRequest.volume = volume;
                    closeRequest.price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Preis zum Schließen
                    closeRequest.type = ORDER_TYPE_SELL; // Verkaufsauftrag zum Schließen

                    // Position schließen
                    if (OrderSend(closeRequest, closeResult))
                    {
                        tradeOpened = false; // Rücksetzen der Handelsflagge
                        Print("Trade geschlossen: ", closeResult.order);
                    }
                    else
                    {
                        Print("Fehler beim Schließen des Handels: ", closeResult.retcode);
                    }
                }
            }
            else
            {
                Print("Keine offene Position für das Symbol gefunden.");
            }
        }
    }
}
//+------------------------------------------------------------------+
 
Alexander Manfred Fleck #:

Here is the New Code, but now i got unsupportes Filling Mode.  The Filling Mode is IOC. In MT5 DE40 the Filling Mode is IOC too. 

I dont know what wents wrong

use this function to get the filling mode 

ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}
 
Lorentzos Roussos #:

use this function to get the filling mode 

Eight years have passed...

Features of the mql5 language, subtleties and tricks - How to use the standard trading class in order-send algorithm?
Features of the mql5 language, subtleties and tricks - How to use the standard trading class in order-send algorithm?
  • 2017.02.25
  • fxsaber
  • www.mql5.com
It would be better to write a separate function for waiting, so that the standard trading class could be used as well. There are two variants of the algorithm, if after ordersend() something must be done immediately: 1
 
 
Lorentzos Roussos #:
ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK ) {   const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);   const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);   return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?          (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?            ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :           (ENUM_ORDER_TYPE_FILLING)Type); }

Thank you. Now i got a other Problem. The trade should open at time xx  ( this works) and close at time xx next Day morning ( that doesnt work)

I think i dont see the little Problem in the code. COmpile error is 'PositionClose' - undeclared identifier 109 25


//+------------------------------------------------------------------+
//|                                                   DAX_Trader.mq5  |
//|                        Von [Ihr Name]                            |
//+------------------------------------------------------------------+
input double LotSize = 1.0;            // Handelslotgröße
input double StopLoss = 700;            // Stop-Loss in Punkten
input double TakeProfit = 700;          // Take-Profit in Punkten
input bool EnableSL = true;             // Stop-Loss aktivieren
input bool EnableTP = true;             // Take-Profit aktivieren
input int BuyHour = 17;                 // Stunde für Buy-Trade
input int BuyMinute = 30;               // Minute für Buy-Trade
input int CloseHour = 8;                // Stunde für Schließen
input int CloseMinute = 30;             // Minute für Schließen

bool tradeOpened = false;                // Flag, um zu prüfen, ob ein Trade geöffnet ist

//+------------------------------------------------------------------+
//| Funktion für den Filling Mode                                     |
//+------------------------------------------------------------------+
ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    datetime currentTime = TimeCurrent();
    MqlDateTime timeStruct;
    TimeToStruct(currentTime, timeStruct); // Zeit in Struktur umwandeln

    int currentHour = timeStruct.hour;   // Stunde extrahieren
    int currentMinute = timeStruct.min;   // Minute extrahieren

    // Prüfen, ob es Zeit ist, einen Trade zu eröffnen
    if (!tradeOpened && currentHour == BuyHour && currentMinute == BuyMinute)
    {
        double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Preis für Kauf
        double sl = EnableSL ? price - StopLoss * _Point : 0; // SL nur setzen, wenn aktiviert
        double tp = EnableTP ? price + TakeProfit * _Point : 0; // TP nur setzen, wenn aktiviert

        // Buy-Trade eröffnen
        MqlTradeRequest request={};
        MqlTradeResult result={};

        // Setzen der Parameter
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol; // Aktuelles Symbol
        request.volume = LotSize; // Lotgröße
        request.price = price; // Preis für Kauf
        request.sl = EnableSL ? sl : 0; // SL nur setzen, wenn aktiviert
        request.tp = EnableTP ? tp : 0; // TP nur setzen, wenn aktiviert
        request.type = ORDER_TYPE_BUY; // Ordertyp
        request.type_filling = GetFillingA(_Symbol); // Filling Mode

        // Order senden
        if (OrderSend(request, result))
        {
            if (result.retcode == TRADE_RETCODE_DONE)
            {
                tradeOpened = true;
                Print("Buy-Trade eröffnet: ", result.order);
            }
            else
            {
                Print("Fehler beim Eröffnen des Handels: ", result.retcode);
            }
        }
        else
        {
            Print("OrderSend fehlgeschlagen: ", GetLastError());
        }
    }

    // Prüfen, ob es Zeit ist, den Trade zu schließen
    if (tradeOpened && currentHour == CloseHour && currentMinute == CloseMinute)
    {
        // Alle offenen Positionen durchlaufen
        for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(_Symbol))
            {
                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    // Position schließen
                    if (PositionClose(_Symbol))
                    {
                        tradeOpened = false; // Rücksetzen der Handelsflagge
                        Print("Buy-Trade geschlossen.");
                    }
                    else
                    {
                        Print("Fehler beim Schließen des Handels: ", GetLastError());
                    }
                }
            }
            else
            {
                Print("Keine offene Buy-Position für das Symbol gefunden.");
            }
        }
    }
}
//+------------------------------------------------------------------+
 
Alexander Manfred Fleck #:

Thank you. Now i got a other Problem. The trade should open at time xx  ( this works) and close at time xx next Day morning ( that doesnt work)

I think i dont see the little Problem in the code. COmpile error is 'PositionClose' - undeclared identifier 109 25


i dont think there is a position close function , did you mean to use the trade.mqh library?

 
Alexander Manfred Fleck #:

Thank you. Now i got a other Problem.

You have a product for MT4 in the market. So you know how to program on MQL4. Then look at the forum threads where they show how to work with orders in MT5 the same way as in MT4.