I'm not sure why my EA is opening multiple orders per bar even though set it to open once per bar. Someone please point out the mistake ?
thanks.
Inserted Code
extern int HMA_period = 10;
extern int HMA_PriceType = 0;
extern int HMA_Method = 3;
extern int Stoploss = 50;
extern double Lots = 0.1;
int start()
{
int signal0 =0; //current signal
int signal1 =0; //previous signal
int bars =0; //number of bars on chart
double previous = iCustom(NULL,0,"HMA Color nrp", HMA_period,HMA_PriceType,HMA_Method,1,2,0,0,1); //previous ma value
double current = iCustom(NULL,0,"HMA Color nrp", HMA_period,HMA_PriceType,HMA_Method,1,2,0,0,0); // current ma value
if (Bars>bars) //if there is new bar on the chart
{
bars=Bars; //run the below code and reset the "bars" variable to equal the number of bars on chart
if (current > previous) //this is buy signal
{
signal0=0; //signal0 = current signal
if (signal0 != signal1) //if previous bar signal sell(1) and current bar signal buy (0), then run the below code.
{
signal1=signal0; //signal1 = previous signal
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS);
OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); // close all short orders
}
OrderSend(Symbol(), 0, Lots, Ask, 3, Ask-Stoploss*Point, NULL, NULL, 00000, 0, Green); // open long order
}
}
if (previous > current)
{
signal0=1;
if (signal0 != signal1)
{
signal1=signal0;
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS);
OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
}
OrderSend(Symbol(), 1, Lots, Bid, 3, Bid+Stoploss*Point, NULL, NULL, 00000, 0, Red);
}
}
}
} thanks.