Please, Need HELP. Need TrailingStop and Break even for this EA - page 2

 
RaptorUK:

OK, fair enough.

You could create a function that looks at the open order(s) and adjusts the SL as per what you want from a trailing SL. You could call this function for each tick at the start of the start function . . . does that help ?

yes a little bit. my english is bad ;-) but i know what you mean. can you mark that positions in my code ?

i wanna try it yet again ;-)

i must more learn, i m a absolutly newbee ;-)

 
sunnyboy20:

yes a little bit. my english is bad ;-) but i know what you mean. can you mark that positions in my code ?

i wanna try it yet again ;-)

Create the Function here . . .

}
   return(0);
  }
//---- 

TrailingStop()  //  <------------------------------------  create the function here
  {

  }

and call it here . . .

//+------------------------------------------------------------------+
int start()
  {

   TrailingStop();      //  <-------- call the function here
 

the first i understand

but the call ? must i add this to the OrderSend ? or before ?

thanks for your help

 

is that so right ?

//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Sebastian Meissner"


extern string  Expert_Name    = "Trading Basti";
extern double iLots=0.1;
extern double iMaximumRisk= 0.02;
extern int iTakeProfit=50;
extern int iStopLoss=650;
extern int TrailingStop = 35;
extern int iMaxTrades=1;
extern int iMagicNumber=336699;

double stochg,stochr;

int iTotalTrades;
int iOrderOpenStatus;
int iErrorNumber;
int currentOrderTicket;

string strErrorMessage;

double LotsOptimized()
  {
  double lot=iLots;
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*iMaximumRisk/1000.0,1);
//---- return lot size
   if(lot<0.1) lot=0.1;
   if(lot>50.0) lot=50.0;
   return(lot);
  }

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
void CheckTrailingStop(int order_Ticket, int trailingStopValue)
{
   double oldStopLoss;
   double newStopLoss;
   double nextStopLoss;
   int _trailingStop = trailingStopValue;
   
   if (order_Ticket < 0) return;
                                    
   if(OrderSelect(order_Ticket,SELECT_BY_TICKET,MODE_TRADES) == false)
   {
      Print("CheckTrailingStop() - OrderSelect returned the error of ", GetLastError());
      return;   
   }
         
   if (OrderType() == OP_BUY)
   {      
      if(Bid-OrderOpenPrice() > Point*_trailingStop)
      {       
         oldStopLoss = OrderStopLoss();
         newStopLoss = Bid-Point*_trailingStop;
         nextStopLoss = oldStopLoss + Point; // Adding 1 point to oldStopLoss, i.e. 76.78 to 76.79 to fix mq4 compare doubles bug         
                    
         if(newStopLoss >= nextStopLoss || OrderStopLoss() == 0)
         {                              
            OrderModify(OrderTicket(),OrderOpenPrice(),newStopLoss,0,0,Green);
            return;
         }
      }
   }
                                             
   if (OrderType() == OP_SELL)
   {               
      if(OrderOpenPrice()-Ask > Point*_trailingStop)
      {
         oldStopLoss = OrderStopLoss();
         newStopLoss = Ask+Point*_trailingStop;
         nextStopLoss = oldStopLoss - Point; // Subtracting 1 point to oldStopLoss, i.e. 76.78 to 76.77 to fix mq4 compare doubles bug
                    
         if(newStopLoss <= nextStopLoss || OrderStopLoss() == 0)
         {                              
            OrderModify(OrderTicket(),OrderOpenPrice(),newStopLoss,0,0,Green);
            return;
         }
      }
   }

   
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//+------------------------------------------------------------------+
//| The functions from this point to the start function are where    |
//| changes are made to test other systems or strategies.            |
//|+-----------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start()
{ 

   if (TrailingStop > 0) CheckTrailingStop(currentOrderTicket, TrailingStop);


  stochg = iCustom (NULL,0,"Stochastic",8,3,3,MODE_SMA,0,MODE_MAIN,0);
  stochr = iCustom (NULL,0,"Stochastic",8,3,3,MODE_SMA,0,MODE_SIGNAL,0);
  double willd       = 81;
  double willu       = 19;
  double will1       = 80;
  double will2       = 20;
//Buy-Logik
  
  
//Sell-Logik

   
// Buy-Order ausführen

iTotalTrades=OrdersTotal();

if (( stochg > will2) && (stochg > stochr) && (stochr < will2) && (stochr > willu) && (iTotalTrades < iMaxTrades))

{ 
double dBuyStopLoss=Ask-(iStopLoss*Point);
double dBuyTakeProfit=Ask+(iTakeProfit*Point);

iOrderOpenStatus=OrderSend (Symbol(), OP_BUY,LotsOptimized(), Ask, dBuyStopLoss, dBuyTakeProfit, "Trading Basti",iMagicNumber,0,Green);
   if (iOrderOpenStatus<0)
      {
      iErrorNumber=GetLastError();
      Print ("Order fehlgeschlagen!: ", iErrorNumber);
      return;
      }
}
      
// Sell-Order ausführen

iTotalTrades=OrdersTotal();

if (( stochg < will1) && (stochg < stochr) && (stochr > will1) && (stochr < willd) && (iTotalTrades < iMaxTrades))

   {
   double dSellStopLoss=Bid+(iStopLoss*Point);
   double dSellTakeProfit=Bid-(iTakeProfit*Point);
 
   
iOrderOpenStatus=OrderSend (Symbol(), OP_SELL,LotsOptimized(), Bid, dSellStopLoss, dSellTakeProfit, "Trading Basti",iMagicNumber,0,Red);
   if (iOrderOpenStatus<0)
      {
      iErrorNumber=GetLastError();
      Print ("Order fehlgeschlagen!: ", iErrorNumber);
      return;
      }
}
   return(0);
  }
//---- 
 

shit it is not right ....

this message shown in strategytester.....

"17:17:07 2011.01.11 00:00 bastiEA GBPUSD,M15: CheckTrailingStop() - OrderSelect returned the error of 0"

help me please, this can not be that hard to make a TrailingStop :(

 
Where are you getting order_Ticket from ?
 
RaptorUK:
Where are you getting order_Ticket from ?

In Meta Editor press Ctrl + F type in currentOrderTicket click Find Next . . . . where do you set a value for currentOrderTicket ?

You can't just copy and paste other peoples code into your "own EA" and expect it to work . . . learn some coding or pay someone to do it for you.

 

i know :(

i m learning but is difficult for me, i can only learning by doing.

thanks for your help i will try it again and again

Reason: