{/'title/':/'根据以下MT5中的EA交易代码,如下://n////////+------------------------------------------------------------------//+//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; // magic number//nulong m_slippage=10; // slippage//n////////-//-//-//nint handle_iMACD; // variable for storing the handle of the iMACD indicator //n////////+------------------------------------------------------------------//+//n//////| Expert initialization function |//n////////+------------------------------------------------------------------//+//nint OnInit()//n {//n////////-//-//-//n if(!m_symbol.Name(Symbol())) // sets symbol name//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////////-//-//- create handle of the indicator iMACD//n handle_iMACD=iMACD(m_symbol.Name(),Period(),SignalPeriod+DF,SignalPeriod+DS+DF,SignalPeriod,PRICE_CLOSE);//n////////-//-//- if the handle is not created //n if(handle_iMACD==INVALID_HANDLE)//n {//n ////////-//-//- tell about the failure and output the error code //n PrintFormat(/'Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d/',//n m_symbol.Name(),//n EnumToString(Period()),//n GetLastError());//n ////////-//-//- the indicator is stopped early //n return(INIT_FAILED);//n }//n////////-//-//-//n return(INIT_SUCCEEDED);//n }//n////////+------------------------------------------------------------------//+//n//////| Expert deinitialization function |//n////////+------------------------------------------------------------------//+//nvoid OnDeinit(const int reason)//n {//n////////-//-//-//n//n }//n////////+------------------------------------------------------------------//+//n//////| Expert tick function |//n////////+------------------------------------------------------------------//+//nvoid OnTick()//n {//n////////-//-//- мы работаем только во время рождения нового бара//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)) // selects the position by index for further access to its properties//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 ////////-//-//- Открытая позиция на этом инструменте уже есть, вторую открывать не будем, //n ////////-//-//- а лучше завершим работу эксперта//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//////| Refreshes the symbol quotes data |//n////////+------------------------------------------------------------------//+//nbool RefreshRates(void)//n {//n////////-//-//- refresh rates//n if(!m_symbol.RefreshRates())//n {//n Print(/'RefreshRates error/');//n return(false);//n }//n////////-//-//- protection against the return value of /'zero/'//n if(m_symbol.Ask()==0 || m_symbol.Bid()==0)//n return(false);//n////////-//-//-//n return(true);//n }//n////////+------------------------------------------------------------------//+//n//////| Check the correctness of the order volume |//n////////+------------------------------------------------------------------//+//nbool CheckVolumeValue(double volume,string &error_description)//n {//n////////-//-//- minimal allowed volume for trade operations//n double min_volume=m_symbol.LotsMin();//n if(volume<min_volume)//n {//n error_description=StringFormat(/'Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f/',min_volume);//n return(false);//n }//n////////-//-//- maximal allowed volume of trade operations//n double max_volume=m_symbol.LotsMax();//n if(volume>max_volume)//n {//n error_description=StringFormat(/'Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f/',max_volume);//n return(false);//n }//n////////-//-//- get minimal step of volume changing//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(/'Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f/',//n volume_step,ratio*volume_step);//n return(false);//n }//n error_description=/'Correct volume value/';//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/',/'#property strict//n//ninput int Lots = 0.01; // 手数//ninput double StopLossPercentage = 0.02; // 止损百分比//ninput double TakeProfitPercentage = 0.03; // 止盈百分比//n//nint OnInit()//n{//n if (!SymbolInfoDouble(Symbol(), SYMBOL_MARGIN_INITIAL)) {//n Print(/'Symbol not found/');//n return INIT_FAILED;//n }//n //n return INIT_SUCCEEDED;//n}//n//nvoid OnDeinit(const int reason)//n{//n // 清理代码//n}//n//nvoid OnTick()//n{//n double diff = iMA(NULL, 12, 0, MODE_EMA, PRICE_CLOSE, 0) - iMA(NULL, 26, 0, MODE_EMA, PRICE_CLOSE, 0);//n double dea = iMA(NULL, 9, 0, MODE_EMA, diff, 0);//n double macd = 2 * (diff - dea / 2);//n //n double ah = iHighest(NULL, 0, MODE_HIGH, 13 * 1, 0);//n double al = iLowest(NULL, 0, MODE_LOW, 13 * 0.3, 0);//n //n double trendLine = iMA(NULL, 34, 0, MODE_EMA, macd, 0);//n //n double k = iMA(NULL, 2, 0, MODE_SMA, macd, 0);//n double d = iMA(NULL, 2, 0, MODE_SMA, k, 0);//n double j = iMA(NULL, 2, 0, MODE_SMA, d, 0);//n //n double previousMacd = iMACD(NULL, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);//n //n if (macd > 0 && previousMacd < macd) {//n // 开多头仓位//n double stopLoss = Ask - (Ask * StopLossPercentage);//n double takeProfit = Ask + (Ask * TakeProfitPercentage);//n int ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, stopLoss, takeProfit, /'MACD EA/', 0, 0, Green);//n //n if (ticket > 0) {//n Print(/'Buy order sent successfully: /', ticket);//n } else {//n Print(/'Failed to send buy order: /', GetLastError());//n }//n } else if (macd < 0 && previousMacd > macd) {//n // 开空头仓位//n double stopLoss = Bid + (Bid * StopLossPercentage);//n double takeProfit = Bid - (Bid * TakeProfitPercentage);//n int ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 0, stopLoss, takeProfit, /'MACD EA/', 0, 0, Red);//n //n if (ticket > 0) {//n Print(/'Sell order sent successfully: /', ticket);//n } else {//n Print(/'Failed to send sell order: /', GetLastError());//n }//n }//n}/

MT5 EA交易代码:MACDSimpleReshetov(barabashkakvn's edition) - 自动开仓和平仓

原文地址: https://www.cveoy.top/t/topic/p6p7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录