Help with Entry Criteria

Hi all,
I have come up with a way by using “Bars” function. When a trade is triggered, I will set a integer count with the bars function (e.g. a buy trade is triggered in bar 122, hence the count will be 122) and when my criteria has being triggered again, I will call the bar function again and compared it with the count and the trade will only trigger if the new count is higher then the previous which is 122 in this case.

However, I am still triggering 2 trades together instead of 1 per criteria. Why is this so?

Please help

Thanks and regards
Terrance

//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = 0;
double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);
double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);
double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);
double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);
double diff1 = shortEma1-longEma1;
double diff2 = shortEma2-longEma2;

mainshortEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);

if ((diff1*diff2)<0)
{
if (shortEma1>longEma1)
isCrossed = 1;
else isCrossed = 2;
}

int BarCounter, BarCounter2;

total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLossPoint,Ask+TakeProfitPoint,
“My EA”,12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("BUY order opened : ",OrderOpenPrice());

BarCounter = Bars;
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+S topLossPoint,Bid-TakeProfitPoint,
“My EA”,12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("SELL order opened : ",OrderOpenPrice());

BarCounter = Bars;
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}

if(total == 1)
{
BarCounter2 = Bars;

if(isCrossed == 1 && shortEma > mainshortEma && longEma > mainshortEma && BarCounter2 > BarCounter)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLossPoint,Ask+TakeProfitPoint,
“My EA”,12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("BUY order opened : ",OrderOpenPrice());

BarCounter = BarCounter2;
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && shortEma < mainshortEma && longEma < mainshortEma && BarCounter2 > BarCounter)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+S topLossPoint,Bid-TakeProfitPoint,
“My EA”,12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES ))
Print("SELL order opened : ",OrderOpenPrice());

BarCounter = BarCounter2;
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}

return(0);
}

Hi,

As the start function gets invoked each tick it’s quite possible that the 1st order has fully not been updated in the history and therefore open trades still thinks it’s zero (just a thought)

Paul

Hi, I’ve tried cutting & pasting code but for some reason I’m getting HTTP errors - anyway that aside, do a google search for Matt Kennel Orderreliable and you should find some great code to handle when CRAPT4 takes a while recording you trade on the history tab (using thsi you will find some other great examples of code from gurus out there)

Hope this helps

Paul.

your variables BarCounter and BarCounter2 should be GLOBAL
insert: int BarCounter, BarCounter2;
before:

//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()

your problem will be solved… hope your EA works…

Thanks for the help guys, managed to solve the error. I am currently working on a new entry requirements for Stochastics, but I am having some problems entering trades with stochastics crossovers. My aim is to open trade when stochastic crosses at the overbrought (level 80) or oversold (level 20) area. However, I am only able to open trade when it crosses. How do I restrict the trades to be carried out at the over brought and oversold regions?

Please help. Thanks.
Terrance

//— input parameters
extern double TakeProfit=530.0;
extern double Lots=0.1;
extern double StopLoss=520.0;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}

//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+

int start()
{

//----
int ticket, total;
double shortStoc, longStoc;
if(Bars<100)
{
Print(“bars less than 100”);
return(0);
}
if(TakeProfit<10)
{
Print(“TakeProfit less than 10”);
return(0); // check TakeProfit
}

//±-----------------------------------------------------------------+
//| expert start function | Stochastic Trigger
//±-----------------------------------------------------------------+
shortStoc = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0); //orignial Stoc movement
longStoc = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0); //orignial Stoc movement

//±-------------------------------------------------------------------------------------+
//| expert start function | Set trigger trade only when line crossed and candle closed |//
//±-------------------------------------------------------------------------------------+
int isCrossed = 0;
double shortStoc1 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,1);
double longStoc1 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
double shortStoc2 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,2);
double longStoc2 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,2);
double diff1 = shortStoc1-longStoc1;
double diff2 = shortStoc2-longStoc2;

if ((diff1*diff2)<0)
{
if (shortStoc1>longStoc1)
isCrossed = 1;
else isCrossed = 2;
}
//±-----------------------------------------------------------------------------------+
//| expert end function | Set trigger trade only when line crossed and candle closed |//
//±-----------------------------------------------------------------------------------+

//±-----------------------------------------------------------------+
//| expert end function | Stochastic Trigger
//±-----------------------------------------------------------------+

//| expert start function | Trigger order
//±-----------------------------------------------------------------------------------------------------------+
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLossPoint,Ask+TakeProfitPoint,
“My EA”,12345,0,Green);
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)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLossPoint,Bid-TakeProfitPoint,
“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);
}
return(0);
}