ony one trade per bar

To add comments, please log in or register
Daniel Bouchard
61
Daniel Bouchard  
void CheckForOpen()
  {
   double ma;
   int    res;

//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
   if(Low[0]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+stoploss*Point,Bid-takeprofit*Point,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(High[0]>ma)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-stoploss*Point,Ask+takeprofit*Point,"",MAGICMA,0,Blue);
      return;
     }
//---
  }

how to have this code to only take one trade per bar

whroeder1
18064
whroeder1  
Daniel Bouchard: how to have this code to only take one trade per bar
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. When you open an order remember the bar time in a static/global variable. Don't check for a new open if they still match.

  3. OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+stoploss*Point,Bid-takeprofit*Point,"",MAGICMA,0,Red);
    OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-stoploss*Point,Ask+takeprofit*Point,"",MAGICMA,0,Blue);
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  4. res=OrderSend(
    res=OrderSend(
    Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
Daniel Bouchard
61
Daniel Bouchard  

thank you for your help.


could this make sense?

 res=OrderSend(Symbol(),OP_BUY,lot2,Ask,3,Bid-stoploss*Point,0,comment,MAGICMA,0,Blue);
      return;


res=OrderSend(Symbol(),OP_SELL,lot3,Bid,3,Ask+stoploss2*Point,0,comment,MAGICMA,0,Red);
      return;
int somefunc()
  {
   static int time=OrderSend(iTime());
   ...
   return(time);

 if time=current(iTime())
break;
  }
To add comments, please log in or register