Send order when MA Cross

 


//+------------------------------------------------------------------+
//| NewCrossAverage.mq4 |
//| doshur |
//| www.doshur.com |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link "www.doshur.com"

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//---- MAs

double Now_MA_Signal, Prev_MA_Signal;
double Now_MA_MainClose, Prev_MA_MainClose;

Now_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,1);
Prev_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,2);

Now_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,1);
Prev_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,2);

//---- Buy

if(Now_MA_MainClose < Now_MA_Signal && Prev_MA_MainClose >= Prev_MA_Signal)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"doshur",0,0,Blue);
}

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






i need to create one buy order when MAcross

this code seem to create lots of buy order when MA crosses, anyone can help?

 
doshur wrote >>


//+------------------------------------------------------------------+
//| NewCrossAverage.mq4 |
//| doshur |
//| www.doshur.com |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link "www.doshur.com"

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//---- MAs

double Now_MA_Signal, Prev_MA_Signal;
double Now_MA_MainClose, Prev_MA_MainClose;

Now_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,1);
Prev_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,2);

Now_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,1);
Prev_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,2);

//---- Buy

if(Now_MA_MainClose < Now_MA_Signal && Prev_MA_MainClose >= Prev_MA_Signal)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"doshur",0,0,Blue);
}

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

i need to create one buy order when MAcross

this code seem to create lots of buy order when MA crosses, anyone can help?

HI,

with ever Tick in the current Bar your EA discover the Cross of MA from before.

Put something like

if (Time[0] == prevtime) return(0);
prevtime = Time[0];

between //---- MAs and //---- Buy

prevtime, in tis case is declared as

static int prevtime = 0; on the bgeinning of your Code (after " extern double Lots..." for Example)

 
doshur wrote >>


//+------------------------------------------------------------------+
//| NewCrossAverage.mq4 |
//| doshur |
//| www.doshur.com |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link "www.doshur.com"

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//---- MAs

double Now_MA_Signal, Prev_MA_Signal;
double Now_MA_MainClose, Prev_MA_MainClose;

Now_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,1);
Prev_MA_Signal = iMA(NULL,0,5,0,MODE_LWMA,PRICE_CLOSE,2);

Now_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,1);
Prev_MA_MainClose = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,2);

//---- Buy

if(Now_MA_MainClose < Now_MA_Signal && Prev_MA_MainClose >= Prev_MA_Signal)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"doshur",0,0,Blue);
}

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

i need to create one buy order when MAcross

this code seem to create lots of buy order when MA crosses, anyone can help?

total=OrdersTotal();
if(total<1)

...

return(0);

 
viennatrader:

HI,

with ever Tick in the current Bar your EA discover the Cross of MA from before.

Put something like

if (Time[0] == prevtime) return(0);
prevtime = Time[0];

between //---- MAs and //---- Buy

prevtime, in tis case is declared as

static int prevtime = 0; on the bgeinning of your Code (after " extern double Lots..." for Example)

Hi

Thanks for the advice

Let me try

 

Its works like magic now

Thanks viennatrader

 
doshur:

Its works like magic now

Thanks viennatrader


Hello doshur

Can you please share final version of this code? Thank you

Reason: