SL and TP system

 
Hi I'm wondering how I can make a TP and SL system based on EMA crosses I attached my code it compiles but does not work. Any help is much appreciated.
if (GetTotalOpenTrades() < MaxTrades) {
         
         if (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;
         
         int CloseLong = OrderClose(Magic, LotsToTrade, FastEMA < SlowEMA, 10, clrGreen);
         }
 
Tiberious:
Hi I'm wondering how I can make a TP and SL system based on EMA crosses I attached my code it compiles but does not work. Any help is much appreciated.

Any Logs are Much Appreciated also... And, a full code??

We don't have a Crystal Ball...

 
Flavio Jarabeck:

Any Logs are Much Appreciated also... And, a full code??

We don't have a Crystal Ball...

Sorry here is the full code. 

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);
      
      if (GetTotalOpenTrades() < MaxTrades) {
         
         if (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 < SlowEMA) {
         
            ShortSetup = True;
         
         }
         
         if (ShortSetup == True) {
         
         int OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); 
         ShortSetup = False;
         
         //int CloseShort = OrderClose(Magic, LotsToTrade, FastEMA > SlowEMA, 10, clrRed);
         
         }
      
      }
   } 
      
     
  }
  


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

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.

I thought this would be apart of trading systems

 
Tiberious:

I thought this would be apart of trading systems

Ideally, trading systems would be discussions about strategies and not about coding.

All MQL4 coding should be in this section.

 
Keith Watford:

Ideally, trading systems would be discussions about strategies and not about coding.

All MQL4 coding should be in this section.

ok

 
Not sure if this code will work but it is saying there is something wrong with line 112 where the code ends. What would I need to return.
//+------------------------------------------------------------------+
//|                                                  EMA AutoBot.mq4 |
//|                                                        Tiberious |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Tiberious"
#property link      "https://www.mql5.com"
#property version   "1.00"
#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);
      
      if (GetTotalOpenTrades() < MaxTrades) {
         
         if (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;
        // }
         
         if (FastEMA < SlowEMA)
            CloseAllBuyTrades();
                 
         }
      else if (GetTotalOpenTrades() < MaxTrades) {
         
         if (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;
}

   double CloseAllBuyTrades() {

   for (int i=OrdersTotal(); i>=0;i--) {
   
      if (OrderSelect(i, SELECT_BY_POS)==true)
      
      if (OrderSymbol() == Symbol()) {
      
      OrderClose (OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, clrAliceBlue);
      }
   }
}


 
 
Tiberious:
Not sure if this code will work but it is saying there is something wrong with line 112 where the code ends. What would I need to return.

I fixed this error it does not work. Still trying to close the open order when ema cross

 
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);
      
      if (GetTotalOpenTrades() < MaxTrades) {
         
         if (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;
         }
       
          if (FastEMA < SlowEMA) {
            
            LongSetup = False;
          }       
          
          if (LongSetup == False) {
          
            OrderClose(Magic, LotsToTrade, Bid, 3, clrAliceBlue);
           
          }

Just tried this put the trade still does not close It might have something to do with Magic, on the reference it says ticket im not sure whats suppose to go there or if this is the correct way to do it

 

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:

   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 (FastEMA_prev < SlowEMA_prev && FastEMA >= SlowEMA) upcross=true;
   if (FastEMA_prev > SlowEMA_prev && FastEMA <= SlowEMA) downcross=true;
Reason: