MQL5

 

Hallo,

mir ist es gelungen  einen Trailing Stop zu programmieren. Leider kann ich keine Funktion (z.B. m_Trade.PositionClose(_Symbol);) einbauen, da ich nicht weis wo der Fehler liegt, also wie ich eine offene Position wieder schließen kann.

Danke schonmal im Vorraus. 

Dateien:
TestAusgabe.ex5  48 kb
 

Möchte gerne helfen aber du müsstest schon etwas code zeigen. .ex5 Dateien sind compelierte dateien

Das ist elementar um dabei helfen zu können.

 

Zu beachten ist dabei das der MT5 in der normalen version immer nur eine Position eines Symbols halten kann.

Die kann dann mit hilfe des Symbols geschlossen werden.

 

In der neuen version mit Hedge Modus kehrt Metaquotes wieder zum bekannten MT4 System zurück

Dann musst du die Position über das Ticket der Position schließen.

Da du ja pro Symbol gleichzeitig eine Long und eine Short Position gehalten werden kann

 

Gruß Christian 

 

Hoffentlich ist er gut zu lesen aber hier ist der Code:

//+------------------------------------------------------------------+

//|                                                  TestAusgabe.mq5 |

//|                                                        Arthur S. |

//+------------------------------------------------------------------+

#property copyright "Arthur S."

#property version   "1.00"

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+


#include <Trade\Trade.mqh>                                                                                   


CTrade m_Trade;


double Trailing_Stop,Trailing_Stop_Wert=0;

double bid;

      

int OnInit()

{

   

   

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   

}

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{    

   bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

   

   bool Buy_Opened;

   bool Sell_Opened;

   

   if(PositionSelect(_Symbol)==true)

   {

      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

      {

         Buy_Opened=true;

         

         if(bid>Trailing_Stop_Wert)

         {

            Trailing_Stop_Wert=bid;

            Trailing_Stop=Trailing_Stop_Wert-0.00015;

         }

         

         long cid=ChartID();

         ResetLastError();

         if(!ObjectCreate(cid,"test",OBJ_HLINE,0,0,Trailing_Stop) || GetLastError()!=0)

            Print("Error creating object: ",GetLastError());

         else

            ChartRedraw(cid);     

      }

      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

      {

         Sell_Opened=true;

      }

   }

   if(PositionSelect(_Symbol)==false)

   {

      m_Trade.Buy(0.1,_Symbol);

      

      if(bid>Trailing_Stop)

      {

         Trailing_Stop_Wert=bid;

         Trailing_Stop=Trailing_Stop_Wert-0.00015;

      }

      

      if(Trailing_Stop>=bid)

      {

         m_Trade.PositionClose(_Symbol);

      }

      

      long cid=ChartID();

      ResetLastError();

      if(!ObjectCreate(cid,"test",OBJ_HLINE,0,0,Trailing_Stop) || GetLastError()!=0)

         Print("Error creating object: ",GetLastError());

      else

         ChartRedraw(cid);      

   }    

}    

 

Nutze bitte immer die SRC Funktion zum posten von Code. Ist oben im Editor beim Forum.

 

ich mache das mal dann ist der Code besser zu lesen.

 Noch mal zur Frage von dir . Problem ist das die Position nicht geschlossen wird richtig ?

 

//+------------------------------------------------------------------+

//|                                                  TestAusgabe.mq5 |

//|                                                        Arthur S. |

//+------------------------------------------------------------------+

#property copyright "Arthur S."

#property version   "1.00"

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+



#include <Trade\Trade.mqh>                                                                                   



CTrade m_Trade;



double Trailing_Stop,Trailing_Stop_Wert=0;

double bid;

      

int OnInit()

{

   

   

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   

}

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{    

   bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

   

   bool Buy_Opened;

   bool Sell_Opened;

   

   if(PositionSelect(_Symbol)==true)

   {

      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

      {

         Buy_Opened=true;

         

         if(bid>Trailing_Stop_Wert)

         {

            Trailing_Stop_Wert=bid;

            Trailing_Stop=Trailing_Stop_Wert-0.00015;

         }

         

         long cid=ChartID();

         ResetLastError();

         if(!ObjectCreate(cid,"test",OBJ_HLINE,0,0,Trailing_Stop) || GetLastError()!=0)

            Print("Error creating object: ",GetLastError());

         else

            ChartRedraw(cid);     

      }

      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

      {

         Sell_Opened=true;

      }

   }

   if(PositionSelect(_Symbol)==false)

   {

      m_Trade.Buy(0.1,_Symbol);

      

      if(bid>Trailing_Stop)

      {

         Trailing_Stop_Wert=bid;

         Trailing_Stop=Trailing_Stop_Wert-0.00015;

      }

      

      if(Trailing_Stop>=bid)

      {

         m_Trade.PositionClose(_Symbol);

      }

      

      long cid=ChartID();

      ResetLastError();

      if(!ObjectCreate(cid,"test",OBJ_HLINE,0,0,Trailing_Stop) || GetLastError()!=0)

         Print("Error creating object: ",GetLastError());

      else

         ChartRedraw(cid);      

   }    

}    

	          
 
Ja genau, das ist das Problem.
 

nach dem ersten überfliegen ....

 

Fehler ist das du erst prüfst ob eine Position offen ist und wenn keine offen ist soll eine geschlossen werden .

 Das muss frei in Ontick stehen .

 

 

 if(PositionSelect(_Symbol)==false)

   {
  if(Trailing_Stop>=bid)

      {

         m_Trade.PositionClose(_Symbol);

      }
 
So könnte es aussehen . (ungetestet)
 if(PositionSelect(_Symbol)==true && Trailing_Stop>=bid)
{

       m_Trade.PositionClose(_Symbol);
}
  
Was auch nicht funktioniert ist das der Trailingstop in der Anweisung "Wenn keine Position Offen ist" steht.
 
hausmannjack:

Hallo,

... Leider kann ich keine Funktion (z.B. m_Trade.PositionClose(_Symbol);) ..

Es gibt so viel code in der CodeBase! Such Dir da oder über google etwas und kopiere Dir das was Du brauchst in Deinen EA.

Das ist schneller, hat weniger Fehler und lernen kann man auch viel!

 
Rückmeldung hausmanjack ?
 
Hat alles sehr gut geklappt, nochmals vielen vielen dank.
Grund der Beschwerde: