تفضل
كود PHP://+------------------------------------------------------------------+
//| Moving Average.mq4 |
//| Copyright 2005-2014, MetaQuotes Software Corp. |
//| http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2005-2014, MetaQuotes Software Corp."
#property link "http://www.mql4.com"
#property description "Test"
#define MAGICMA 20131111
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LastOrderInfo(string info)
{
for(int r=OrdersHistoryTotal()-1;r>=0;r--)
{
bool select=OrderSelect(r,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGICMA)
{
if(info=="Type")return(OrderType());
else if(info=="Profit")return(OrderProfit());
}
}
return(0);
}
int TotalLossNumber()
{
int num=0;
for(int y=OrdersHistoryTotal()-1;y>=0;y--)
{
bool select=OrderSelect(y,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MAGICMA)
{
if(OrderProfit()<0)num++;
else return(num);
}
}
return(num);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma;
int res;
int res1;
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- sell conditions
if(Close[1]<Open[1] &&TotalLossNumber()==0 ) //1
{
res=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"BO exp:60",MAGICMA,0,Red);
}
//--- buy conditions
if(Close[1]>Open[1]&&TotalLossNumber()==0) //2
{
res=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"BO exp:60",MAGICMA,0,Blue);
}
//---
if(TotalLossNumber()<4&&LastOrderInfo("Profit")<0)
{
if(LastOrderInfo("Type")==OP_BUY)
{
res1=OrderSend(Symbol(),OP_BUY,0.05,Ask,0,0,0,"BO exp:60",MAGICMA,0,Blue);
}
else if(LastOrderInfo("Type")==OP_SELL)
{
res1=OrderSend(Symbol(),OP_SELL,0.05,Bid,0,0,0,"BO exp:60",MAGICMA,0,Red);
}
}
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma;
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- get Moving Average
//---
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(Close[1]<Open[1]) //1
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Close[1]>Open[1]) //2
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
//+------------------------------------------------------------------+

