: | : | :期货量化学习 | :期货量化 |
返回列表 发帖

[转载]MetaTrader编程中级(0)--一个EA练习

[转载]MetaTrader编程中级(0)--一个EA练习

  1. MetaTrader编程中级(0)--一个EA练习

  2. 这里提供配套的一个指标和一个EA,
  3. 从中可以学会,1 EA的文件结构, 2 指标的文件结构 巩固和发展前面的入门材料.

  4. 进一步可以分析这个EA的表现,来认识 (1) 这个EA有哪些"诡异"的想象,原因是什么? (2)这个指标和EA可以使用吗?如何使用? (3)EA是如何优化的? (4) 不同公司不同数据,不同时间段 表现如何? 分析原因?(5)如何改进?
  5. 声明,这个EA是学习编程用的, 若你要使用,当然后果自负. 有关问题我可能选择回答.
  6. 这时最近偶然设计的一个EA, 内容很简单, 但我认为分析它 有很好的学习价值.

  7. 我初步的测试结果最好时, 5.13~5.24十天从10000到18000, EUR, 目前的缺省参数就是,使用了MT自己的数据MetaQuotes-Demo. 但是却可能不能用.


  8. 指标文件: 存为0meme.mq4


  9. #property indicator_chart_window

  10. //#property indicator_separate_window
  11. #property indicator_buffers 2
  12. #property indicator_color1 Aqua//Lime
  13. #property indicator_color2 Red


  14. double buffer10[];
  15. double buffer20[];
  16. extern int period=79;//10;
  17. extern int price=5; // 0 or other = (H+L)/2
  18.             // 1 = Open
  19.             // 2 = Close
  20.             // 3 = High
  21.             // 4 = Low
  22.             // 5 = (H+L+C)/3
  23.             // 6 = (O+C+H+L)/4
  24.             // 7 = (O+C)/2
  25. extern double lwei= 0.67;
  26. bool Mode_Fast= False;

  27. int init()
  28. {
  29.   SetIndexArrow(0,67);//200);
  30.   SetIndexArrow(1,68);//202);  
  31.   SetIndexBuffer(0,buffer10);
  32.   SetIndexBuffer(1,buffer20);
  33. return(0);
  34. }


  35. int deinit()
  36. {
  37. int i;
  38. return(0);
  39. }


  40. double Value=0,Value1=0,Value2=0,Fish=0,Fish1=0,Fish2=0;

  41. double _price;
  42. double MinL=0;
  43. double MaxH=0;            


  44. void getprice0(int i)
  45. {
  46. MaxH = High[iHighest(NULL,0,MODE_HIGH,period,i)];
  47.   MinL = Low[iLowest(NULL,0,MODE_LOW,period,i)];
  48.   switch (price)
  49.   {
  50.   case 1: _price = Open[i]; break;
  51.   case 2: _price = Close[i]; break;
  52.   case 3: _price = High[i]; break;
  53.   case 4: _price = Low[i]; break;
  54.   case 5: _price = (High[i]+Low[i]+Close[i])/3; break;
  55.   case 6: _price = (Open[i]+High[i]+Low[i]+Close[i])/4; break;
  56.   case 7: _price = (Open[i]+Close[i])/2; break;
  57.   default: _price = (High[i]+Low[i])/2; break;
  58.   }

  59. }
  60. datetime lasttime = NULL;
  61. int start()
  62. {

  63. int i;
  64. int barras;
  65. if ((lasttime != NULL ) && ((Time[0] - lasttime) < Period( ) )) return;
  66. lasttime = Time[0];
  67. SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3);
  68.   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3);

  69. barras = Bars;
  70. if (Mode_Fast)
  71.   barras = 100;
  72. i = 0;
  73. while(i<barras)
  74.   {
  75.   getprice0(i);
  76. // Value = 0.33*2*((_price-MinL)/(MaxH-MinL)-0.5) + 0.67*Value1;  
  77.   Value = (1-lwei)*2*((_price-MinL)/(MaxH-MinL)-0.5) + lwei*Value1;  
  78.   Value=MathMin(MathMax(Value,-0.999),0.999);
  79.   Fish = 0.5*MathLog((1+Value)/(1-Value))+0.5*Fish1;

  80.    
  81.   buffer10[i] = EMPTY_VALUE;
  82.   buffer20[i] = EMPTY_VALUE;

  83.   if ( (Fish<0) && (Fish1>0))
  84.   {
  85.   buffer10[i] = Low[i];

  86.   }  
  87.   if ((Fish>0) && (Fish1<0))
  88.   { buffer20[i] = High[i];

  89.   }   
  90.   Value1 = Value;
  91.   Fish2 = Fish1;
  92.   Fish1 = Fish;
  93. i++;
  94.   }
  95. return(0);
  96. }
  97. //+------------------------------------------------------------------+




  98. EA文件: 存为你喜欢的名字

  99. CODE:


  100. //+------------------------------------------------------------------+
  101. //|                           Designed by OKwh, China   |
  102. //|                   Copyright 2006, OKwh |
  103. //|                                             |
  104. //+------------------------------------------------------------------+
  105. #property copyright "Copyright 2006, OKwh "
  106. #property link     ""
  107. #define MAGICMA 200610011231
  108. //本程序适合200603-04-05, 07-08

  109. extern int whichmethod = 3;//1;//4;   //1~4 select no T/P no S/L,has T/P no S/L, no T/P has S/L, has T/P has S/L,
  110. extern double TakeProfit = 51;//104;   //point 方法2时无效
  111. extern   double StopLoss = 20;//25;   //point
  112. extern double Lots = 1;     // default lots
  113. extern double MaximumRisk     = 0.3;   //0.1,or 0.8   0.3
  114. extern double DecreaseFactor   = 8; // factor if many lose 8* 3 5
  115. extern double TrailingStop =13;//10~14 15;// 60;   // T/S

  116. extern   int maxOpen = 4;//3;   //最多持仓限制 3* 10,9,8,6,5
  117. extern   int maxLots = 5;   //最多单仓限制 7* 5 3 1
  118. extern int trdTime = 2; //1 //0 一次,1 2次, 2,3次

  119. extern int bb = 1;
  120. datetime lasttime = NULL;
  121. int i, res;
  122. int init()   {     return(0); }
  123. int deinit() {   return(0); }
  124. //+------------------------------------------------------------------+
  125. //| Calculate optimal lot size                           |
  126. double LotsOptimized()  
  127. {
  128. double lot=Lots;
  129. int   orders=HistoryTotal();   // history orders total
  130. int   losses=0;             // number of losses orders without a break
  131. // if (AccountBalance() < 8000)     MaximumRisk = 0.1;
  132. // else MaximumRisk = 0.3;inin = 5;inin = 10;inin = 20;inin = 20;inin = 20;inin = 20;
  133. double fm =0;
  134. if (AccountBalance() > 200000)   fm = AccountBalance()- 100000 ;
  135. if (AccountBalance() < 100000)   {   MaximumRisk = 0.5;   }
  136. if (AccountBalance() < 80000)   {   MaximumRisk = 0.5; }
  137. if (AccountBalance() < 50000)   {   MaximumRisk = 0.5; }
  138. if (AccountBalance() < 20000)   {   MaximumRisk = 0.4; }
  139. if (AccountBalance() < 12000)   {   MaximumRisk = 0.2;   }
  140. fm = AccountFreeMargin();
  141. if(AccountLeverage()<=0)
  142.   {
  143.     Print("CalculateVolume: invalid AccountLeverage() [",AccountLeverage(),"]");
  144.     return(0);
  145.   }
  146.   double lot_min =MarketInfo(Symbol(),MODE_MINLOT);
  147.   double lot_max =MarketInfo(Symbol(),MODE_MAXLOT);
  148.   double lot_step=MarketInfo(Symbol(),MODE_LOTSTEP);
  149. if(lot_min<0 || lot_max<=0.0 || lot_step<=0.0)
  150.   {
  151.     Print("CalculateVolume: invalid MarketInfo() results [",lot_min,",",lot_max,",",lot_step,"]");
  152.     return(0);
  153.   }

  154.   lot=NormalizeDouble(fm *MaximumRisk/MarketInfo(Symbol(), MODE_MARGINREQUIRED),1);

  155.   if(lot<0.1) lot=0.1;
  156. if(lot>maxLots) lot=maxLots;
  157. // if(lot<lot_min) lot=lot_min;
  158. //   if(lot>lot_max) lot=lot_max;
  159. return(lot);
  160. }

  161. void CheckForCloseBuy()
  162. {
  163. //LotsOptimized();
  164. if (OrdersTotal( ) > 0 )      
  165. {
  166.   //下单前先检查持有的单
  167.   for(i=OrdersTotal()-1;i>=0;i--)
  168.   {
  169.     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
  170.     if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  171.   //   if (CurTime() < (OrderOpenTime()+ Period( )*gap2*60)) break ; //10倍当前时间间隔,才可去判断是否结束或更新
  172.   if(OrderType()==OP_BUY)
  173.   { //if (false == OrderClose(OrderTicket(),OrderLots(),Bid,3,White)) Sleep(5000);
  174.   OrderClose(OrderTicket(),OrderLots(),Bid,3,White); Sleep(5000);
  175.   }
  176.   }
  177. }
  178. }

  179. void CheckForCloseSel()
  180. {
  181. //LotsOptimized();
  182. if (OrdersTotal( ) > 0 )      
  183. {
  184.   //下单前先检查持有的单
  185.   for(i=OrdersTotal()-1;i>=0;i--)
  186.   {
  187.     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
  188.     if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  189.   // if (CurTime() < (OrderOpenTime()+ Period( )*gap2*60)) break ; //10倍当前时间间隔,才可去判断是否结束或更新
  190.     if(OrderType()==OP_SELL)
  191.     {
  192.     //if (false == OrderClose(OrderTicket(),OrderLots(),Ask,3,White))   Sleep(5000);
  193.     OrderClose(OrderTicket(),OrderLots(),Ask,3,White);   Sleep(5000);
  194.     }
  195.   }
  196. }
  197. }



  198. extern int period=81;//79;//76;//10;
  199. extern int price=5;//6; // 0 or other = (H+L)/2
  200.             // 1 = Open
  201.             // 2 = Close
  202.             // 3 = High
  203.             // 4 = Low
  204.             // 5 = (H+L+C)/3
  205.             // 6 = (O+C+H+L)/4
  206.             // 7 = (O+C)/2
  207. extern double lwei=0.69;// 0.67;//0.74;//0.67;
  208. extern int pos = 1;
  209. int ZigJuge()
  210. {
  211. double x,y;


  212. x= iCustom(NULL,0,"0meme",period,price,lwei,0,pos);
  213. y= iCustom(NULL,0,"0meme",period,price,lwei,1,pos);
  214.   if ((y != EMPTY_VALUE))//1上穿2
  215. {
  216. //Print ("last = ",iCustom(NULL,0,"1LineCro1EA",9,3,3,17,1,9,21,17,5,5,0), " x= ",x, " y= ",y);
  217.   return (-1);// setBuy(i);
  218.     }
  219. if ((x != EMPTY_VALUE))//1上穿2
  220. {
  221. //Print ("last = ",iCustom(NULL,0,"1LineCro1EA",9,3,3,17,1,9,21,17,5,5,0), " x= ",x, " y= ",y);
  222.   return (1);// setBuy(i);
  223.   }

  224. return (0);

  225. }
  226. int nowZigJuge = 0;
  227. void CheckForOpen()
  228. {
  229. if ((lasttime != NULL ) && ((Time[0] - lasttime) < Period( ) )) return;
  230. lasttime = Time[0];
  231. nowZigJuge = ZigJuge();
  232. if (nowZigJuge == 1) //买//上穿买 先结束卖的
  233.       CheckForCloseSel();
  234. if (nowZigJuge == -1) //卖//下穿卖 先结束买的
  235. CheckForCloseBuy();

  236. if (TimeDayOfWeek(CurTime()) == 1)
  237.   {
  238.   if (TimeHour(CurTime()) < 3 ) return;//周一早x8点前不做
  239.   }
  240.   if (TimeDayOfWeek(CurTime()) == 5)
  241.   {
  242.   if (TimeHour(CurTime()) > 19 ) return;//周五晚x11点后不做中国时间
  243.   }

  244. if (((Ask-Bid)/Point)> 5) return ; //如果市场剧烈动荡,不做

  245. if (OrdersTotal( ) >= maxOpen) return ;   //如果已持有开仓数达到,不做

  246.   TradeOK();

  247. if (OrdersTotal( ) >= maxOpen) return ;   //如果已持有开仓数达到,不做
  248. TradeOK();

  249. if (trdTime==2)
  250. {
  251. if (OrdersTotal( ) >= maxOpen) return ;   //如果已持有开仓数达到,不做
  252. TradeOK();

  253. if (OrdersTotal( ) >= maxOpen) return ;   //如果已持有开仓数达到,不做
  254. TradeOK();
  255. }

  256. }
  257. void TradeOK()
  258. {
  259. int error ;
  260. if (nowZigJuge == 1) //买//上穿买 先结束卖的
  261. // if (PriLiv() == 1) //买//上穿买 先结束卖的
  262.   {

  263.   // if (iOpen(NULL,PERIOD_H4,1)>iClose(NULL,PERIOD_H4,1)) return;
  264.     switch (whichmethod)
  265.     {
  266.     case 1:   res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
  267.     case 2:   res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,0,"",MAGICMA,0,Blue);   break;
  268.     case 3:   res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
  269.     case 4:   res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
  270.     default : res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
  271.     }
  272.     if (res <=0)
  273.     {
  274.     error=GetLastError();
  275.     if(error==134)Print("Received 134 Error after OrderSend() !! ");         // not enough money
  276.     if(error==135) RefreshRates();   // prices have changed
  277.     }
  278.     Sleep(5000);
  279.     return ;  
  280.   }

  281. if (nowZigJuge == -1) //卖//下穿卖 先结束买的
  282. // if (PriLiv() == -1) //买//上穿买 先结束卖的
  283.   {
  284. //   if (iOpen(NULL,PERIOD_H4,1)<iClose(NULL,PERIOD_H4,1)) return;
  285.     switch (whichmethod)
  286.     {
  287.     case 1:   res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
  288.     case 2:   res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,0,"",MAGICMA,0,Red);     break;
  289.     case 3:   res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
  290.     case 4:   res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
  291.     default : res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
  292.     }
  293.     if (res <=0)
  294.     {
  295.     error=GetLastError();
  296.     if(error==134) Print("Received 134 Error after OrderSend() !! ");         // not enough money
  297.     if(error==135) RefreshRates();   // prices have changed
  298.     }
  299.     Sleep(5000);
  300.     return ;  
  301.   }


  302. }

  303. void CTP()   //固定10点止赢
  304. {
  305. // Print("into CTP");
  306. bool bs = false;
  307. for (int i = 0; i < OrdersTotal(); i++)
  308. {
  309.   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
  310.   if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
  311.   //   if (CurTime() < (OrderOpenTime()+ Period( )*gap2*2*60)) continue ;
  312.   if (OrderType() == OP_BUY)
  313.   {
  314.     if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
  315.     {
  316.     if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
  317.     {

  318.       bs = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Green);
  319.       // Print("into CTP buy ", bs);
  320.     }
  321.     }
  322.   }
  323.   else if (OrderType() == OP_SELL)
  324.   {
  325.     if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
  326.     {
  327.     if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
  328.       (OrderStopLoss() == 0))
  329.     {   
  330.       bs = OrderModify(OrderTicket(), OrderOpenPrice(),
  331.         Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Tan);
  332.     //   Print("into CTP Sell", bs);
  333. }
  334.     }
  335.   }
  336. }
  337. }

  338. //| expert start function                             |
  339. //+------------------------------------------------------------------+
  340. int start()
  341. {
  342. CheckForOpen();
  343. if (bb==1)     CTP();   //bb ==1 跟踪
  344. return(0);
  345. }
  346. //+------------------------------------------------------------------+
复制代码

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

返回列表