Fractal High Verkaufen Fractal Low Kaufen Trailing Stop Stop Loss und Take Profit.

 

Hallo. Habe mich an einem Code Versucht bekomme es aber irgendwie nicht hin das er weder meinen Trailing Stop noch meinen Stop Loss oder Take Profit ausführt. Für Hilfe wäre ich Dankbar.

Hier der Code:

#include <Trade/Trade.mqh>

input double LotSize = 0.1; // Handelslosgröße
input double TrailingStop = 150; // Trailing stop in Punkten
input double StopLoss = 50; // Stop loss in Punkten
input double TakeProfit = 100; // Take profit in Punkten

int barsTotal;
int handle;
double fractalHighBuffer[];
double fractalLowBuffer[];

CTrade trade;

void OnTick()
{
    handle = iFractals(_Symbol, PERIOD_CURRENT);
    double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    // Überprüfen, ob es offene Positionen gibt
    if (PositionsTotal() == 0)
    {
        int bars = Bars(_Symbol, Period());
        if (barsTotal != bars)
        {
            barsTotal = bars;

            double fracUpper[];
            double fracLower[];

            // Fraktale Hochwerte abrufen
            int copiedUpper = CopyBuffer(handle, 0, 2, 1, fracUpper);

            // Fraktale Tiefwerte abrufen
            int copiedLower = CopyBuffer(handle, 1, 2, 1, fracLower);

            // Überprüfen Sie, ob ein Verkaufssignal vorliegt (Fraktal mit hohem Hoch)
            if (copiedUpper > 0 && fracUpper[0] != 0.0)
            {
                Print("Verkaufssignal erkannt. Verkaufen bei ", Ask);
                ulong posTicket = trade.Sell(LotSize, Ask, 0, 0, 0);
                if (posTicket != 0)
                {
                    double SL = Ask + StopLoss * _Point;
                    double TP = Ask - TakeProfit * _Point;
                    trade.PositionModify(posTicket, SL, TP);
                }
            }

            // Überprüfen Sie, ob ein Kaufsignal vorliegt (Fraktal mit tiefem Tief)
            if (copiedLower > 0 && fracLower[0] != 0.0)
            {
                Print("Kaufsignal erkannt. Kaufen bei ", Bid);
                ulong posTicket = trade.Buy(LotSize, Bid, 0, 0, 0);
                if (posTicket != 0)
                {
                    double SL = Bid - StopLoss * _Point;
                    double TP = Bid + TakeProfit * _Point;
                    trade.PositionModify(posTicket, SL, TP);
                }
            }
        }
    }
}

void OnStart()
{
    barsTotal = Bars(_Symbol, Period());
}

void OnTimer()
{
    // Diese Funktion wird aufgerufen, wenn ein Timer-Ereignis ausgelöst wird, falls erforderlich.
}
 

Wenn Du OnTick() und OnStart() verwendest, solltest Du Dich erst einmal informieren:

Hier mal, was für einen Anfänger sehr sinnvoll ist (MQL unterscheidet ich etwas von C und C++):

 1. Broker: https://www.mql5.com/de/forum/446736
 2. MQL5:
    Wenn Du den Cursor auf eine MQL-Funktion setzt und F1 drückst, siehst Du direkt die Referenz, viele mit Beispielen zum Kopieren und Einfügen der schnellsten Form des Programmierens.
    https://www.mql5.com/de/articles/496
    https://www.mql5.com/de/articles/100
    https://www.mql5.com/de/articles/599
    und zur Fehlersuche, wenn das Programm  nicht tut, was es soll: https://www.metatrader5.com/de/metaeditor/help/development/debug
    Es gibt fast nichts, das nicht schon für MT4/5 programmiert wurde!
    => Suchen in den Artikeln: https://www.mql5.com/de/articles
    => Suchen in der Codebase: https://www.mql5.com/de/code
    => Suchen allgemein: https://www.mql5.com/de/search oder über Google mit: "site:mql5.com .." (verzeiht Schreibfehler)
    https://www.mql5.com/de/search#!keyword=kochbuch
 3. Indikatoren: siehe diese Artikelserie:
    https://www.mql5.com/de/users/m.aboud/publications
    Jeder Artikel erklärt einen (im MT5 integrierten) Indikator und, was er einem sagt, und wie man ihn in einem EA verwenden könnte.
 4. Foundations of Computer Science: http://i.stanford.edu/~ullman/focs.html
    MQL5: https://www.mql5.com/files/pdf/mql5_german.pdf, https://www.mql5.com/files/docs/mt5/mql5/chm/mql5_german.chm
 5. Installiern & nutzen des Markets: https://www.mql5.com/en/forum/366161


Broker suchen
Broker suchen
  • 2023.05.03
  • www.mql5.com
MetaQuotes (MQ) ist KEIN Broker und bietet auch keine Finanz-, Anlage-, Makler-, Handels- oder Datenfeed-Dienstleistungen an und ist auch nicht an...
 

Hallo. Danke für deine Hilfe un die hilfreichen Links. Habe in meinem Code die On Start gesucht und dabei gesehen das ich die falsche Gepostet hatte. Hänge die richtige noch einmal an.

#include <Trade/Trade.mqh>
input double Lots = 0.2;
input double RiskPercent = 2.0;
input string TimeStart = "07:00";
input string TimeEnd = "18:30";

input int OrderDistPoints = 200;
input int TpPoints = 200;
input int SlPoints = 200;
input int TslPoints = 5;
input int TslTriggerPoints = 10;
input int Magic = 111;
input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;
input int ExpirationHours = 50;
int handle;
int barsTotal;

CTrade trade;
ulong posTicket;
ulong buyPos, sellPos;

int OnInit()
  {

   handle = iFractals(_Symbol,PERIOD_CURRENT);

   barsTotal = iBars(_Symbol,PERIOD_CURRENT);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
   datetime timeEnd = StringToTime(TimeEnd);
   datetime timeStart = StringToTime(TimeStart);
   
   
   
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   
  
  int bars = iBars(_Symbol,Timeframe);
   if(barsTotal != bars && TimeCurrent() > timeStart && TimeCurrent() < timeEnd){
      barsTotal = bars;  
      
  
 
  double fracUpper[];
  double fracLower[]; 
  
  CopyBuffer(handle,UPPER_LINE,2,1,fracUpper);
  CopyBuffer(handle,LOWER_LINE,2,1,fracLower);
  
  bool sellPos = false;
  bool buyPos = false;
  
  if(fracUpper[0] != EMPTY_VALUE){
    Print("sell signal...");
    sellPos = true;
    if(posTicket > 0){
     if(PositionSelectByTicket(posTicket)){
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){
        if(trade.PositionClose(posTicket));
          if (posTicket = 0)
          {
           double SL = fracUpper[0] - TslPoints * _Point;
                    trade.PositionModify(posTicket, SL, 0);
        }
        
      }
     }else{ 
      }
      {
       posTicket = 0;
     }
    
    }
    
  }
   if(fracLower[0] != EMPTY_VALUE){
      Print("buy signal...");
      buyPos = true;
          if(posTicket > 0){
     if(PositionSelectByTicket(posTicket)){
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
        if(trade.PositionClose(posTicket));
           if (posTicket = 0)
           {
            double SL = fracLower[0] + TslPoints * _Point;
                    trade.PositionModify(posTicket, SL, 0);
        }
      }
     }else{
     }
     {        
       posTicket = 0;
     }
    
    }
      
  }
  

 
  
   if(posTicket <=0){
   if(sellPos && !buyPos){
      if(trade.Sell(0.1)){
       posTicket = trade.ResultOrder();
      
      }
   }
   if(buyPos && !sellPos){
    if(trade.Buy(0.1)){
     posTicket = trade.ResultOrder();
    }
    }
   }
  }
  }
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
{
   if (trans.type == TRADE_TRANSACTION_ORDER_ADD)
   {
      COrderInfo order;
      if (order.Select(trans.order))
      {
         if (order.Magic() == Magic)
         {
            if (order.OrderType() == ORDER_TYPE_BUY)
            {
               buyPos = order.Ticket();
            }
            else if (order.OrderType() == ORDER_TYPE_SELL)
            {
               sellPos = order.Ticket();
            }
         }
      }
   }
}

void processPos(ulong & posTicket)
{
   if (posTicket <= 0)
      return;
   if (OrderSelect(posTicket))
      return;

   CPositionInfo pos;
   if (!pos.SelectByTicket(posTicket))
   {
      posTicket = 0;
      return;
   }
   else
   {
      if (pos.PositionType() == POSITION_TYPE_BUY)
      {
         double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

         if (bid > pos.PriceOpen() + TslTriggerPoints * _Point)
         {
            double sl = bid - TslPoints * _Point;
            sl = NormalizeDouble(sl, _Digits);

            if (sl > pos.StopLoss())
            {
               trade.PositionModify(pos.Ticket(), sl, pos.TakeProfit());
            }
         }
      }
      else if (pos.PositionType() == POSITION_TYPE_SELL)
      {
         double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

         if (ask < pos.PriceOpen() - TslTriggerPoints * _Point)
         {
            double sl = ask + TslPoints * _Point;
            sl = NormalizeDouble(sl, _Digits);

            if (sl < pos.StopLoss() || pos.StopLoss() == 0)
            {
               trade.PositionModify(pos.Ticket(), sl, pos.TakeProfit());
            }
         }
      }
   }
}
void executeBuy(double entry)
{
   entry = NormalizeDouble(entry, _Digits);

   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   if (ask > entry - OrderDistPoints * _Point)
      return;

   double tp = entry + TpPoints * _Point;
   tp = NormalizeDouble(tp, _Digits);

   double sl = entry - SlPoints * _Point;
   sl = NormalizeDouble(sl, _Digits);

   double lots = Lots;
   if (RiskPercent > 0)
      lots = calcLots(entry - sl);
   datetime timeEnd = StringToTime(TimeEnd);

   datetime expiration = iTime(_Symbol, Timeframe, 0) + ExpirationHours * PeriodSeconds(PERIOD_H1);

   if (expiration < timeEnd)
   {
      trade.BuyStop(lots, entry, _Symbol, sl, tp, ORDER_TIME_SPECIFIED, expiration);
   }
   else
   {
      trade.BuyStop(lots, entry, _Symbol, sl, tp, ORDER_TIME_SPECIFIED, timeEnd);
   }

   Print(expiration);

   buyPos = trade.ResultOrder();
}

void executeSell(double entry)
{
   entry = NormalizeDouble(entry, _Digits);

   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   if (bid < entry + OrderDistPoints * _Point)
      return;

   double tp = entry - TpPoints * _Point;
   tp = NormalizeDouble(tp, _Digits);

   double sl = entry + SlPoints * _Point;
   sl = NormalizeDouble(sl, _Digits);

   double lots = Lots;
   if (RiskPercent > 0)
      lots = calcLots(sl - entry);
   datetime timeEnd = StringToTime(TimeEnd);

   datetime expiration = iTime(_Symbol, Timeframe, 0) + ExpirationHours * PeriodSeconds(PERIOD_H1);

   if (expiration < timeEnd)
   {
      trade.SellStop(lots, entry, _Symbol, sl, tp, ORDER_TIME_SPECIFIED, expiration);
   }
   else
   {
      trade.SellStop(lots, entry, _Symbol, sl, tp, ORDER_TIME_SPECIFIED, timeEnd);
   }

   sellPos = trade.ResultOrder();
}

double calcLots(double slPoints)
{
   double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100;

   double ticksize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   double tickvalue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   double lotstep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);

   double moneyPerLotstep = slPoints / ticksize * tickvalue * lotstep;
   double lots = MathFloor(risk / moneyPerLotstep) * lotstep;

   lots = MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX));
   lots = MathMax(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN));

   return lots;
}
 

Gut!

Und jetzt setzt Du einen Breakpoint (siehe oben 'Debugger') an den ifs for OrderModify() und prüfe im bzw. mit den Debugger, warum Dein Programm nicht zu diesem Befehl kommt.

Das wäre die normale Vorgangsweise.
 
Danke. Dann werde ich mich mal in den Debugger Einlesen und Einarbeiten wie ich damit meine Fehler finde.
Grund der Beschwerde: