Stochastic indicator counts too many intersections in the same time which cause placing too many orders !!

 
#include<Trade\Trade.mqh>
CTrade trade;


void OnTick()
{

  double KArray[];
  double DArray[];
  ArraySetAsSeries(KArray,true);
  ArraySetAsSeries(DArray,true);
  int StochasticDefinition = iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,STO_LOWHIGH);
  CopyBuffer(StochasticDefinition,0,0,3,KArray);
  CopyBuffer(StochasticDefinition,1,0,3,DArray);
  double KValue0 = KArray[0];
  double DValue0 = DArray[0];
  double KValue1 = KArray[1];
  double DValue1 = DArray[1];
  
   if(KValue0<=20 && DValue0<=20)
    {
     if( (KValue0>DValue0) && (KValue1<DValue1))
       {
       printf("Intersection");
       }
    }
   if(KValue0>=80 && DValue0>=80)
    {
     if( (KValue0<DValue0) && (KValue1>DValue1) )
       {
         
       printf("Intersection");
       }
    }
    

}

This is the code I used to make an order according to the Stochastic indicator if it's an intersection under 20 I'll buy and if it's above 80 I'll sell.

But when I run the code I get too many intersections very close to each other which cause my EA to make too many orders;

Please if anyone had had this problem before and found a solution please help me.

Thank you :)

 
#include<Trade\Trade.mqh>
CTrade trade;

int StochasticDefinition;
//+------------------------------------------------------------------+
void OnInit()
  {
//---
   StochasticDefinition=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,STO_LOWHIGH);
   if(StochasticDefinition == INVALID_HANDLE)
     {
      printf("Error creating \"Stochastic\" indicator");
      ExpertRemove();
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

void OnTick()
  {
   static datetime time_bar=0;
//---
   double KArray[];
   double DArray[];
   ArraySetAsSeries(KArray,true);
   ArraySetAsSeries(DArray,true);
   CopyBuffer(StochasticDefinition,0,0,3,KArray);
   CopyBuffer(StochasticDefinition,1,0,3,DArray);
   double KValue0 = KArray[0];
   double DValue0 = DArray[0];
   double KValue1 = KArray[1];
   double DValue1 = DArray[1];

   datetime t_bar=iTime(_Symbol,PERIOD_CURRENT,0);
   if(KValue0<=20 && DValue0<=20)
     {
      if((KValue0>DValue0) && (KValue1<DValue1))
        {
         if(time_bar==t_bar)
            return;
         time_bar = t_bar;
         printf("Intersection");
        }
     }
   if(KValue0>=80 && DValue0>=80)
     {
      if((KValue0<DValue0) && (KValue1>DValue1))
        {
         if(time_bar==t_bar)
            return;
         time_bar = t_bar;
         printf("Intersection");
        }
     }

  }
//+------------------------------------------------------------------+

One of the options

 
Konstantin Nikitin:

One of the options

Thank you so much.. its much better but still sometimes make two orders for one intersection
 
This is a test. One bar, one position. You can also check the price step.
 
Konstantin Nikitin:
This is a test. One bar, one position. You can also check the price step.
How can I do that please ?
 
#include<Trade\Trade.mqh>
CTrade trade;

input int Step=100;

int StochasticDefinition;
double prise_buy  = DBL_MAX,
       price_sell = 0.0;
//+------------------------------------------------------------------+
void OnInit()
  {
//---
   StochasticDefinition=iStochastic(_Symbol,_Period,5,3,3,MODE_SMA,STO_LOWHIGH);
   if(StochasticDefinition==INVALID_HANDLE)
     {
      printf("Error creating \"Stochastic\" indicator");
      ExpertRemove();
     }
  }
//+------------------------------------------------------------------+

void OnTick()
  {
   static datetime time_bar=0;
//---
   double KArray[];
   double DArray[];
   ArraySetAsSeries(KArray,true);
   ArraySetAsSeries(DArray,true);
   CopyBuffer(StochasticDefinition,0,0,3,KArray);
   CopyBuffer(StochasticDefinition,1,0,3,DArray);
   double KValue0 = KArray[0];
   double DValue0 = DArray[0];
   double KValue1 = KArray[1];
   double DValue1 = DArray[1];
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   datetime t_bar=iTime(_Symbol,PERIOD_CURRENT,0);

// open BUY
   if(prise_buy<=ask)
      if(KValue0<=20 && DValue0<=20)
        {
         if((KValue0>DValue0) && (KValue1<DValue1))
           {
            if(time_bar==t_bar)
               return;
            time_bar   = t_bar;
            prise_buy  = ask-Step*_Point;
            price_sell = 0.0;
            printf("Intersection");
           }
        }
// open SELL
   if(price_sell>=bid)
      if(KValue0>=80 && DValue0>=80)
        {
         if((KValue0<DValue0) && (KValue1>DValue1))
           {
            if(time_bar==t_bar)
               return;
            time_bar   = t_bar;
            price_sell = bid+Step*_Point;
            prise_buy  = DBL_MAX;
            printf("Intersection");
           }
        }

  }
//+------------------------------------------------------------------+

Something like that

Reason: