Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
Can anyone tell me why the EA doesnt trade off the signals from the indicator? And offer any code fixes? My Understanding of how MQL works and how ticks and bars are triggered and code executed must be flawed.
//+------------------------------------------------------------------+
//| Enter Some Details About This Function Here |
//+------------------------------------------------------------------+
void CloseOrders(int Mode)
{
int cnt = 0;
int TotalOrders = 0;
//+------------------------------------------------------------------
//| Here we have been given the green light to enter a new up trade
//| we need to get the correct lots to be used and determine our initial
//| stop based off our foundation lines. Typically if this is a new trend
//| change entry then we will need to search previous bars for the high/Low
//| to get our initial stop.
//+------------------------------------------------------------------
void Enter_New_Up()
{
double Lots = 1.0;
double MyAsk = 0.0;
int Ticket = -1;
//+------------------------------------------------------------------
//| Here we have been given the green light to enter a new dn trade
//| we need to get the correct lots to be used and determine our initial
//| stop based off our foundation lines. Typically if this is a new trend
//| change entry then we will need to search previous bars for the high/Low
//| to get our initial stop.
//+------------------------------------------------------------------
void Enter_New_Dn()
{
double Lots = 1.0;
double MyBid = 0.0;
int Ticket = -1;
//+------------------------------------------------------------------+
//| Test MA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- buffers
double ExtMapBuffer1[];
int Spread = 0;
datetime MyTime = 0;
int Trend = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexLabel(0, "Trend");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double Temp1 = 0.0;
double Temp2 = 0.0;
//----+ check whether the amount of bars is sufficient for correct
// calculation of the indicator
if(Bars - 1 < 21)
return(0);
//----+ Entering of integer variables and obtaining of bars already counted
int MaxBar, limit, counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0)
return(-1);
//---- the last counted bar must be re-counted
if(counted_bars > 0)
counted_bars--;
//---- determining of the oldest bar number, starting from which
// all bars will be re-counted
MaxBar = Bars - 1 - 21;
//---- determining of the oldest bar number, starting from which
// only new bars will be re-counted
limit = (Bars - 1 - counted_bars);
//---- initialization of null
if(limit > MaxBar)
{
for(int bar = Bars - 1; bar >= limit; bar--)
{
ExtMapBuffer1[bar] = -1;
}
limit = MaxBar;
}
Without penetrating the code in too much detail, I would suggest that you make the indicator compute and assign bar 1 when invoked. There is all too much logic about "counted bars" in the indicator; drop all that and make it assign bar==1 the way it is supposed to.
Of course, if you would want to use it as a "normal" indicator it should calculate for earlier bars as well, but since the indicator keeps state between invocations (the "Trend" variable), it is ill suited for normal use. The "proper" way to carry extra state would be to use a hidden buffer.
Further, you would need to cater for it being invoked on every tick rather than only at the bar opening tick. The EA invokes the indicator only at the bar opening tick, which is when the EA chooses to operate. When dropped on a chart, the indicator will be invoked once initially, with the purpose to paint the history, and then with every tick. On the basis of how the EA uses it, this indicator is supposed to only pay attention to the bar opening tick, and then paint bar==1.
int start() { for ( int bar = Bars - IndicatorCounted(); bar >= 0; bar-- ) { indicate( bar ); } return( 0 ); }
void indicate(int bar) { if ( buffer[ bar ] != EMPTY_VALUE ) return; buffer[ bar ] = computeValueForBar( bar ); }
// This function will be called only once for any bar double computeValueForBar(int bar) { ... }
In your case, the c16r function would call iMA(..., PRICE_CLOSE, bar+1 ) and iMA(..., PRICE_CLOSE, bar+2 ) as well as look at buffer[bar+1] to figure out the value for buffer[bar]. Note that you shouldn't use iMA(..., PRICE_CLOSE, bar) because that is logically to refer into the future, which is illogical
By that template, the indicator determines its value for a bar immediately with the first tick for that bar. As it should, the computation for a bar makes use of data that exists prior to the bar, and therefore the value is known with the first tick.
EDIT: I forgot to mention that the EA should ask for the bar 0 data of the indicator...
Last edited by ralph.ronnquist; 02-27-2007 at 02:54 AM.
Thank you for taking the time to point me in the right direction. With the above code I can use to contstruct an indicator that will only update on each new bar and shows history.
From the calling EA how can I use the EA to backtest and only have the EA run on new bars?
Sorry, I was a bit brief there, as I kind of thought you had that one sorted. Basically you use the EA you presented, but make it ask for the bar 0 value:
PHP Code:
Entry = iCustom(NULL,0,"Test MA",0,0)
The indicator produces its bar 0 value on the first invocation with that bar as bar 0, and it does not change that value, ever.
So with your EA, you would restrict the invocations to be with the first tick for each bar, which is enough for the indicator to operate. And you might run your backtest in "open bars only" mode, I suppose.