Stopp unter Kerze - Seite 2

 
MK-Trading:

Hallo, 

habe leider nichts dazu gefunden gibt es eine Möglichkeit bzw. Einstellung bei MT5 das sich der Stopp automatisch immer auf die nächste Kerze anpasst?

   Hallo,

   hier ist der EA der den Stopp unter eine Kerze setzt. Wird aber ohne zusätzliche Strategie das Depot auf Null setzen! Deswegen nicht im realen Account Testen!!


//+------------------------------------------------------------------+
//|                                             Stop Unter Kerze.mq5 |
//|                                       Copyright 2021, Pay24Money |
//|                                                  https://p24m.eu |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Pay24Money"
#property link      "https://p24m.eu"
#property version   "1.00"

#include <Trade/Trade.mqh>
#include <Trade/SymbolInfo.mqh>
CTrade m_trade;
CSymbolInfo m_symbol;
CPositionInfo  m_position;
CHistoryOrderInfo m_history;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
input ulong         Magic = 0;    // Magicnummer 0 alle orders beachten (12345) Nur unter bestimmte Magic Nummer)
input bool   OrdersSetzen = true; // Auf false setzen, wenn Orders manuel gesetzt werden!
input double         Lots = 0.10; // Volumeneingabe

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {

   if(!m_symbol.Name(_Symbol)) // sets symbol name
      RefreshRates();
//---
   m_trade.SetExpertMagicNumber(Magic);
//---
   m_trade.SetMarginMode();
   m_trade.SetDeviationInPoints(100);
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

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

}

void OnTick() {

   double Ask      = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); // Ask aktueller Preis
   double Bid      = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); // Bid aktueller Preis
   ulong  Spread   = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);                      // Aktueller Spread

   double LetzteKerzeLow  = iLow(NULL,PERIOD_CURRENT,1);                  // Tief von letzte Kerze
   double LetzteKerzeHigh = iHigh(NULL,PERIOD_CURRENT,1);                 // Hoch von letzte Kerze

//+------------------------------------------------------------------+
//| Trailing Funktion                                                |
//+------------------------------------------------------------------+
   if (Fun_New_Bar(_Symbol,PERIOD_CURRENT)==true) {
      Trailing();
   }

   static ENUM_POSITION_TYPE tipe_letzte_position=-1;
   for(int i=PositionsTotal()-1; i>=0; i--)                                // Gibt die Anzahl der aktuellen Positionen zurück
      if(m_position.SelectByIndex(i))                                      // wählt die Position nach Index für den weiteren Zugriff auf seine Eigenschaften aus
         if(m_position.Symbol()== _Symbol && m_position.Magic()==Magic) {
            tipe_letzte_position=m_position.PositionType();                // Ruft den Positionstyp ab
            return;
         }

   // Orders automatich setzen! Achtung es werden immer nach einem Stop ein Order gesetzt!
   // OrdersSetzen auf False setzen!!
   if(OrdersSetzen == true) {

      string symbol = _Symbol;                                      // specify the symbol, at which the order is placed
      int    digits = (int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal places
      double  point = SymbolInfoDouble(symbol,SYMBOL_POINT);        // point
      double    bid = SymbolInfoDouble(symbol,SYMBOL_BID);          // current bid price
      double    ask = SymbolInfoDouble(symbol,SYMBOL_ASK);          // current buy price

      if(bid > LetzteKerzeHigh) {

         double price = ask*point;                                   // unnormalized open price
         price = NormalizeDouble(price,digits);               // normalizing open price
         double    SL = LetzteKerzeLow - (Spread*point);             // Stop Loss in points
         SL = NormalizeDouble(SL,digits);                  // normalizing Stop Loss

         m_trade.Buy(Lots,NULL,price,SL,0,NULL);                     // Order senden


         RefreshRates();
      } else if(ask < LetzteKerzeLow) {

         double price = bid*point;                                   // unnormalized open price
         price = NormalizeDouble(price,digits);               // normalizing open price
         double    SL = LetzteKerzeHigh + (Spread*point);             // Stop Loss in points
         SL = NormalizeDouble(SL,digits);                  // normalizing Stop Loss

         m_trade.Sell(Lots,NULL,price,SL,0,NULL);                     // Order senden

         RefreshRates();
      }
   }

}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates() {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
}
//+------------------------------------------------------------------+
// Trailingstop
void Trailing() {
   bool TrailingStop = true;

   ulong  Spread   = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);                      // Aktueller Spread
   double LetzteKerzeLow  = iLow(NULL,PERIOD_CURRENT,1);                  // Tief von letzte Kerze
   double LetzteKerzeHigh  = iHigh(NULL,PERIOD_CURRENT,1);                // Hoch von letzte Kerze
   string symbol = _Symbol;
   double    bid = SymbolInfoDouble(symbol,SYMBOL_BID);          // current bid price
   double    ask = SymbolInfoDouble(symbol,SYMBOL_ASK);          // current buy price
   for(int i=PositionsTotal()-1; i>=0; i--) // Gibt die Anzahl der offenen Positionen zurück

      if(m_position.SelectByIndex(i)) {
         RefreshRates();

         //---
         double price_current = m_position.PriceCurrent();
         double price_open    = m_position.PriceOpen();
         double stop_loss     = m_position.StopLoss();
         double take_profit   = m_position.TakeProfit();
         double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
         double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         double SLBUY = LetzteKerzeLow - (Spread*_Point);               // Buy Stop Loss in points
         double SLSELL = LetzteKerzeHigh + (Spread*_Point);             // Sell Stop Loss in points
         double TralStopBuy = SLBUY * _Point;
         double TralStopSell = SLSELL * _Point;
         //---
         if(m_position.Symbol()== _Symbol && m_position.Magic()==Magic) { // Prüft nach Symbol und Magicnummer
            if(m_position.PositionType()==POSITION_TYPE_BUY) {
               if(TrailingStop && bid > price_open) {
                  if(!m_trade.PositionModify(m_position.Ticket(),m_symbol.NormalizePrice(SLBUY),take_profit))
                     RefreshRates();
                  continue;
               }
            }

            if (m_position.PositionType()==POSITION_TYPE_SELL) {
               if(TrailingStop && ask < price_open) {
                  if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(SLSELL),take_profit))
                     RefreshRates();
                  continue;
               }
            }
         }
      }
}
//+------------------------------------------------------------------+
//| Funktion Timeframe und Chartperiod                               |
//+------------------------------------------------------------------+
bool Fun_New_Bar(string symbol,ENUM_TIMEFRAMES timeframe) {
   static datetime Prev_Time=0;
   if(Prev_Time == 0) {
      Prev_Time = iTime(symbol,timeframe,0);
      return(false);
   }
   if(Prev_Time!=iTime(symbol,timeframe,0)) {
      Prev_Time = iTime(symbol,timeframe,0);
      return(true);
   }
   return(false);
}
//+------------------------------------------------------------------+

Ich hoffe, dass ich Ihnen helfen konnte.


Gruß Igor.

 
Pay24Money:

   Hallo,

   hier ist der EA der den Stopp unter eine Kerze setzt. Wird aber ohne zusätzliche Strategie das Depot auf Null setzen! Deswegen nicht im realen Account Testen!!


Ich hoffe, dass ich Ihnen helfen konnte.


Gruß Igor.

Danke schon mal von mir! :-) Eine daran angelehnte, etwas komplexere Trailing-Stopp-Logik nutze ich gerne in meinem manuellen Trading.

Diesen EA zu verstehen und zu modifizieren werde ich deshalb zu meinem ersten Projekt machen, aus dem ich hoffentlich viel lernen kann.
Grund der Beschwerde: