zs65221 发表于 2017-3-7 23:19:33

我只想要一个就几行代码的单边EA,我试新手,编不好

//+------------------------------------------------------------------+
//|                                                         单边.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                 |
//+------------------------------------------------------------------+
int start()                                     // 特别函数 start
{
   int Dist_SL =7;                           // 设定止损位 7 个点
   int Dist_TP =4;                              // 设定止盈位 4 个点
   double Prots=0.1;                           // 交易使用 1% 的保证金
   string Symb=Symbol();                        // 交易对象名称
   double Win_Price=WindowPriceOnDropped();   // 脚本被拖拉进的窗口,价格点位
   Alert("The price is set by the mouse as Price = ",Win_Price);// 点击鼠标设定的价格
//------------------------------------------------------------------------------- 2 --
   while(true)                                  // 建仓过程循环
   {
      int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 最小止损/止盈点位
      double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 最小交易手数
      double Free   =AccountFreeMargin();       // 保证金
      double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每手所需保证金
      double Lot=MathFloor(Free*Prots-One_Lot*Min_Lot)*Min_Lot;// 总手数
      //------------------------------------------------------------------------- 3 --
      double Price=Win_Price;                   // 点击鼠标设定的价格
      if (NormalizeDouble(Price,Digits)<      // 若小于下限   
         NormalizeDouble(Ask+Min_Dist*Point,Digits))
      {                                       // 仅可 BuyStop 挂单!
         Price=Ask+Min_Dist*Point;            // 没有平仓
         Alert("Changed the requested price: Price = ",Price);
      }
      //------------------------------------------------------------------------- 4 --

      double TP=Price + Dist_TP*Point;          // 止盈报价
      if (Dist_TP < Min_Dist)                   // 若低于下限
      {
         TP=Price + Min_Dist*Point;             // 以下限为止盈点位
         Alert(" Increased the distance of TP = ",Min_Dist," pt");
      }
      //------------------------------------------------------------------------- 6 --
      Alert("The request was sent to the server. Waiting for reply..");
      int ticket=OrderSend(Symb, OP_BUYSTOP, Lot, Price, 0, 0, TP);
      //------------------------------------------------------------------------- 7 --
      if (ticket>0)                           // 服务器接受挂单!:)
      {
         Alert ("Placed order BuyStop ",ticket);
         break;                                 // 退出挂单
      }
      //------------------------------------------------------------------------- 8 --
      int Error=GetLastError();               // 挂单被拒绝 :(
      switch(Error)                           // 可克服的错误
      {
         case 129:Alert("Invalid price. Retrying..");
            RefreshRates();                     // 更新数据
            continue;                           // 继续
         case 135:Alert("The price has changed. Retrying..");
            RefreshRates();                     // 更新数据
            continue;                           // 继续
         case 146:Alert("Trading subsystem is busy. Retrying..");
            Sleep(500);                         // 简单处理方案
            RefreshRates();                     // 更新数据
            continue;                           // 继续
      }
      switch(Error)                           // 致命错误
      {
         case 2 : Alert("Common error.");
            break;                              // 退出本 'switch'
         case 5 : Alert("Outdated version of the client terminal.");
            break;                              // 退出本 'switch'
         case 64: Alert("The account is blocked.");
            break;                              // 退出本 'switch'
         case 133:Alert("Trading fobidden");
            break;                              // 退出本 'switch'   
         default: Alert("Occurred error ",Error);// 其他错误
      }
      break;                                    // 退出挂单
   }
//------------------------------------------------------------------------------- 9 --
   Alert ("The script has completed its operations -----------------------------");
   return 0;                                    // 退出 start()
}
//------------------------------------------------------------------------------- 10 --

爻爻 发表于 2020-5-20 22:58:54

路过,支持一下啦

悠悠 发表于 2020-6-17 13:27:50

好帖,来顶下

之乎者烨 发表于 2020-7-2 11:14:58

不错,支持下楼主

゛北城别 发表于 2020-7-15 12:30:22

谢谢楼主分享

hxmzj 发表于 2020-8-14 11:32:58

帮你顶下哈!!

学汇生存 发表于 2020-8-26 22:38:26

帮你顶下哈!!

风的温度 发表于 2021-7-8 15:33:51

{:1_181:}

股票配资小吴 发表于 2023-10-4 21:22:20

{:1_194:}

轩越 发表于 2024-1-25 16:06:15

顶下
页: [1]
查看完整版本: 我只想要一个就几行代码的单边EA,我试新手,编不好