Download MetaTrader 5
To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
fxtrader_
247
fxtrader_ 2016.02.25 21:52 

Hi,

I mide some change to this EA from 

OP_BUY

to

OP_BUYLIMIT

and it work fine expet (Stop loss and tak profit) it is not working?

//+------------------------------------------------------------------+
//|                                         My Moving Average EA.mq4 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+


#property copyright "Copyright 2015, AL-Tubaiti Software Corp."
#property link      "https://www.mql5.com"

extern int     PriceLevel =10;
extern int         TakeProfit = 300;
extern int         StopLoss   = 20;
extern double  LotSize    = 1.0;

extern bool    UseMoveToBreakEven=false;
extern int     WhenToMoveToBe = 100;
extern int     PipsToLockIn = 10;

extern bool    UseTrailingStop = true;
extern int     WhenToTrail = 20;
extern int     TrailAmount =20;

extern bool    UseCandleTrail = false;
extern int     PadAmount =10;
extern int     CandleBack =5;


      extern int FastMATime = 0;
      extern int FastMAPeriod = 5;
      extern int FastMAType = 0; //0:SMA 1:EMA 2:SMMA 3:LWMA
      extern int FastMAPrice = 0;
      extern int FastMAShift = 0;
      extern int SlowMATime = 0;
      extern int SlowMAPeriod = 30;
      extern int SlowMAType = 1; //0:SMA 1:EMA 2:SMMA 3:LWMA
      extern int SlowMAPrice = 0;
      extern int SlowMAShift = 0;



extern int     MagicNumber = 1234;
double pips;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(TickSize == 0.00001 || TickSize == 0.001)
   pips = TickSize*10;
   else pips = TickSize;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  if(OpenOrdersThisPair(Symbol())>=1)
  {
      if(UseMoveToBreakEven)MoveToBreakeven();
      if(UseTrailingStop)AdjustTrail();
  }
  if(IsNewCandle())CheckForMaTrade();
   
//---
   return(0);
  }
//+------------------------------------------------------------------+

void MoveToBreakeven()
{
   for(int b = OrdersTotal()-1; b >= 0; b--);
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()== MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice() > WhenToMoveToBe*pips)
                  if(OrderOpenPrice()>OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE);
            
   }
   for(int s = OrdersTotal()-1;s>=0; s--)
   {
   if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
           if(OrderType()==OP_SELL)
              if(OrderOpenPrice()-Ask>WhenToMoveToBe*pips)
                 if(OrderOpenPrice()<OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE);
    }
}

//+------------------------------------------------------------------+
//|           trailing stop function                                 |
//+------------------------------------------------------------------+

void AdjustTrail()
{
int buyStopCandle  = iLowest (NULL,0,1,CandleBack,0);
int SellStopCandle = iHighest(NULL,0,2,CandleBack,0);

// buy order section
   for(int b=OrdersTotal() -1; b >= 0; b--)
      {
      if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol() ==Symbol())
               if(OrderType()==OP_BUY)
               if(UseCandleTrail)
               { 
                  if(IsNewCandle())
                     if(OrderStopLoss()<Low[buyStopCandle]-PadAmount*pips)
                        OrderModify(OrderTicket(),OrderOpenPrice(), Low[buyStopCandle]-PadAmount*pips,OrderTakeProfit(),0,clrNONE);
               }     
              else if(Bid-OrderOpenPrice()> WhenToTrail*pips)
                     if(OrderStopLoss()<Bid-pips*TrailAmount)
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*TrailAmount),OrderTakeProfit(),0,clrNONE);
                        
      }
      
      
      
// sell order section
   for(int s=OrdersTotal()-1; s>=0; s--)
      {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber() == MagicNumber)
            if(OrderSymbol() == Symbol())
               if(OrderType() ==OP_SELL)
                  if(UseCandleTrail)
                  {
                     if(IsNewCandle())
                        if(OrderStopLoss()>High[SellStopCandle]+PadAmount*pips)
                           OrderModify(OrderTicket(),OrderOpenPrice(),High[SellStopCandle]+PadAmount*pips,OrderTakeProfit(),0,clrNONE);
                  }
                 else if(OrderOpenPrice() -Ask>WhenToTrail*pips)
                        if(OrderStopLoss()>Ask+TrailAmount*pips || OrderStopLoss() ==0)
                           OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*pips),OrderTakeProfit(),0,clrNONE);
                     
      }

}

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

bool IsNewCandle()
{
   static int BarsOnChart=0;
   if(Bars ==BarsOnChart)
   return(false);
   BarsOnChart = Bars;
   return(true);
}
//+------------------------------------------------------------------+

void CheckForMaTrade()
{

//+------------------------------------------------------------------+
      double
      FastMACurrent,FastMAPrevious,SlowMACurrent,SlowMAPrevious;
      
      int Current = 0;
      
      FastMACurrent = iMA(NULL, FastMATime, FastMAPeriod, FastMAShift, FastMAType, FastMAPrice, Current + 0);
      FastMAPrevious = iMA(NULL, FastMATime, FastMAPeriod, FastMAShift, FastMAType, FastMAPrice, Current + 1);
      
      SlowMACurrent = iMA(NULL, SlowMATime, SlowMAPeriod, SlowMAShift, SlowMAType, SlowMAPrice, Current + 0);
      SlowMAPrevious = iMA(NULL, SlowMATime, SlowMAPeriod, SlowMAShift, SlowMAType, SlowMAPrice, Current + 1);
      
      
//-------

   double CrossUp = FastMACurrent > SlowMACurrent&& FastMAPrevious < SlowMAPrevious;
   double CrossDn = FastMACurrent < SlowMACurrent&& FastMAPrevious > SlowMAPrevious;
   
   
/////////////////////////////////

  if( CrossUp ) OrderEntry(0);
  if(  CrossDn ) OrderEntry(1);
         
/////////////////////////////////
         
         
}

void OrderEntry(int direction)
{
         if (direction ==0)
          {
            double bsl=0;
            double btp=0;
            if(StopLoss!=0)bsl = Ask-(StopLoss*pips);
               if(TakeProfit!=0)btp = Ask+(TakeProfit*pips);
                  if(OpenOrdersThisPair(Symbol())==0)int buyticket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,Ask - PriceLevel * Point,3,0,0, NULL,MagicNumber,TimeCurrent()+1800 ,clrGreen);
                     if(buyticket > 0) OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,clrGreen);
          }
         if (direction ==1)
          {
            double ssl=0;
            double stp=0;
            if(StopLoss !=0)ssl = Bid+(StopLoss*pips);
               if(TakeProfit !=0)stp = Bid-(TakeProfit*pips);
                  if(OpenOrdersThisPair(Symbol())==0)int sellticket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Bid + PriceLevel * Point,3,0,0, NULL,MagicNumber,TimeCurrent()+1800 ,clrRed);
                     if(sellticket > 0) OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrGreen);
          }

} 

int OpenOrdersThisPair(string pair)
{
   int total=0;
      for(int i=OrdersTotal()-1; i >= 0; i--)
      {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
       if(OrderSymbol() == pair) total++;
      }
      return(total);
}
Keith Watford
Moderator
8017
Keith Watford 2016.02.25 22:18  
You are trying to set the SL and TP relative to the current price, they should be relative to the entry price
fxtrader_
247
fxtrader_ 2016.02.25 22:26  

Thanks GumRai,

should i have to change this


if(StopLoss!=0)bsl = Ask-(StopLoss*pips);

to

if(StopLoss!=0)bsl = Ask-(PriceLevel*pips);

Keith Watford
Moderator
8017
Keith Watford 2016.02.26 00:01  
entry=Ask - PriceLevel * Point;
if(StopLoss!=0)bsl = entry-(StopLoss*pips);
if(TakeProfit!=0)btp = entry+(TakeProfit*pips);
.
fxtrader_
247
fxtrader_ 2016.02.27 12:54  

Hi GumRai,

Thank you for your patience, I modify the code as you told , But some times it is hit the take profit without closing the trade. as it is showing in the attached photo.
Also some time it is not showing stop loss or take profit?

I think the problem with the (Stop Loss , Take profit ) Setting, But i'm not able to find the solution. Please help.



//+------------------------------------------------------------------+
//|                                         My Moving Average EA.mq4 |
//|                        Copyright 2015, AL-Tubaiti Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+


#property copyright "Copyright 2015, AL-Tubaiti Software Corp."
#property link      "https://www.mql5.com"

   extern int     PriceLevel = 10;
   
   extern int      TakeProfit = 50;
   extern int      StopLoss   = 20;
   extern double  LotSize    = 1.0;
   
   
   extern bool    UseMoveToBreakEven=false;
   extern int     WhenToMoveToBe = 100;
   extern int     PipsToLockIn = 5;
   
   extern bool    UseTrailingStop = true;
   extern int     WhenToTrail = 15;
   extern int     TrailAmount =15;
   
   
   extern bool    UseCandleTrail = false;
   extern int     PadAmount =10;
   extern int     CandleBack =5;

   extern int REDMA    = 5;
   extern int YELLOWMA = 21;
   extern int BLUEMA   = 50;

extern int     MagicNumber = 1234;
double pips;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(TickSize == 0.00001 || TickSize == 0.001)
   pips = TickSize*10;
   else pips = TickSize;
  
   Comment("\nTestv4.mq4");
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  if(OpenOrdersThisPair(Symbol())>=1)
  {
      if(UseMoveToBreakEven)MoveToBreakeven();
      if(UseTrailingStop)AdjustTrail();
  }
  if(IsNewCandle())CheckForMaTrade();
   
//---
   return(0);
  }
//+------------------------------------------------------------------+

void MoveToBreakeven()
{
   for(int b = OrdersTotal()-1; b >= 0; b--);
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()== MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice() > WhenToMoveToBe*pips)
                  if(OrderOpenPrice()>OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE);
            
   }
   for(int s = OrdersTotal()-1;s>=0; s--)
   {
   if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
           if(OrderType()==OP_SELL)
              if(OrderOpenPrice()-Ask>WhenToMoveToBe*pips)
                 if(OrderOpenPrice()<OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE);
    }
}

//+------------------------------------------------------------------+
//|           trailing stop function                                 |
//+------------------------------------------------------------------+

void AdjustTrail()
{
int buyStopCandle  = iLowest (NULL,0,1,CandleBack,0);
int SellStopCandle = iHighest(NULL,0,2,CandleBack,0);

// buy order section
   for(int b=OrdersTotal() -1; b >= 0; b--)
      {
      if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol() ==Symbol())
               if(OrderType()==OP_BUY)
               if(UseCandleTrail)
               { 
                  if(IsNewCandle())
                     if(OrderStopLoss()<Low[buyStopCandle]-PadAmount*pips)
                        OrderModify(OrderTicket(),OrderOpenPrice(), Low[buyStopCandle]-PadAmount*pips,OrderTakeProfit(),0,clrNONE);
               }     
              else if(Bid-OrderOpenPrice()> WhenToTrail*pips)
                     if(OrderStopLoss()<Bid-pips*TrailAmount)
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*TrailAmount),OrderTakeProfit(),0,clrNONE);
                        
      }
      
      
      
// sell order section
   for(int s=OrdersTotal()-1; s>=0; s--)
      {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber() == MagicNumber)
            if(OrderSymbol() == Symbol())
               if(OrderType() ==OP_SELL)
                  if(UseCandleTrail)
                  {
                     if(IsNewCandle())
                        if(OrderStopLoss()>High[SellStopCandle]+PadAmount*pips)
                           OrderModify(OrderTicket(),OrderOpenPrice(),High[SellStopCandle]+PadAmount*pips,OrderTakeProfit(),0,clrNONE);
                  }
                 else if(OrderOpenPrice() -Ask>WhenToTrail*pips)
                        if(OrderStopLoss()>Ask+TrailAmount*pips || OrderStopLoss() ==0)
                           OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*pips),OrderTakeProfit(),0,clrNONE);
                     
      }

}

bool IsNewCandle()
{
   static int BarsOnChart=0;
   if(Bars ==BarsOnChart)
   return(false);
   BarsOnChart = Bars;
   return(true);
}
//+------------------------------------------------------------------+



void CheckForMaTrade()
{

//RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

      double M30_Red_0,M30_Yellow_0, M30_Red_1,M30_Yellow_1;
      
        M30_Red_0    = iMA(Symbol(),PERIOD_M30,5, 0,MODE_SMA,PRICE_CLOSE,0);
        M30_Yellow_0 = iMA(Symbol(),PERIOD_M30,21,0,MODE_SMA,PRICE_CLOSE,0);

        M30_Red_1    = iMA(Symbol(),PERIOD_M30,5, 0,MODE_SMA,PRICE_CLOSE,1);
        M30_Yellow_1 = iMA(Symbol(),PERIOD_M30,21,0,MODE_SMA,PRICE_CLOSE,1);


      double CrossUp = M30_Red_1 < M30_Yellow_1 && M30_Red_0 > M30_Yellow_0;
      double CrossDn = M30_Red_1 > M30_Yellow_1 && M30_Red_0 < M30_Yellow_0;


//RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

  if( CrossUp) OrderEntry(0);
  if( CrossDn) OrderEntry(1);
         
//RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
         
}

void OrderEntry(int direction)
{
         if (direction ==0)
          {
            double bsl=0;
            double btp=0;
            
            //if(StopLoss!=0)bsl = Ask-(StopLoss*pips);
            //   if(TakeProfit!=0)btp = Ask+(TakeProfit*pips);
               
            double entry = Ask - PriceLevel * pips;
            if(StopLoss!=0)bsl = entry-(StopLoss*pips);
            if(TakeProfit!=0)btp = entry+(TakeProfit*pips);
               
                  if(OpenOrdersThisPair(Symbol())==0)int buyticket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,entry,3,0,0, NULL,MagicNumber,TimeCurrent()+5400,clrDarkOrange);
                     if(buyticket > 0) OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,clrGreen);
          }
         if (direction ==1)
          {
            double ssl=0;
            double stp=0;
            
            //if(StopLoss !=0)ssl = Bid+(StopLoss*pips);
            //   if(TakeProfit !=0)stp = Bid-(TakeProfit*pips);
               
            double Sentry = Bid + PriceLevel * pips;
            if(StopLoss!=0)ssl = Sentry + (StopLoss*pips);
            if(TakeProfit!=0)stp = Sentry - (TakeProfit*pips);

                  if(OpenOrdersThisPair(Symbol())==0)int sellticket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Sentry,3,0,0, NULL,MagicNumber,TimeCurrent()+5400,clrYellow);
                     if(sellticket > 0) OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrGreen);
          }

}

int OpenOrdersThisPair(string pair)
{
   int total=0;
      for(int i=OrdersTotal()-1; i >= 0; i--)
      {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
       if(OrderSymbol() == pair) total++;
      }
      return(total);
}
fxtrader_
247
fxtrader_ 2016.02.27 18:10  

Hi,

I think i found the problem, I changed this line

OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrGreen);

to

OrderModify(sellticket,Sentry,ssl,stp,0,clrGreen)


and it seem to work.


Please confirm if this is right?

Keith Watford
Moderator
8017
Keith Watford 2016.02.27 23:19  

As you didn't select the order before using OrderOpenPrice(), then it could cause an error.

I don;t believe that that explains the image that you showed earlier though.

It would appear that, because of the spread, the Ask did not go low enough to trigger the order. So it remained a limit order.

A limit order has to be triggered before it can hit TP

/
To add comments, please log in or register