//+------------------------------------------------------------------+
//| KD.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//这是帮别人写的,主要是KD下到15,开始向上时做多,用户可以根据自己的需要,修改KD的判断,也可以修改成MACD 或者MA的自动化交易
#define MAGICKD 20130923
extern double MaxRisk = 0.02;
extern double KPeriod = 5;
extern double DPeriod = 3;
extern double Slowing = 3;
extern double StopLoss = 100;
extern double TakeProfit = 100;
double OpenPrice;
double StopLossPrice;
double TakeProfitPrice;
double KCurrent, KPrevious, DCurrent, DPrevious;
bool IsInOpen()
{
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICKD || OrderSymbol()!=Symbol()) continue;
OpenPrice = OrderOpenPrice();
if(OrderType()==OP_BUY)
{
StopLossPrice = OpenPrice-StopLoss*Point;
TakeProfitPrice = OpenPrice+TakeProfit*Point;
}
else// if(OrderType()==OP_SELL)
{
StopLossPrice = OpenPrice+StopLoss*Point;
TakeProfitPrice = OpenPrice-TakeProfit*Point;
}
return (true);
}
return (false);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=0;
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaxRisk/1000.0,1);
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
if(IsInOpen())
return;
//---- sell conditions
/*if(KCurrent>85 && DCurrent>85 && KCurrent<KPrevious && DCurrent<DPrevious)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICKD,0,Red);
return;
}*/
//---- buy conditions
if(KCurrent<15 && DCurrent<15 && KCurrent>KPrevious && DCurrent>DPrevious)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICKD,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
//----
if(IsInOpen())
{
if(OrderType()==OP_BUY)
{
if(Bid<StopLossPrice || Bid>TakeProfitPrice)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
}
}
else// if(OrderType()==OP_SELL)
{
if(Ask>StopLossPrice || Ask<TakeProfitPrice)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
}
}
}
//----
}
void recalc()
{
KCurrent = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_EMA,PRICE_CLOSE,MODE_MAIN,0);
KPrevious = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_EMA,PRICE_CLOSE,MODE_MAIN,1);
DCurrent = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,0);
DPrevious = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,1);
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//----
recalc();
//----
CheckForOpen();
CheckForClose();
//----
}
//+------------------------------------------------------------------+