Cannot figure out why this ea only opens and then close one trade when I back test.

[Deleted]  

This is my first program in MQL4. I am just trying to learn the basics but continue to get hung up on little things. I was hoping that someone might be willing to help.

The strategy for this ea is as follows

OpenLong: 10EMA>20EMA, K Stoch >D Stoch, ADX >= 20, and there is not already an open long order

CloseLong: 5EMA < 10EMA or ADX <20

The reason I have the reset in the code is because without it, during back test I get a series of about 30 trade that open and close with each tick, driving my account balance into the ground.

OpenClose: 10EMA < 20EMA, K Stoch < D Stoch, and ADX >=20, and there is not already an open short order.

Close Long: 5EMA > 10EMA or ADX <20

The reason I have the reset in the code is because without it, during back test I get a series of about 30 trade that open and close with each tick, driving my account balance into the ground.

The code is below:

//ExternalVariables
extern double LotSize = 0.5;
extern int StopLoss = 20;
extern int Slippage = 5;
extern int MagicNumber = 123;

   //ADXExternalVariables
     extern int ADXPeriod = 14;
     extern int ADXParaVal = 20;
   
   //EMAExternalVariables
     extern int FastEMAPeriod = 10;
     extern int SlowEMAPeriod = 20;
     extern int CloseEMAPeriod = 5;
   
   //StochasticOsillatorExternalVariables
     extern int KPeriod = 14;
     extern int DPeriod = 3;
     extern int SlowingPeriod = 3;
     
//GlobalVariables
int BuyTicket = 0;
int SellTicket = 0;
double UsePoint;
double UseSlippage;
int Check1;
int Check2;
int Count = 0;
bool StatusOfOrderSelect1;
bool StatusOfOrderSelect2;

//InitFuntion
int init()
   {
      UsePoint = PipPoint(Symbol()); 
      UseSlippage = GetSlippage(Symbol(),Slippage);
   }
   
//StartFunction
int start()
   {
      //CallADX
      double ADX = iADX(NULL,0,ADXPeriod,MODE_CLOSE,MODE_MAIN,0);
      
      //CallFast,Slow,andCloseEMAs
      double FastEMA = iMA(NULL,0,FastEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);
      double SlowEMA = iMA(NULL,0,SlowEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);
      double CloseEMA = iMA(NULL,0,CloseEMAPeriod,0,MODE_EMA,MODE_CLOSE,0);
      
      //CallStochasticOsillator
      double KStochastic = iStochastic(NULL,0,KPeriod,DPeriod,SlowingPeriod,MODE_SMA,0,MODE_MAIN,0);
      double DStochastic = iStochastic(NULL,0,KPeriod,DPeriod,SlowingPeriod,MODE_SMA,0,MODE_SIGNAL,0);
      
      //OpenLong
      if(ADX >= ADXParaVal && CloseEMA > FastEMA > SlowEMA && KStochastic > DStochastic && BuyTicket == 0)
         {
            double OpenLongPrice = Ask;
            
            //Calculate StopLoss
            if(StopLoss > 0) double OpenLongStopLoss = OpenLongPrice - (StopLoss * UsePoint);
            
            //Open long order
            BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenLongPrice,UseSlippage,OpenLongStopLoss,0,"Open Long Order",MagicNumber,0,Green);
            Alert("BuyTicket = ",BuyTicket);
         }
         
      //CloseLong
      if(CloseEMA < FastEMA && BuyTicket  != 0 || ADX < ADXParaVal && BuyTicket != 0)
         {
            StatusOfOrderSelect1 = OrderSelect(BuyTicket,SELECT_BY_TICKET);
            
            Alert("The Status of Order Select1 is...",StatusOfOrderSelect1);
            
            Check1 = GetLastError();
            
            if(Check1 != 0) 
               {
                  Alert("Error with Close Long so control returned");
               }
            
            if(OrderCloseTime() == 0)
               {
                  double LongCloseLots = OrderLots();
                  double LongClosePrice = Bid;
                  bool LongClosed = OrderClose(BuyTicket,LongCloseLots,LongClosePrice,UseSlippage,Red);
               }
         }
         
      //OpenLongReset
      if(CloseEMA < FastEMA < SlowEMA && BuyTicket != 0) BuyTicket = 0;
      
      //OpenShort
      if(ADX >= ADXParaVal && CloseEMA < FastEMA < SlowEMA && KStochastic < DStochastic && SellTicket == 0)
         {
            double OpenShortPrice = Bid; 
            
            //Calculate StopLoss
            if(StopLoss > 0) double OpenShortStopLoss = OpenShortPrice + (StopLoss * UsePoint);
            
            //Open short order
            SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenShortPrice,UseSlippage,OpenShortStopLoss,0,"Open Short Order",MagicNumber,0,Green);
         }
         
      //CloseShort
      if(CloseEMA > FastEMA && SellTicket != 0 || ADX < ADXParaVal && SellTicket != 0)
         {
            StatusOfOrderSelect2 = OrderSelect(SellTicket,SELECT_BY_TICKET);
            
            Alert("The Status of Order Select2 is...",StatusOfOrderSelect2);
            
            Check2 = GetLastError();
            
            if(Check2 != 0)
               {
                  Alert("Error with Close Short so control returned");
               }
            
            if(OrderCloseTime() == 0)
               {
                  double ShortCloseLots = OrderLots();
                  double ShortClosePrice = Ask;
                  bool ShortClosed = OrderClose(SellTicket,ShortCloseLots,ShortClosePrice,UseSlippage,Red);
               }
         }
         
      //OpenShortReset
      if(CloseEMA > FastEMA > SlowEMA && SellTicket != 0) SellTicket = 0;
         
      //TickCounter
      
      double Price = Bid;
      Count++;
      Alert("New tick ",Count,"   Price = ",Price);
      
      //ReturnControl
      return(0);
         
    }
    
//PipPointFunction
double PipPoint(string Currency)
   {
      int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
      if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
      if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001; 
      return(CalcPoint);
   }

//GetSlippageFunction  
double GetSlippage(string Currency, int SlippagePips)
   {
      int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
      if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
      if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10; 
      return(CalcSlippage);
   } 

Thank you to those who help

ZH

Simon Gniadkowski
17410

I don't know for a fact . . . but is this legitimate ? have you tested it ?

CloseEMA < FastEMA < SlowEMA
[Deleted]  
RaptorUK:

I don't know for a fact . . . but is this legitimate ? have you tested it ?


What do you mean by you last comment? I have tested it over the past month 5min chart and it only makes one trade. Opens short and then closes next tick.
Simon Gniadkowski
17410

I mean have you specifically tested this to see if it actually gives the result you are looking for ?

CloseEMA < FastEMA < SlowEMA

What I'm getting at is this code the same as . . .

CloseEMA < FastEMA && FastEMA < SlowEMA

or the same as . . .

(CloseEMA < FastEMA) < SlowEMA

or maybe . . .

CloseEMA < (FastEMA < SlowEMA)
[Deleted]  
RaptorUK:

I mean have you specifically tested this to see if it actually gives the result you are looking for ?


I dont know how to only test only one line of code. I only know how to make alerts to see if it returns a value I want when its trading live.
[Deleted]  

i will run back tests to see if your suggestions help

[Deleted]  

still no luck :(

Simon Gniadkowski
17410

It's more of a question than a suggestion . . I don't know the answer myself . . . you could use a print statement or two and see what they show.

Print("CloseEMA= ",CloseEMA, " FastEMA= ",FastEMA, " SlowEMA= ",SlowEMA);
Print(CloseEMA < FastEMA < SlowEMA);

Simon Gniadkowski
17410
Shouldn't you actually set BuyTicket = 0 when you actually close the current Buy order or it hits it's SL ? Similar for SellTicket = 0 . . .