How to stop EA from sending orders.

 

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;
     
      }
     
  
  }

 
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.
 
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.

 
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.

 

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.

 
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.
 
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
 
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 ...
 
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.
 
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.

 
honest_knave:



Thank you very much for your patience.  you have been very helpful.
Reason: