How To Limit To One Trade Per Stochastic Crossing

 

Hello,

Newbie, I created my Stochastic EA to learn MQL4. Sure, my code could be more beautiful, but it works.

Below the code for opening a Buy and closing it.

Seeking to improve, my question is as follows :

You can view on the same bar, I get several Trades when I have a small "TakeProfit" (exemple with 5 pips on 1hour chart).

I am looking for a solution to limit to only one transaction between two crossings stochastic.

I you have a idea.


Thanks




      if (
         (Total==0) && 
         (Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
         (Stochastic_Previous < Stochastic_Signal_Previous) 
         )
         {
         if (Sto_Take_Profit == 0)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Mono Stochastic : Achat",0,0,Blue);
            Total=OrdersTotal();
            }
         if (Sto_Take_Profit > 0)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(Sto_Take_Profit/10000),"Mono Stochastic : Achat",0,0,Blue);
            Total=OrdersTotal();
            }
         }

      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))
                  {
                  OrderClose(OrderTicket(), Lots, Bid, 3, Red);
                  Total=OrdersTotal();
                  }
               }
            }
         }




 

The variable total might be losing its value in each running of start() function. It depends how it is initialized in your code (not shown). To solve the issue, try making the assignment before the if(...) conditions:


       Total=OrdersTotal();     // <----------  HERE
       if (
         (Total==0) && 
         (Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
         (Stochastic_Previous < Stochastic_Signal_Previous) 
         )
         {
         if (Sto_Take_Profit == 0)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Mono Stochastic : Achat",0,0,Blue);
            
            }
         if (Sto_Take_Profit > 0)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(Sto_Take_Profit/10000),"Mono Stochastic : Achat",0,0,Blue);
            }
         }

      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))
                  {
                  OrderClose(OrderTicket(), Lots, Bid, 3, Red);
                  Total=OrdersTotal();
                  }
               }
            }
         }


Bye

AM.

 

@ arsouille: Hi arsouille! Enter code as instructed below and you should be happy.

// define "BarsCount" as an intiger data type and assign it the value of 0;

int BarsCount=0; 

// place the above code where you would define your variables. (underneath #property copyright "Copyright 2012, MetaQuotes Software Corp."
//                                                                         #property link      "http://www.metaquotes.net" );
// and above the int init() function;



// this code goes where you will place your orders, so if you were going to create parameters for your ordersend signal, this code would go in there with your signal parameters;

if(Bars>BarsCount){

// the code below goes underneath your order send function, ( I would recommend placing this directly under the ordersend() function) ;

BarsCount=Bars;

// place another comment after you have inserted this code into your ea to confirm that your "multiple order" problem has been solved.

 

@ arsouille: Also, it wouldn't hurt to be sure that you have added the below code to your ordersend parameters to be SURE that you are opening only one order...

if(OrdersTotal()<1){
  {
   // send a buy order;
  }
}

// the code above goes right in there with "if(Bars>BarsCount){", where your ordersend signal parameters are located;

 

Thank you for your answers,

Here is the my code a little more complete with the "Total = OrdersTotal ();" which was a little earlier in my code.

I joined your advice (BarsCount) and this works better generally, except sometimes as in the picture, two "Sells" on


Thanks again, I advance




Total=OrdersTotal();
   
      //+------------------------------------------------------------------+
      //|                  Buy and Close Buy order                         |
      //+------------------------------------------------------------------+
      if (
         (Total==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",0,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",0,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))
                  {
                  OrderClose(OrderTicket(), Lots, Bid, 3, Red);
                  Total=OrdersTotal();
                  }
               }
            }
         }
   
      //+------------------------------------------------------------------+
      //|                  Sell and Close Sell order                       |
      //+------------------------------------------------------------------+ 
      if (
         (Total==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",0,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",0,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))
                  {
                  OrderClose(OrderTicket(), Lots, Ask, 3, Blue);
                  Total=OrdersTotal();
                  }
               }
            }
         }


 

Your code might be looking it is working well but running on an account you will find out it has bugs you don't see with backtesting...

For beginning you have to change.......

this......

       Total=OrdersTotal();     // <----------  HERE
       if (
         (Total==0) && 

.

What trades do you count ????

 
arsouille:

Hello,

Newbie, I created my Stochastic EA to learn MQL4. Sure, my code could be more beautiful, but it works.

Below the code for opening a Buy and closing it.

Seeking to improve, my question is as follows :

You can view on the same bar, I get several Trades when I have a small "TakeProfit" (exemple with 5 pips on 1hour chart).

I am looking for a solution to limit to only one transaction between two crossings stochastic.

I you have a idea.


Thanks


try this:

Up at the top where you declare your veriables put:

---->

static bool ITradedOnThisBar;

         if (Sto_Take_Profit == 0 && ITradedOnThisBar != Bars)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Mono Stochastic : Achat",0,0,Blue);
            ITradedOnThisBar = Bars;
            }
         if (Sto_Take_Profit > 0   && ITradedOnThisBar != Bar)
            {
            Achat=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+(Sto_Take_Profit/10000),"Mono Stochastic : Achat",0,0,Blue);
            
            ITradedOnThisBar = Bars;
            }
         }


***** more information on my topic.  https://forum.mql4.com/46774 
if the above conditions are not the same one, define 2 various boolean and use each one for his own trading condition.
 

deVries,

I use "Total=OdersTotal" because I prefer only one active trade (pending or open) in my real account.

Why this condition could generate a bug?

Comment out this condition does not change my backtest


Total=OrdersTotal();

      if (
         //(Total==0) && 
         (Stochastic_Current > (Stochastic_Signal_Current + Sto_Secu_Faux_Signal)) && 
         (Stochastic_Previous < Stochastic_Signal_Previous)


hmrt135, your solution is similar than WhooDoo22,thanks

 

What do you count.......


int OrdersTotal( ) 
Returns market and pending orders count.
 

deVries,

Sorry if I do not understand, my English and my knowledge in MQL4 are too tight.

For me, when I ask "OrdersTotal" in my variable Total, I save the number of Open and pending trade in my account (https://book.mql4.com/functions/trading)

Is it a mistake to write this ?

Made in, if I remove this condition, the unfolding is not correct. Several operations can simultaneously open



My last code (integrate Magic Number and BarsCount when I use a TakeProfit)

I'll try to find an alternative

Total=OrdersTotal();
   
      //+------------------------------------------------------------------+
      //|                  Buy and Close Buy order                         |
      //+------------------------------------------------------------------+
      if (
         //(Total==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))
                  {
                  OrderClose(OrderTicket(), Lots, Bid, 3, Red);
                  Total=OrdersTotal();
                  }
               }
            }
         }
   
      //+------------------------------------------------------------------+
      //|                  Sell and Close Sell order                       |
      //+------------------------------------------------------------------+ 
      if (
         //(Total==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();
                  }
               }
            }
         }





 
deVries:
int OrdersTotal( ) 
Returns market and pending orders count.

What do you count.......




Answer you count all market trades and pending orders means if you have the EA running for EURUSD and you have open a GBPUSD trade then OrdersTotal() is counting also the GBPUSD trade and your EA won't open a new trade even if there is no trade EURUSD.... so you have to check magicnumber and Symbol if you count the trades of your EA ....


https://www.mql5.com/en/forum/141276 see comment there WHRoeder
Reason: