|
以下的源码:- //+------------------------------------------------------------------+
- //| macdNeuROExample.mq5 |
- //|程序猿的仔仔
- //| http://www.24krmb.com/?1922 |
- //+------------------------------------------------------------------+
- #property copyright "wangyu204"
- #property link "http://www.fxunion.com"
- #property version "1.00"
- //本EA通过模拟过去训练最佳权重w数据,操作未来
- #incLUde
- #include
- //神经网络权重 每个因子一个权值
- input double w0=0.5;
- input double w1=0.5;
- input double w2=0.5;
- input double w3=0.5;
- input double w4=0.5;
- input double w5=0.5;
- input double w6=0.5;
- input double w7=0.5;
- input double w8=0.5;
- input double w9=0.5;
- input double w10=0.5;
- input double w11=0.5;
- input double w12=0.5;
- input double w13=0.5;
- input double w14=0.5;
- input double w15=0.5;
- input double w16=0.5;
- input double w17=0.5;
- input double w18=0.5;
- input double w19=0.5;
- //全局变量
- int iMACD_handle;//macd indicator handle
- double iMACD_mainbuf[];//dynamic array for storing indicator values
- double iMACD_signalbuf[];//dynamic array for storing indicator values
- double inputs[20];//输入因子
- double weight[20];//对应输入权重
- string my_symbol;
- ENUM_TIMEFRAMES my_timeframe;
- double lot_size;
- //输出
- double out;
- CTrade m_Trade; //交易相关
- CPOsitionInfo m_Position;//持仓相关
- //+------------------------------------------------------------------+
- //| Expert initialization FUnction |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //---
- my_symbol=Symbol();
- my_timeframe=PERIOD_CURRENT;
- lot_size=SymbolInfoDouble(my_symbol,SYMBOL_VOLUME_MIN);
- iMACD_handle=iMACD(my_symbol,my_timeframe,12,26,9,PRICE_CLOSE);
- if(iMACD_handle==INVALID_HANDLE)
- {
- Print("Failed to get the indicator handle");
- return(-1);
- }
-
- ChartIndicatorAdd(ChartID(),0,iMACD_handle);
- ArraySetAsSeries(iMACD_mainbuf,true);//设置数组序列
- ArraySetAsSeries(iMACD_signalbuf,true);
-
- //place weights into the array
- weight[0]=w0;
- weight[1]=w1;
- weight[2]=w2;
- weight[3]=w3;
- weight[4]=w4;
- weight[5]=w5;
- weight[6]=w6;
- weight[7]=w7;
- weight[8]=w8;
- weight[9]=w9;
- weight[10]=w10;
- weight[11]=w11;
- weight[12]=w12;
- weight[13]=w13;
- weight[14]=w14;
- weight[15]=w15;
- weight[16]=w16;
- weight[17]=w17;
- weight[18]=w18;
- weight[19]=w19;
-
- return(0);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //---
- IndicatorRelease(iMACD_handle);//delete the indicator handle and deallocate the memory space
- ArrayFree(iMACD_mainbuf);//free dynamic array
- ArrayFree(iMACD_signalbuf);
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- int err1=0,err2=0;
- err1=CopyBuffer(iMACD_handle,0,2,ArraySize(inputs)/2,iMACD_mainbuf);
- err2=CopyBuffer(iMACD_handle,0,2,ArraySize(inputs)/2,iMACD_signalbuf);
- if(err1<0 || err2<0)
- {
- Print("Failed to copy data from the indicator buffer");
- return;
- }
- //input标准化
- double d1=-1.0;
- double d2=1.0;
- double x_min=MathMin(iMACD_mainbuf[ArrayMinimum(iMACD_mainbuf)],iMACD_signalbuf[ArrayMinimum(iMACD_signalbuf)]);
- double x_max=MathMax(iMACD_mainbuf[ArrayMaximum(iMACD_mainbuf)],iMACD_signalbuf[ArrayMaximum(iMACD_signalbuf)]);
- for(int i=0;i
- {
- inputs[i*2]=(iMACD_mainbuf[i]-x_min)*(d2-d1)/(x_max-x_min)+d1;
- inputs[i*2+1]=(iMACD_signalbuf[i]-x_min)*(d2-d1)/(x_max-x_min)+d1;
- }
- //out
- out=calculateNeuron(inputs,weight);
-
- if(out<0)
- {
- if(m_Position.Select(my_symbol))
- {
- if(m_Position.PositionType()==POSITION_TYPE_SELL){m_Trade.PositionClose(my_symbol);}
- if(m_Position.PositionType()==POSITION_TYPE_BUY){return;}
- }
- m_Trade.Buy(lot_size,my_symbol);
- }
- if(out>=0)
- {
- if(m_Position.Select(my_symbol))
- {
- if(m_Position.PositionType()==POSITION_TYPE_BUY){m_Trade.PositionClose(my_symbol);}
- if(m_Position.PositionType()==POSITION_TYPE_SELL){return;}
- }
- m_Trade.Sell(lot_size,my_symbol);
- }
- }
- //+------------------------------------------------------------------+
- double calculateNeuron(double &x[],double &w[])
- {
- double NET=0.0;
- for(int i=0;i
- {
- NET+=x[i]*w[i];
- }
- NET*=0.1;//additional coefficient d=0.1
- return(activateNeuron(NET));
- }
- //激励函数
- double activateNeuron(double x)
- {
- double out;
- out=(exp(x)-exp(-x))/(exp(x)+exp(-x));
- return(out);
- }
复制代码
|
|