SL and TP system - page 2

 
lippmaje:

To check for an indicator to cross a certain threshold you need two values, the actual one and the one from the previous bar/candle.

Then the check goes like that:

   if (previous_value < threshold && current_value >= threshold) upcross = true;

   if (previous_value > threshold && current_value <= threshold) downcross = true;

To put it into context your code would look something like this:

I appreciate that now that I have the EMA cross more accurate how would I do about closing the order when the opposite cross occurs?

 
I actually did this but orders do not open at the cross. Here is the code.
void OnTick()
  {
      //Indicators
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double FastEMA_prev = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double SlowEMA_prev = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      
      if (GetTotalOpenTrades() < MaxTrades) {
         
         if (FastEMA_prev < SlowEMA_prev && FastEMA >= SlowEMA){
               
            LongSetup = True;
         }
         
         if (LongSetup == True) {
         
         int OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, 0, 0, "Buy Order", Magic, 0, clrGreen);
         LongSetup = False;
         }
         
      
      else if (GetTotalOpenTrades() < MaxTrades) {
         
         if (FastEMA_prev > SlowEMA_prev && FastEMA <= SlowEMA) {
         
            ShortSetup = True;
         
         }
         
         if (ShortSetup == True) {
         
         int OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
         ShortSetup = False;
         
         
         
       }
     }
   }           
 }
 
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double FastEMA_prev = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double SlowEMA_prev = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);

?????????

 
lippmaje:

?????????

Same result

Files:
 

This screenshot actually says nothing.

We can't see your broken code if you do not show it. You have been asked several times now to reveal your code. If you keep it, ok but don't complain about things that won't work at your side. Bye. -

 
lippmaje:

This screenshot actually says nothing.

We can't see your broken code if you do not show it. You have been asked several times now to reveal your code. If you keep it, ok but don't complain about things that won't work at your side. Bye. -

I have showed the whole code before I didnt think I needed to everytime

#property strict                             //Will throw errors
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
string BotName = "AutoEMA";
int Magic = 500;
int MaxTrades = 1;
int MaxCloseSpreadPips = 10;

double LotsToTrade = 0.1;     //Lot Size
//double StopLoss = 0;          //Fixed SL
//double ProfitTarget = 0;      //Fixed TP

bool LongSetup = False;
bool ShortSetup = False;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }
//Expert Tick Function
void OnTick()
  {
      //Indicators
      double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
      double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
      double FastEMA_prev = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1);
      double SlowEMA_prev = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1);
      
      if (GetTotalOpenTrades() < MaxTrades) {
         
         if (FastEMA_prev < SlowEMA_prev && FastEMA >= SlowEMA){
               
            LongSetup = True;
         }
         
         if (LongSetup == True) {
         
         int OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, 0, 0, "Buy Order", Magic, 0, clrGreen);
         LongSetup = False;
         }
          
      
      else if (GetTotalOpenTrades() < MaxTrades) {
         
         if (FastEMA_prev > SlowEMA_prev && FastEMA <= SlowEMA) {
         
            ShortSetup = True;
         
         }
         
         if (ShortSetup == True) {
         
         int OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
         ShortSetup = False;
         
         
         
       }
     }
   }           
 }


// Return 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;
}

 
Tiberious:

I have showed the whole code before I didnt think I needed to everytime

You have to show it again of course if you fixed it and it's still not working.

The code looks ok and should open trades when the fast MA crosses the slow one, that is on Mar 1st (up) and 2nd (down cross).

If it doesn't it may be for other reasons. You need to check the return value of OrderSend and GetLastError() code.


And beside that you should fix this line:

      for (int t=0; t<OrdersTotal(); t++) {

Always loop back to front:

      for (int t=OrdersTotal()-1; t>=0; t--) {


(ignore the link to order_calc_margin, it's auto-generated by this brain-dead forum software)

 
lippmaje:

You have to show it again of course if you fixed it and it's still not working.

The code looks ok and should open trades when the fast MA crosses the slow one, that is on Mar 1st (up) and 2nd (down cross).

If it doesn't it may be for other reasons. You need to check the return value of OrderSend and GetLastError() code.


And beside that you should fix this line:

Always loop back to front:


(ignore the link to order_calc_margin, it's auto-generated by this brain-dead forum software)

How do I check the value of OrderSend and GetLastError. Do I put it inside the last bracket?

 
Ok I fixed the problem where it wouldn't buy/sell on the cross, now I am back to making a close order function when the EMA crosses back over. So if the ema crosses for a buy when it crosses for a sell I want it to exit.
//+------------------------------------------------------------------+
//|                                                  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;

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)) {
            
            Comment ("Buy");
            
            int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 10, 0, 0, "Buy Order", Magic, 0, clrGreen);
      }
  }
      
      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;
 }
 
I think this should work but I am using the OrderClose wrong, anyone know how I could correctly use this?
//+------------------------------------------------------------------+
//|                                                  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){
      
         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;
 }

Reason: