My EA only buy not sell

 
Hi there, i am coding the EA for buy and sell but in somehow, my EA only open the buy order, please help me a check :( . This was really weird :( 
//+------------------------------------------------------------------+
//|                                              Scalping System.mq4 |
//|                                                    Raymond's Bui |
//|                   https://www.facebook.com/raymond.buithanhphat/ |
//+------------------------------------------------------------------+
#property copyright "Raymond's Bui"
#property link      "https://www.facebook.com/raymond.buithanhphat/"
#property version   "1.00"
#property strict
//--- input parameters
input int      Ema1=19; // Ema short period:
input int      Ema2=39; // Ema long period:
input double   FixedLots=0.01; // Fixed lot size(0 if not use):
input double   Stoploss=0.5; // Stoploss in Pips:
input double   Spread=3.0; // Max spread in Pips:
input double   MaximalLots=40.0; // Maximal lots size:
input double   MinimalLots=0.01; // Minimal lots size:
input double   LotSize=0.2; // Lot size per $1000:
input int Magic_Number =   8071994; //Input the magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
double lotsize(){
   if(FixedLots > 0){
   return FixedLots;
   }
   else {double size = AccountBalance() / 1000 * LotSize;
      if(FixedLots ==0){
         if(size < MinimalLots){
         return MinimalLots;
         }
         if(size > MaximalLots){
         return MaximalLots;
         }
      }
   return size;}
}
int CountSellPosition(){

   int NumberOfSellPositions= 0;
   for(int i=OrdersTotal()-1;i >=0; i--){
   // select an open trade
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      // get the currency pair
      string CurrencyPair=OrderSymbol();
      if(_Symbol== CurrencyPair)
         {if(OrderType()==OP_SELL){
      NumberOfSellPositions=NumberOfSellPositions+1;
   }}
  }
  return NumberOfSellPositions;
}
int CountBuyPosition(){

   int NumberOfBuyPositions= 0;
   for(int i=OrdersTotal()-1;i >=0; i--)
   {
   // select an open trade
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   // get the currency pair
   string CurrencyPair=OrderSymbol();
   if(_Symbol== CurrencyPair)
   if(OrderType()==OP_BUY)
   {
   NumberOfBuyPositions=NumberOfBuyPositions+1;
   }
  }
  return NumberOfBuyPositions;
}

void OnTick()
  {
//---
  double atr = iATR(_Symbol,_Period,8,0);
  double ema1 = iMA(_Symbol,_Period,Ema1,0,MODE_EMA, PRICE_CLOSE,0);
  double lastEma1 = iMA(_Symbol,_Period,Ema1,0,MODE_EMA, PRICE_CLOSE,4);
  double ema2 = iMA(_Symbol,_Period,Ema2,0,MODE_EMA, PRICE_CLOSE,0);
  double lastEma2 = iMA(_Symbol,_Period,Ema2,0,MODE_EMA, PRICE_CLOSE,4);
  int ticket;
  double SLBuy = Ask - Stoploss;
  double SLSell = Bid - Stoploss;
  double ATR = 3*atr;
  if(ema1 > ema2 && (ema1-lastEma1>0)&& (ema2-lastEma2>0)&& CountBuyPosition() <1){
   ticket = OrderSend(Symbol(), OP_BUY, lotsize(),Ask,5, SLBuy, Ask + ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)){
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", SLBuy, " TP: ", Ask + ATR);}
          }}
          else Print("Error Opening BUY  Order: ", GetLastError());
   
  if(ema1 < ema2 && (lastEma1-ema1>0)&& (lastEma2-ema2>0)  && CountSellPosition() <1){
   ticket = OrderSend(Symbol(), OP_SELL, lotsize(),Bid,5,SLSell , Bid - ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)){
            Print("Sell Order Opened: ", OrderOpenPrice(), " SL:",SLSell, " TP: ", Bid - ATR);
            
          }}
          else
            Print("Error Opening BUY  Order: ", GetLastError());
   }
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
 
   ticket = OrderSend(Symbol(), OP_BUY, lotsize(),Ask,5, SLBuy, Ask + ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);
⋮
   ticket = OrderSend(Symbol(), OP_SELL, lotsize(),Bid,5,SLSell , Bid - ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  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.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  double SLBuy = Ask - Stoploss;
  double SLSell = Bid - Stoploss;
The SL for a buy is below the market. For a sell it is above.
 
William Roeder #:

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  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.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Your SL=0.5 is less than the spread.
Okay so this mean i should buy at bid and sell and bid too. Is that right?
 
What part of “You buy at the Ask and sell at the Bid.” was unclear?
 
William Roeder #:
What part of “You buy at the Ask and sell at the Bid.” was unclear?
Sorry, English is not my native language, a little bid I am not clear but I get the point about the Ask and Bid in order. I really appreciate your help,sir. Thank you very much
 
William Roeder #:
What part of “You buy at the Ask and sell at the Bid.” was unclear?

I am fixed into this and now , it is only sell :(

  double SLBuy = Bid - Stoploss*_Point;
  double SLSell = Ask + Stoploss*_Point;
  double ATR = 3*atr;
  if(ema1 > ema2 && (ema1-lastEma1>0)&& (ema2-lastEma2>0)&& CountBuyPosition() <1){
   ticket = OrderSend(Symbol(), OP_BUY, lotsize(),Bid,5, SLBuy, Bid + ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)){
            Print("BUY Order Opened: ", OrderOpenPrice(), " SL:", SLBuy, " TP: ", Bid + ATR);}
          }}
          else Print("Error Opening BUY  Order: ", GetLastError());
   
  if(ema1 < ema2 && (lastEma1-ema1>0)&& (lastEma2-ema2>0)  && CountSellPosition() <1){
   ticket = OrderSend(Symbol(), OP_SELL, lotsize(),Bid,5,SLSell , Bid - ATR, "Raymond's Buy trend VERS 5.0",Magic_Number,0,Blue);
        if(ticket > 0){
          if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)){
            Print("Sell Order Opened: ", OrderOpenPrice(), " SL:",SLSell, " TP: ", Bid - ATR);
            
          }}
          else
            Print("Error Opening BUY  Order: ", GetLastError());
   }