有您的持续关注
我们会做得更好

均线系统 V1.0 EA



均线系统 V1.0 EA是根据均线开发的智能交易系统。带有中文注释,不失为新手学习编写EA最佳的参考资料。

本网收集整理智能交易EA仅为MQL4编程爱好者提供语法及相关编程技巧的学习研究之用,智能交易存在极高的风险测试请用模拟盘,请勿用于实盘。均线系统 V1.0 EA源码:

//+------------------------------------------------------------------+
//|                                                均线系统 V1.0.mq4 |
//|                                                           臭猴子 |
//+------------------------------------------------------------------+
#property copyright "臭猴子"
#property link      "http://www.125808047.com/"

//定义外部变量,该变量可在外部修改
extern int SHORT = 8;
extern int LONG = 20;
extern int TREND = 60;
extern double Lots = 0.1;
int MagicNumber = 1122;

bool cg = false;
//+------------------------------------------------------------------+
//| 系统初始化函数,对当前图表窗口左上角的提示消息进行初始化                                 |
//+------------------------------------------------------------------+
int init()
  {
//----
   ObjectCreate("comment_label",OBJ_LABEL,0,0,0);
   ObjectCreate("comment_labe2",OBJ_LABEL,0,0,0);
   ObjectCreate("comment_labe3",OBJ_LABEL,0,0,0);
   ObjectCreate("comment_labe4",OBJ_LABEL,0,0,0);
   ObjectCreate("comment_labe5",OBJ_LABEL,0,0,0);
   ObjectCreate("comment_labe6",OBJ_LABEL,0,0,0);
   
   ObjectSet("comment_label",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_label",OBJPROP_YDISTANCE,20);
   
   ObjectSet("comment_labe2",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_labe2",OBJPROP_YDISTANCE,40);
   
   ObjectSet("comment_labe3",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_labe3",OBJPROP_YDISTANCE,60);

   ObjectSet("comment_labe4",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_labe4",OBJPROP_YDISTANCE,80);

   ObjectSet("comment_labe5",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_labe5",OBJPROP_YDISTANCE,100);

   ObjectSet("comment_labe6",OBJPROP_XDISTANCE,10);
   ObjectSet("comment_labe6",OBJPROP_YDISTANCE,120);
   
   ObjectSetText("comment_label","MaCurrentShort = ",9,"Arial",Yellow);
   ObjectSetText("comment_labe2","MaPreviousShort = ",9,"Arial",Yellow);
   ObjectSetText("comment_labe3","MaCurrentLong = ",9,"Arial",Yellow);
   ObjectSetText("comment_labe4","MaPreviousLong = ",9,"Arial",Yellow);
   ObjectSetText("comment_labe5","MaCurrentTrend = ",9,"Arial",Yellow);
   ObjectSetText("comment_labe6","MaPreviousTrend = ",9,"Arial",Yellow);
   WindowRedraw();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| 销毁函数,用于在系统执行完毕后销毁左上角的提示文字                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("comment_label");
   ObjectDelete("comment_labe2");
   ObjectDelete("comment_labe3");
   ObjectDelete("comment_labe4");
   ObjectDelete("comment_labe5");
   ObjectDelete("comment_labe6");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| 主函数部分,价格每波动一个点则start函数执行一次                                          |
//+------------------------------------------------------------------+
int start()
  {
  //定义变量,用于存储均线数值
  double MaCurrentShort, MaPreviousShort, MaCurrentLong, MaPreviousLong, MaCurrentTrend, MaPreviousTrend;

  //开始计算均线数据
   MaCurrentShort=NormalizeDouble(iMA(NULL,0,SHORT,0,MODE_EMA,PRICE_CLOSE,0),5);  //当前的短均线值
   MaPreviousShort=NormalizeDouble(iMA(NULL,0,SHORT,0,MODE_EMA,PRICE_CLOSE,1),5);      //上一根K线的短均线值
   
   
   MaCurrentLong=NormalizeDouble(iMA(NULL,0,LONG,0,MODE_SMA,PRICE_CLOSE,0),5);   //当前的长均线值
   MaPreviousLong=NormalizeDouble(iMA(NULL,0,LONG,0,MODE_SMA,PRICE_CLOSE,1),5);  //上一根K线的长均线值 
   
   MaCurrentTrend=NormalizeDouble(iMA(NULL,0,TREND,0,MODE_EMA,PRICE_CLOSE,0),5);   //长周期的当前均线值
   MaPreviousTrend=NormalizeDouble(iMA(NULL,0,TREND,0,MODE_EMA,PRICE_CLOSE,2),5);  //长周期的上一根K线均线值

   //屏幕左上角显示提示信息
   ObjectSetText("comment_label","MaCurrentShort = " + MaCurrentShort,9,"Arial",Yellow);
   ObjectSetText("comment_labe2","MaPreviousShort = " + MaPreviousShort,9,"Arial",Yellow);
   ObjectSetText("comment_labe3","MaCurrentLong = " + MaCurrentLong,9,"Arial",Yellow);
   ObjectSetText("comment_labe4","MaPreviousLong = " + MaPreviousLong,9,"Arial",Yellow);
   ObjectSetText("comment_labe5","MaCurrentTrend = " + MaCurrentTrend,9,"Arial",Yellow);
   ObjectSetText("comment_labe6","MaPreviousTrend = " + MaPreviousTrend,9,"Arial",Yellow);
   
   //在总手数为零的条件下判断是否满足下单条件
   if (OrdersTotal() == 0)
      {
      if (BuyOrderBool(MaCurrentShort, MaPreviousShort, MaCurrentLong, MaPreviousLong, MaCurrentTrend, MaPreviousTrend) == true)
         {
            cg = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, "Order Buy", MagicNumber, 0, Yellow);
         }
      else if (SellOrderBool(MaCurrentShort, MaPreviousShort, MaCurrentLong, MaPreviousLong, MaCurrentTrend, MaPreviousTrend) == true)
         {
            cg = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, "Order Sell", MagicNumber, 0, Yellow);
         }
      }
   
   //循环遍历当前所有的订单,判断是否满足平仓条件,如果满足则平仓
   for(int pos=0; pos<OrdersTotal();pos++) 
      {
         cg = OrderSelect(pos, SELECT_BY_POS, MODE_TRADES);
            if (OrderType() == OP_BUY)
               {
                  if (BuyCloseBool(MaCurrentShort, MaPreviousShort, MaCurrentLong, MaPreviousLong) == true)
                     {
                        //Print("MaCurrentShort = ", MaCurrentShort, ",MaPreviousShort = ", MaPreviousShort, ",MaCurrentLong = ", MaCurrentLong, ",MaPreviousLong = ", MaPreviousLong);
                        CloseAllOrder();
                     }
                }
            else if (OrderType() == OP_SELL)
               {
                  if (SellCloseBool(MaCurrentShort, MaPreviousShort, MaCurrentLong, MaPreviousLong) == true)
                     {
                        //Print("MaCurrentShort = ", MaCurrentShort, ",MaPreviousShort = ", MaPreviousShort, ",MaCurrentLong = ", MaCurrentLong, ",MaPreviousLong = ", MaPreviousLong);
                        CloseAllOrder();
                     }
               }
      }
   
      
//主函数结束
   return(0);
  }
   
//该函数用来判断当前是否满足短均线上穿长均线,同时趋势向上
bool BuyOrderBool(double MaCurrentShort, double MaPreviousShort, double MaCurrentLong, double MaPreviousLong, double MaCurrentTrend, double MaPreviousTrend)
{
   if ( MaCurrentShort > MaCurrentLong && MaPreviousShort < MaPreviousLong) 
      {
         return(true);
      }
   else
      {
         return(false);
      }
   return(false); 
}

//该函数用来判断当前是否满足短均线下穿长均线,同时趋势向下
bool SellOrderBool(double MaCurrentShort, double MaPreviousShort, double MaCurrentLong, double MaPreviousLong, double MaCurrentTrend, double MaPreviousTrend)
{
   if ( MaCurrentShort < MaCurrentLong && MaPreviousShort > MaPreviousLong) 
      {
         return(true);
      }
   else
      {
         return(false);
      }
   return(false); 
}

//该函数用来判断多单是否满足平仓条件,如果短均线下穿长均线则返回true,否则返回false
bool BuyCloseBool(double MaCurrentShort, double MaPreviousShort, double MaCurrentLong, double MaPreviousLong)
{
   if (MaCurrentShort < MaCurrentLong && MaPreviousShort > MaPreviousLong)
      {
         return (true);
      }
   else
      {
         return (false);
      }
   return (false);
}

//该函数用来判断空单是否满足平仓条件,如果短均线上穿长均线则返回true,否则返回false
bool SellCloseBool(double MaCurrentShort, double MaPreviousShort, double MaCurrentLong, double MaPreviousLong)
{
   if (MaCurrentShort > MaCurrentLong && MaPreviousShort < MaPreviousLong)
      {
         return (true);
      }
   else
      {
         return (false);
      }
   return (false);
}

//该函数用于平仓,会将所持订单全部平仓,不返回值
void CloseAllOrder()
{
   bool OrderCloseBool = true;
   for(int pos1=0; pos1<OrdersTotal();pos1++) 
      {
         cg = OrderSelect(pos1, SELECT_BY_POS, MODE_TRADES);
            if (OrderType() == OP_BUY)
               {
                  cg = OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
               }
            else
               {
                  cg = OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
               }   
      }
}
  
  
//+------------------------------------------------------------------+

如您喜欢此文章请点下面分享按钮↴峰汇在线 » 均线系统 V1.0 EA
上一篇:
下一篇:
分享到:更多 ()