Expert advisor closing trades early after the first following candle and stopping the simulation

 
Whenever I run it through the strategy tester, the trade always closes on the first candle after opening and then the simulation stops out. First time writing one of these, but any help would be much appreciated. Code is as follows
#include <Trade/Trade.mqh>

CTrade trade;


int OnInit()
  {
   Print("Starting");
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   Print("Ondeinit");
  }

void OnTick(){

   static datetime timestamp;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);

   if(timestamp != time){
      timestamp = time;
      static int handleSMA = iMA(_Symbol,PERIOD_CURRENT,
20 ,0,MODE_EMA,PRICE_CLOSE);  
      double slowMaArray[];
      CopyBuffer(handleSMA, 0, 1,2, slowMaArray);
      ArraySetAsSeries(slowMaArray,true);

      static int handleFMA = iMA(_Symbol,PERIOD_CURRENT,
10 ,0,MODE_EMA,PRICE_CLOSE);
      double fastMaArray[];
      CopyBuffer(handleFMA, 0, 1,2, fastMaArray);
      ArraySetAsSeries(fastMaArray,true);


      if(fastMaArray[0] > slowMaArray[0] && fastMaArray[1] < slowMaArray[1]){
         Print("Fastma is greater than slow ma");
         double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         double sl = ask - 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
         double tp = ask + 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
         trade.Buy(0.01,_Symbol, ask,sl,tp);
         //Print("Buying at ", ask," ",tp," ", sl);
      }

      if(fastMaArray[0] < slowMaArray[0] && fastMaArray[1] > slowMaArray[1]){
         Print("Fastma is not greater than slow ma");
         double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         double sl = bid + 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
         double tp = bid - 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
         trade.Sell(0.01,_Symbol, bid,sl,tp);
         //Print("Selling at ", bid," ",tp," ", sl);
      }
  }
  }
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
Jimmy9618:
Whenever I run it through the strategy tester, the trade always closes on the first candle after opening and then the simulation stops out. First time writing one of these, but any help would be much appreciated. Code is as follows

Please send a screenshot of your tester journal

 
Chioma Obunadike #:

Please send a screenshot of your tester journal

Thanks for the reply. Here's a screenshot of the tester journal