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 identifiedThe problem it is only allowed to open when OrdersTotal()==0
The problem it is only allowed to open when OrdersTotal()==0
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.
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.
//+------------------------------------------------------------------+ //| 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; }
Use a 'trade once per bar' flag.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.
-------------------------------