KDJ自动化交易

//+------------------------------------------------------------------+
//|                                               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();
//----
  }
//+------------------------------------------------------------------+
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值