Need a little help on making my EA take only 1 trade then stop

 

Hey guys so last year I developed an EA to automatically take trades for me. My strategy works very well as long as you know how to read price trends. Anyways as you can tell my code is quite basic and I only knew enough to code this EA since my strategy is real easy to understand. So now I would like my EA to only take one trade and the shut down. Where and what should I code into my EA for this to become possible. I believe it has to do with my code in the bottom "TotalNoOforders". But not too sure can anyone please help


//+------------------------------------------------------------------+
//|                                            GloriousFXMonster.mq4 |
//|                                  Copyright 2017, Angel Dickinson |
//|                              https://www.facebook.com/RealGFXCEO |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Angel Dickinson"
#property link      "https://www.facebook.com/RealGFXCEO"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;

void OnTick()
  {
  
  if (TotalNoOfOrders(Symbol()) > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
}
//---
   
  
//+------------------------------------------------------------------+

int TotalNoOfOrders(string symb){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==symb && OrderMagicNumber()==MagicNumber)
         i++; 
   }
   return(i);
}
 
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print...
  1. OrderSend does not return a boolean (and never zero which is false,) so your test is always true.Check your return codes for errors.

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)
 
whroeder1:
  1. OrderSend does not return a boolean (and never zero which is false,) so your test is always true.Check your return codes for errors.

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

My problem is not in the execution of trades though even with what you are sayin. Everytime a trades gets activated its exactly where I would've got in if I was trading manually. The EA has yet to fail when it comes to execution. I just want to code it to only take 1 trade and then stop.


BTW I dont have any error when I compile

 
You can use ExpertRemove() after the trade has been placed.
 
//+------------------------------------------------------------------+
//|                                            GloriousFXMonster.mq4 |
//|                                  Copyright 2017, Angel Dickinson |
//|                              https://www.facebook.com/RealGFXCEO |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Angel Dickinson"
#property link      "https://www.facebook.com/RealGFXCEO"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;

void OnTick()
  {
  
  if (TotalNoOfOrders(Symbol()) > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    

    if(OrdersTotal()==0){ 
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
  }
}
//---
   
  
//+------------------------------------------------------------------+

int TotalNoOfOrders(string symb){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==symb && OrderMagicNumber()==MagicNumber)
         i++; 
   }
   return(i);
}
 
Marco vd Heijden:

hey I see you added "if(OrdersTotal()==0)" how would this code effect the robot? This code doesn't change anything in the system. Like I said all I need is to make it take one trade and then stop thats it. Nothing is wrong with the coding in the entry. The entry and exit rules are all fine they dont need to be changed all I need is a code for the bot to make it only take one trade and then stop.

 
Keith Watford:
You can use ExpertRemove() after the trade has been placed.

ok anything else that needs to be added?

 
Angel Dickinson:

ok anything else that needs to be added?

No, ExpertRemove() will remove the EA from the chart.

 
Keith Watford:

No, ExpertRemove() will remove the EA from the chart.

ok so like this?

I highlighted the void expertremove() I just feel like there is something missing or maybe I placed it in the wrong spot
//+------------------------------------------------------------------+
//|                                            GloriousFXMonster.mq4 |
//|                                  Copyright 2017, Angel Dickinson |
//|                              https://www.facebook.com/RealGFXCEO |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Angel Dickinson"
#property link      "https://www.facebook.com/RealGFXCEO"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;

void OnTick()
  {
  
  if (TotalNoOfOrders(Symbol()) > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
}
//--- Rule to Remove Expert
   
  void  ExpertRemove();


//+------------------------------------------------------------------+

int TotalNoOfOrders(string symb){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==symb && OrderMagicNumber()==MagicNumber)
         i++; 
   }
   return(i);
}
 
 if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }

You have completely ignored WHRoeder's post. Get the result from OrderSend() correctly.

You only want the EA to place one trade, so if an OrderSend() is successful, THEN remove the EA.

Reason: