Need an EA that can open more than one trade

 

Hello traders,

I need your expertise to find an EA that can open more than one trade ( if needed by the criteria).

For example, the Buy order should be opened if the Stochastic is crossing up the 20 level and close the trade when the Stochastic reaches 80 level. Also the EA should open a Sell order if the Stochastic is crossing down the 80 level and close the trade when the Stochastic reaches 20 level.

Please look at the picture attached to see opportunities to open a  

Actually I have written the code but it does not open more than one trade at a time. I appreciate if any one can help me out. 

-------------------------------

input double TakeProfit    =100000;
input double Lots          =0.1;
input double TrailingStop  =0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double CurrentSTO,PreviousSTO;
   int    cnt,ticket,total;
//---
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
   if(Bars<100)
     {
      Print("bars less than 100");
      return;
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return;
     }
//--- to simplify the coding and speed up access data are put into internal variables

    CurrentSTO=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,1);
    PreviousSTO=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,2);
    
//--------------------------------------------------------------------------      
   
   total=OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      //--- check for long position (BUY) possibility
      if(CurrentSTO>20 && PreviousSTO<20)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point," ",16002,0,Green); 
         
         //Ask+TakeProfit*Point
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }
      //--- check for short position (SELL) possibility
      if(CurrentSTO<80 && PreviousSTO>80)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," ",16002,0,Red);
         //Bid-TakeProfit*Point        
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());
        }       
      //--- exit from the "no opened orders" block
      total = 0 ;
      return;
     }
//--- it is important to enter the market correctly, but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- should it be closed?
            if(CurrentSTO>80)
              {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_BID),0,Green);
               //TakeProfit==(MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice());
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {
            //--- should it be closed?
            if(CurrentSTO<20)
              {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_ASK),0,Red);
               //TakeProfit==(MarketInfo(Symbol(),MODE_ASK)-OrderOpenPrice());
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }
//---
  }
//+------------------------------------------------------------------+
Files:
Capture12.JPG  37 kb
 
mjstock78:

Hello traders,

I need your expertise to find an EA that can open more than one trade ( if needed by the criteria).

For example, the Buy order should be opened if the Stochastic is crossing up the 20 level and close the trade when the Stochastic reaches 80 level. Also the EA should open a Sell order if the Stochastic is crossing down the 80 level and close the trade when the Stochastic reaches 20 level.

Please look at the picture attached to see opportunities to open a  

Actually I have written the code but it does not open more than one trade at a time. I appreciate if any one can help me out. 

-------------------------------

   total=OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
The problem it is only allowed to open when OrdersTotal()==0
 
Marco vd Heijden:
The problem it is only allowed to open when OrdersTotal()==0
Any Idea how I can fix this code?
 
mjstock78:
Any Idea how I can fix this code?

Fix in what sense?

If you allow for more order it will simply open up more orders at the exact moment the first order would be opened.

That would be the same as simply raising the Lotsize for that one order.

 
Marco vd Heijden:

Fix in what sense?

If you allow for more order it will simply open up more orders at the exact moment the first order would be opened.

That would be the same as simply raising the Lotsize for that one order.

I would like to open more than one trade whenever the open trade condition meets. Please look at the attached picture in the first comment to get more clarity. Thanks.
 
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
input double TakeProfit    =100000;
input double Lots          =0.1;
input double TrailingStop  =0;
sinput int maxorders=10;// Max Orders

int order;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double CurrentSTO,PreviousSTO;
   int    cnt,ticket,total;
//---
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
   if(Bars<100)
     {
      Print("bars less than 100");
      return;
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return;
     }
//--- to simplify the coding and speed up access data are put into internal variables

   CurrentSTO=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,1);
   PreviousSTO=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,2);

//--------------------------------------------------------------------------      

   total=OrdersTotal();
   if(total<maxorders)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      //--- check for long position (BUY) possibility
      if(CurrentSTO>20 && PreviousSTO<20)
        {
         if(order!=1)
           {
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point," ",16002,0,Green);

            //Ask+TakeProfit*Point
            if(ticket>0)
              {
               order=1;
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("BUY order opened : ",OrderOpenPrice());
              }
            else
               Print("Error opening BUY order : ",GetLastError());
            return;
           }
        }
      //--- check for short position (SELL) possibility
      if(CurrentSTO<80 && PreviousSTO>80)
        {
         if(order!=2)
           {
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," ",16002,0,Red);
            //Bid-TakeProfit*Point        
            if(ticket>0)
              {
               order=2;
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("SELL order opened : ",OrderOpenPrice());
              }
            else
               Print("Error opening SELL order : ",GetLastError());
           }
        }
      //--- exit from the "no opened orders" block
      total=0;
      return;
     }
//--- it is important to enter the market correctly, but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- should it be closed?
            if(CurrentSTO>80)
              {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_BID),0,Green);
               //TakeProfit==(MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice());
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {
            //--- should it be closed?
            if(CurrentSTO<20)
              {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_ASK),0,Red);
               //TakeProfit==(MarketInfo(Symbol(),MODE_ASK)-OrderOpenPrice());
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }
//---
  }
//+------------------------------------------------------------------+
 

Use a 'trade once per bar' flag.

static datetime prevTime;
if(Time[0]!=prevTime)
  {
   //---
   if(AccountFreeMargin()<(1000*Lots))
     {
      Print("We have no money. Free Margin = ",AccountFreeMargin());
      return;
     }
   //--- check for long position (BUY) possibility
   if(CurrentSTO>20 && PreviousSTO<20)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point," ",16002,0,Green); 
      
      //Ask+TakeProfit*Point
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
           {
            Print("BUY order opened : ",OrderOpenPrice());
            prevTime=Time[0];
           }
        }
      else
         Print("Error opening BUY order : ",GetLastError());
      return;
     }
   //--- check for short position (SELL) possibility
   if(CurrentSTO<80 && PreviousSTO>80)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," ",16002,0,Red);
      //Bid-TakeProfit*Point        
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
           {
            Print("SELL order opened : ",OrderOpenPrice());
            prevTime=Time[0];
           }
        }
      else
         Print("Error opening SELL order : ",GetLastError());
     }       
   //--- exit from the "no opened orders" block
   return;
  }
 
Marco vd Heijden:
Thanks Marco , but it does no work the way I expect. It opens trade when buy criteria meets but doesn't close them when close criteria meets. For example it Opens trade every time that Stochastic is crossing up the 20 level but does not close them when Stochastic is above 80 level until it reaches the maxorder numbers. If you look at the attached picture in the first comment, you will see in that picture, only two Sell trades are opened and as soon as the Stochastic crosses the 20 level both Sell trades should be closed. Other cases may happen that Stochastic crosses down the 80 level couple of times (e.g. 4 times) then goes down and fall below 20 level,that's when all sell trades (those 4 trades that are open) should be closed. Hope that I have clarified everything. Also I highly appreciate your kind prompt replies.
 
Ernst Van Der Merwe:

Use a 'trade once per bar' flag.

Thanks a lot Ernst, you solved the problem. It works perfectly.
Reason: