My First EA

 

Good Evening Fellow Traders,

This is my first attempt at creating an EA. I am not too bothered about the strategy and I doubt it would be profitable. This is more for a learning experience and to be guided by people much more knowledgeable than myself!

I just want it to execute "some" trades  (backtesting), I believe all of the syntax is correct as I do not get any errors or warnings when compiling. However when backtesting in mt4 I press start and I immediately get a clown horn so I assume a parameter is missing?

Hoping that someone will be kind enough to point out the mistake which I am sure is obvious to those in the know!


thanks in advance! 


JB

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

   //External Variables
   extern double FixedLotSize = 0.01;
   extern int StopLoss = 5;
   extern int TakeProfit= 10;
   extern int Slippage= 3;
    
   input int SmallEMA= 60;
   input int LargeEMA= 250;
   
   //Global Variables
   int BuyTicket;
   int SellTicket;
   int OrderNumber;

 //+------------------------------------------------------------------+  
 
void OnTick()

{
   //We create a string variable for signal
   string signal ="";
  
   //We define the EA
   double K0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double D0=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   double K1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   double D1=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   
   //We calculate the small EMA
   double SmallEMA1 = iMA(_Symbol,_Period,SmallEMA,0,MODE_EMA,PRICE_CLOSE,1);
   
   //We calculate the large EMA
   double LargeEMA1 = iMA(_Symbol,_Period,LargeEMA,0,MODE_EMA,PRICE_CLOSE,1);
   
      //We calculate the small EMA
   double SmallEMA2 = iMA(_Symbol,_Period,SmallEMA,0,MODE_EMA,PRICE_CLOSE,2);
   
   //We calculate the large EMA
   double LargeEMA2 = iMA(_Symbol,_Period,LargeEMA,0,MODE_EMA,PRICE_CLOSE,2);
   
//+------------------------------------------------------------------+   
   //Sell signal - Crossover in Overbought Region
      if ((K0 > 83) && (D0 > 83))
      if ((D0 > K0) && (D1 > K1))
      
   // 250EMA > 60 EMA
      if (LargeEMA1 > SmallEMA1)
      
    // 250EMA < 60 EMA before
      if (LargeEMA2 < SmallEMA2)     
   {
      signal= "Sell";
   }
      
   //Buy Signal - Crossover in Oversold Region
      if ((K0 < 17) && (D0 < 17))
      if ((D0 < K0) && (D1 > K1))

   // 250EMA < 60 EMA
      if (LargeEMA1 < SmallEMA1)
      
   // 250EMA > 60 EMA before
      if (LargeEMA2 > SmallEMA2)  
   {
      signal= "Buy";
   }
 
//+------------------------------------------------------------------+   
   //Define a variable to store a result of the Order Function
   
   
   //Sell Order
   if(signal=="Sell" && OrdersTotal()==0)
   {
   OrderNumber=OrderSend(_Symbol,OP_SELL,FixedLotSize,Bid,Slippage,Bid+50*_Point,Bid-100*_Point,NULL,0,0,Red);
   }
  
   //Buy Order
   if(signal=="Buy" && OrdersTotal()==0)
   {
   OrderNumber=OrderSend(_Symbol,OP_BUY,FixedLotSize,Ask,Slippage,Ask-5*_Point,Ask+100*_Point,NULL,0,0,Green);
   }
   
   // Order Check
    if (OrderNumber>0)
   {
   Print ("Order",OrderNumber,"Open");
   }
   else
   {
   Print("Order Failed with Error -",GetLastError());
   }
   {
   //Chart Output for the Signal
   Comment ("The current signal is: ",signal); 
   }
}
 

Check the journal for errors.

What is a clown horn?

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford: What is a clown horn?
The OP is probably referring to the sound that MetaTrader makes when a test comes to an end (sounds like a toy to me).
 
Fernando Carreiro:
The OP is probably referring to the sound that MetaTrader makes when a test comes to an end (sounds like a toy to me).

Ohhh, that squeaky sound.

In that case, it sounds like a critical error and so it should be reported in the journal.

Or maybe the testing period is very short.

 
Agree, need to see the journal report.
 

Okay, so after a long breakaway from this I have got it to execute trades, a great success (partially) however it only executes sell signals. I get no errors or warnings when compiling this code but I don't think my syntax of all of the variables that decide whether it is a long or short signal is taken into account? is there a way for me to check to see when "running" the code which lines execute?

I imagine I need to create an If else statement between the sell and buy variables for it to trigger both longs and shorts. Which is the next item to work on - any useful posts specific to my problem or quick pointers greatly appreciated. I want to learn the debugging myself but could spend hours looking through potentially irrelevant material until I find a very specific instance where it is a similar problem.


thanks in advance as always!


JB