Hello Everyone,
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.
Here is the Test EA
PHP Code:
//+------------------------------------------------------------------+
//| Test MA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
extern int MyTarget = 15;
extern int MyStop = 15;
int Trend = 0;
int BuySignal = 0;
int SellSignal = 0;
int MyMagic = 12345;
int Slippage = 3;
int Entry = 0;
datetime MyTime = 0;
string FX_Symbol = "";
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
FX_Symbol = Symbol();
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int Trend = 0;
if ( MyTime != Time[0] )
{
MyTime = Time[0];
Entry = iCustom(NULL,0,"Test MA",0,1);
BuySignal = 0;
SellSignal = 0;
if ( Entry == 1 )
BuySignal = 1;
if (Entry == -1 )
SellSignal = 1;
if ( BuySignal == 1 )
{
CloseOrders(OP_SELL);
Enter_New_Up();
}
if ( SellSignal == 1 )
{
CloseOrders(OP_BUY);
Enter_New_Dn();
}
}
return(0);
}
//+------------------------------------------------------------------+
//| Enter Some Details About This Function Here |
//+------------------------------------------------------------------+
void CloseOrders(int Mode)
{
int cnt = 0;
int TotalOrders = 0;
TotalOrders = OrdersTotal();
for ( cnt = 0; cnt < TotalOrders; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() == FX_Symbol && OrderMagicNumber() == MyMagic )
{
if ( OrderType() == OP_SELL && Mode == OP_SELL)
OrderClose(OrderTicket(), 1, Ask, Slippage);
if ( OrderType() == OP_BUY && Mode == OP_BUY)
OrderClose(OrderTicket(), 1, Bid, Slippage);
}
}
}
//+------------------------------------------------------------------
//| 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;
SellSignal = 0;
BuySignal = 1;
MyAsk = Ask;
Ticket = OrderSend(FX_Symbol, OP_BUY, Lots, MyAsk, Slippage, MyAsk - (MyStop * Point), MyAsk + (MyTarget * Point), "", MyMagic, 0, Blue);
return(0);
}
//+------------------------------------------------------------------
//| 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;
BuySignal = 0;
SellSignal = 1;
MyBid = Bid;
Ticket = OrderSend(FX_Symbol, OP_SELL, Lots, MyBid, Slippage, MyBid + (MyStop * Point), MyBid - (MyTarget * Point), "", MyMagic, 0, Red);
return(0);
}
//+------------------------------------------------------------------+
Here is the Indicator
PHP Code:
//+------------------------------------------------------------------+
//| 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;
}
//+--- basic loop of indicator calculation
for(bar = limit; bar > 0; bar--)
{
Temp1 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,bar);
Temp2 = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,bar+1);
ExtMapBuffer1[bar] = 0;
if ( Temp1 > Temp2 && Trend != 1)
{
ExtMapBuffer1[bar] = 1;
Trend = 1;
}
if ( Temp1 < Temp2 && Trend != -1)
{
ExtMapBuffer1[bar] = -1;
Trend = -1;
}
}
return(0);
}
//+------------------------------------------------------------------+
THank you for your Time and Effort
EK