论坛风格切换切换到宽版
  • 612阅读
  • 0回复

MT4自带的EA怎么加上止赢止损设置 [复制链接]

上一主题 下一主题
离线myto
 

发帖
79
金钱
0
威望
0
贡献
79
交易币
0
只看楼主 倒序阅读 使用道具 楼主  发表于: 2011-12-06
下面是MT4软件上面自带的系统Moving Average,怎么在这个系统上加上止赢止损设置呢?? oWL_Hh%-f`  
Hc =QSP  
  1. //+------------------------------------------------------------------+
  2. //|                                               Moving Average.mq4 |
  3. //|                      Copyright ?2005, MetaQuotes Software Corp. |
  4. //|                                       http://www.metaquotes.net/ |
  5. //+------------------------------------------------------------------+
  6. #define MAGICMA  20050610
  7. extern double Lots               = 0.1;
  8. extern double MaximumRisk        = 0.02;
  9. extern double DecreaseFactor     = 3;
  10. extern double MovingPeriod       = 12;
  11. extern double MovingShift        = 6;
  12. //+------------------------------------------------------------------+
  13. //| Calculate open positions                                         |
  14. //+------------------------------------------------------------------+
  15. int CalculateCurrentOrders(string symbol)
  16.   {
  17.    int buys=0,sells=0;
  18. //----
  19.    for(int i=0;i<OrdersTotal();i++)
  20.      {
  21.       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
  22.       if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
  23.         {
  24.          if(OrderType()==OP_BUY)  buys++;
  25.          if(OrderType()==OP_SELL) sells++;
  26.         }
  27.      }
  28. //---- return orders volume
  29.    if(buys>0) return(buys);
  30.    else       return(-sells);
  31.   }
  32. //+------------------------------------------------------------------+
  33. //| Calculate optimal lot size                                       |
  34. //+------------------------------------------------------------------+
  35. double LotsOptimized()
  36.   {
  37.    double lot=Lots;
  38.    int    orders=HistoryTotal();     // history orders total
  39.    int    losses=0;                  // number of losses orders without a break
  40. //---- select lot size
  41.    lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
  42. //---- calcuulate number of losses orders without a break
  43.    if(DecreaseFactor>0)
  44.      {
  45.       for(int i=orders-1;i>=0;i--)
  46.         {
  47.          if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
  48.          if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
  49.          //----
  50.          if(OrderProfit()>0) break;
  51.          if(OrderProfit()<0) losses++;
  52.         }
  53.       if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
  54.      }
  55. //---- return lot size
  56.    if(lot<0.1) lot=0.1;
  57.    return(lot);
  58.   }
  59. //+------------------------------------------------------------------+
  60. //| Check for open order conditions                                  |
  61. //+------------------------------------------------------------------+
  62. void CheckForOpen()
  63.   {
  64.    double ma;
  65.    int    res;
  66. //---- go trading only for first tiks of new bar
  67.    if(Volume[0]>1) return;
  68. //---- get Moving Average
  69.    ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  70. //---- sell conditions
  71.    if(Open[1]>ma && Close[1]<ma)  
  72.      {
  73.       res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
  74.       return;
  75.      }
  76. //---- buy conditions
  77.    if(Open[1]<ma && Close[1]>ma)  
  78.      {
  79.       res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
  80.       return;
  81.      }
  82. //----
  83.   }
  84. //+------------------------------------------------------------------+
  85. //| Check for close order conditions                                 |
  86. //+------------------------------------------------------------------+
  87. void CheckForClose()
  88.   {
  89.    double ma;
  90. //---- go trading only for first tiks of new bar
  91.    if(Volume[0]>1) return;
  92. //---- get Moving Average
  93.    ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  94. //----
  95.    for(int i=0;i<OrdersTotal();i++)
  96.      {
  97.       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
  98.       if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  99.       //---- check order type
  100.       if(OrderType()==OP_BUY)
  101.         {
  102.          if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
  103.          break;
  104.         }
  105.       if(OrderType()==OP_SELL)
  106.         {
  107.          if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
  108.          break;
  109.         }
  110.      }
  111. //----
  112.   }
  113. //+------------------------------------------------------------------+
  114. //| Start function                                                   |
  115. //+------------------------------------------------------------------+
  116. void start()
  117.   {
  118. //---- check for history and trading
  119.    if(Bars<100 || IsTradeAllowed()==false) return;
  120. //---- calculate open orders by current symbol
  121.    if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
  122.    else                                    CheckForClose();
  123. //----
  124.   }
  125. //+------------------------------------------------------------------+
 

  • 在线咨询
  • 400-881-0680