Basic EA based on basic StochOsc

 

Hi. Can someone help me, what is wrong in this simple EA, it doesn't trigger the orders when Stochastic crosses the Signal line. I made the Strategy Test in a 15min timeframe with thousands of bars and total orders are 0.

#define MAGICMA 10460211

extern double Lots = 0.1;
extern double TrailingStop = 0;
extern int    Kperiod=89;
extern int    Dperiod=55;
extern int    Slowing=5;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double StoCurrent, StoPrevious, SignalCurrent;
   double SignalPrevious;
   int cnt, ticket, total;

// initial data checks

   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);
     }
// --------
   StoCurrent=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,0);
   StoPrevious=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,1);
   SignalCurrent=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,1,MODE_SIGNAL,0);
   SignalPrevious=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,1,MODE_SIGNAL,1);
   
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if((StoCurrent>SignalCurrent && StoPrevious<SignalPrevious))
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"Forex15min",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if((StoCurrent<SignalCurrent && StoPrevious>SignalPrevious))
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"Forex15min",MAGICMA,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
     
// --------  

   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&       // check for opened position 
         OrderSymbol()==Symbol() &&    // check for symbol
         OrderMagicNumber() == MAGICMA)// check for magic number
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if((StoCurrent<SignalCurrent && StoPrevious>SignalPrevious))
              {
               OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)  
                  {                 
                   OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
                   return(0); // exit
                  }
              }    
           }
         else // go to short position
           {
            // should it be closed?
            if((StoCurrent>SignalCurrent && StoPrevious<SignalPrevious))
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))  
                  {                 
                   OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
                   return(0); // exit
                  }
              } 
           }
        }
     }
   return(0);
  }
// the end.

Best regards.

 
   StoCurrent=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,0);
   StoPrevious=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,1);
   SignalCurrent=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,1,MODE_SIGNAL,0);
   SignalPrevious=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,1,MODE_SIGNAL,1);
   
Should be:

   StoCurrent=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,0);
   StoPrevious=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,1,MODE_MAIN,1);
   SignalCurrent=iStochastic(NULL,0,Dperiod,Dperiod,Slowing,MODE_SMA,1,MODE_SIGNAL,0);
   SignalPrevious=iStochastic(NULL,0,Dperiod,Dperiod,Slowing,MODE_SMA,1,MODE_SIGNAL,1);
      if(OrderType()<=OP_SELL &&       // check for opened position 

//What do you intend to do with that line?
Your Trailing Stop Code is not a Trailing Stop, but a Takeprofit

only looked shortly over the code. maybe more bugs


//z

 

Thank you very much Zzuegg. Seems to me you are a very experienced programmer, you identified all the problems very quickly. I tested with other conditions to open/close orders that resulted in various trades, but the system always closed the positions without the indicator giving a signal and I was thinking why was that happening. Now you said all, I have a takeprofit code instead of a trailing stop. Let me tell I am very new with MQL4 and I am trying to learn. About the line

SignalCurrent=iStochastic(NULL,0,Dperiod,Dperiod,Slowing,MODE_SMA,1,MODE_SIGNAL,0);

I think I saw in the documentation an explanation wich says we should write "Dperiod,0,0" instead of "Dperiod,Dperiod,Slowing". I will do what you say.

Thank you very much my friend for your help, it was very useful. I wish you the best trades.
 

Hi,

Good day,

I would suggest you write individual functions and call the functions to the start() main function to ease everything to you. In this case, you will be able to figure out what the problem is.

I don't recommend writing a code all together because it would take you time to solve any single problem. And maybe your eyes would hurt you at the end of the day trying to find out what's going on.

Simply, divide the code and make it functions and then as I said call all functions you need to main function.

Do you know what " kiss" mean???

Someone says " Keep It Simple Simple" :)

Best wishes,

SF

 
for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&       // check for opened position 
         OrderSymbol()==Symbol() &&    // check for symbol
         OrderMagicNumber() == MAGICMA)// check for magic number

You must count down when closing/deleting orders

You should test the orderSelect

The ordertype < op_sell is necessary only in history to avoid cr/bal https://www.mql5.com/en/forum/126192

for(cnt=0;cnt<total;cnt++) if(
   OrderSelect(cnt, SELECT_BY_POS)
&& OrderSymbol()==Symbol()       // check for symbol
&& OrderMagicNumber() == MAGICMA)// check for magic number
{

should write "Dperiod,0,0" instead of "Dperiod,Dperiod,Slowing"
.You must supply ALL parameters.
 

Thanks for the help. Slowly I am learning the basic principles to get the code working. I changed the parameters "Dperiod,0,0" to "Kperiod,Dperiod,Slowing" but in the mode_signal and now EA is working as it should be.

Now I am trying to write a code to allow the orders triggering only after the current bar is completed, or at the new bar opening. For instance, I want to wait for a Cross between 2 Moving Averages at the end time of the bar and not during the bar, because many times the Moving Averages can cross between start time and end time but at the end time they are not crossed and for the next bars they just get away from each other, resulting in a wrong signal. I tryed some code like the next one but it didn't work like it should be, many orders were triggered unexpectedly.

Best regards.

bool init_variables; 
datetime PreviousBar; 

int init()
  {
   init_variables = true; 
   return(0);
  }

bool NewBar()
  {
   if(PreviousBar<Time[0])
   {
    PreviousBar = Time[0];
    return(true);
   }
   else
   {
    return(false);
   }
   return(false);
  }

int start()
  {
   if(init_variables == true)
      {
      PreviousBar = Time[0];    
      init_variables = true; 
      }
   if(NewBar() == true)
   {
   total=OrdersTotal();
   if(total<1)

   etc etc etc etc, rest of the code.
Reason: