Ian, it's not such a mystery. You said yourself that the MAs crossed in lower time frames. If you look at the bar in which your trade was closed, the 5EMA very well could have crossed below the 10LWMA and triggered the close. The chart you attached represents the moving averages calculated on PRICE_CLOSE and doesn't represent the internal bar price movement since the MAs are calculated with the most recent bars current price representing close. I'm not in front of MT currently, but try plotting a 5EMA based on lows of the bars and see what happens.
Mookfx
Hi all you savvy MT4 people,
I am working on a moving average cross EA and have been seeing some strange trade executions happening when I use the Strategy Tester in MT4. I've included my code here, but in a nutshell, my system will go long when the 5EMA crosses above the 10LWMA and go short when the 5EMA crosses below the 10LWMA on the 1 HOUR chart. There are some other filters for entry, but that's ancillary to the issue.
My exit criteria is just the opposite of my entry. So if I'm LONG, the system looks for the 5EMA to cross below the 10LWMA. SHORT close logic is just the inverse of this.
As you can see in the chart below, I entered LONG when the 5EMA crossed above the 10LWMA, which makes sense. However, the system closed that position 9 bars later EVEN THOUGH THE 2 MAs DID NOT CROSS ON THE 1 HR CHART. I've studied all the program logic to see if any other criteria (e.g.: trailing stop, take-profit, etc.) might have triggered this exit, but couldn't find anything. The 2 moving averages did cross on shorter timeframes (15M, 5M) but since I am running this on the 1H timeframe, I'm mystified why the long position was closed since my code calculates the MAs based on PRICE_CLOSE.
Any insight based on the attached chart and the program code below?? I'm stumped. This issue is causing very misleading backtesting data and I really need to figure it out.
Thanks in advance,
Ian
Ian, it's not such a mystery. You said yourself that the MAs crossed in lower time frames. If you look at the bar in which your trade was closed, the 5EMA very well could have crossed below the 10LWMA and triggered the close. The chart you attached represents the moving averages calculated on PRICE_CLOSE and doesn't represent the internal bar price movement since the MAs are calculated with the most recent bars current price representing close. I'm not in front of MT currently, but try plotting a 5EMA based on lows of the bars and see what happens.
Mookfx
Hi Mookfx,
Yes, I know that the MAs did cross at lower timeframes, but logic would dictate that if my program had the MAs set using the 1H timeframe and am using PRICE_CLOSE as the price point for defining them, then if they didn't cross at the hourlevel using the 1H CLOSE price (regardless of what they did at the sub-hour level) then I shouldn't get any order execution, right?
I'm still learning much about MetaTrader so I may be under a complete misapprehension, but if this is not how the code works, then I think it would be causing many issues for other MA cross systems that are timeframe-specific. For instance, if I were only interested in those cases when the MAs crossed on the 4H chart, I wouldn't want my orders being executed if they crossed during the 1H interval, would I? That would seem to defeat the purpose of defining the MA cross at the 4H level...
Am I missing something here?
- Ian
iboersma
make the changes below you are testing tick data as it is, no matter what the chart time fram is. Useing 0 will exit the trade on intrabar conditions. The one will be the final tick on the bar that will be tested to see if the cross was made.
The CockeyedCowboy
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//MA and Filter Variable Definition
EMA1 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
WMA1 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,1);
EMA2 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,2);
WMA2 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,2);
RSI = iRSI(NULL,0,RSIPer,PRICE_CLOSE,0);
Stoch = iStochastic(NULL,PERIOD_H1,StochK,StochD,StochSlow ,MODE_SMA,0,MODE_MAIN,0);
int isCrossed = 0;
if (EMA1 > WMA1 && EMA2 <= WMA2)
{isCrossed = 1;
}
if (EMA1 = WMA2)
{isCrossed = 2;
}
Hi Mookfx,
Yes, I know that the MAs did cross at lower timeframes, but logic would dictate that if my program had the MAs set using the 1H timeframe and am using PRICE_CLOSE as the price point for defining them, then if they didn't cross at the hourlevel using the 1H CLOSE price (regardless of what they did at the sub-hour level) then I shouldn't get any order execution, right?
I'm still learning much about MetaTrader so I may be under a complete misapprehension, but if this is not how the code works, then I think it would be causing many issues for other MA cross systems that are timeframe-specific. For instance, if I were only interested in those cases when the MAs crossed on the 4H chart, I wouldn't want my orders being executed if they crossed during the 1H interval, would I? That would seem to defeat the purpose of defining the MA cross at the 4H level...
Am I missing something here?
- Ianiboersma
make the changes below you are testing tick data as it is, no matter what the chart time fram is. Useing 0 will exit the trade on intrabar conditions. The one will be the final tick on the bar that will be tested to see if the cross was made.
The CockeyedCowboy
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//MA and Filter Variable Definition
EMA1 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
WMA1 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,1);
EMA2 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,2);
WMA2 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,2);
RSI = iRSI(NULL,0,RSIPer,PRICE_CLOSE,0);
Stoch = iStochastic(NULL,PERIOD_H1,StochK,StochD,StochSlow ,MODE_SMA,0,MODE_MAIN,0);
int isCrossed = 0;
if (EMA1 > WMA1 && EMA2 <= WMA2)
{isCrossed = 1;
}
if (EMA1 = WMA2)
{isCrossed = 2;
}
Hey Cowboy,
Thanks. That did the trick!
- Ian
EA Trade Execution Question
I have spent a lot of time creating EA's on the VTTrader platform. There is one thing that is preventing me from enjoying success with them. VTTrader has removed the intrabar feature from its programming platform; hence trade execution occurs at the close, which is very stupid.
Here is my question: Does Metatrader EA's execute trades intrabar or does it have the same restriction as VTTrader?
I don't want to waste my time learning MQL4 if Metatrader has the same stupid restriction. Further, I hear the learning curve is steeper with MQL4.
I am tired of f*&^ing around with VTTrader. They have been promising the return of the intrabar feature for five years.
Thanks,
Wolf
Some other thread related to this subject is the following https://www.mql5.com/en/forum/176568
Wolf
You can make as meny trades you want during a bar. Even a one minute bar, but your broker may not like it.
Keit
I have spent a lot of time creating EA's on the VTTrader platform. There is one thing that is preventing me from enjoying success with them. VTTrader has removed the intrabar feature from its programming platform; hence trade execution occurs at the close, which is very stupid.
Here is my question: Does Metatrader EA's execute trades intrabar or does it have the same restriction as VTTrader?
I don't want to waste my time learning MQL4 if Metatrader has the same stupid restriction. Further, I hear the learning curve is steeper with MQL4.
I am tired of f*&^ing around with VTTrader. They have been promising the return of the intrabar feature for five years.
Thanks,
Wolf- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all you savvy MT4 people,
I am working on a moving average cross EA and have been seeing some strange trade executions happening when I use the Strategy Tester in MT4. I've included my code here, but in a nutshell, my system will go long when the 5EMA crosses above the 10LWMA and go short when the 5EMA crosses below the 10LWMA on the 1 HOUR chart. There are some other filters for entry, but that's ancillary to the issue.
My exit criteria is just the opposite of my entry. So if I'm LONG, the system looks for the 5EMA to cross below the 10LWMA. SHORT close logic is just the inverse of this.
As you can see in the chart below, I entered LONG when the 5EMA crossed above the 10LWMA, which makes sense. However, the system closed that position 9 bars later EVEN THOUGH THE 2 MAs DID NOT CROSS ON THE 1 HR CHART. I've studied all the program logic to see if any other criteria (e.g.: trailing stop, take-profit, etc.) might have triggered this exit, but couldn't find anything. The 2 moving averages did cross on shorter timeframes (15M, 5M) but since I am running this on the 1H timeframe, I'm mystified why the long position was closed since my code calculates the MAs based on PRICE_CLOSE.
Any insight based on the attached chart and the program code below?? I'm stumped. This issue is causing very misleading backtesting data and I really need to figure it out.
Thanks in advance,
Ian
//+----------------------------------------------------------------------------------------+
//| 5/10 MA Cross w RSI and Stoch Filter |
//| Ian Boersma - Copyright 2006 |
//| |
//+----------------------------------------------------------------------------------------+
#property copyright "Ian Boersma"
//---- input parameters
extern double TakeProfit=100;
extern double Lots=1;
extern double TrailingStop=35;
extern int ShortEma = 5;
extern int LongWma = 10;
extern int RSIPer = 14;
extern int StochK = 10;
extern int StochD = 3;
extern int StochSlow = 3;
extern int ExecuteOrders = 1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function +
//| This is where we initialize internal variables for program use + |
//+------------------------------------------------------------------+
int start()
{
//----
//LotCalc Declarations
int cnt, ticket, total;
//MAs and Filter Variable Declarations
double RSI;
double Stoch;
double EMA1,EMA2,WMA1,WMA2;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//MA and Filter Variable Definition
EMA1 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
WMA1 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,0);
EMA2 = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
WMA2 = iMA(NULL,0,LongWma,0,MODE_LWMA,PRICE_CLOSE,1);
RSI = iRSI(NULL,0,RSIPer,PRICE_CLOSE,0);
Stoch = iStochastic(NULL,PERIOD_H1,StochK,StochD,StochSlow,MODE_SMA,0,MODE_MAIN,0);
int isCrossed = 0;
if (EMA1 > WMA1 && EMA2 <= WMA2)
{isCrossed = 1;
}
if (EMA1 = WMA2)
{isCrossed = 2;
}
//We check the trading terminal to see if it has any existing orders
if(ExecuteOrders==1) //If we want to use the EA to actually make trades...
{total = OrdersTotal();
if(total < 1) //If there are no existing orders
{
if(isCrossed == 1 && RSI >= 50 && Stoch < 80) //if cross indicates LONG swing...
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,0,Ask+TakeProfit*Point,"My EA",12345,0,Yellow);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && RSI 20) //if cross indicates SHORT swing...
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
//Position exit function...
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
OrderPrint();
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
// Check if we have a SHORT cross condition...
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// Check our trailing stop to see if it needs to be adjusted
if(TrailingStop>0){
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// Check if we have a LONG cross condition...
if(isCrossed == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// Check our trailing stop to see if it needs to be adjuste
if(TrailingStop>0){
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}}
//+------------------------------------------------------------------+