设为首页 收藏本站 切换语言

自制通道交易法

| 发表于 2022-11-25 09:35:42 | 显示全部楼层 |复制链接
关于止损点的问题,不建议超出几个固定点就止损。

  1. 价格到达上下轨位,下单的止损为前一高点,高点与轨道线价格差建议不超20点,要以盈亏比设定是否合算。如果超过前期高点,考虑反向开单,开仓时点为突破止损线后回采。
  2. //============原则
  3. 高胜算:
  4. 盈亏的概率(次数亏少盈多)
  5. 盈亏的空间(金额点数上亏少盈多)
  6. 盈亏的频率(时间)(避免频繁交易,适度)
  7. //===========实施方案
  8. 消息:基本面
  9. 无重大消息,按势走;
  10. 重大消息,按行情EA做;
  11. 势:200日、100日,50日MA,通道线、Chart以4小时及日线为准。1小时Chart进场准备。
  12. 态:实际上在势的关键位置,要考虑Bar的形态组合,如孕线,外包线等。指标的优劣,sdr,macd,rsi,kd
  13. 位:盈亏比,止损与盈利空间是否合适。
  14. //自制通道线买入卖出法.mq4
  15. //+-----------半自动,边测试完善---------------------+
  16. //|                        自制通道线买入卖出法.mq4 |
  17. //|                        Copyright 2016, ong. |
  18. //|                        https://www.ong.com |
  19. //+------------------------------------------------------------------+
  20. #property copyright "Copyright 2016, ong."
  21. #property link      "https://www.ong.com"
  22. #property version   "1.00"
  23. #property strict
  24. //----draw chanel and paste it's name--------
  25. //extern string ChanelnameA="ch_EURUSD";
  26. string ChanelnameA=NULL;
  27. extern string currency="通道线作用货币名";
  28. extern string Strade="紧急交易关闭";
  29. extern double 保平触发值=100;
  30. extern double 利润防守比例=0.7;
  31. extern double Lot=0.03;//下单量
  32. //---S1--if rsi_sell_1<45,can't sell
  33. extern double rsi_sell_1=50;
  34. extern double rsi_sell_2=70;
  35. extern double sto_sell_1=45;
  36. //---S2--if rsi_sell
  37. extern double rsi_sell_3=30;
  38. extern double sto_sell_2=40;
  39. //------------------------------------
  40. //---B1--if rsi_sell_1<45,can't buy
  41. extern double rsi_buy_1=30;
  42. extern double rsi_buy_2=50;
  43. extern double sto_buy_1=75;
  44. //---B2--if rsi_sellsto_sell_2,cann't buy;
  45. extern double rsi_buy_3=75;
  46. extern double sto_buy_2=90;
  47. //----------
  48. double rsi0=0;
  49. double sto0=0;
  50. //----------------------------------------------------------------------------------
  51. double arraya[4];
  52. int MagicBuyLimit=1101;
  53. int MagicSellLimit=1102;
  54. int glbOrderType=NULL;
  55. int glbOrderTicket=NULL;
  56. string glbOrderSymbol=NULL;
  57. double glbOrderOpenPrice=NULL;
  58. double glbOrderStopLoss=NULL;
  59. double glbOrderLots=NULL;
  60. double glbOrderTp=NULL;
  61. double glbOrderP=NULL;
  62. double glbOrderPoint=NULL;
  63. double p_sl=0;
  64. double glbOrderDigits=NULL;
  65. double glbMinlot=NULL;
  66. string PH="ph_"+StringSubstr(ChanelnameA,3,0);
  67. //每一次编写,并不刷新MT窗口中"已经运行的"EA参数设定值。只有在MT中退出EA,再运行"编写"EA,上部的外部参数才生效。
  68. //+------------------------------------------------------------------+
  69. //| Expert initialization function                                   |
  70. //+------------------------------------------------------------------+
  71. int OnInit()
  72.   {
  73.    return(INIT_SUCCEEDED);
  74.   }
  75. //+------------------------------------------------------------------+
  76. //| Expert deinitialization function                                 |
  77. //+------------------------------------------------------------------+
  78. void OnDeinit(const int reason)
  79.   {
  80. //---
  81.   }
  82. //+------------------------------------------------------------------+
  83. //| Expert tick function                                             |
  84. //+------------------------------------------------------------------+
  85. void OnTick()
  86.   {
  87. //----only effect to the order that it's magicnumber is null(无幻号订单,手动单的自动处理)--开始
  88.   if (OrderFind(NULL))
  89.   {
  90.       //---if the value of  "sl","tp" isn't set,then set that firstly!
  91.       if (glbOrderType==0||glbOrderType==2||glbOrderType==4)
  92.        {
  93.         if (glbOrderType==0)
  94.         {
  95.         double yldsb=(Bid-glbOrderOpenPrice);//盈利点数
  96.         //----
  97.          if (yldsb>=80*glbOrderPoint&&glbOrderStopLoss<(glbOrderOpenPrice+yldsb*glbOrderPoint*0.5))
  98.            {
  99.            double slb=NULL;
  100.            if (yldsb==80*glbOrderPoint)  slb=glbOrderOpenPrice;
  101.            if (yldsb>150*glbOrderPoint && yldsb<400*glbOrderPoint )  slb=glbOrderOpenPrice+yldsb*0.5;
  102.            if (yldsb>400*glbOrderPoint)  slb=glbOrderOpenPrice+yldsb*0.7;
  103.            if (OrderModify(glbOrderTicket,glbOrderOpenPrice,slb,glbOrderTp,0,clrRosyBrown)==FALSE)
  104.             Print("自选单错误号:",GetLastError());
  105.            }
  106.          }                  
  107.          if (glbOrderStopLoss==NULL )
  108.            {
  109.            if (OrderModify(glbOrderTicket,glbOrderOpenPrice,glbOrderOpenPrice-150*glbOrderPoint,glbOrderOpenPrice+MathAbs(arraya[2]),0,clrRosyBrown)==FALSE)
  110.            Print("自选单错误号:",GetLastError());        
  111.            }
  112.         }
  113.       if (glbOrderType==1||glbOrderType==3||glbOrderType==5)
  114.        {
  115.        if (glbOrderType==1)
  116.         {
  117.         double yldss=(glbOrderOpenPrice-Ask);//盈利点数
  118.        //----
  119.         if (yldss>=80*glbOrderPoint&&glbOrderStopLoss>(glbOrderOpenPrice-yldss*0.5))
  120.           {
  121.           double sls=NULL;
  122.           if (yldss==80*glbOrderPoint)  sls=glbOrderOpenPrice;
  123.           if (yldss>150*glbOrderPoint && yldss<400*glbOrderPoint )  sls=glbOrderOpenPrice+yldss*0.5;
  124.           if (yldss>400*glbOrderPoint)  sls=glbOrderOpenPrice+yldss*0.7;         
  125.           if (OrderModify(glbOrderTicket,glbOrderOpenPrice,sls,glbOrderTp,0,clrRosyBrown)==FALSE)
  126.            Print("自选单错误号:",GetLastError());
  127.           }
  128.         }
  129.         if (glbOrderStopLoss==NULL)//未设止损的,设定止损
  130.           {
  131.           if (OrderModify(glbOrderTicket,glbOrderOpenPrice,glbOrderOpenPrice+150*Point,glbOrderOpenPrice-MathAbs(arraya[2])*0.8,0,clrRosyBrown)==FALSE)
  132.            Print("自选单错误号:",GetLastError());        
  133.           }
  134.         }
  135.    }
  136. //-----------------手动单处理结束——————————————————————————————————————
  137. //---
  138. //-在当前"图表Chart"中找物件,找到返回窗口(window)号,主窗为0,其它1,2....否则负数--
  139. // if find the chanel line in the original "currncy" chart of draw the line .
  140. //"if" operator only effect to the next line, not all yet! if you want effcet to muti line,use the {} include it!
  141. if (Strade!="紧急交易关闭")
  142.   {
  143.    ;
  144.    PlaySound("alert.wav");
  145.   }
  146.   if (Lot>0.1)
  147.   {
  148.    ;
  149.    PlaySound("alert.wav");
  150.   }
  151. ChanelnameA="ch_"+Symbol();
  152. // Print("趋势线名称",ChanelnameA);
  153.    if (ObjectFind(0,ChanelnameA)==0 && Symbol()==StringSubstr(ChanelnameA,3,0)&& Lot<=0.1)
  154.    {
  155. //-----------------------------------------------------------
  156. //-----------------------------------------------------
  157.     string name=ChanelnameA;
  158.     //调动自定义通道函数
  159.     chanel(name);
  160.     double sl =arraya[0]+ 150*Point;//selllimit's sl
  161.     double tp =arraya[1]+ 150*Point;//selllimit's tp
  162.     Print("卖单时的sl::",sl);
  163.     double slb =arraya[1]- 150*Point;//buylimit's sl
  164.     double tpb =arraya[0]- 150*Point;//buylimit's tp
  165. rsi0=iRSI(NULL,0,14,PRICE_CLOSE,0);
  166. sto0=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0);
  167.    //Print("数组高值symbol----",Symbol(),":::",arraya[0]);
  168.    //Print("fffffffffffffff",glbOrderSymbol);
  169. //   Print("数组低值",arraya[1]);
  170. //   Print("数组差值",arraya[2]);
  171.     //%%%%-------order not exist------------------------------------------------------------
  172.     //%%%%-------order not exist------------------------------------------------------------   
  173.     // if order's symbol = the chart's symbol and magicnumber not exist
  174.     //---selllimit no exist------------
  175.      if (OrderFind( MagicSellLimit)==false &&arraya[3]<1)
  176.        {
  177.        //-----sell紧急模式,such as "sell_EURUSD"
  178.        //Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
  179.         if (StringSubstr(Strade,0,4)=="sell" && StringSubstr(Strade,5,0)==Symbol())
  180.           {
  181.            if (OrderSend(Symbol(),OP_SELL,Lot,Ask,3,sl, tp,"",MagicSellLimit,0,Red)==false)
  182.                  Print("err(",GetLastError(),")");        
  183.                   Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
  184.           }
  185.        //-----sell正常模式-----
  186.          if (((rsi0>rsi_sell_1&&rsi0
  187.            {
  188.             Print("可执行卖","sto0的值",sto0);
  189.             if ((arraya[0]-Ask)Ask && Strade=="紧急交易关闭" )
  190.               {
  191.                 if (OrderSend(Symbol(),OP_SELLLIMIT,Lot,arraya[0]-Point*50,3,sl, tp,"",MagicSellLimit,0,Red)==false)
  192.                  Print("err(",GetLastError(),")");
  193.               }
  194.            }
  195.        }
  196.     // ----buylimit no exist ----
  197.      if (OrderFind( MagicBuyLimit)==false && arraya[3]<1)
  198.        {
  199.        //------buy(bull)-紧急模式---such as bull_EURUSD----
  200.                if (StringSubstr(Strade,0,4)=="bull" && StringSubstr(Strade,5,0)==Symbol())
  201.           {
  202.            if (OrderSend(Symbol(),OP_BUY,Lot,Bid,3,slb, tpb,"",MagicBuyLimit,0,Red)==false)
  203.                Print("err(",GetLastError(),")");        
  204.                Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
  205.           }
  206.        //------buy(bull) 正常模式-------
  207.         if (((rsi0>rsi_buy_1&&rsi0sto_buy_1)||rsi0>rsi_buy_3 || sto0>sto_buy_2)==false)
  208.          {
  209.            Print("可执行买","sto0的值",sto0);
  210.          if ((Ask-arraya[1])arraya[1]&& Strade=="紧急交易关闭" )
  211.            {
  212.            if (OrderSend(Symbol(),OP_BUYLIMIT,Lot,arraya[1]+Point*50,3,slb, tpb,"",MagicBuyLimit,0,Red)==false)
  213.              Print("err(",GetLastError(),")");
  214.            }
  215.          }
  216.        }
  217.     //%%%%-------order not exist------over---------------------------------------------
  218.    //@@@@@@-----selllimit--order exist------------------------------------------------------------
  219.    if (OrderFind( MagicSellLimit)==true && glbOrderSymbol==Symbol())
  220.    {
  221.      //-1-modify the trading order---when profit is higher 200 points than openprice,设定盈利25点保平
  222.      if (glbOrderType==1)
  223.         {
  224.         Print("dddddddddddddddddddd",glbOrderStopLoss);
  225.         double sell_ask=glbOrderOpenPrice-Ask;//即时sell单盈利点数
  226.         double sell_slj=glbOrderOpenPrice-sell_ask*利润防守比例;//sell单阶段性步步盈利,追踪止损值
  227.         if (sell_ask>保平触发值*Point && sell_ask<200*Point)
  228.            {
  229.            //tp upto 150 point,保平
  230.            double s=glbOrderOpenPrice-50*Point;
  231.            Print("ssssssssss",s);
  232.            if(OrderModify(glbOrderTicket, glbOrderOpenPrice,(glbOrderOpenPrice-50*Point), tp, 0, Green) == false)
  233.               Print("Err (", GetLastError(), ")");
  234.            }
  235.         if ( sell_ask>200*Point&& glbOrderStopLoss> sell_slj  )
  236.            {
  237.            Print("jjjjjjjjjjjj",sell_slj);
  238.            if(OrderModify(glbOrderTicket,glbOrderOpenPrice,sell_slj, tp, 0, Green) == false)
  239.               Print("Err (", GetLastError(), ")");
  240.            }      
  241.          //Print("有魔号的订单号",glbOrderTicket);
  242.         // Print("订单币种",OrderSymbol());
  243.         }
  244.      //-2-modify the pending sell order---
  245.      //-when the price of ask or bid  is bigger or sammer then openprice(200 point)
  246.      if (glbOrderType==3)
  247.         {
  248.         if (arraya[0]-Ask>200*Point || Ask-arraya[0]>0)
  249.            {      
  250.            if (OrderDelete(glbOrderTicket)==false)
  251.            Print("ERR(",GetLastError(),")");
  252.            }
  253.         if ( OrderModify(glbOrderTicket, arraya[0]-50*Point, sl, tp, 0, Green) == false)
  254.             Print("Err (", GetLastError(), ")");
  255.       //   Print("OPEN_TYPE:",glbOrderType);
  256.         }
  257.     }
  258.    //@@@@@@@@@-------selllimit----order exist----over-----
  259.     //######-----buylimit--order exist------------------
  260.    if (OrderFind( MagicBuyLimit)==true && glbOrderSymbol==Symbol())
  261.    {
  262.      //-1-modify the trading order---when profit is higher 200 points than openprice,设定盈利25点保平
  263.      if (glbOrderType==0)
  264.         {
  265.         double buy_bid=Bid-glbOrderOpenPrice;//即时buy单盈利点数
  266.         Print("即时buy单Bid数",Bid,"即时buy单开仓价",glbOrderOpenPrice);
  267.         Print("即时buy单盈利点数",buy_bid);
  268.         double buy_slj=glbOrderOpenPrice+buy_bid*利润防守比例;//buy单阶段性步步为赢,追踪止损值      
  269.          if (buy_bid>保平触发值*Point && buy_bid<200*Point)
  270.           {
  271.            if(OrderModify(glbOrderTicket, glbOrderOpenPrice,(glbOrderOpenPrice+50*Point), tpb, 0, Green) == false)
  272.               Print("Err (", GetLastError(), ")kkkkkkkkk");
  273.           }
  274.          if (buy_bid>200*Point&& buy_slj>glbOrderStopLoss)
  275.           {
  276.            if(OrderModify(glbOrderTicket, glbOrderOpenPrice, buy_slj, tpb, 0, Green) == false)
  277.               Print("Err (aaa", GetLastError(), ")");
  278.           }   
  279.         }
  280.      //-2-modify the pending buy order---
  281.      //-when the price of ask or bid  is bigger or sammer then openprice(200 point)
  282.      if (glbOrderType==2&& glbOrderSymbol==Symbol())
  283.         {
  284.         Print("挂买单时BID值",Bid);
  285.         if (Bid-arraya[1]>200*Point||Bid
  286.            {      
  287.            if (OrderDelete(glbOrderTicket)==false)
  288.            Print("ERRbbb(",GetLastError(),")");
  289.            }
  290.         if ( OrderModify(glbOrderTicket, arraya[1]+50*Point, slb, tpb, 0, Green) == false)
  291.             Print("Errccc (", GetLastError(),")","买单时的止损", slb,"买单时的止盈",tpb);            
  292.       //   Print("OPEN_TYPE:",glbOrderType);
  293.         }
  294.     }
  295.    //#######--------buylimit---order exist----over---------------
  296. }
  297. //---
  298.   }
  299. //+----------------------------------------------------------------------+
  300. //+------------自定义函数chanel()-----start---------------------+
  301. //|求取通道高低值
  302. //|create a funtion the chanel angle is up or down
  303. //|when the price of first point is bigger than second one,the chanel line is up!
  304. //|暂定两个点的为主线,对线为次线。MT4定义主线上左边点为点1,右边点为点2,对应的次线上的点为点3.
  305. //|取得点3时间对应的主线价格,可知两线差。
  306. //|直接取得主线即时价格,可知次线即时价。
  307. void chanel( string name)
  308. {
  309. ObjectFind(0,name);
  310. double pa=ObjectGet(name,OBJPROP_PRICE3);
  311. //如果不加(datetime),就会出现possible loss of data due to type conversion提示,说见博客
  312. double pb=ObjectGetValueByTime(0,name, (datetime)ObjectGet(name,OBJPROP_TIME3),0);
  313. double pd=NormalizeDouble(pa-pb,Digits);
  314. //pm is the now price of main line,ps is the now price of secondary line
  315. double pm=ObjectGetValueByShift(name,0);
  316. double ps=pm+pd;
  317. //if pd>0 ,次线价高
  318.    double phcn=0;
  319.    double plcn=0;
  320.   if (pd>0)
  321.    {
  322.    phcn=NormalizeDouble(ps,Digits);
  323.    plcn=NormalizeDouble(pm,Digits);
  324.    }
  325.   else
  326.    {
  327.    phcn=NormalizeDouble(pm,Digits);
  328.    plcn=NormalizeDouble(ps,Digits);
  329.    }
  330. ArrayInitialize(arraya,NULL);
  331. arraya[0]=phcn;
  332. arraya[1]=plcn;
  333. arraya[2]=pd;
  334. //-------------price out chanel----------
  335. //datetime some_time=D'2016.05.13 09:00';
  336. datetime some_time=(datetime)ObjectGet(name,OBJPROP_TIME1);
  337. int bars_ct1=iBarShift(Symbol(),PERIOD_CURRENT,some_time);//chanel's TIME point shift number
  338. int i=NULL;
  339. for ( i=0;i
  340.   {
  341.   Comment("bar's number ",bars_ct1,Symbol()," 未突破! ","突破的位置:---");
  342.    double chi=ObjectGetValueByShift(name,i);
  343.    double cli=chi+pd;
  344.    arraya[3]=0;
  345.   if (pd>0)
  346.    {
  347.     cli=ObjectGetValueByShift(name,i);
  348.     chi=cli+pd;
  349.    }
  350.      if (Close>chi||Close
  351.      {
  352.           arraya[3]=1;//有突破通道现象
  353.       Comment("bar's number ",bars_ct1,Symbol()," 突破!!!! ","突破的位置:::",i);
  354.       Print(Symbol(),"有突破通道现象",i);
  355.       break;
  356.       //comment\print:comment区别
  357.       // comment保持最后的状态不变。如上面最后i值为21,如果后面没有条件触发,comment会在主图一直显示21.所以在上面加了一行comment
  358.       //而print,如果没有条件触发,则不再输出任何内容。
  359.      }
  360.    }
  361. //Comment("ww11ww",High[29],High[2]);
  362. return;
  363. }
  364. //+------------------chanel()---over---------------------------------+
  365. //+------------------自定义orderfind()---start-----------------------+
  366. //|find the Order of EA auto place   exist or no?
  367. //|
  368. //|                                                                  |
  369. //+------------------------------------------------------------------+
  370. bool OrderFind(int Magic)
  371.   {
  372.    glbOrderType = -1;
  373.    glbOrderTicket = -1;
  374.    int total = OrdersTotal();
  375.    bool res = false;
  376.    for(int cnt = 0 ; cnt < total ; cnt++)
  377.      {
  378.        if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==false)
  379.        Print("ERR(",GetLastError(),")");
  380.        if(OrderMagicNumber() == Magic && OrderSymbol() == Symbol()&& Symbol()==StringSubstr(ChanelnameA,3,0))
  381.          {
  382.            glbOrderType = OrderType();
  383.            glbOrderSymbol=OrderSymbol();
  384.            glbOrderTicket = OrderTicket();
  385.            glbOrderOpenPrice=OrderOpenPrice();
  386.            glbOrderStopLoss=OrderStopLoss();
  387.            glbOrderLots=OrderLots();
  388.            glbOrderTp=OrderTakeProfit();
  389.            glbOrderP=OrderProfit();
  390.            glbOrderPoint=Point;
  391.            glbOrderDigits=Digits;
  392.            glbMinlot  = MarketInfo(OrderSymbol(),MODE_MINLOT);
  393.            Print(Symbol(),"ggggggggggggggggg",glbMinlot,"点值",glbOrderPoint);
  394.            res = true;
  395.          }
  396.      }
  397.    return(res);
  398.   }
  399. //------------------orderfind()----over-------------------------------+
  400. //+------------自定义sl_line()-----start-------------这部分没写完现在无效--------+
  401. //|sl is stopline,find object of "sl_bl","sl_sl"
  402. //|line value as the value of sl
  403. bool sl_line( string linename)
  404. {
  405. bool res=false;
  406.   if (ObjectFind(0,linename)==0 && Symbol()==StringSubstr(linename,5,0))
  407.     {
  408.      p_sl=NormalizeDouble(ObjectGetDouble(0,linename,OBJPROP_PRICE,0),Digits);
  409.      res=true;
  410.      Print("找到了!值:",p_sl);
  411.     }
  412.    else    Print("ERR(",GetLastError(),")没有损线");
  413. return(res);
  414.    }
  415. //+----------------sl_line()---over---------------------------------+
复制代码
如果有帮助,就支持一下我呗
举报

评论 使用道具

精彩评论8

shao
DDD
| 发表于 2022-11-25 09:36:55 | 显示全部楼层
啥样通道
举报

点赞 评论 使用道具

daerwushen
DD
| 发表于 2022-11-25 09:42:15 | 显示全部楼层
效果图有吗
举报

点赞 评论 使用道具

mic
DD
| 发表于 2022-11-25 20:41:52 | 显示全部楼层
有实例说明吗
举报

点赞 评论 使用道具

dongxu64
DDD
| 发表于 2022-11-28 11:15:15 | 显示全部楼层
好好学习,谢谢楼主。补张图更好
举报

点赞 评论 使用道具

qwe11
CCC
| 发表于 2022-11-28 15:22:15 | 显示全部楼层
补张图更好
举报

点赞 评论 使用道具

日拱一卒
DD
| 发表于 2022-11-29 15:11:00 | 显示全部楼层
谢谢楼主。补张图更好
举报

点赞 评论 使用道具

zhy2505000
DD
| 发表于 2022-11-30 11:03:54 | 显示全部楼层
学习了.有创新.
举报

点赞 评论 使用道具

kenlee1628
DDD
| 发表于 2022-12-2 08:21:45 | 显示全部楼层
无私分享
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)