SL and TP system - page 3

 
OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue);  

Where do you select the order?

 
Keith Watford:

Where do you select the order?

So I would have to select the LongSetup as the order?, Because I dont select an order for this

 
      if (LongSetup == True && FastEMA < SlowEMA){
      
         OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue);  
         LongSetup = False;
      }

Your order close logic is in the wrong place, check your if statements and balance them.

And as mentioned before you need to have an order selected before you could close it. 

 
lippmaje:

Your order close logic is in the wrong place, check your if statements and balance them.

And as mentioned before you need to have an order selected before you could close it. 

Im not sure what you mean by saying balance my if statements

 
I tried this not sure if it will work, or if this is what you meant but Im getting an error with the ending brackets
//+------------------------------------------------------------------+
//|                                                  EMA Cross 2.mq4 |
//|                                                        Tiberious |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Tiberious"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double LotsToTrade = 0.1;     //Lot Size
int Magic = 500;
int MaxTrades = 1;

bool LongSetup = False;
bool ShortSetup = False;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastSlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastFastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      
      if (GetTotalOpenTrades() < MaxTrades) {
      
          if ((LastFastEMA < LastSlowEMA)
                   &&(FastEMA > SlowEMA)) {
                   
            LongSetup = True;
      }
      
      if (LongSetup == True){
            
            Comment ("Buy");
            
            int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 0, 0, 0, "Buy Order", Magic, 0, clrGreen);
      }
      
      if (LongSetup == True && FastEMA < SlowEMA){
         CloseAllTrades();
         //OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue);  
         //LongSetup = False;
      }
      
   }
 
      
      else if (GetTotalOpenTrades() < MaxTrades) {
            
       if ((LastFastEMA > LastSlowEMA)
               && (FastEMA < SlowEMA)) {
            
            
            Comment ("Sell");
            int OrderResultShort = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
      }
 }
}
// Return total number of open trades
int GetTotalOpenTrades() {
      
      int TotalTrades = 0;
      
      //Loop through open orders and add them to TotalTrades
      for (int t=OrdersTotal()-1; t>=0; 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;
 }

double CloseAllTrades() {

   for(int i=OrdersTotal(); i>=0;i--) {
   
      if (OrderSelect(i,SELECT_BY_POS)== True)
      
      if (OrderSymbol() == Symbol()) {
      
         OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue);
      }
   }
 }
 
You may want to take some basic lessions in computer programming before going on, or have someone develop this EA for you.
 
lippmaje:
You may want to take some basic lessions in computer programming before going on, or have someone develop this EA for you.

Id rather make it on my own

 
Tiberious:

Id rather make it on my own

Im pretty close to finishing it aswell

 
void CloseAllTrades() {

   for(int i=OrdersTotal()-1; i>=0;i--) {
   
      if (OrderSelect(i,SELECT_BY_POS)  )
 
lippmaje:

My bad for all the questions. Thanks for taking the time to help out. I switched out the code for yours but trades are still not closing. Any idea why?

//+------------------------------------------------------------------+
//|                                                  EMA Cross 2.mq4 |
//|                                                        Tiberious |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Tiberious"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double LotsToTrade = 0.1;     //Lot Size
int Magic = 500;
int MaxTrades = 1;

bool LongSetup = False;
bool ShortSetup = False;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastSlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double LastFastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      
      if (GetTotalOpenTrades() < MaxTrades) {
      
          if ((LastFastEMA < LastSlowEMA)
                   &&(FastEMA > SlowEMA)) {
                   
            LongSetup = True;
      }
      
      if (LongSetup == True){
            
            Comment ("Buy");
            
            int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 0, 0, 0, "Buy Order", Magic, 0, clrGreen);
      }
      
      if (LongSetup == True && FastEMA < SlowEMA){
         CloseAllTrades();
         //OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue);  
         //LongSetup = False;
      }
      
   }
 
      
      else if (GetTotalOpenTrades() < MaxTrades) {
            
       if ((LastFastEMA > LastSlowEMA)
               && (FastEMA < SlowEMA)) {
            
            
            Comment ("Sell");
            int OrderResultShort = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
      }
  }
}

// Return total number of open trades
int GetTotalOpenTrades() {
      
      int TotalTrades = 0;
      
      //Loop through open orders and add them to TotalTrades
      for (int t=OrdersTotal()-1; t>=0; 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() {
   
   for (int i=OrdersTotal()-1; i>=0; i--) {
   
      if (OrderSelect(i, SELECT_BY_POS))
      
      if (OrderSymbol() == Symbol()) {
      
         OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue);
    }
  }
}
Reason: