Download MetaTrader 5
To add comments, please log in or register
Read the interviews of the Automated Trading Championship participants
oladapolaleye
182
oladapolaleye 2014.07.28 09:52 
Please help me with the code for my EA to make one trade per indicator cross signal and wait for another cross again to make another trade decision. Thanks.
whroeder1
13652
whroeder1 2014.07.28 11:43  
oladapolaleye: help me with the code for my EA to make one trade per indicator cross signal and wait for another cross again to make another trade decision.
You have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
oladapolaleye
182
oladapolaleye 2014.07.28 14:08  
WHRoeder:
oladapolaleye: help me with the code for my EA to make one trade per indicator cross signal and wait for another cross again to make another trade decision.
You have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.



WHRoeder, Thanks for your response and readiness to help. I asked for help based on what I have tried but didn't know what to do to get result.

I attached my attempt to this.

Now, I've an indicator on indicator window with 2 lines Red and Blue, it shows trend without direction when blue line is above its red line which runs for hours.  then I use trend following indicator to place order within this period but sometimes the trend following indicator gives multiple signals in which some occur at price reversing point. Therefore, I need a code to apply so that after when blue is above its red, the trend following indicator can only execute one trade and wait until that blue turn below red and turn up again above its red.

Any solution appreciated.

Regards

//+------------------------------------------------------------------+
//|                                             Waddah Expert A3.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double TakeProfit = 1000;
extern double Lots = 0.1;
extern double StopLoss = 300;
extern double TrailingStop = 300;
extern double AtrPeriod = 14;
extern double MatrPeriod = 14;

extern int MA_Period = 14;
extern int RSIPeriod1 = 14;
extern int RSIPeriod2 = 28;

int New_Bar;
int Time_0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double ATR, MATR, Gap, WA1, WA2;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
// to simplify the coding and speed up access
// data are put into internal variables
   
   ATR = iCustom(NULL,0,"Matrik",AtrPeriod,MatrPeriod,0,0);
   MATR = iCustom(NULL,0,"Matrik",AtrPeriod,MatrPeriod,1,0);
   Gap = ATR - MATR;
   WA1 = iCustom(NULL,0,"Waddah_Attar_Def_RSI",RSIPeriod1,RSIPeriod2,MA_Period,0,0);
   WA2 = iCustom(NULL,0,"Waddah_Attar_Def_RSI",RSIPeriod1,RSIPeriod2,MA_Period,0,1);
   
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
     New_Bar=0;
    if(Time_0!=Time[0])
    {
    New_Bar=1;
    Time_0=Time[0];
    }
      // check for long position (BUY) possibility
      if(New_Bar==1 && ATR>MATR && Gap>=0.0002 && WA1>0 && WA1>WA2)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Waddah Expert A3",00003,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(New_Bar==1 && ATR>MATR && Gap>=0.0002 && WA1<0 && WA1<WA2) 
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"Waddah Expert A3",00003,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(MATR>ATR || WA1<0)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(MATR>ATR || WA1>0)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.
Files:
matrik.mq4 3 kb
oladapolaleye
182
oladapolaleye 2014.07.28 14:10  
Screenshot for illustration
oladapolaleye
182
oladapolaleye 2014.07.28 14:46  
Screenshot
whroeder1
13652
whroeder1 2014.07.28 20:46  
oladapolaleye: the trend following indicator can only execute one trade and wait until that blue turn below red and turn up again above its red.
if(New_Bar==1 && ATR>MATR && Gap>=0.0002 && WA1>0 && WA1>WA2)
You are checking for a level, Instead check for a cross.
if(Time_0 == Time[0]) return(0); Time_0 = Time[0];

int  ATRprev =  ATR;
int MATRprev = MATR;
 ATR = ...
MATR = ...
bool isCross = (ATRprev > MATRprev) != (ATR > MATR);

:
     if(isCross && ATR>MATR && Gap>=0.0002 && WA1>0 && WA1>WA2)
oladapolaleye
182
oladapolaleye 2014.08.01 13:54  
WHRoeder:
oladapolaleye: the trend following indicator can only execute one trade and wait until that blue turn below red and turn up again above its red.
if(New_Bar==1 && ATR>MATR && Gap>=0.0002 && WA1>0 && WA1>WA2)
You are checking for a level, Instead check for a cross.
if(Time_0 == Time[0]) return(0); Time_0 = Time[0];

int  ATRprev =  ATR;
int MATRprev = MATR;
 ATR = ...
MATR = ...
bool isCross = (ATRprev > MATRprev) != (ATR > MATR);

:
     if(isCross && ATR>MATR && Gap>=0.0002 && WA1>0 && WA1>WA2)

Thanks for your response but it still makes more than a trade within the cross up.
/
To add comments, please log in or register