Sell Position wird nicht geschlossen

 

Alles funktioniert eigentlich gut, bis auf das schließen meiner Positionen, passiert einfach nichts, keine Fehlermeldung, return code ist 0. Bin neu hab also keine Ahnung was ich hier falsch mache (ich weiß ist ziemlich schlecht formattiert und überkompliziert):

#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <trade/trade.mqh>

input ENUM_TIMEFRAMES timeframe = PERIOD_M15;
input int tpPoints = 150;
input int slPoints = 3000;
input int bandsPeriod = 20;
input int bandsShift = 0;
input double bandsDeviation = 2.0;
input double Lots = 0.01;
input int cooldown = 500;
input int tradeStartHour = 4;
input int tradeEndHour = 22;
input int maxSpread = 500;
int cool = 0;
int tradeOpen = 0;
double accountBalance;
CTrade trade;
ulong myticket;

int totalBars;
int BandsHandle = iBands(_Symbol, timeframe, bandsPeriod, bandsShift, bandsDeviation, PRICE_CLOSE);
int RsiHandle = iRSI(_Symbol, timeframe, 14, PRICE_CLOSE);

int OnInit()
  {
   totalBars = iBars(_Symbol, timeframe);
   return(INIT_SUCCEEDED);
   
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
{
datetime currentTime = TimeCurrent();
MqlDateTime timeInfo;
TimeToStruct(currentTime, timeInfo);
int currentHour = timeInfo.hour;
int barsNow = iBars(_Symbol, timeframe);

if(barsNow > totalBars){
//new bar was created
Print("New Bar");
totalBars = barsNow;

if(currentHour >= tradeStartHour && currentHour < tradeEndHour) {
   double currentSpread = SymbolInfoDouble(_Symbol, SYMBOL_ASK) - SymbolInfoDouble(_Symbol, SYMBOL_BID);
   if(MathRound(currentSpread) <= maxSpread * _Point) {
      if(cool == 0) {
      if(tradeOpen == 0) {
         Print("Trading allowed");
         double middleLine[];
         double upperBand[];
         double lowerBand[];
         double rsi[];
         CopyBuffer(BandsHandle, 0, 1, 1, middleLine);
         CopyBuffer(BandsHandle, 1, 1, 1, upperBand);
         CopyBuffer(BandsHandle, 2, 1, 1, lowerBand);
         CopyBuffer(RsiHandle, 0, 1, 1, rsi);
      
         double close = iClose(_Symbol, timeframe, 1);
      
         if(close < lowerBand[0] && rsi[0] < 30){
            //buy signal
            double entry = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            entry = NormalizeDouble(entry, _Digits);
            double tp = NormalizeDouble(middleLine[0], _Digits);
            //entry + tpPoints * _Point;
            tp = NormalizeDouble(tp, _Digits);
            double sl = entry - slPoints * _Point;
            sl = NormalizeDouble(sl, _Digits);
            accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
            trade.Buy(Lots, _Symbol, entry, sl, tp, "Karobot");
         
            tradeOpen = 1;
         } else if(close > upperBand[0] && rsi[0] > 70){
            //sell signal
            double entry = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            entry = NormalizeDouble(entry, _Digits);
            double tp = NormalizeDouble(middleLine[0], _Digits);
            //entry - tpPoints * _Point;
            tp = NormalizeDouble(tp, _Digits);
            double sl = entry + slPoints * _Point;
            sl = NormalizeDouble(sl, _Digits);
            accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
            trade.Sell(Lots, _Symbol, entry, sl, tp, "Karobot");
         
            tradeOpen = 1;
         }
      } else {
         Print("trade is open");
         double middleLine[];
         CopyBuffer(BandsHandle, 0, 1, 1, middleLine);
         
         double close = iClose(_Symbol, timeframe, 1);
         
         if(close > middleLine[0]){
            Print("should close buys now");
            for (int i = 0; i < PositionsTotal(); i++)
            {
               myticket = PositionGetTicket(i);
               Print("Ticket: ", myticket);
               // Get the ticket number of the position at index i
               // Check if the selected position is a buy position
               if (PositionGetInteger(POSITION_TYPE, myticket) == POSITION_TYPE_BUY)
               {
                  Print("Identified buy pos, closing it now...");
                  // Close the buy position
                  trade.PositionClose(myticket);
               }
            }
         } else if(close < middleLine[0]){
            Print("should close sells now");
            Print("Sells should really close now");
            // Loop through all positions
            for (int i = 0; i < PositionsTotal(); i++)
            {
               myticket = PositionGetTicket(i);
               Print("Ticket: ", myticket);
               // Get the ticket number of the position at index i
               // Check if the selected position is a sell position
               if (PositionGetInteger(POSITION_TYPE, myticket) == POSITION_TYPE_SELL)
               {
                  Print("Identified sell pos, closing it now...");
                  // Close the sell position
                  trade.PositionClose(myticket);
               }
            }
         }
         
         if(PositionsTotal() == 0){
            if(accountBalance > AccountInfoDouble(ACCOUNT_BALANCE)){
               cool = cooldown;
            }
            tradeOpen = 0;
         }
      }
   } else {
      cool = cool - 1;
   }
  }
} else if(PositionsTotal() != 0){
         Print("Trade open in restricted time");
         double middleLine[];
         CopyBuffer(BandsHandle, 0, 1, 1, middleLine);
         
         double close = iClose(_Symbol, timeframe, 1);
         
         if(close > middleLine[0])
         {
            Print("should close buys now");
            for (int i = 0; i < PositionsTotal(); i++)
            {
               myticket = PositionGetTicket(i);
               Print("Ticket: ", myticket);
               // Get the ticket number of the position at index i
               // Check if the selected position is a buy position
               if (PositionGetInteger(POSITION_TYPE, myticket) == POSITION_TYPE_BUY)
               {
                  Print("Identified buy pos, closing it now...");
                  // Close the buy position
                  trade.PositionClose(myticket);
               }
            }
          } else if(close < middleLine[0])
          {
            Print("should close sells now");
            Print("Sells should really close now");
            // Loop through all positions
            for (int i = 0; i < PositionsTotal(); i++)
            {
               myticket = PositionGetTicket(i);
               Print("Ticket: ", myticket);
               // Get the ticket number of the position at index i
               // Check if the selected position is a sell position
               if (PositionGetInteger(POSITION_TYPE, myticket) == POSITION_TYPE_SELL)
               {
                  Print("Identified sell pos, closing it now...");
                  // Close the sell position
                  trade.PositionClose(myticket);
               }
            }
          }
} 

}
}

Sollte eigentlich trades schließen wenn sie die mittlere Linie überkreuzen. Siehe angehangenes Bild von Strategy Tester.

Dateien:
trouble.png  137 kb
karobotv2.mq5  7 kb
 

Die Funktion PositionClose(..) gibt zurück, ob sie erfolgreich war und, wenn nicht, setzt sie die Fehlernummer, beides wird von Dir nicht geprüft.

Geh mit dm Debugger auch durch die Funktion PositionClose() und beobachte was passiert. So erkennst Du was schief läuft:

Code debugging:  https://www.metatrader5.com/de/metaeditor/help/development/debug

Code-Debugging - Programme entwickeln - MetaEditor Hilfe
  • www.metatrader5.com
MetaEditor hat einen eingebauten Debugger, mit dem Sie die Programmausführung Schritt für Schritt (durch einzelne Funktionen) ü...
 
Wie schon gesagt eine Fehlernummer hat es nicht gesetzt bzw. ich habe 0 bekommen und würde vermuten, dass 0 bedeutet kein Fehler. Ich probiere jetzt trotzdem nochmal den debugger, danke für die Antwort.
Grund der Beschwerde: