How To Limit To One Trade Per Stochastic Crossing - page 2

 

deVries,

Thanks.

By this code, I think I have included your comments but it can certainly be improved.

I still listen to all remarks to improve if you see aberrations writing

Thanks again

Total=OrdersTotal();
Compteur=0;
   
   for(int i=0; i<Total; i++)
      {                                                                                                                                   
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         { 
         if ((OrderCloseTime()==0) && (OrderMagicNumber()==Magic_Number))
            {
            Compteur = Compteur + 1;
            }
         }
      }
   
   
   
   //+------------------------------------------------------------------+
   //|                  Buy and Close Buy order                         |
   //+------------------------------------------------------------------+
   if (
      (Compteur==0) && 
      (Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
      (Stochastic_Previous < Stochastic_Signal_Previous) &&
      ((DayOfWeek()==0 && Hour()>=Dimanche_Ouverture) || 
      (DayOfWeek()==1 && Hour()>=Lundi_Ouverture && Hour()<=Lundi_Fermeture) ||
      (DayOfWeek()==2 && Hour()>=Mardi_Ouverture && Hour()<=Mardi_Fermeture) ||
      (DayOfWeek()==3 && Hour()>=Mercredi_Ouverture && Hour()<=Mercredi_Fermeture) ||
      (DayOfWeek()==4 && Hour()>=Jeudi_Ouverture && Hour()<=Jeudi_Fermeture) ||
      (DayOfWeek()==5 && Hour()>=Vendredi_Ouverture && Hour()<=Vendredi_Fermeture))
      )
      {
      if (Sto_Take_Profit == 0)
         {
         Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Mono Stochastic : Achat",Magic_Number,0,Blue);
         Total=OrdersTotal();
         }
         
      if ((Sto_Take_Profit > 0) && (Bars != BarsCount))
         {
         Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(Sto_Take_Profit/10000),"Mono Stochastic : Achat",Magic_Number,0,Blue);
         Total=OrdersTotal();
         BarsCount=Bars;
         }
      }


   if ((Stochastic_Current < Stochastic_Signal_Current) && (Stochastic_Previous > Stochastic_Signal_Previous))
      {
      for(int x=0; x<Total; x++)
         {                                                                                                                                   
         if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_BUY) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Bid, 3, Red);
               Total=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }
   
   
   //+------------------------------------------------------------------+
   //|                  Sell and Close Sell order                       |
   //+------------------------------------------------------------------+ 
   if (
      (Compteur==0) && 
      ((Stochastic_Current + Sto_Secu_Faux_Signal) < Stochastic_Signal_Current) &&
      (Stochastic_Previous > Stochastic_Signal_Previous) &&
      ((DayOfWeek()==0 && Hour()>=Dimanche_Ouverture) || 
      (DayOfWeek()==1 && Hour()>=Lundi_Ouverture && Hour()<=Lundi_Fermeture) ||
      (DayOfWeek()==2 && Hour()>=Mardi_Ouverture && Hour()<=Mardi_Fermeture) ||
      (DayOfWeek()==3 && Hour()>=Mercredi_Ouverture && Hour()<=Mercredi_Fermeture) ||
      (DayOfWeek()==4 && Hour()>=Jeudi_Ouverture && Hour()<=Jeudi_Fermeture) ||
      (DayOfWeek()==5 && Hour()>=Vendredi_Ouverture && Hour()<=Vendredi_Fermeture))
      )
      {
      if (Sto_Take_Profit == 0)
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total=OrdersTotal();
         }
         
      if ((Sto_Take_Profit > 0) && (Bars != BarsCount))
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-(Sto_Take_Profit/10000),"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total=OrdersTotal();
         BarsCount=Bars;
         }
      }
      
      
   if ((Stochastic_Current > Stochastic_Signal_Current) && (Stochastic_Previous < Stochastic_Signal_Previous))
      {
      for(int y=0; y<Total; y++)
         {                                                                                                                                   
         if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_SELL) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Ask, 3, Blue);
               Total=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }
      
      
   Comment(Compteur);


 
Total=OrdersTotal();
Compteur=0;
   
   for(int i=0; i<Total; i++)     // don't count up....  checking trades 
      {                                                                                                                                   
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         { 
         if ((OrderCloseTime()==0) && (OrderMagicNumber()==Magic_Number))     //Why don't you check the Symbol 
            {
            Compteur = Compteur + 1;
            }
         }
      }

Not only look but also Read this topic. Checking trades of your EA count down.

 

Thanks deVries,

Very good Topic. I think I have followed the advice

 Total_Orders=OrdersTotal();
 Compteur=0;

   for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --)
      {
      if( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;   // <-- Si OrderSelect échoue, la boucle passe à la Position_Index suivante
   
      if ((OrderCloseTime()==0) && (OrderMagicNumber()==Magic_Number) && (OrderSymbol() == Symbol()))
         {
         Compteur = Compteur + 1;
         }     
      }



 

Not only that part but also you have to change

  if ((Stochastic_Current < Stochastic_Signal_Current) && (Stochastic_Previous > Stochastic_Signal_Previous))
      {
      for(int x=0; x<Total; x++)
         {                                                                                                                                   
         if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_BUY) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Bid, 3, Red);
               Total=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }

where it is counting up and

if ((Stochastic_Current > Stochastic_Signal_Current) && (Stochastic_Previous < Stochastic_Signal_Previous))
      {
      for(int y=0; y<Total; y++)
         {                                                                                                                                   
         if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_SELL) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Ask, 3, Blue);
               Total=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }
this part....
 

deVries,

I had already changed to keep the same logic but not shown


   //+------------------------------------------------------------------+
   //|  Compte le nombre d'ordre actif ou pending en cours avec cet EA  |
   //+------------------------------------------------------------------+ 
   
   Total_Orders=OrdersTotal();
   Compteur=0;

   for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --)
      {
      if( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;   // <-- Si OrderSelect échoue, la boucle passe à la Position_Index suivante
   
      if ((OrderCloseTime()==0) && (OrderMagicNumber()==Magic_Number) && (OrderSymbol() == Symbol()))
         {
         Compteur = Compteur + 1;
         }     
      }
   
   
   //+------------------------------------------------------------------+
   //|                  Buy and Close Buy order                         |
   //+------------------------------------------------------------------+
   if (
      (Compteur==0) && 
      (Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
      (Stochastic_Previous < Stochastic_Signal_Previous) &&
      ((DayOfWeek()==0 && Hour()>=Dimanche_Ouverture) || 
      (DayOfWeek()==1 && Hour()>=Lundi_Ouverture && Hour()<=Lundi_Fermeture) ||
      (DayOfWeek()==2 && Hour()>=Mardi_Ouverture && Hour()<=Mardi_Fermeture) ||
      (DayOfWeek()==3 && Hour()>=Mercredi_Ouverture && Hour()<=Mercredi_Fermeture) ||
      (DayOfWeek()==4 && Hour()>=Jeudi_Ouverture && Hour()<=Jeudi_Fermeture) ||
      (DayOfWeek()==5 && Hour()>=Vendredi_Ouverture && Hour()<=Vendredi_Fermeture))
      )
      {
      if (Sto_Take_Profit == 0)
         {
         Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Mono Stochastic : Achat",Magic_Number,0,Blue);
         Total_Orders=OrdersTotal();
         }
         
      if ((Sto_Take_Profit > 0) && (Bars != BarsCount))
         {
         Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(Sto_Take_Profit/10000),"Mono Stochastic : Achat",Magic_Number,0,Blue);
         Total_Orders=OrdersTotal();
         BarsCount=Bars;
         }
      }


   if ((Stochastic_Current < Stochastic_Signal_Current) && (Stochastic_Previous > Stochastic_Signal_Previous))
      {
      for(int x = Total_Orders - 1; x >= 0 ; x --)
         {                                                                                                                                   
         if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_BUY) && (OrderSymbol() == Symbol()) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Bid, 3, Red);
               Total_Orders=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }
   
   
   //+------------------------------------------------------------------+
   //|                  Sell and Close Sell order                       |
   //+------------------------------------------------------------------+ 
   if (
      (Compteur==0) && 
      ((Stochastic_Current + Sto_Secu_Faux_Signal) < Stochastic_Signal_Current) &&
      (Stochastic_Previous > Stochastic_Signal_Previous) &&
      ((DayOfWeek()==0 && Hour()>=Dimanche_Ouverture) || 
      (DayOfWeek()==1 && Hour()>=Lundi_Ouverture && Hour()<=Lundi_Fermeture) ||
      (DayOfWeek()==2 && Hour()>=Mardi_Ouverture && Hour()<=Mardi_Fermeture) ||
      (DayOfWeek()==3 && Hour()>=Mercredi_Ouverture && Hour()<=Mercredi_Fermeture) ||
      (DayOfWeek()==4 && Hour()>=Jeudi_Ouverture && Hour()<=Jeudi_Fermeture) ||
      (DayOfWeek()==5 && Hour()>=Vendredi_Ouverture && Hour()<=Vendredi_Fermeture))
      )
      {
      if (Sto_Take_Profit == 0)
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total_Orders=OrdersTotal();
         }
         
      if ((Sto_Take_Profit > 0) && (Bars != BarsCount))
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-(Sto_Take_Profit/10000),"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total_Orders=OrdersTotal();
         BarsCount=Bars;
         }
      }
      
      
   if ((Stochastic_Current > Stochastic_Signal_Current) && (Stochastic_Previous < Stochastic_Signal_Previous))
      {
      for(int y = Total_Orders - 1; y >= 0 ; y --)
         {                                                                                                                                   
         if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
            { 
            if ((OrderCloseTime()==0) && (OrderType()==OP_SELL) && (OrderSymbol() == Symbol()) && (OrderMagicNumber()==Magic_Number))
               {
               OrderClose(OrderTicket(), Lots, Ask, 3, Blue);
               Total_Orders=OrdersTotal();
               Compteur=0;
               }
            }
         }
      }
 
(Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
      (Stochastic_Previous < Stochastic_Signal_Previous) &&

How do you get those values ???

 

DeVries,

I get by external parts

extern string Stochastic_Période = "=== Paramètrage du Stochastic ===";
extern int k_Periode = 18;
extern int d_Periode = 50;
extern int Slowing = 9;
extern int Sto_Periode = 60;
extern int Sto_Method = 1;
extern int Sto_Price = 0;
extern int Sto_Shift = 0;
extern int Sto_Shift_Previous = 1;
extern int Sto_Secu_Faux_Signal = 0;


and the stochastics calculation

   //+------------------------------------------------------------------+
   //| Récupération des Stochastics                                     |
   //+------------------------------------------------------------------+ 
   Stochastic_Current = iStochastic(Symbol(),Sto_Periode,k_Periode,d_Periode,Slowing,Sto_Method,Sto_Price,MODE_MAIN,Sto_Shift);
   Stochastic_Previous = iStochastic(Symbol(),Sto_Periode,k_Periode,d_Periode,Slowing,Sto_Method,Sto_Price,MODE_MAIN,Sto_Shift + Sto_Shift_Previous);
   Stochastic_Signal_Current = iStochastic(Symbol(),Sto_Periode,k_Periode,d_Periode,Slowing,Sto_Method,Sto_Price,MODE_SIGNAL,Sto_Shift);
   Stochastic_Signal_Previous = iStochastic(Symbol(),Sto_Periode,k_Periode,d_Periode,Slowing,Sto_Method,Sto_Price,MODE_SIGNAL,Sto_Shift + Sto_Shift_Previous);

If you see something that can be improved...

 




If you look to the sell trades of your EA Then why do you think it is trading two bars same crossing

The bar before first sell green line > red line stochastic

at first bar with sell at opening still green line > red line

price moves down red line becomes > green line sell opened

pinbar==> we see price close almost at openingprice green line again > red line

and you have had a sell trade while it looks there was no crossing

.

Taking Stochastic current at bar 0 and recalculate every tick will make it trade this way

If you don't want this to happen you have to wait for bar closing and look that time for opening new trade

If you set extern int Sto_Shift = 0; //it trades at bar 0

If you set extern int Sto_Shift = 1 ; // you will trade on the values at close bar

 
      {
      if (Sto_Take_Profit == 0)
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total=OrdersTotal();
         }
         
      if ((Sto_Take_Profit > 0) && (Bars != BarsCount))
         {
         Vente=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-(Sto_Take_Profit/10000),"Mono Stochastic : Vente",Magic_Number,0,Red);
         Total=OrdersTotal();
         BarsCount=Bars;
         }
      }

What improvements do you think you can make on your OrderSend() function
 

My Sto_Take_Profit is bad

I have seen examples with "point", but it is not very clear and the result was incorrect

double MarketInfo( string symbol, int type) 
double point =MarketInfo("EURUSD",MODE_POINT);