How to make it possible on EA to allow only one trade?

 

Hi guys,


Whenever a previous order was closed, immediately a new order took place (in same bar), yes i know the new order meets entering a new position...


However, I would like to let only one trade (I am assuming orderclosed also a trade) , i mean whenever a trade is closed, none of the orders should take place until the current candle closes...



what should I do to interfere new trades on same bar?


Sincerely Thank You 

 

Get the time that the order was closed, check if it is >= the current bar open time.

If it is, don't open another trade.

 
yyencilek:

Hi guys,


Whenever a previous order was closed, immediately a new order took place (in same bar), yes i know the new order meets entering a new position...


However, I would like to let only one trade (I am assuming orderclosed also a trade) , i mean whenever a trade is closed, none of the orders should take place until the current candle closes...



what should I do to interfere new trades on same bar?


Sincerely Thank You 

there are several ways to do it

search forums "only one trade per bar" or smth like this

 
Keith Watford:

Get the time that the order was closed, check if it is >= the current bar open time.

If it is, don't open another trade.

thank u very much, i will try coding ...


  int magicNB =  479;

  double mson=   iMA(NULL,0,200,1,MODE_SMA,PRICE_CLOSE,0);  

  datetime closing= OrderCloseTime();

  datetime openbartime= Time[0];

  int ticket;



//my Buy order:

 

if(OrderSelect(ticket ,SELECT_BY_TICKET,MODE_HISTORY)==true)

  {

    if (closing>= openbartime) 

  {

    //buy 

  if(Close[1]> Open[1] && Close[1]> mson && OrdersTotal()<1) 

  {

  ticket= OrderSend(NULL,OP_BUY,1,Ask,50,mson - 600*_Point,Ask+8000*_Point,NULL,magicNB);

  if(ticket<0) Print("OrderSend failed with error #",GetLastError()); 

  }
 

Hi,


I tried below code to prevent repeated orders but it even could not get the order executed...


especially is highlighted code is wrong? since i wanted  "Get the time that the order was closed and check if it is >= the current bar"


sincerely thank u 



Code:

  int magicNB =  479;

  double mson=   iMA(NULL,0,200,1,MODE_SMA,PRICE_CLOSE,0);  

  datetime closing= OrderCloseTime();

  datetime openbartime= Time[0];

  int ticket;


//my Buy order:

 

if(OrderSelect(ticket ,SELECT_BY_TICKET,MODE_HISTORY)==true)

  {

    if (closing>= openbartime) 

  {

    //buy 

  if(Close[1]> Open[1] && Close[1]> mson && OrdersTotal()<1) 

  {

  ticket= OrderSend(NULL,OP_BUY,1,Ask,50,mson - 600*_Point,Ask+8000*_Point,NULL,magicNB);

  if(ticket<0) Print("OrderSend failed with error #",GetLastError()); 

  }

 
  1.   datetime closing= OrderCloseTime();

    You can not use any Trade Functions until you first select an order.

  2.   int ticket;
    if(OrderSelect(ticket ,SELECT_BY_TICKET,MODE_HISTORY)==true)
    The pool is irrevalent when you select by ticket.
  3. You don't have a ticket number to select any order.
  4. You don't test if the selected ticket is open, pending, closed, or deleted.
  5. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  6.  ticket= OrderSend(NULL,OP_BUY,1,Ask,50,mson - 600*_Point,Ask+8000*_Point,NULL,magicNB);

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
  7. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    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.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
yyencilek:

Please edit your post and

use the code button (Alt+S) when pasting code

Reason: