登陆/注册 搜索

USERCENTER

SEARCHSITE

搜索

查看: 2537

[MT4] 神经网络EA---MT5

[复制链接]

79

主题

320

回帖

1504

活跃度

回帖达人实盘认证实名认证发贴达人本站牛人

程序猿的仔仔实盘认证 发表于 2018-3-30 09:23:18 | 显示全部楼层 | 关注 | 私信
以下的源码:
  1. //+------------------------------------------------------------------+
  2. //|                                             macdNeuROExample.mq5 |
  3. //|程序猿的仔仔
  4. //|                                           http://www.24krmb.com/?1922 |
  5. //+------------------------------------------------------------------+
  6. #property copyright "wangyu204"
  7. #property link      "http://www.fxunion.com"
  8. #property version   "1.00"
  9. //本EA通过模拟过去训练最佳权重w数据,操作未来
  10. #incLUde
  11. #include
  12. //神经网络权重  每个因子一个权值
  13. input double w0=0.5;
  14. input double w1=0.5;
  15. input double w2=0.5;
  16. input double w3=0.5;
  17. input double w4=0.5;
  18. input double w5=0.5;
  19. input double w6=0.5;
  20. input double w7=0.5;
  21. input double w8=0.5;
  22. input double w9=0.5;
  23. input double w10=0.5;
  24. input double w11=0.5;
  25. input double w12=0.5;
  26. input double w13=0.5;
  27. input double w14=0.5;
  28. input double w15=0.5;
  29. input double w16=0.5;
  30. input double w17=0.5;
  31. input double w18=0.5;
  32. input double w19=0.5;
  33. //全局变量
  34. int iMACD_handle;//macd indicator handle
  35. double iMACD_mainbuf[];//dynamic array for storing indicator values
  36. double iMACD_signalbuf[];//dynamic array for storing indicator values
  37. double inputs[20];//输入因子
  38. double weight[20];//对应输入权重
  39. string my_symbol;
  40. ENUM_TIMEFRAMES my_timeframe;
  41. double lot_size;
  42. //输出
  43. double out;
  44. CTrade m_Trade; //交易相关
  45. CPOsitionInfo m_Position;//持仓相关
  46. //+------------------------------------------------------------------+
  47. //| Expert initialization FUnction                                   |
  48. //+------------------------------------------------------------------+
  49. int OnInit()
  50.   {
  51. //---
  52.    my_symbol=Symbol();
  53.    my_timeframe=PERIOD_CURRENT;
  54.    lot_size=SymbolInfoDouble(my_symbol,SYMBOL_VOLUME_MIN);
  55.    iMACD_handle=iMACD(my_symbol,my_timeframe,12,26,9,PRICE_CLOSE);
  56.    if(iMACD_handle==INVALID_HANDLE)
  57.      {
  58.       Print("Failed to get the indicator handle");
  59.       return(-1);
  60.      }
  61.   
  62.    ChartIndicatorAdd(ChartID(),0,iMACD_handle);
  63.    ArraySetAsSeries(iMACD_mainbuf,true);//设置数组序列
  64.    ArraySetAsSeries(iMACD_signalbuf,true);
  65.   
  66.    //place weights into the array
  67.    weight[0]=w0;
  68.    weight[1]=w1;
  69.    weight[2]=w2;
  70.    weight[3]=w3;
  71.    weight[4]=w4;
  72.    weight[5]=w5;
  73.    weight[6]=w6;
  74.    weight[7]=w7;
  75.    weight[8]=w8;
  76.    weight[9]=w9;
  77.    weight[10]=w10;
  78.    weight[11]=w11;
  79.    weight[12]=w12;
  80.    weight[13]=w13;
  81.    weight[14]=w14;
  82.    weight[15]=w15;
  83.    weight[16]=w16;
  84.    weight[17]=w17;
  85.    weight[18]=w18;
  86.    weight[19]=w19;
  87.   
  88.    return(0);
  89.   }
  90. //+------------------------------------------------------------------+
  91. //| Expert deinitialization function                                 |
  92. //+------------------------------------------------------------------+
  93. void OnDeinit(const int reason)
  94.   {
  95. //---
  96.    IndicatorRelease(iMACD_handle);//delete the indicator handle and deallocate the memory space
  97.    ArrayFree(iMACD_mainbuf);//free dynamic array
  98.    ArrayFree(iMACD_signalbuf);
  99.   }
  100. //+------------------------------------------------------------------+
  101. //| Expert tick function                                             |
  102. //+------------------------------------------------------------------+
  103. void OnTick()
  104.   {
  105. //---
  106.    int err1=0,err2=0;
  107.    err1=CopyBuffer(iMACD_handle,0,2,ArraySize(inputs)/2,iMACD_mainbuf);
  108.    err2=CopyBuffer(iMACD_handle,0,2,ArraySize(inputs)/2,iMACD_signalbuf);
  109.    if(err1<0 || err2<0)
  110.      {
  111.       Print("Failed to copy data from the indicator buffer");
  112.       return;
  113.      }
  114.    //input标准化
  115.    double d1=-1.0;
  116.    double d2=1.0;
  117.    double x_min=MathMin(iMACD_mainbuf[ArrayMinimum(iMACD_mainbuf)],iMACD_signalbuf[ArrayMinimum(iMACD_signalbuf)]);
  118.    double x_max=MathMax(iMACD_mainbuf[ArrayMaximum(iMACD_mainbuf)],iMACD_signalbuf[ArrayMaximum(iMACD_signalbuf)]);
  119.    for(int i=0;i
  120.      {
  121.       inputs[i*2]=(iMACD_mainbuf[i]-x_min)*(d2-d1)/(x_max-x_min)+d1;
  122.       inputs[i*2+1]=(iMACD_signalbuf[i]-x_min)*(d2-d1)/(x_max-x_min)+d1;
  123.      }
  124.    //out
  125.    out=calculateNeuron(inputs,weight);
  126.   
  127.    if(out<0)
  128.      {
  129.       if(m_Position.Select(my_symbol))
  130.         {
  131.          if(m_Position.PositionType()==POSITION_TYPE_SELL){m_Trade.PositionClose(my_symbol);}
  132.          if(m_Position.PositionType()==POSITION_TYPE_BUY){return;}
  133.         }
  134.       m_Trade.Buy(lot_size,my_symbol);
  135.      }
  136.    if(out>=0)
  137.      {
  138.       if(m_Position.Select(my_symbol))
  139.         {
  140.          if(m_Position.PositionType()==POSITION_TYPE_BUY){m_Trade.PositionClose(my_symbol);}
  141.          if(m_Position.PositionType()==POSITION_TYPE_SELL){return;}
  142.         }
  143.       m_Trade.Sell(lot_size,my_symbol);
  144.      }
  145.   }
  146. //+------------------------------------------------------------------+
  147. double calculateNeuron(double &x[],double &w[])
  148. {
  149.    double NET=0.0;
  150.    for(int i=0;i
  151.      {
  152.       NET+=x[i]*w[i];
  153.      }
  154.    NET*=0.1;//additional coefficient d=0.1
  155.    return(activateNeuron(NET));
  156. }
  157. //激励函数
  158. double activateNeuron(double x)
  159. {
  160.    double out;
  161.    out=(exp(x)-exp(-x))/(exp(x)+exp(-x));
  162.    return(out);
  163. }
复制代码


0

主题

61

回帖

12

活跃度
易指禅 发表于 2020-8-31 12:21:05 | 显示全部楼层 | 关注 | 私信
非常感谢楼主的无私分享

10

主题

537

回帖

6489

活跃度

回帖达人实盘认证实名认证

youbin9实盘认证 发表于 2022-9-24 18:30:45 | 显示全部楼层 | 关注 | 私信

挖矿学习,感谢分享

0

主题

906

回帖

2210

活跃度

回帖达人实盘认证实名认证

铅华浮尘实盘认证 发表于 2022-10-23 09:05:52 | 显示全部楼层 | 关注 | 私信
谢谢分享,好好学习量化
温馨提示
无充值,无付费,唯一微信公众号24KRMB,珍惜账号,理性讨论 知道啦

评论管理|实名认证|黑名单|手机版|倡议书|版权声明|24KRMB ( 鄂ICP备19016902号 )

GMT+8, 2024-10-18 21:19 , Processed in 1.074622 second(s), 38 queries , Gzip On.

Copyright © 2012-2024 24KRMB.COM

Powered by 阿里云提供驱动 UI: 240701

快速回复 返回列表