Help me fix my EA so that it trades once per signal when it hit either SL, TP or trailing stop. I want it to wait for next signal - page 3

 
Oliver Gideon Amofa Appiah:
Hello Tony,
Can you please insert your idea into the code and let us see if it works? 
I got to learn this 
#property copyright " "
#property link      " "
#property description   "Stochastic EA"
#property version   "1.0"
#property strict

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=50;
extern double TakeProfit=100;
extern int TrailingStop=30;
extern int Slippage=3;


string LastSignal;

int init()
{
LastSignal="none";
return(0);
}
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)>iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // Here is your open buy rule
     {
        if(LastSignal!="buy";) { result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Stoch EA",MagicNumber,0,Blue); }
        if(result>0)
        {
         LastSignal="buy";
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)<iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // Here is your open Sell rule
     {
        if(LastSignal!="sell";){ result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Stoch EA",MagicNumber,0,Red); }
        if(result>0)
        {
         LastSignal="sell";
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)<iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)>iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
 

That is not what he wants.

If the signal is still long it should open another long after the previous long has closed.
 
I am trying all your suggestions,  I'll get back to you. Thanks guys
 
Tony check this error out,  please 
(screenshot) 
Files:
 
Marco vd Heijden:

That is not what he wants.

If the signal is still long it should open another long after the previous long has closed.

Thought he said 1 trade per signal so if it trades buy it waits for sell signal to trade again no? Oliver kindly clarify this.

 
Yes,  yes, Exactly !

Tony, this is what I'm looking for. 

1 trade per signal so if it trades buy it waits for sell signal to trade again.


 
Oliver Gideon Amofa Appiah:
Tony check this error out,  please 
(screenshot) 
#property copyright " "
#property link      " "
#property description   "Stochastic EA"
#property version   "1.0"
#property strict

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=50;
extern double TakeProfit=100;
extern int TrailingStop=30;
extern int Slippage=3;


string LastSignal;

int init()
{
LastSignal="none";
return(0);
}
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)>iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // Here is your open buy rule
     {
        if(LastSignal!="buy") { result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Stoch EA",MagicNumber,0,Blue); }
        if(result>0)
        {
         LastSignal="buy";
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)<iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // Here is your open Sell rule
     {
        if(LastSignal!="sell"){ result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Stoch EA",MagicNumber,0,Red); }
        if(result>0)
        {
         LastSignal="sell";
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)<iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if((iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_MAIN,0)>iStochastic(NULL,0,34,55,3,MODE_EMA,1,MODE_SIGNAL,0))) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

Try this one. Just curious Oliver are you a coder?

 
Oliver Gideon Amofa Appiah:
Yes,  yes, Exactly !

Tony, this is what I'm looking for. 

1 trade per signal so if it trades buy it waits for sell signal to trade again.


That's not what you told me.

1 Trade per signal can be buy or sell signal it was not specified.

But now you have 4 types to choose from so it's all good.

 
Tonny Obare:

Try this one. Just curious Oliver are you a coder?

It is quite obvious that he is not a coder.

He has used an EA Builder to create the EA.

Now he wants your free help to improve the EA so that he can sell it.

 
It is an infringement of the conditions to remove the header when you use the EA builder. Why have you done so Oliver?