Download MetaTrader 5

How to stop EA from sending orders.

To add comments, please log in or register
Sean Meagher
236
Sean Meagher  

Hello everyone.  Brand new coding baby here...haha.  I just got my first EA to place its first order...hurrrray.  However, I can't get the dang thing to stop placing the order on every tick.  I tried this "if (t < 1)" snippet that looked like it was used to stop the function from repeating. Not working.  Any advice would be appreciated.


 extern double Lots = 1;
extern double Slippage = 20;
extern double TP = 100;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int ticket1;
   int t = 0;
  
      if (t < 1)
  
      {
  
      ticket1 = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,NULL,Bid - TP * Point,NULL,1);
      t = t + 1;
     
      }
     
  
  }

honest_knave
Moderator
2326
honest_knave  
if(OrdersTotal()<1)
That is a very rudimentary approach, and you'll be wanting to look into a more sophisticated approach using magic numbers and filtering by symbol name etc. once you get the hang of things.
whroeder1
15934
whroeder1  
Sean Meagher:I can't get the dang thing to stop placing the order on every tick
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2.    int ticket1;
       int t = 0;
    Non-static/non-global variables loose their value when they go out of scope.
  3. Even if you change them, it would never trade again. Use a OrderSelect loop. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

  4. ticket1 = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,NULL,Bid - TP * Point,NULL,1);
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

    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.

Sean Meagher
236
Sean Meagher  
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. Non-static/non-global variables loose their value when they go out of scope.
  3. Even if you change them, it would never trade again. Use a OrderSelect loop. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

  4. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

    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.


Thanks whroeder1...I'll attach code here.

void OnTick()
  {
  
   int ticket1;
   int t = 0;
   
      if (t < 1)
    
         {
   
         ticket1 = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,NULL,Bid - TP * Point,NULL,1);
         t = t + 1;
      
         }
  
  }

More specifically my question here is :

If "t"starts out as zero, then "if (t < 1) would be true, so the order would get sent.

As the order is sent, it is my understanding that "t = t +1" would increase the value of "t" to 1.

Therefore on the next tick, the "if (t < 1)" would no longer be true, so the order would not get sent a second time.

However when I run it the order is placed again on the second tick, and every tick thereafter.

As I am learning I thought the code inside the {} would not get executed if (1 < 1) which is false.

Thanks again.

honest_knave
Moderator
2326
honest_knave  

As @whroeder1 has already said, your variable "t" is not remembered between ticks. Each tick, it resets to 0.

If you want to remember the value of "t", you need to use a static variable or a variable with global scope.

Or use the OrdersTotal() line I posted above.

Sean Meagher
236
Sean Meagher  
honest_knave:

As @whroeder1 has already said, your variable "t" is not remembered between ticks. Each tick, it resets to 0.

If you want to remember the value of "t", you need to use a static variable or a variable with global scope.

Or use the OrdersTotal() line I posted above.


ahhhhhh...thank you for dumbing this down for me.  I think I got it.
Marco vd Heijden
Moderator
5563
Marco vd Heijden  
Sean Meagher:

As I am learning I thought the code inside the {} would not get executed if (1 < 1) which is false.

Thanks again.

But t is never 1.

t will always be 0.

   int t = 0; // set t to zero
whroeder1
15934
whroeder1  
Sean Meagher:


"t"starts out as zero, then "if (t < 1) would be true, so the order would get sent.

As the order is sent, it is my understanding that "t = t +1" would increase the value of "t" to 1.

Correct. Then next tick
"t"starts out as zero, then "if (t < 1) would be true, so the order would get sent.

As the order is sent, it is my understanding that "t = t +1" would increase the value of "t" to 1.

Correct. Then next tick ...
Sean Meagher
236
Sean Meagher  
whroeder1:
Correct. Then next tickCorrect. Then next tick ...
Very well explained.  So I am assuming that all values go back to there original initialization for each new tick.  I'm just curious now if there is a way to change the value of a variable as the ticks come in.
honest_knave
Moderator
2326
honest_knave  
Sean Meagher:
Very well explained.  So I am assuming that all values go back to there original initialization for each new tick.  I'm just curious now if there is a way to change the value of a variable as the ticks come in.

whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. Non-static/non-global variables loose their value when they go out of scope.
  3. Even if you change them, it would never trade again. Use a OrderSelect loop. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

  4. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.

    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.


honest_knave:

As @whroeder1 has already said, your variable "t" is not remembered between ticks. Each tick, it resets to 0.

If you want to remember the value of "t", you need to use a static variable or a variable with global scope.

Or use the OrdersTotal() line I posted above.

Sean Meagher
236
Sean Meagher  
honest_knave:



Thank you very much for your patience.  you have been very helpful.
To add comments, please log in or register