//+------------------------------------------------------------------+
//| 双MA.mq4 |
//| |
//+------------------------------------------------------------------+
//--- input parameters
extern int shortp=20;
extern int longp=40;
extern double Lots=0.01;
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line10)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"My MA",12345,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);
}
//平仓-------------------------------------------------------------------------------------------------------
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);// close position
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"My MA",12345,0,Red);
return(0); // exit
}
}
if(OrderType()==OP_SELL) // go to short position
{
if(isCrossed == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);// close position
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"My MA",12345,0,Green);
return(0); // exit
}
}
}
}
return(0);
}
|