my ea opens long position with big gap price but not short position....

 

my ea opens Long position with big gap price but not short position...

plz anyone help me?

//+------------------------------------------------------------------+
//|                              Expert Advisor for MetaTrader 4     |
//|                                                    version 1     |
//|                                           Copyright 2012 Kay     |
//|                                         EA Conversion by Kay     |
//+------------------------------------------------------------------+
//| VERSION HISTORY                                                  |
//| 1.0  - Initial release.                                          |
//+------------------------------------------------------------------+

//EXTERNAL VARIABLES
extern bool   CompoundGrowth    = false;
extern double lots              = 0.1;
extern double Lots;
extern int    Lotsdevide        = 700;
extern bool   UseStopLoss       = true;
extern int    StopLoss          = 1000;
extern bool   UseTakeProfit     = false;
extern int    TakeProfit        = 0;
extern bool   CloseOnOpposite   = true;
extern bool   UseTrailingStop   = true;
extern int    TrailingStop      = 100;
bool          AutoStopLoss      = false;
extern double AutoSLLevel;
bool          AutoTakeProfit    = false;
extern double AutoTPLevel;
extern int    MaxOrder          = 1;
extern int    emaX              = 10;


//INTERNAL VARIABLES
double CandleTime;
string OrdSignal;
int    i;
int    ticket;
int    slippage=5;
double StopLossLevel;
double TakeProfitLevel;

//INITIALISATION
int init()
{ 
   CandleTime=Time[0];   
   
   return(0);
}

//MAIN ROUTINE
int start()
{
 //Comment("\n" ,Time[0]);
 
   //COMPOUND GROWTH
   if(CompoundGrowth)
   {
      Lots=0;
      double Thousands = AccountBalance() / Lotsdevide;
      double RoundDown = MathFloor(Thousands);
      if (RoundDown < 1) { RoundDown =1; }
      Lots= NormalizeDouble(lots * RoundDown,2);      
    }  
      if(Lots < 0.1) { Lots =0.1; }
        
 /*  //Close Position when opposite signal comes
      //Close Longs
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1) > (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1))){
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0) < (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0))){
         CloseLongs();
      }}
      //Close Shorts
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1) < (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1))){
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0) > (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0))){ 
         CloseShorts();
      }}   */
   
   //if(CandleTime!=Time[0])
   //{
            
   //Open Position
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2) < (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,2))){
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1) > (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1))){ 
         CloseShorts();
         if(CandleTime!=Time[0])
         {
         OpenBuy();
         }
         CandleTime=Time[0];
      }}
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2) > (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,2))){
      if(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1) < (iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,1))){
         CloseLongs();
         if(CandleTime!=Time[0])
         {
         OpenSell();
         }
         CandleTime=Time[0];
      }}
      //CandleTime = Time[0];
   //}  
     // CandleTime = Time[0];
   //TrailingStop
   if(UseTrailingStop)
   {
      TrailingStopLongs();
      TrailingStopShorts();
   }
}
          
int CloseLongs()
{
   if(OrdersTotal() > 0){
      for(i=0;i<OrdersTotal();i++){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderSymbol()==Symbol()){
               if(OrderMagicNumber()==111){
                  if(OrderType()==OP_BUY){
                     OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Green);}}}}}}

int CloseShorts()
{
   if(OrdersTotal() > 0){
      for(i=0;i<OrdersTotal();i++){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderSymbol()==Symbol()){
               if(OrderMagicNumber()==222){
                  if(OrderType()==OP_SELL){
                     OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Green);}}}}}}


int TrailingStopLongs()
{
   for (int i = 0; i < OrdersTotal(); i ++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber()==111 && OrderType()==OP_BUY) 
      {
         if(Bid - OrderOpenPrice() > Point * TrailingStop) 
         {     
            double oldStopLoss = OrderStopLoss();
            double newStopLoss = Bid-Point*TrailingStop;
            double nextStopLoss = oldStopLoss + Point; // Adding 1 point to oldStopLoss, i.e. 76.78 to 76.79   
                  
            if(newStopLoss >= nextStopLoss) 
            { // checking that the new stoploss price is at least 1 point or greater then the old stoploss
            OrderModify(OrderTicket(),OrderOpenPrice(),newStopLoss,0,0,Green);
            continue;
            }
         }
      }
   }
}

int TrailingStopShorts()
{
   for (int i = 0; i < OrdersTotal(); i ++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber()==222 && OrderType()==OP_SELL)
      {
         if(OrderOpenPrice() - Ask > Point * TrailingStop)
         {
            double oldStopLoss = OrderStopLoss();
            double newStopLoss = Ask + Point*TrailingStop;
            double nextStopLoss = oldStopLoss + Point; // Adding 1 point to oldStopLoss, i.e. 76.78 to 76.79
            
            if(newStopLoss >= nextStopLoss) // checking that the new stoploss price is at least 1 point or greater then the old stoploss
            {
            OrderModify(OrderTicket(),OrderOpenPrice(),newStopLoss,0,0,Green);
            continue;
            }
         }
      }
   }
}

int OpenSell()
{
   if(OrdersTotal() < MaxOrder)
      {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,0,0,"",222,0,Orange);
      if(ticket>0)
      {
         Print("Sell Order Opened :", OrderOpenPrice() + " Symbol :", Symbol() + " Magic :", OrderMagicNumber());
      }
      else {Print("Sell Order Failed :", GetLastError());}
      Comment("\n" ,"BUY");
      SellModify();
   }
}

int OpenBuy()
{
   if(OrdersTotal() < MaxOrder)
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,0,0,"",111,0,DodgerBlue);
      if(ticket>0)
      {
         Print("Buy Order Opened :", OrderOpenPrice() + " Symbol :", Symbol() + " Magic :", OrderMagicNumber());
      }
      else {Print("Buy Order Failed :", GetLastError());}
      Comment("\n" ,"Sell");
      BuyModify();
   }
}

int BuyModify()
{
   if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
   if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

   for(i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==111 && OrderType()==OP_BUY)
         {
            OrderModify(ticket,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,Yellow);
         }
   }
}

int SellModify()
{
   if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
   if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;
   
   for(i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==222 && OrderType()==OP_SELL)
         {
            OrderModify(ticket,OrderOpenPrice(),StopLossLevel,TakeProfitLevel,0,Green);
         }
   }
}
 

Spread weekendtesting that time very big

Buy trades are opened at Ask not Bid

Your Sell trade is opened at the normal Bid price which you see on your chart

 

//devries

thx for fast response.

but i dont understand clearly...

can u plz explain more? about "Buy trades are opened at Ask not Bid". isn't Ask right? to long position?

 
kayny:

//devries

thx for fast response.

but i dont understand clearly...

can u plz explain more? about "Buy trades are opened at Ask not Bid". isn't Ask right? to long position?

Ofcours Ask is right for buy trades

For the highest candel price is your highest Bid value not Ask value Ask is higher then Bid (Bid + Spread = ASK)

sO THE GAP has to do with Spread you are buying at Ask

 
The candles are Bid prices, your buy's OPENED above the candle because of the spread. Your sell's CLOSED above the candle because of the spread.
 

wow.... that was scared...

my main broker's spread is 1.6 but i tested with Alpari... they have 5~6pip spread... never know;;;;

thx guys for ur response

 
kayny:

wow.... that was scared...

my main broker's spread is 1.6 but i tested with Alpari... they have 5~6pip spread... never know;;;;

thx guys for ur response

You are testing in weekend normally the spread with Alpari is not that high but when markets are closed you get this spread
Reason: