# Code correct but wrong signal random error

567

I wrote this code with two As soon as I have an cross of moving averages between two candles, step to the next step where I have a for loop, which identifies a Heiken candles pattern. the signal is correct, but if I have an cross of moving averages between two candles, without the pattern true .... I sent orders for no apparent reason.

The code looks okay but I do not understand what is wrong.

int Step  = 0;
int Step1 = 0;

int X    = 1;
int Y    = 1;

int TicketB;
int TicketS;

extern int StopLoss   = 200;
extern int TakeProfit = 200;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
TicketB = -1;
TicketS = -1;

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

if(NewBar())
{
FunzioneSELL();
}}

//+------------------------------------------------------------------+

//--------------------------------------------------------------------------------------------------------------------------------------+
//                                                Condition SELL                                                                        |
//--------------------------------------------------------------------------------------------------------------------------------------+
//Condizione SELL 1
void FunzioneSELL (){

if(Step == 0
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 2) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 2) // Media exp 6 > Media Ssempl 12
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 < Media Ssempl 12
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 2) // Media exp 6 Candela1 < Media exp 6 Candela2
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT,  12,0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 Candela1 < Media exp 6 Candela2
&&(iMA(NULL, PERIOD_CURRENT, 12,0, MODE_SMA, PRICE_CLOSE, 1) - iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 1))> 2*Point // Media exp 6 Candela1 < Media exp 6 Candela2
)

{
Print("Primo Step_Sell");
ObjectCreate(0,"IncrocioMM Sell"+ IntegerToString(X),OBJ_ARROW_CHECK,0,TimeCurrent(),High[0] + 200*Point());
Step++;
X++;
}

if(Step == 1 )

{

int l;

for(l=0; l<4; l++){

if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+l) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+l) // Red Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+l) // Green Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+l) // Green Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+l))// Green Heiken

{
if(TicketS < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));
else Print("Ordine SELL inviato correttamente");
Step = 0;
l = 4;
}

else if(l>=4) Step = 0;
}
}
}

//--------------------------------------------------------------------------------------------------------------------------------------
bool NewBar(){

static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return (false);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------+
//--------------------------------------------------------------------------------------------------------------------------------------+

if(Step1 == 0
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 2) < iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 2) // Media exp 6 > Media Ssempl 12
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 < Media Ssempl 12
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 2) // Media exp 6 Candela1 < Media exp 6 Candela2
&& iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 Candela1 < Media exp 6 Candela2
&&(iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) - iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1))> 2*Point // Media exp 6 Candela1 < Media exp 6 Candela2
)

{
Step1++;
Y++;
}

if(Step1 == 1  )
{

int m;

for(m=0; m<4; m++){

if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+m) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+m)  // Green Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+m)  // Red Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+m)  // Red Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+m)) // Red Heiken

{
if(TicketB < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));
Step1 = 0;
m = 4;
}

else if(m>=4) Step1 = 0;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------
567

It can be a conflict between more orders?
Moderator
31473

fly7680:
It can be a conflict between more orders?
if(Step==1)

{

int l;

for(l=0; l<4; l++)
{

if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+l) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+l) // Red Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+l) // Green Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+l) // Green Heiken
&& iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+l))// Green Heiken

{
if(TicketS<0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));
else Print("Ordine SELL inviato correttamente");
Step=0;
l=4;
}

else if(l>=4) Step=0;
}
}
}
This reset of the Step variable should be outside the loop. It's never executed.
567

Thanks Alain Verleyen, that was the bug, now it works!