post the code with your edits -- explain what edits you made -- let's see.
If you are interested in doing it yourself, there is a chance of figuring it out.
#property copyright"Copyright 2009-2013, MetaQuotes Software Corp."#property link"http://www.mql5.com"#property version"5.20"#property description"It is important to make sure that the expert works with a normal"#property description"chart and the user did not make any mistakes setting input"#property description"variables (Lots, TakeProfit, TrailingStop) in our case,"#property description"we check TakeProfit on a chart of more than 2*trend_period bars"//---#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
//---inputdouble InpLots =0.1; // Lots.inputint InpTakeProfit =50; // Take Profit (in pips)inputint InpTrailingStop =30; // Trailing Stop Level (in pips)inputint InpMACDOpenLevel =3; // MACD open level (in pips)inputint InpMACDCloseLevel=2; // MACD close level (in pips)inputint InpMATrendPeriod =26; // MA trend period//---int ExtTimeOut=10; // time out in seconds between trade operations//+------------------------------------------------------------------+//| MACD Sample expert class|//+------------------------------------------------------------------+class CSampleExpert
{
protected:
double m_adjusted_point; // point value adjusted for 3 or 5 points
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CPositionInfo m_position; // trade position object
CAccountInfo m_account; // account info wrapper//--- indicatorsint m_handle_macd; // MACD indicator handle//--- indicator buffersdouble m_buff_MACD_main[]; // MACD indicator main buffer//--- indicator data for processingdouble m_macd_current;
double m_macd_previous;
//---double m_macd_open_level;
double m_macd_close_level;
double m_traling_stop;
double m_take_profit;
public:
CSampleExpert(void);
~CSampleExpert(void);
bool Init(void);
void Deinit(void);
bool Processing(void);
protected:
bool InitCheckParameters(constint digits_adjust);
bool InitIndicators(void);
bool LongClosed(void);
bool ShortClosed(void);
bool LongModified(void);
bool ShortModified(void);
bool LongOpened(void);
bool ShortOpened(void);
};
//--- global expert
CSampleExpert ExtExpert;
//+------------------------------------------------------------------+//| Constructor|//+------------------------------------------------------------------+
CSampleExpert::CSampleExpert(void) : m_adjusted_point(0),
m_handle_macd(INVALID_HANDLE),
m_macd_current(0),
m_macd_previous(0),
m_macd_open_level(0),
m_macd_close_level(0),
m_traling_stop(0),
m_take_profit(0)
{
ArraySetAsSeries(m_buff_MACD_main,true);
}
//+------------------------------------------------------------------+//| Destructor|//+------------------------------------------------------------------+
CSampleExpert::~CSampleExpert(void)
{
}
//+------------------------------------------------------------------+//| Initialisation and checking for input parameters |//+------------------------------------------------------------------+bool CSampleExpert::Init(void)
{
//--- initialize common information
m_symbol.Name(Symbol()); // symbol
m_trade.SetExpertMagicNumber(12345); // magic//--- tuning for 3 or 5 digitsint digits_adjust=1;
if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
digits_adjust=10;
m_adjusted_point=m_symbol.Point()*digits_adjust;
//--- set default deviation for trading in adjusted points
m_macd_open_level =InpMACDOpenLevel*m_adjusted_point;
m_macd_close_level=InpMACDCloseLevel*m_adjusted_point;
m_traling_stop =InpTrailingStop*m_adjusted_point;
m_take_profit =InpTakeProfit*m_adjusted_point;
//--- set default deviation for trading in adjusted points
m_trade.SetDeviationInPoints(3*digits_adjust);
//---if(!InitCheckParameters(digits_adjust))
return(false);
if(!InitIndicators())
return(false);
//--- succeedreturn(true);
}
//+------------------------------------------------------------------+//| Checking for input parameters|//+------------------------------------------------------------------+bool CSampleExpert::InitCheckParameters(constint digits_adjust)
{
//--- initial data checksif(InpTakeProfit*digits_adjust<m_symbol.StopsLevel())
{
printf("Take Profit must be greater than %d",m_symbol.StopsLevel());
return(false);
}
if(InpTrailingStop*digits_adjust<m_symbol.StopsLevel())
{
printf("Trailing Stop must be greater than %d",m_symbol.StopsLevel());
return(false);
}
//--- check for right lots amountif(InpLots<m_symbol.LotsMin() || InpLots>m_symbol.LotsMax())
{
printf("Lots amount must be in the range from %f to %f",m_symbol.LotsMin(),m_symbol.LotsMax());
return(false);
}
if(MathAbs(InpLots/m_symbol.LotsStep()-MathRound(InpLots/m_symbol.LotsStep()))>1.0 E-10)
{
printf("Lots amount is not corresponding with lot step %f",m_symbol.LotsStep());
return(false);
}
//--- warningif(InpTakeProfit<=InpTrailingStop)
printf("Warning: Trailing Stop must be less than Take Profit");
//--- succeedreturn(true);
}
//+------------------------------------------------------------------+//| Initialisation of the indicators|//+------------------------------------------------------------------+bool CSampleExpert::InitIndicators(void)
{
//--- create MACD indicatorif(m_handle_macd==INVALID_HANDLE)
if((m_handle_macd=iMACD(NULL,0,12,26,9,PRICE_CLOSE))==INVALID_HANDLE)
{
printf("Error creating MACD indicator");
return(false);
}
//--- create EMA indicator and add it to collection//--- succeedreturn(true);
}
//+------------------------------------------------------------------+//| Check for long position closing|//+------------------------------------------------------------------+bool CSampleExpert::LongClosed(void)
{
bool res=false;
//--- should it be closed?if(m_macd_current<0 && m_macd_previous>0)
if(m_macd_current<m_macd_close_level)
{
//--- close positionif(m_trade.PositionClose(Symbol()))
printf("Long position by %s to be closed",Symbol());
elseprintf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment());
//--- processed and cannot be modified
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for short position closing|//+------------------------------------------------------------------+bool CSampleExpert::ShortClosed(void)
{
bool res=false;
//--- should it be closed?if(m_macd_current>0 && m_macd_previous<0)
if(MathAbs(m_macd_current)>m_macd_close_level)
{
//--- close positionif(m_trade.PositionClose(Symbol()))
printf("Short position by %s to be closed",Symbol());
elseprintf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment());
//--- processed and cannot be modified
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for long position modifying|//+------------------------------------------------------------------+bool CSampleExpert::LongModified(void)
{
bool res=false;
//--- check for trailing stopif(InpTrailingStop>0)
{
if(m_symbol.Bid()-m_position.PriceOpen()>m_adjusted_point*InpTrailingStop)
{
double sl=NormalizeDouble(m_symbol.Bid()-m_traling_stop,m_symbol.Digits());
double tp=m_position.TakeProfit();
if(m_position.StopLoss()<sl || m_position.StopLoss()==0.0)
{
//--- modify positionif(m_trade.PositionModify(Symbol(),sl,tp))
printf("Long position by %s to be modified",Symbol());
else
{
printf("Error modifying position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f",sl,tp);
}
//--- modified and must exit from expert
res=true;
}
}
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for short position modifying |//+------------------------------------------------------------------+bool CSampleExpert::ShortModified(void)
{
bool res=false;
//--- check for trailing stopif(InpTrailingStop>0)
{
if((m_position.PriceOpen()-m_symbol.Ask())>(m_adjusted_point*InpTrailingStop))
{
double sl=NormalizeDouble(m_symbol.Ask()+m_traling_stop,m_symbol.Digits());
double tp=m_position.TakeProfit();
if(m_position.StopLoss()>sl || m_position.StopLoss()==0.0)
{
//--- modify positionif(m_trade.PositionModify(Symbol(),sl,tp))
printf("Short position by %s to be modified",Symbol());
else
{
printf("Error modifying position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f",sl,tp);
}
//--- modified and must exit from expert
res=true;
}
}
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for long position opening|//+------------------------------------------------------------------+bool CSampleExpert::LongOpened(void)
{
bool res=false;
//--- check for long position (BUY) possibilityif(m_macd_current>0 && m_macd_previous<0)
if(MathAbs(m_macd_current)>(m_macd_open_level))
{
double price=m_symbol.Ask();
double tp =m_symbol.Bid()+m_take_profit;
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,InpLots,price)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
//--- open positionif(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,0.0,tp))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,tp);
}
}
//--- in any case we must exit from expert
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for short position opening|//+------------------------------------------------------------------+bool CSampleExpert::ShortOpened(void)
{
bool res=false;
//--- check for short position (SELL) possibilityif(m_macd_current<0 && m_macd_previous>0)
if(m_macd_current<(m_macd_open_level) )
{
double price=m_symbol.Bid();
double tp =m_symbol.Ask()-m_take_profit;
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,InpLots,price)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
//--- open positionif(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,0.0,tp))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,tp);
}
}
//--- in any case we must exit from expert
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| main function returns true if any position processed |//+------------------------------------------------------------------+bool CSampleExpert::Processing(void)
{
//--- refresh ratesif(!m_symbol.RefreshRates())
return(false);
//--- refresh indicatorsif(BarsCalculated(m_handle_macd)<2 )
return(false);
if(CopyBuffer(m_handle_macd,0,0,2,m_buff_MACD_main) !=2 )
return(false);
// m_indicators.Refresh();//--- to simplify the coding and speed up access//--- data are put into internal variables
m_macd_current =m_buff_MACD_main[0];
m_macd_previous =m_buff_MACD_main[1];
//--- it is important to enter the market correctly, //--- but it is more important to exit it correctly.... //--- first check if position exists - try to select itif(m_position.Select(Symbol()))
{
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
//--- try to close or modify long positionif(LongClosed())
return(true);
if(LongModified())
return(true);
}
else
{
//--- try to close or modify short positionif(ShortClosed())
return(true);
if(ShortModified())
return(true);
}
}
//--- no open position identifiedelse
{
//--- check for long position (BUY) possibilityif(LongOpened())
return(true);
//--- check for short position (SELL) possibilityif(ShortOpened())
return(true);
}
//--- exit without position processingreturn(false);
}
//+------------------------------------------------------------------+//| Expert initialisation function|//+------------------------------------------------------------------+intOnInit(void)
{
//--- create all necessary objectsif(!ExtExpert.Init())
return(INIT_FAILED);
//--- secceedreturn(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert new tick handling function|//+------------------------------------------------------------------+voidOnTick(void)
{
staticdatetime limit_time=0; // last trade processing time + timeout//--- don't process if timeoutif(TimeCurrent()>=limit_time)
{
//--- check for data
{
//--- change limit time by timeout in seconds if processedif(ExtExpert.Processing())
limit_time=TimeCurrent()+ExtTimeOut;
}
}
}
I removed all data about"moving average" and "MACD signal line", all I could find.
bool CSampleExpert::LongOpened(void)
{
bool res=false;
//--- check for long position (BUY) possibilityif(m_macd_current>0 && m_macd_previous<0)
if(MathAbs(m_macd_current)>(m_macd_open_level))
In this location, instead of a value of "0", the midline was given. I replaced it with "0" everywhere, when selling, and when closing traces.
Выберите подходящую торговую стратегию и оформите подписку на нее в пару кликов. Все Сигналы сопровождаются подробной статистикой и графиками. Станьте Поставщиком торговых сигналов и продавайте подписку тысячам трейдеров по всему миру. Наш сервис позволит вам хорошо зарабатывать на прибыльной стратегии даже при небольшом стартовом капитале...
Removed all data about"moving average" and "MACD signal line", all that I could find.
In this place, instead of the value "0", the average line was specified. Replaced it with a value of "0" everywhere, when selling, and when closing the traces.
on the long example:
bool CSampleExpert::LongOpened(void)
{
bool res=false;
//--- check for long position (BUY) possibilityif(m_macd_current>0 && m_macd_previous<0)
if(MathAbs(m_macd_current)>(m_macd_open_level))
{
Yes, but this condition should be either removed or set correctly -- above is a picture of EA performance with this condition removed.
I didn't change the condition, but I changed other values.
inputdouble InpLots =0.1; // Lots.inputint InpTakeProfit =0; // Take Profit (in pips)inputint InpTrailingStop =0; // Trailing Stop Level (in pips)inputint InpMACDOpenLevel =0; // MACD open level (in pips)inputint InpMACDCloseLevel=0; // MACD close level (in pips)inputint InpMATrendPeriod =26; // MA trend period
I changed OpenLevel and Closelevel to 0. If I understood correctly, this is one of the conditions for opening a position.
Position opening occurs through crossing "0", but for some reason the deal is closed after 10 pips. And it is desirable that the deal was opened after the second bar, because the first bar is not formed, and there are false entry.
I also set that the deal was made not earlier than in 60 seconds, it is 10, and he every 10 seconds, during the minute makes a buy or sell, ie for a minute 6 deals. You set 1 lot, for a minute there is a purchase of 6 lots or sale, depending on where the line "0" is crossed.
int ExtTimeOut=60; // time out in seconds between trade operations
I partially figured it out, I set TakeProfit to 10000 and it started to roll over normally, but it misses some trades. It is necessary to add a function that the rollover occurs on the second bar, false trades are obtained, and more likely because of this missing trades.
If TakeProfit is set to the required amount, for example on "RTS" 100 points, it closes a deal on TP, and opens a new one when the indicator crosses "0". I tested it on minutes, as there are a lot of signals.
post the code with your edits -- explain what edits you made -- let's see.
If you are interested in doing it yourself, there is a chance of figuring it out.
I removed all data about"moving average" and "MACD signal line", all I could find.
In this location, instead of a value of "0", the midline was given. I replaced it with "0" everywhere, when selling, and when closing traces.
Removed all data about"moving average" and "MACD signal line", all that I could find.
In this place, instead of the value "0", the average line was specified. Replaced it with a value of "0" everywhere, when selling, and when closing the traces.
on the long example:
-- what role does the highlighted condition play?
Here's what I have after correcting the above highlighted (small comments on the specifics of the openings):
using long as an example:
-- what role does the highlighted condition play?
MathAbs(m_macd_current)>m_macd_open_level - current value of MACD indicator main line modulo
MathAbs(m_macd_current)>m_macd_open_level - current value of the main line of the MACD indicator modulo
yes, only this condition should be either removed or set correctly -- above is a picture of EA performance with this condition removed
Yes, but this condition should be either removed or set correctly -- above is a picture of EA performance with this condition removed.
I didn't change the condition, but I changed other values.
I changed OpenLevel and Closelevel to 0. If I understood correctly, this is one of the conditions for opening a position.
And I disabled Takeprofit and Stoploss.
Position opening occurs through crossing "0", but for some reason the deal is closed after 10 pips. And it is desirable that the deal was opened after the second bar, because the first bar is not formed, and there are false entry.
I also set that the deal was made not earlier than in 60 seconds, it is 10, and he every 10 seconds, during the minute makes a buy or sell, ie for a minute 6 deals. You set 1 lot, for a minute there is a purchase of 6 lots or sale, depending on where the line "0" is crossed.
I partially figured it out, I set TakeProfit to 10000 and it started to roll over normally, but it misses some trades. It is necessary to add a function that the rollover occurs on the second bar, false trades are obtained, and more likely because of this missing trades.
If TakeProfit is set to the required amount, for example on "RTS" 100 points, it closes a deal on TP, and opens a new one when the indicator crosses "0". I tested it on minutes, as there are a lot of signals.