EA交易策略:MACDSimpleReshetov自动开仓平仓,盈利90%以上
"//+------------------------------------------------------------------+\n//| MACDSimpleReshetov(barabashkakvn's edition).mq5 |\n//| Copyright © 2006, Yury V. Reshetov |\n//| http://reshetov.xnet.uz/ |\n//+------------------------------------------------------------------+\n#property copyright "Copyright © 2006, Yury V. Reshetov ICQ: 282715499"\n#property link "http://reshetov.xnet.uz/"\n#property version "1.000"\n//---\n#include <Trade\PositionInfo.mqh>\n#include <Trade\Trade.mqh>\n#include <Trade\SymbolInfo.mqh> \nCPositionInfo m_position; // trade position object\nCTrade m_trade; // trading object\nCSymbolInfo m_symbol; // symbol info object\n//--- input parameters\ninput double Lots = 2; // 开仓手数\ninput uchar DF = 1; // 快速均线到信号线的增长幅度\ninput uchar DS = 2; // 慢速均线到快速均线的增长幅度\ninput uchar SignalPeriod= 10; // 信号线周期\ninput ulong m_magic=42828093; // 幻数\nulong m_slippage=10; // 滑点\n//---\nint handle_iMACD; // iMACD指标的句柄\n//+------------------------------------------------------------------+\n//| Expert初始化函数 |\n//+------------------------------------------------------------------+\nint OnInit()\n {\n//---\n if(!m_symbol.Name(Symbol())) // 设置交易品种名称\n return(INIT_FAILED);\n RefreshRates();\n\n string err_text="";\n if(!CheckVolumeValue(Lots,err_text))\n {\n Print(err_text);\n return(INIT_PARAMETERS_INCORRECT);\n }\n//---\n m_trade.SetExpertMagicNumber(m_magic);\n//---\n if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))\n m_trade.SetTypeFilling(ORDER_FILLING_FOK);\n else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))\n m_trade.SetTypeFilling(ORDER_FILLING_IOC);\n else\n m_trade.SetTypeFilling(ORDER_FILLING_RETURN);\n//---\n m_trade.SetDeviationInPoints(m_slippage);\n//--- 创建iMACD指标的句柄\n handle_iMACD=iMACD(m_symbol.Name(),Period(),SignalPeriod+DF,SignalPeriod+DS+DF,SignalPeriod,PRICE_CLOSE);\n//--- 如果句柄创建失败\n if(handle_iMACD==INVALID_HANDLE)\n {\n //--- 输出失败信息和错误代码\n PrintFormat("无法创建%s/%s的iMACD指标句柄,错误代码%d",\n m_symbol.Name(),\n EnumToString(Period()),\n GetLastError());\n //--- 提前停止指标\n return(INIT_FAILED);\n }\n//---\n return(INIT_SUCCEEDED);\n }\n//+------------------------------------------------------------------+\n//| Expert去初始化函数 |\n//+------------------------------------------------------------------+\nvoid OnDeinit(const int reason)\n {\n//---\n\n }\n//+------------------------------------------------------------------+\n//| Expert每个tick的函数 |\n//+------------------------------------------------------------------+\nvoid OnTick()\n {\n//--- 只在每个新的bar时工作\n static datetime PrevBars=0;\n datetime time_0=iTime(0);\n if(time_0==PrevBars)\n return;\n PrevBars=time_0;\n//--- MACD主线数值\n double main=iMACDGet(MAIN_LINE,0);\n//--- MACD信号线数值\n double signal=iMACDGet(SIGNAL_LINE,0);\n//--- 打开的仓位\n for(int i=PositionsTotal()-1;i>=0;i--)\n if(m_position.SelectByIndex(i)) // 选择指定索引的仓位以访问其属性\n if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)\n {\n if(m_position.PositionType()==POSITION_TYPE_BUY && main<0) // 如果我们买入,但信号是卖出\n {\n //--- 不行,离场\n m_trade.PositionClose(m_position.Ticket());\n return;\n }\n else if(m_position.PositionType()==POSITION_TYPE_SELL && main>0) // 如果我们卖出,但信号是买入\n {\n //--- 退还资金\n m_trade.PositionClose(m_position.Ticket());\n return;\n }\n //--- 在该工具上已经有一个开放的仓位,不再开启第二个仓位,最好停止Expert的工作\n return;\n }\n//--- 如果两条线都在同一方向,则进入市场\n if((mainsignal)>0)\n {\n //--- 如果是买入信号\n if(main>0)\n {\n //--- 买入\n if(main>signal)\n m_trade.Buy(Lots,m_symbol.Name());\n return;\n }\n else\n {\n //--- 卖出\n if(main<signal)\n m_trade.Sell(Lots,m_symbol.Name());\n return;\n }\n }\n }\n//+------------------------------------------------------------------+\n//| 刷新交易品种行情数据 |\n//+------------------------------------------------------------------+\nbool RefreshRates(void)\n {\n//--- 刷新行情\n if(!m_symbol.RefreshRates())\n {\n Print("刷新行情出错");\n return(false);\n }\n//--- 对返回值为“零”的情况进行保护\n if(m_symbol.Ask()==0 || m_symbol.Bid()==0)\n return(false);\n//---\n return(true);\n }\n//+------------------------------------------------------------------+\n//| 检查订单手数的正确性 |\n//+------------------------------------------------------------------+\nbool CheckVolumeValue(double volume,string &error_description)\n {\n//--- 交易操作的最小允许手数\n double min_volume=m_symbol.LotsMin();\n if(volume<min_volume)\n {\n error_description=StringFormat("手数小于最小允许值SYMBOL_VOLUME_MIN=%.2f",min_volume);\n return(false);\n }\n//--- 交易操作的最大允许手数\n double max_volume=m_symbol.LotsMax();\n if(volume>max_volume)\n {\n error_description=StringFormat("手数大于最大允许值SYMBOL_VOLUME_MAX=%.2f",max_volume);\n return(false);\n }\n//--- 获取手数变化的最小步长\n double volume_step=m_symbol.LotsStep();\n int ratio=(int)MathRound(volume/volume_step);\n if(MathAbs(ratiovolume_step-volume)>0.0000001)\n {\n error_description=StringFormat("手数不是最小步长SYMBOL_VOLUME_STEP=%.2f的倍数,最近的正确手数是%.2f",\n volume_step,ratio*volume_step);\n return(false);\n }\n error_description="正确的手数值";\n return(true);\n }\n//+------------------------------------------------------------------+ \n//| Checks if the specified filling mode is allowed | \n//+------------------------------------------------------------------+ \nbool IsFillingTypeAllowed(int fill_type)\n {\n//--- Obtain the value of the property that describes allowed filling modes \n int filling=m_symbol.TradeFillFlags();\n//--- Return true, if mode fill_type is allowed \n return((filling & fill_type)==fill_type);\n }\n//+------------------------------------------------------------------+ \n//| Get Time for specified bar index | \n//+------------------------------------------------------------------+ \ndatetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)\n {\n if(symbol==NULL)\n symbol=m_symbol.Name();\n if(timeframe==0)\n timeframe=Period();\n datetime Time[1];\n datetime time=0;\n int copied=CopyTime(symbol,timeframe,index,1,Time);\n if(copied>0)\n time=Time[0];\n return(time);\n }\n//+------------------------------------------------------------------+\n//| Get value of buffers for the iMACD |\n//| the buffer numbers are the following: |\n//| 0 - MAIN_LINE, 1 - SIGNAL_LINE |\n//+------------------------------------------------------------------+\ndouble iMACDGet(const int buffer,const int index)\n {\n double MACD[1];\n//--- reset error code \n ResetLastError();\n//--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index \n if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0)\n {\n //--- if the copying fails, tell the error code \n PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError());\n //--- quit with zero result - it means that the indicator is considered as not calculated \n return(0.0);\n }\n return(MACD[0]);\n }\n//+------------------------------------------------------------------+\n
原文地址: https://www.cveoy.top/t/topic/p6pV 著作权归作者所有。请勿转载和采集!