Bars cross over Moving Average, possible?

 

I try to code this

Bars (Ask Price) over moving average period 30 then buy and vise versa

most of the trend code wrote as 2 moving average line cross then buy,

so i like to write only bar and one single moving average

the code is
int start()
  {
//----
   int MALine, CurrentPrice;
   CurrentPrice = Ask+20*Point;
   MALine = iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
   
   int ticket, total;
   
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
      }
     
//start trade - buy
total = OrdersTotal();
if(total < 1) //if no trade open
   {
   if (CurrentPrice > MALine)
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoplose*Point,Ask+TakeProfit*Point,"My EA",MagicNumber,0,Red);
      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);
      }
   }

//----
   return(0);
  }

but it nothing show, code wrong? please give suggestion or can't write this way

Reason

Enter market after 20pips to avoid re-paint

Files:
test-buy.mq4  3 kb
 
20 pips over ma
int start()
  {
//----
   int MALine, CurrentPrice;
   //CurrentPrice = Ask+20*Point;      ==>  not this way
   MALine = iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
   
   int ticket, total;
   
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
      }
     
//start trade - buy
total = OrdersTotal();
if(total < 1) //if no trade open
   {
//   if (CurrentPrice > MALine)           wrong
    if(Ask>=MALine+20*Point)       //4 Digit broker
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoplose*Point,Ask+TakeProfit*Point,"My EA",MagicNumber,0,Red);
      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);
      }
   }

//----
   return(0);
  }
Make a new parameter and replace the 20 for something like pipsfromline (extern int)
 
deVries:
20 pips over ma Make a new parameter and replace the 20 for something like pipsfromline (extern int)

Thanks deVries, I will try and learn more, Great Help
 
int start()
  {
//----
   int MALine, CurrentPrice;
   MALine = iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
   
   int ticket, total;
   
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
      }
     
//start trade - buy
total = OrdersTotal();
if(total < 1) //if no trade open
   {
   if(Ask>MALine+30*Point)
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoplose*Point,Ask+TakeProfit*Point,"My EA",MagicNumber,0,Red);
      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);
      }
   }

//----
   return(0);
  }
I try to code, and the ea buy even the ask price is below MA, any one can give suggestion
Files:
 
FXEWEN:
I try to code, and the ea buy even the ask price is below MA, any one can give suggestion

MALine should be a double! Try this

//+------------------------------------------------------------------+
//|                                                        xtest.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern int TakeProfit = 30;
extern int Stoplose = 20;
extern double Lots = 0.1;
extern int MagicNumber = 11111;
double  Pips;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
    Pips=Set_Pips();
//----
   return(0);
  }
int deinit()
  {
//----
   
//----
   return(0);
  }
int start()
  {
   double MALine; 
   int CurrentPrice;
   MALine = iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
   int ticket, total;
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
      }     
//start trade - buy
total = OrdersTotal();
if(total < 1) //if no trade open
   {
   if(Ask>MALine +30*Pips)
      {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoplose*Pips,Ask+TakeProfit*Pips,"My EA",MagicNumber,0,Red);
      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);
      }
   }
   return(0);
  }
//+------------------------------------------------------------------+
double Set_Pips()
{
        double pips=Point; //<---- make pips local and return it
        if(Digits==3)
        {
                pips=0.01;
        }
        if(Digits==5)
        {
                pips=0.0001;
        }
        return(pips);
}

 
Your EA must adjust for 4/5 digit brokers. TP, SL AND SLIPPAGE. Danjp Don't hard code numbers - your solution will not work on metals, and can't adjust slippage.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
 

Hallo everybody, i'm new in this and maybe you can help me with.

I saw this code and is approximately like one of my strategyes, but...i want it to open buy orders when a white candle crosses above my MA with 20 Pips and close when a black candle crosses under the MA

with 20 Pips, but simultaneously to open an sell order and close when the next white candle cross above the MA with 20 Pips, and so one.

Thank you, and i am sorry if i post wrong or i upset someone.