EA closes trades immediately after opening

 
Here is the code ive used before but for some reason it will not place trades now.
//+------------------------------------------------------------------+
//|                                                      Cortana.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int Magic = 2323;
int MaxTrades = 1;

double LotsToTrade = 0.01;
double StopLoss = 15;
double ProfitTarget = 50;

int MaxCloseSpreadPips = 20;

// Trade Time Delay
int TradeDelayTimeSeconds = (1 * 1 * 15 * 60);      //15 minute delay
datetime LastTradePlacedTimestamp = 0;

bool LongSetup = False;
bool ShortSetup = False;


int OnInit()
  {


   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function   Will Run every tick                                          |
//+------------------------------------------------------------------+
void OnTick()
{
      //Indicators**************************************************************
      //Moving Average
      double MovingAvgFilter = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
      //Candle 0 Stochastic
      double KM0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
      double DS0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
      //Candle 1 Stochastic
      double KM1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
      double DS1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
      
   
      
      if (GetTotalOpenTrades() < MaxTrades){
      
      if ((TimeCurrent()- LastTradePlacedTimestamp) < TradeDelayTimeSeconds) return;
   
   //Buy Paramaters
   if ( PRICE_CLOSE > MovingAvgFilter && (KM0 < 20) && (DS0 < 20) && (KM1 < DS1) && (KM0 > DS0) ) {
   
         LongSetup = True;
      }
      
      if (LongSetup == True) {
   
      double OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, StopLoss, ProfitTarget, NULL, Magic, 0, clrAqua); 
      LongSetup = False;
      LastTradePlacedTimestamp = TimeCurrent();
   }
   
   else if ( PRICE_CLOSE < MovingAvgFilter && (KM0 > 80) && (DS0 > 80) && (KM1 > DS1) && (KM0 < DS0) ) {
         
         ShortSetup = True;
   }
   
   if (ShortSetup == True) {
   
     double OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, StopLoss, ProfitTarget, NULL, Magic, 0, clrRed);
     ShortSetup = False;
     LastTradePlacedTimestamp = TimeCurrent();
    }
    
     if (GetTotalProfits() > ProfitTarget) CloseAllTrades();
     if (GetTotalProfits() < StopLoss) CloseAllTrades();

  }
}
//Return the total Number of open Trades
int GetTotalOpenTrades() {

   int TotalTrades = 0;
   
   //Loop through open orders and add them to TotalTrades
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
         if(OrderSymbol() != Symbol()) continue;
         if(OrderMagicNumber() != Magic) continue;
         if(OrderCloseTime() != 0) continue;
         
         TotalTrades = (TotalTrades +1);
      }
   }
   
   return TotalTrades;
   
  }
  
void CloseAllTrades() {

   int CloseResult = 0;
   
   for (int t=0; t<OrdersTotal(); t++){
   
    if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
    
      if (OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrAliceBlue);
      if (OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrCrimson);
      
      t--;
   }
   
 } 
   return;
}
//Check total profit for all trades combined
double GetTotalProfits() {

   double TotalProfits = 0.0;
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
      
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderCloseTime() != 0) continue;
         TotalProfits = (TotalProfits + OrderProfit());
      }
   }
   return TotalProfits;

} 
 
Tiberious:
Here is the code ive used before but for some reason it will not place trades now.
//+------------------------------------------------------------------+
//|                                                      Cortana.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int Magic = 2323;
int MaxTrades = 1;

input double LotsToTrade = 0.01;
input double StopLoss = 150;
input double ProfitTarget = 500;

input int MaxCloseSpreadPips = 20;

// Trade Time Delay
int TradeDelayTimeSeconds = (1 * 1 * 15 * 60);      //15 minute delay
datetime LastTradePlacedTimestamp = 0;

bool LongSetup = False;
bool ShortSetup = False;


int OnInit()
  {


   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function   Will Run every tick                                          |
//+------------------------------------------------------------------+
void OnTick()
{
      //Indicators**************************************************************
      //Moving Average
      double MovingAvgFilter = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
      //Candle 0 Stochastic
      double KM0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
      double DS0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
      //Candle 1 Stochastic
      double KM1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
      double DS1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
      
   
      
      if (GetTotalOpenTrades() < MaxTrades){
      
      if ((TimeCurrent()- LastTradePlacedTimestamp) < TradeDelayTimeSeconds) return;
   
   //Buy Paramaters
   if ( PRICE_CLOSE > MovingAvgFilter && (KM0 < 20) && (DS0 < 20) && (KM1 < DS1) && (KM0 > DS0) ) {
   
         LongSetup = True;
      }
      
      if (LongSetup == True) {
   
      double OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, Ask-(StopLoss*Point), Ask+(ProfitTarget*Point), NULL, Magic, 0, clrAqua); 
       Print("BUy ",Ask," Stop ",Ask-StopLoss*Point," TP ", Ask+ProfitTarget*Point," Error ",GetLastError()); 
     
       LongSetup = False;
      LastTradePlacedTimestamp = TimeCurrent();
   }
   
   else if ( PRICE_CLOSE < MovingAvgFilter && (KM0 > 80) && (DS0 > 80) && (KM1 > DS1) && (KM0 < DS0) ) {
         
         ShortSetup = True;
   }
   
   if (ShortSetup == True) {
   
     double OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, Bid+(StopLoss*Point), Bid-(ProfitTarget*Point), NULL, Magic, 0, clrRed);
     ShortSetup = False;
       Print("Sell ",Bid," Stop ",Bid+StopLoss*Point," TP ", Bid-ProfitTarget*Point," Error ",GetLastError()); 
     LastTradePlacedTimestamp = TimeCurrent();
    }
    
     if (GetTotalProfits() > ProfitTarget) CloseAllTrades();
     if (GetTotalProfits() < StopLoss) CloseAllTrades();

  }
}
//Return the total Number of open Trades
int GetTotalOpenTrades() {

   int TotalTrades = 0;
   
   //Loop through open orders and add them to TotalTrades
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
         if(OrderSymbol() != Symbol()) continue;
         if(OrderMagicNumber() != Magic) continue;
         if(OrderCloseTime() != 0) continue;
         
         TotalTrades = (TotalTrades +1);
      }
   }
   
   return TotalTrades;
   
  }
  
void CloseAllTrades() {

   int CloseResult = 0;
   
   for (int t=0; t<OrdersTotal(); t++){
   
    if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
    
      if (OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrAliceBlue);
      if (OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrCrimson);
      
      t--;
   }
   
 } 
   return;
}
//Check total profit for all trades combined
double GetTotalProfits() {

   double TotalProfits = 0.0;
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
      
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderCloseTime() != 0) continue;
         TotalProfits = (TotalProfits + OrderProfit());
      }
   }
   return TotalProfits;

} 
 
Mehmet Bastem:

Thanks I made the corrections, now only sell trades are being placed and being closed immediately 

 

I fixed the problem with only sell trades happening by using a 1 SMA.  I still have a the problem with orders closing imminently. What could be causing this problem?

//+------------------------------------------------------------------+
//|                                                      Cortana.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int Magic = 2323;
int MaxTrades = 1;

input double LotsToTrade = 0.01;
input double StopLoss = 150;
input double ProfitTarget = 350;

input int MaxCloseSpreadPips = 20;

// Trade Time Delay
int TradeDelayTimeSeconds = (1 * 1 * 30 * 60);      //30 minute delay
datetime LastTradePlacedTimestamp = 0;

bool LongSetup = False;
bool ShortSetup = False;


int OnInit()
  {


   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function   Will Run every tick                                          |
//+------------------------------------------------------------------+
void OnTick()
{
      //Indicators**************************************************************
      //Moving Average
      double MovingAvgFilter = iMA(Symbol(), 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
      double MovingAvg = iMA(Symbol(), 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      //Candle 0 Stochastic
      double KM0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
      double DS0 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
      //Candle 1 Stochastic
      double KM1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
      double DS1 = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
      
   
      
      if (GetTotalOpenTrades() < MaxTrades){
      
      if ((TimeCurrent()- LastTradePlacedTimestamp) < TradeDelayTimeSeconds) return;
   
   //Buy Paramaters
   if ( MovingAvg > MovingAvgFilter && (KM0 < 20) && (DS0 < 20) && (KM1 < DS1) && (KM0 > DS0) ) {
   
         LongSetup = True;
      }
      
      if (LongSetup == True) {
   
      double OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, Ask-(StopLoss*Point), Ask+(ProfitTarget*Point), NULL, Magic, 0, clrAqua); 
       Print("Buy ",Ask," Stop ",Ask-StopLoss*Point," TP ", Ask+ProfitTarget*Point," Error ",GetLastError()); 
     
       LongSetup = False;
      LastTradePlacedTimestamp = TimeCurrent();
   }
   
   else if ( MovingAvg < MovingAvgFilter && (KM0 > 80) && (DS0 > 80) && (KM1 > DS1) && (KM0 < DS0) ) {
         
         ShortSetup = True;
   }
   
   if (ShortSetup == True) {
   
     double OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, Bid+(StopLoss*Point), Bid-(ProfitTarget*Point), NULL, Magic, 0, clrRed);
     ShortSetup = False;
       //Print("Sell ",Bid," Stop ",Bid+StopLoss*Point," TP ", Bid-ProfitTarget*Point," Error ",GetLastError()); 
     LastTradePlacedTimestamp = TimeCurrent();
    }
    
     if (GetTotalProfits() > ProfitTarget) CloseAllTrades();
     if (GetTotalProfits() < StopLoss) CloseAllTrades();

  }
}
//Return the total Number of open Trades
int GetTotalOpenTrades() {

   int TotalTrades = 0;
   
   //Loop through open orders and add them to TotalTrades
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
         if(OrderSymbol() != Symbol()) continue;
         if(OrderMagicNumber() != Magic) continue;
         if(OrderCloseTime() != 0) continue;
         
         TotalTrades = (TotalTrades +1);
      }
   }
   
   return TotalTrades;
   
  }
  
void CloseAllTrades() {

   int CloseResult = 0;
   
   for (int t=0; t<OrdersTotal(); t++){
   
    if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
    
      if (OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrAliceBlue);
      if (OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrCrimson);
      
      t--;
   }
   
 } 
   return;
}
//Check total profit for all trades combined
double GetTotalProfits() {

   double TotalProfits = 0.0;
   for (int t=0; t<OrdersTotal(); t++) {
      
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) {
      
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderCloseTime() != 0) continue;
         TotalProfits = (TotalProfits + OrderProfit());
      }
   }
   return TotalProfits;

} 


 

 
Tiberious:

I fixed the problem with only sell trades happening by using a 1 SMA.  I still have a the problem with orders closing imminently. What could be causing this problem?


 

This depends on your strategy. What are the conditions for opening BUY position? It depends on your code below. If you claim that there are no errors in your BUY code, you will be helped if you upload the screenshot from the BUY Position here.

if (PRICE_CLOSE> MovingAvgFilter && (KM0 <20) && (DS0 <20) && (KM1 <DS1) && (KM0> DS0))

 
Mehmet Bastem:

This depends on your strategy. What are the conditions for opening BUY position? It depends on your code below. If you claim that there are no errors in your BUY code, you will be helped if you upload the screenshot from the BUY Position here.

if (PRICE_CLOSE> MovingAvgFilter && (KM0 <20) && (DS0 <20) && (KM1 <DS1) && (KM0> DS0))

Here is the Image showing the journal and the chart

Reason: