Zweistufige Modifizierung von Offenen Positionen

Genkov | 25 März, 2016

Einführung

Der Artikel mit dem Namen "T.DeMark's Approach to Technical Analysis" enthält die empfohlenen Koeffizienten der Korrekturlänge, insbesondere 0.382 und 0.618. Die Verwendung dieser Koeffizienten bei dem Öffnen von Positionen , können Sie unnötige Situationen des Schließens und Neu-Öffnen von Positionen in Situationen nah am Trend vermeiden. Die Funktion funktioniert gut, besonders in der Situation des Auftretens von Divergenz.

Dieser Ansatz, sofern der Gewinnwert zurückgesetzt wird, hilft das Aufkommen eines "günstigen" Trends zu erkennen. Zum Beispiel, wie es in Abb. 1 verglichen mit Abb. 2 gezeigt wird.



Funktion Algorithmus

Die erste Änderung wird durch den angegebenen TrailingStop durchgeführt, die nachfolgenden setzen den StopLoss kleiner als die mögliche Korrekturebene von 1 oder 2 Punkten (in diesem Fall ist der Korrektur-Koeffizient = 0,382 "Coeff_"). Erhöhen Sie den Wert des Take Profit bei jedem Schritt um, zum Beispiel, die Hälfte des TrailingStop Werts (Sie können auch einen anderen Wert wählen!). Der Wert des TakeProfit kann ebenfalls geändert werden. Zu diesem Zweck sollte der Null-Wert des externen March = 0; Operator am Anfang des Programms eingestellt werden.

Es wäre klüger für Trader, die direkt beim Trading durchgeführte Adressanalyse-Unterstützung programmspezifischer Aktionen bevorzugen, die MagicNumber Variable direkt in den Code des Expert Advisor selbst zu übertragen, in dem die Position geöffnet wird. Sie können ausführliche Informationen über die spezifische Adressunterstützung in dem Buch von S. Kovalyov lesen, veröffentlich auf der MQL4.com Webseite.


Untersuchen wir den Code der potentiellen Funktion in einem EA und die Kommentare im Detail:

//+------------------------------------------------------------------+
//|      Two-stage variant of TrailingStop       Modify_2step v5.mq4 |
//|   Orders modification: re-placing StopLoss and TakeProfit        |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, GenKov"
#property link      Genkov@bk.ru
//+------------------------------------------------------------------+

/* Magic=N

Magic=N - dieser Operator sollte während dem Öffnen der Position direkt den die Einhaltung der Bedingung prüfenden Operator in das Programm selbst (Expert Advisor), und in die Funktion! Ich konnte keinen universellen Modifizierer erstellen, vielleicht in Hinblick auf die Unvorhersehbarkeit des Marktes, da es für mich so scheint, so dass die Funktion der Verfolgung (Bewegung von S/L und T/P) und die Position-Schließbedingungen für jede Art der Position-Öffnungsbedingungen geschrieben werden sollte (durch Magic=N).

extern double    March         =  1;  // step of increasing TakeProfit 
                                      // step 0 doesn't increase T/P.

S/L muss um 1 Punkt kleiner sein als Trailing Stop, um S/L bei der ersten Auslösung auf eine sichere Ebene zu bringen. Auf eine solche Weise, schützen wir uns selbst vor möglichen Verlusten (Kapitalmanagement).

extern double    StopLoss      = 15;  
extern double    TrailingStop  = 16;  
extern double    TakeProfit    = 60;  // fitting with the tester
//+------------------------------------------------------------------+
//void TrailingStop()
  int start()                                   
   {
   //----------------------------------------------------------------+
   int point  = MarketInfo(Symbol(),MODE_POINT);      // Point size 
   int StopLev= MarketInfo(Symbol(),MODE_STOPLEVEL);  
   double half_Trail = MathRound(TrailingStop/2);//half TrailingStop
   double Step = March*half_Trail;  //value of TakeProfit increase
  if (TrailingStop<0) return;
   { 
   for (int i=0; i<OrdersTotal(); i++)
    {
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
    if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
    if (OrderType()==OP_BUY)
     {

Erste Stufe der BUY Position Änderung

if(OrderStopLoss()<OrderOpenPrice())//if S/L is less than the order open price
      {   // and if the difference between the current price and the position opening price is greater than T/S
      if(Bid-OrderOpenPrice()>TrailingStop*Point) // && 
       {     // and if OrderStopLoss() is less than the difference between the current price and T/S
       if(OrderStopLoss()<Bid-TrailingStop*Point)
        {    // calculate new T/P value
        double Now_T_P=(OrderTakeProfit()+Step*Point);
       { 
       OrderModify(OrderTicket(),OrderOpenPrice(),
       OrderStopLoss()+TrailingStop*Point,
       OrderTakeProfit()+Step*Point,0,Aqua); // increasing T/P value
      return;
      }
     }
    }
   }

Es kann jedoch die folgende Situation auftreten: TakeProfit wird 2-3 Punkte höher als die vorher geplante Gewinnebene, stoppt und beginnt sich langsam zu verringern.



Um verpasste Gewinne zu vermeiden, geben wir den Operator der Situationskontrolle ein, der die Order an der geplanten Gewinnebene schließen wird. Wenn der Kurs weiter steigt, wird die Verschiebung von StopLoss und TakeProfit fortgesetzt.

if(Bid-OrderOpenPrice()>=TakeProfit*Point && (Pr_Op_1-Pr_Op_0)>2*Point) 
   {
    // Print(" Bid= ",Bid," >= ",OrderTakeProfit()," Magic= ",Magic);
    OrderClose(OrderTicket(),Lots,Bid,2,Red);
    }
  if(OrderStopLoss()>=OrderOpenPrice()) // StopLoss is on a lossless level
   {    // calculate correction coefficient
    double Coeff_up = NormalizeDouble((Bid-OrderOpenPrice())*0.382,Digits);
    // and if the differnece between the current and the open price of the position is greater than corr. coefficient 
    if(Bid-OrderOpenPrice()>Coeff_up) 
     {    // calculate the value of new StopLoss with the margin of 2 points
      double New_S_Loss = Bid-Coeff_up-2*Point;
      // and if the value of new StopLoss is higer than the current one
      if(New_S_Loss-OrderStopLoss()>3*Point)
       {     // move S/L and T/P
        OrderModify(OrderTicket(),OrderOpenPrice(),
        New_S_Loss,OrderTakeProfit()+Step*Point,0,Yellow);
        }
//        Print(" Bid-OrderOpenPrice()= ",Bid-OrderOpenPrice());
//        Print("  2   Coeff_up= ",Coeff_up," Order_S_Los= ",New_S_Loss," Bid= ",Bid);
       return;
       }
      }
     }

// Zweite Stufe der BUY Position Änderung

Der Ansatz für die Short-Position ist der gleiche wie oben beschrieben, also sind hier weniger Kommentare.

// ---------------------------- 1 stage of modification -----SELL-------------&
   else  if(OrderType()==OP_SELL) 
    {
   if(OrderStopLoss()>OrderOpenPrice())//if S/L is greater than order open price
     {
     if(OrderOpenPrice()-Ask>TrailingStop*Point && 
        OrderStopLoss()>Ask+TrailingStop*Point)
     { 
      OrderModify(OrderTicket(),OrderOpenPrice(),
      Ask+TrailingStop*Point,OrderTakeProfit()-Step*Point,0,SkyBlue);
      return;
      }
     }
if(OrderOpenPrice()-Ask>=TakeProfit*Point && (Pr_Op_0-Pr_Op_1)>2*Point) 
   {
    OrderClose(OrderTicket(),Lots,Bid,2,Red);
    }     
// ---------------------------- 2 stage of modification -----SELL-------------&
   if(OrderStopLoss()<=OrderOpenPrice()) // StopLoss is on a lossless level
    { // calculate correction coefficient
     double Coeff_down = NormalizeDouble((OrderOpenPrice()-Ask)*0.382,Digits);
     // and if the difference between the price of position opening and the current price is greater than corr. coefficient 
    if(OrderOpenPrice()-Ask>Coeff_down) 
     {    // calculate the value of new StopLoss with the margin of 2 points
      New_S_Loss = Ask+Coeff_down+2*Point; 
      // and if the value of new StopLoss is less than the current value
      if(New_S_Loss-OrderStopLoss()>3*Point)
       {     // move S/L and T/P   
       OrderModify(OrderTicket(),OrderOpenPrice(),
       New_S_Loss,OrderTakeProfit()-Step*Point,0,Khaki);
      return;
      }
     }
    }
   }
  } 
 //  -----------------------------------------------------------------------------------

Um diesen EA in eine Funktion zu ändern, ist es notwendig die die spezielle int start() Funktion, die sich am Anfang des Programms befindet, aus-zu-kommentieren, und mit der unkommentierten Beschreibung der TrailingStop() Funktion am Anfang des Programms zu ersetzen. Entfernen Sie die Kommentierung des Funktionsaufrufs:

//TrailingStop();

am Ende des Programms.

Wenn wir den unten dargestellten Block hinzufügen, können wir die Effizienz der Funktion prüfen, in dem wir sie als EA in einem Tester verwenden.

// --------------------------------------------------------------------------------
  
   double Macd_m15_0= iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   double Macd_m15_1= iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   
   if(OrdersTotal()<2)
    {
    if(Macd_m15_0<Macd_m15_1)
     {
     OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"",Magic,0,Red);
     }
    if(Macd_m15_0>Macd_m15_1)
     {
     OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"",Magic,0,Blue);
     }
    return(0);
   }
// --------------------------------------------------------------------------------
//  TrailingStop();
  }
   return(0);
  }
// --- end --- &

Entfernen Sie nun die detaillierten Kommentare aus dem oben erwähnten Code-Text und bilden Sie ihn als eine ausführende Funktion: wir erhalten eine ausführbare Datei, für die empfohlen wird sie in dem Verzeichnis terminal_folder\experts\include mit der .mqh Erweiterung zu speichern, oder in den Terminal_Ordner\Libraries Verzeichnis mit der mq4 Erweiterung.



//+------------------------------------------------------------------+
//|                                             Modify_2_Step v5.mq4 |
//|                                         Copyright © 2008, GenKov |
//|                                                     Genkov@bk.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, GenKov"
#property link      "Genkov@bk.ru"
extern double    March         =  1;  // the step of TakeProfit increase
                                      // "0" step doesn't increase T/P.
// S/L must be less than TrailingStop by 1 point to bring the S/L  
// on a safe level in the time of the very first triggering
extern double    StopLoss      = 15;  
extern double    TrailingStop  = 16;
extern double Lots             = 0.1;  
extern double    TakeProfit    = 60;  // fitting with the tester
void TrailingStop()
  {
   int Magic=3090;  //  number of condition that opens position
   int point  = MarketInfo(Symbol(),MODE_POINT);      // Point size 
   int StopLev= MarketInfo(Symbol(),MODE_STOPLEVEL);  
   double half_Trail = MathRound(TrailingStop/2);//half TrailingStop
   double Step = March*half_Trail;  //TakeProfit-а increase size
  if (TrailingStop<0) return;
   { 
   for (int i=0; i<OrdersTotal(); i++)
    {//1 +cycle by orders search
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
    if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
    if (OrderType()==OP_BUY)
     {
// --------------------------- 1 stage of modification -----BUY-------------&     
    if(OrderStopLoss()<OrderOpenPrice())//if the S/L is less than the order open price
      {   // and if the difference between the current and the open price is greater than T/S
      if(Bid-OrderOpenPrice()>TrailingStop*Point) // && 
       {     // and if OrderStopLoss() is less than the difference between current price andT/S
       if(OrderStopLoss()<Bid-TrailingStop*Point)
        {    // calculate new T/P value
        double Now_T_P=(OrderTakeProfit()+Step*Point);
       { 
       OrderModify(OrderTicket(),OrderOpenPrice(),
       OrderStopLoss()+TrailingStop*Point,
       OrderTakeProfit()+Step*Point,0,Aqua); // increase T/P value
      return;
      }
     }
    }
   }
  if(Bid-OrderOpenPrice()>=TakeProfit*Point) 
   {
    OrderClose(OrderTicket(),Lots,Bid,2,Red);
    }
//------------------------- 2 stage of modification -----BUY---------------&
  if(OrderStopLoss()>=OrderOpenPrice()) // StopLoss is on the lossless level
   {    // calculate correction coefficient
    double Coeff_up = NormalizeDouble((Bid-OrderOpenPrice())*0.382,Digits);
    // and if the difference between the current and the position price is greater than correction coefficient
    if(Bid-OrderOpenPrice()>Coeff_up) 
     {    // clculate new StopLoss value with the margin of 6 points
      double New_S_Loss = Bid-Coeff_up-6*Point-StopLev*Point;
      // if the value of new StopLoss is greater than the current value
      if((New_S_Loss-OrderStopLoss())<2*Point)
       {     // move S/L and T/P
        OrderModify(OrderTicket(),OrderOpenPrice(),
        OrderStopLoss(),OrderTakeProfit()+Step*Point/2,0,Yellow);
        }
        else
        {
         OrderModify(OrderTicket(),OrderOpenPrice(),
        New_S_Loss+1*Point,OrderTakeProfit()+Step*Point,0,Yellow);
        }
       return;
       }
      }
     }
// ---------------------------- 1 stage of modification -----SELL-------------&
  else if(OrderType()==OP_SELL)
    {
   if(OrderStopLoss()>OrderOpenPrice())//if S/L is greater than the order open price
     {
     if(OrderOpenPrice()-Ask>TrailingStop*Point && 
        OrderStopLoss()>Ask+TrailingStop*Point)
     { 
      OrderModify(OrderTicket(),OrderOpenPrice(),
      Ask+TrailingStop*Point,OrderTakeProfit()-Step*Point,0,SkyBlue);
      return;
      }
     }
if(OrderOpenPrice()-Ask>=TakeProfit*Point) 
   {
    OrderClose(OrderTicket(),Lots,Bid,2,Red);
    }     
// ---------------------------- 2 stage of modification -----SELL-------------&
   if(OrderStopLoss()<=OrderOpenPrice()) // StopLoss is on the lossless level
    if(OrderOpenPrice()-Ask>=OrderTakeProfit()) OrderClose(OrderTicket(),Lots,Ask,2,Red);   
    { // calculate correction coefficient
     double Coeff_down = NormalizeDouble((OrderOpenPrice()-Ask)*0.382,Digits);
     // and if the difference between the position open price and the current price is greater than corr. coefficient
    if(OrderOpenPrice()-Ask>Coeff_down) 
     {   // calculate the value of new StopLoss with the margin of 6 points
      New_S_Loss = Ask+Coeff_down+6*Point; 
      // and if the value of new StopLoss is less than the current value
      if((OrderStopLoss()-New_S_Loss-StopLev*Point)>=10*Point)
       {    // move S/L and T/P
       OrderModify(OrderTicket(),OrderOpenPrice(),
       New_S_Loss-5*Point,OrderTakeProfit()-Step*Point,0,Khaki);
      return;
      }
     }
    }
   }
  } 
return(0);
}
}
// ---end----- void TrailingStop()--------------------------------&
  // this block is only for error controlling in the function code
  //  int start()  
  //   {
  //    if(25>26) TrailingStop();
  //    }
  // --------------------------------

Fazit

Es sollte angemerkt werden, dass, im Vergleich zu dem "beispielhaften Trailing-Stop", beschrieben in dem "Ein Muster Trailing Stop und Ausstieg aus dem Markt" Artikel von Sergey Kravchuk, die vorgeschlagene Version einfacher zu verstehen ist und in meinem EA funktioniert hat (um die Wahrheit zu sagen, auf einem Demokonto) und, wie ich denke, er zu dem aggressiven und moderaten Trailing passt.

Die angehängten Versionen:

v4 - mit Schließen durch S/L; v5 - mit vorhergesagtem Schließen durch T/P; v6 - mit einem Blick auf die Vorhersage und Adressunterstützung durch Magic Number.