How to open new order or pending order immediately? - page 2

 
01005379:


Yes, it probably would, but why would you want to create an endless loop?

If you want to open an order as soon as you attach the EA to the chart, then putting the order in init should work.

But if you put the order in an endless loop, it may open order after order(depending on your code

Also it would have to be coded so that it doesn't repeat the OrderSend if you change time-frames, restart the platform etc

 
Thank you for the answer! EA has written in code to prevent opening new orders. What is the other way (without endless loop) to open order if there is no price change? I'm looking what WHRoeder write but need some help with timer, OnTimer(), EventSetTimer(),...
 
01005379:
Thank you for the answer! EA has written in code to prevent opening new orders. What is the other way (without endless loop) to open order if there is no price change? I'm looking what WHRoeder write but need some help with timer, OnTimer(), EventSetTimer(),...

int OnInit()
  {
//--- create timer
   EventSetTimer(1); // 1 second
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   // YOUR CODE HERE, executed every second
  }
 
Will try it at market open. Thank you!
 
extern string note1="***************************************************";
extern int Ura=11;
extern int Minuta=0;
extern int Sekunda1=20;
extern int Sekunda2=59;
extern double Razlika=10.0;
extern double SL=10.0;
extern double Loti=1.0;
extern string note2="***************************************************";
double Spread=3;
extern int Ura_zapre_pending=11;
extern int Minuta_zapre_pending=3;
extern int Cas_zapre_trejd=5;
extern double Loti_zapre_trejd=0.5;
bool Trading_ON_OFF=true;
bool Set_buy=true;
bool Set_sell=true;
int MagicNumber = 2910986;
double places;
color  FontColor=White;
color  FontColor1=Blue;
int    FontSize=7;
int    FontSize1=30;
string FontType="Arial";
string FontType1="Arial";
string note6="Upper left=0; Upper right=1";
string note7="Lower left=2; Lower right=3";
int    WhatCorner=0;
string trading;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   if (Digits==3 || Digits==5) 
      places=10.0; 
   else 
      places=1.0;
   //return(0);
//--- create timer
   EventSetTimer(1); // 1 second
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
   ObjectDelete("Pips");
   ObjectDelete("Pips1");
   ObjectDelete("Pips2");
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   // YOUR CODE HERE, executed every second
   int ticket;
   bool delete_pending, zapre_buy, zapre_sell;
   
   string UraString=DoubleToStr(Ura,0);//Profit,2
   string MinutaString=DoubleToStr(Minuta,0);
   string Sekunda1String=DoubleToStr(Sekunda1,0);
   string Sekunda2String=DoubleToStr(Sekunda2,0);
   string RazlikaString=DoubleToStr(Razlika,0);
   string SLString=DoubleToStr(SL,0);
   string LotiString=DoubleToStr(Loti,1);
   string Ura_zapre_pendingString=DoubleToStr(Ura_zapre_pending,0);
   string Minuta_zapre_pendingString=DoubleToStr(Minuta_zapre_pending,0);
   string Cas_zapre_trejdString=DoubleToStr(Cas_zapre_trejd,0);
   string Loti_zapre_trejdString=DoubleToStr(Loti_zapre_trejd,1);
   
   ObjectCreate("Pips", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Pips","     "+UraString+":"+MinutaString+":"+Sekunda1String+"/"+Sekunda2String+"    Razlika: "+RazlikaString+"    SL: "+SLString+"    Loti Pending: "+LotiString, FontSize, FontType, FontColor);
   ObjectSet("Pips", OBJPROP_CORNER, WhatCorner);
   ObjectSet("Pips", OBJPROP_XDISTANCE, 210);
   ObjectSet("Pips", OBJPROP_YDISTANCE, 2);
   
   ObjectCreate("Pips1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Pips1","         Zapre pending: "+Ura_zapre_pendingString+":"+Minuta_zapre_pendingString, FontSize, FontType, FontColor);
   ObjectSet("Pips1", OBJPROP_CORNER, WhatCorner);
   ObjectSet("Pips1", OBJPROP_XDISTANCE, 440);
   ObjectSet("Pips1", OBJPROP_YDISTANCE, 2);
   
   ObjectCreate("Pips2", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Pips2","         Zapre trejd: "+Cas_zapre_trejdString+"min    Loti zapre trejd: "+Loti_zapre_trejdString, FontSize, FontType, FontColor);
   ObjectSet("Pips2", OBJPROP_CORNER, WhatCorner);
   ObjectSet("Pips2", OBJPROP_XDISTANCE, 540);
   ObjectSet("Pips2", OBJPROP_YDISTANCE, 2);
   
   if(Trading_ON_OFF){
      if(Set_buy){
         if (Hour()==Ura && Minute()==Minuta && Seconds()>=Sekunda1 && Seconds()<=Sekunda2){
            ticket=-1;
            while (ticket<0){   
               RefreshRates();
               ticket=OrderSend(Symbol(),OP_BUYSTOP,Loti,Ask+(Razlika*places)*Point,5*places,Ask+(Razlika*places)*Point-(SL*places)*Point,0, "News_EA",MagicNumber,0,Green);            
               //Sleep(100);
            }
            if(ticket>0){
               Set_buy=false;
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUYSTOP placed.");
            }
            else{
               Print("Error sending BUYSTOP order : ",GetLastError()); 
               //return(0); 
            }
         }
      }

      if(Set_sell){
         if(Hour()==Ura && Minute()==Minuta && Seconds()>=Sekunda1 && Seconds()<=Sekunda2){      
            ticket=-1;
            while (ticket<0){
               RefreshRates();
               ticket=OrderSend(Symbol(),OP_SELLSTOP,Loti,Bid-(Razlika*places)*Point,5*places,Bid-(Razlika*places)*Point+((SL+Spread)*places)*Point,0, "News_EA",MagicNumber,0,Red);            
               //Sleep(100);
            }
            if(ticket>0){
               Set_sell=false;
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELLSTOP palced.");
            }
            else{
               Print("Error sending SELLSTOP order : ",GetLastError()); 
               //return(0); 
            }
         }
      }
      
      if(Hour()==Ura_zapre_pending && Minute()>=Minuta_zapre_pending){
         for(int cnt4=0;cnt4<OrdersTotal();cnt4++){
            OrderSelect(cnt4,SELECT_BY_POS,MODE_TRADES);
            if(  ((OrderType()==OP_SELLSTOP)||(OrderType()==OP_BUYSTOP)) && (OrderSymbol()==Symbol()) ){
               delete_pending=false;
               while(!delete_pending){
                  RefreshRates();
                  delete_pending=OrderDelete(OrderTicket());              
                  //Sleep(1000);
               }
            }
         }
      }
   
         for(int cnt5=0;cnt5<OrdersTotal();cnt5++){
            OrderSelect(cnt5,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_BUY){   
               if(  (TimeCurrent()-OrderOpenTime())>=Cas_zapre_trejd*60 && OrderSymbol()==Symbol() && OrderLots()>Loti_zapre_trejd ){
                  zapre_buy=false;
               while(!zapre_buy){
                  RefreshRates();
                  zapre_buy=OrderClose(OrderTicket(), Loti_zapre_trejd, Bid, 3*places,Blue);               
                  //Sleep(1000);
               }
            }
         }
         else if(OrderType()==OP_SELL){
            if(  (TimeCurrent()-OrderOpenTime())>=Cas_zapre_trejd*60 && OrderSymbol()==Symbol() && OrderLots()>Loti_zapre_trejd ){
               zapre_sell=false;
               while(!zapre_sell){
                  RefreshRates();
                  zapre_sell=OrderClose(OrderTicket(), Loti_zapre_trejd, Ask, 3*places,Blue);               
                  //Sleep(1000);
               }
            }
         }      
      }
   }
}
Here is my code. EA still opening orders only when price change...
 
Any help? Thank you!
 
01005379:
Here is my code. EA still opening orders only when price change...


How do you know?

Price is changing all the time (usually)

 
I tested it. How else could I know. Price is often unchanged for more then 10 seconds and it is easy to test this.
 

I ran a test with this code. I disconnected from the internet, so no new ticks possible.

int OnInit()
  {
//--- create timer
   EventSetTimer(5);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   OrderSend(Symbol(),OP_BUYSTOP,1,1.5000,50,0,0,NULL,0,0,0);
  }
//+------------------------------------------------------------------+

Timer worked perfectly

03:10:41.156 Expert testtimer EURUSD,M30: loaded successfully
03:11:32.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:11:37.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:11:42.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:11:47.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:11:52.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:11:57.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:02.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:07.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:12.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:17.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:22.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:27.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:32.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:37.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:42.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:47.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:52.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:12:57.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:02.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:07.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:12.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:17.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:22.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:27.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:32.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:37.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:42.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:47.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:52.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:13:57.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:02.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:07.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:12.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:17.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:22.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:27.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:32.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:37.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:42.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:47.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:52.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:14:57.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:15:02.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:15:07.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
03:15:12.968 '3940924': order buy stop 1.00 EURUSD opening at 1.50000 sl: 0.00000 tp: 0.00000 failed [Common error]
 
Possible. But why my code does not work as I want? Any idea?