Download MetaTrader 5

Activate pending order when the bar close under him.

To add comments, please log in or register
domdix27
65
domdix27  

Hi I have a question .. How can I do this?

I want place pending order but when the bar touch it the order must not active but only when the bar close under him.

This is my code:

void OnStart()
  {
   int    ticket,expiration;
   double point;
   double price;
   double stop;
//----
   point=MarketInfo(Symbol(),MODE_POINT);
   expiration=CurTime()+PERIOD_D1*60;
//----

  



     price = Bid-100*point;
     stop=Bid + 20 * Point;
      ticket=OrderSend(Symbol(),OP_SELLSTOP,0.10,price,3,stop,price-40*Point,"some comment",NULL,expiration,Green);

     
      //---- 10 seconds wait
      Sleep(10000);
      
           

     
  
     price = Ask+100*point;
     stop=Ask - 20 * Point;
      ticket=OrderSend(Symbol(),OP_BUYSTOP,0.10,price,3,stop,price+40*Point,"some comment",NULL,expiration,Green);

      
      //---- 10 seconds wait
      Sleep(10000);
     
}


//----
   return(0);
  }

I try with this

int BarCount;
 
init()
 {
BarCount = Bars - 1;
}
 
start()
{


}


//then if 
if(BarCount != Bars){

OrderSend(...)
}

but it doesn t work for me please help

whroeder1
17093
whroeder1  
domdix27: I want place pending order but when the bar touch it the order must not active but only when the bar close under him.
  1. Can't be done; pending orders are always active and become open orders when touched.

  2. price = Bid-100*point;
    stop=Bid + 20 * Point;
    ticket=OrderSend(Symbol(),OP_SELLSTOP
    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.)

  3. if(BarCount != Bars){
    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You must RefreshRates after Sleep to use any pre-defined variables and series arrays.
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference
domdix27
65
domdix27  
whroeder1:
  1. Can't be done; pending orders are always active and become open orders when touched.

  2. 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.
    • 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.)

  3. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

  4. You must RefreshRates after Sleep to use any pre-defined variables and series arrays.

thanks for the corrections, I fix the script as soon as possible... so there isn t a way to do what i want? (even without pending order)

Alain Verleyen
Moderator
35118
Alain Verleyen  
domdix27:

thanks for the corrections, I fix the script as soon as possible... so there isn t a way to do what i want? (even without pending order)

Of course it's possible but not with pending orders.
domdix27
65
domdix27  
Alain Verleyen:
Of course it's possible but not with pending orders.

Please tell me how

Fernando Carreiro
3942
Fernando Carreiro  
domdix27: Please tell me how

With Market orders (instead of Pending), supported by code/logic in your EA to implement the functionality that you want. It is not always easy to explain "how" to beginner coders, because it requires more skill and knowledge of coding and logic that a beginner does not have.

That is why many times we suggest to beginners to use the Freelance section, because the time and effort required to explain all the intricate details is quite a bit.

Please don't be angry if we don't give you a clear, short, direct, easy-to-understand solution, but it is NEVER that simple!

EDIT: It is one of those cases of either you know how or you don't and you just have to take some time to develop your skills and knowledge until you do. Sorry, I am not trying to be mean - its just how it is! All we can try to do is point you in the right direction!

domdix27
65
domdix27  
Fernando Carreiro:

With Market orders (instead of Pending), supported by code/logic in your EA to implement the functionality that you want. It is not always easy to explain "how" to beginner coders, because it requires more skill and knowledge of coding and logic that a beginner does not have.

That is why many times we suggest to beginners to use the Freelance section, because the time and effort required to explain all the intricate details is quite a bit.

Please don't be angry if we don't give you a clear, short, direct, easy-to-understand solution, but it is NEVER that simple!

EDIT: It is one of those cases of either you know how or you don't and you just have to take some time to develop your skills and knowledge until you do. Sorry, I am not trying to be mean - its just how it is! All we can try to do is point you in the right direction!


Thanks but I'm headstrong and I do this code ... why order send don t work?

#property copyright "Poul Trade Forum"
#property link      "http://forex.kbpauk.ru/"

#property indicator_chart_window
//#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  MediumBlue
#property indicator_color2  DarkGreen
#property indicator_color3  FireBrick
#property indicator_color4  DarkGreen
#property indicator_color5  FireBrick
#property indicator_color6  DarkGreen
#property indicator_color7  FireBrick
#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1
#property  indicator_width4  1
#property  indicator_width5  1
#property  indicator_width6  1
#property  indicator_width7  1
//---- input parameters

//---- buffers
double PBuffer[];
double S1Buffer[];
double R1Buffer[];
double S2Buffer[];
double R2Buffer[];
double S3Buffer[];
double R3Buffer[];
string Pivot = "Pivot Point", Sup1 = "S 1", Res1 = "R 1";
string Sup2="S 2", Res2="R 2", Sup3="S 3", Res3="R 3";
int fontsize = 10;
double P, S1, R1, S2, R2, S3, R3;
double LastHigh, LastLow, x;
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete("Pivot");
   ObjectDelete("Sup1");
   ObjectDelete("Res1");
   ObjectDelete("Sup2");
   ObjectDelete("Res2");
   ObjectDelete("Sup3");
   ObjectDelete("Res3");   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexStyle(6, DRAW_LINE);
   SetIndexBuffer(0, PBuffer);
   SetIndexBuffer(1, S1Buffer);
   SetIndexBuffer(2, R1Buffer);
   SetIndexBuffer(3, S2Buffer);
   SetIndexBuffer(4, R2Buffer);
   SetIndexBuffer(5, S3Buffer);
   SetIndexBuffer(6, R3Buffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Pivot Point");
   SetIndexLabel(0, "Pivot Point");
//----
   SetIndexDrawBegin(0,1);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()

  {
   int counted_bars = IndicatorCounted();

   int limit, i;
//---- indicator calculation
   if(counted_bars == 0)
     {
       x = Period();
       if(x > 240) 
           return(-1);
       ObjectCreate("Pivot", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Pivot", "                 Pivot Point", fontsize, "Arial", MediumBlue);
       ObjectCreate("Sup1", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Sup1", "      S 1", fontsize, "Arial", DarkGreen);
       ObjectCreate("Res1", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Res1", "      R 1", fontsize, "Arial", FireBrick);
       ObjectCreate("Sup2", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Sup2", "      S 2", fontsize, "Arial", DarkGreen);
       ObjectCreate("Res2", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Res2", "      R 2", fontsize, "Arial", FireBrick);
       ObjectCreate("Sup3", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Sup3", "      S 3", fontsize, "Arial", DarkGreen);
       ObjectCreate("Res3", OBJ_TEXT, 0, 0, 0);
       ObjectSetText("Res3", "      R 3", fontsize, "Arial", FireBrick);
     }
   if(counted_bars < 0) 
       return(-1);
   //---- last counted bar will be recounted
   //   if(counted_bars>0) counted_bars--;
   limit = (Bars - counted_bars) - 1;
//----
   for(i = limit; i >= 0; i--)
     { 
       if(High[i+1] > LastHigh) 
           LastHigh = High[i+1];
       //----
       if(Low[i+1] < LastLow) 
           LastLow=Low[i+1];
       if(TimeDay(Time[i]) != TimeDay(Time[i+1]))
         { 
           P = (LastHigh + LastLow + Close[i+1]) / 3;
           R1 = (2*P) - LastLow;
           S1 = (2*P) - LastHigh;
           R2 = P + (LastHigh - LastLow);
           S2 = P - (LastHigh - LastLow);
           R3 = (2*P) + (LastHigh - (2*LastLow));
           S3 = (2*P) - ((2* LastHigh) - LastLow); 
           LastLow = Open[i]; 
           LastHigh = Open[i];
           //----
           ObjectMove("Pivot", 0, Time[i], P);
           ObjectMove("Sup1", 0, Time[i], S1);
           ObjectMove("Res1", 0, Time[i], R1);
           ObjectMove("Sup2", 0, Time[i], S2);
           ObjectMove("Res2", 0, Time[i], R2);
           ObjectMove("Sup3", 0, Time[i], S3);
           ObjectMove("Res3", 0, Time[i], R3);
         }
       PBuffer[i] = P;
       S1Buffer[i] = S1;
       R1Buffer[i] = R1;
       S2Buffer[i] = S2;
       R2Buffer[i] = R2;
       S3Buffer[i] = S3;
       R3Buffer[i] = R3;
     }
     
     
         if(Bid+1*Point >= P && Bid-1*Point <= P)
     {
  int ticket=   OrderSend(Symbol(),OP_BUY,0.10,Ask,30,0,0,"Buy Order",0,Green);
      Alert("It is there");
      
      }
//----
return(0);
}

when the bar touch the line the alert work but order send no why???

Fernando Carreiro
3942
Fernando Carreiro  
domdix27: Thanks but I'm headstrong and I do this code ... why order send don t work? when the bar touch the line the alert work but order send no why???

You cannot do "trading" in an Indicator, only in EAs (Expert Advisor). You are mixing the two concepts.

Either you code an Indicator (for Visual Chart purposes) or you code an EA for trading automatically. Your code is mixing the two things and that is why the OrderSend() does not work.

As I stated, you are still a beginnner and quite new at this! Also, try to adapt to the New style for MQL4 and not the old code style which is considered obsolete (even though it still works).

Updated MQL4 - MQL4 Reference
Updated MQL4 - MQL4 Reference
  • docs.mql4.com
Updated MQL4 - MQL4 Reference
domdix27
65
domdix27  
Fernando Carreiro:

You cannot do "trading" in an Indicator, only in EAs (Expert Advisor). You are mixing the two concepts.

Either you code an Indicator (for Visual Chart purposes) or you code an EA for trading automatically. Your code is mixing the two things and that is why the OrderSend() does not work.

As I stated, you are still a beginnner and quite new at this! Also, try to adapt to the New style for MQL4 and not the old code style which is considered obsolete.


Oh I have thought about it thanks for the confirmation so please tell me one thing how i call an element on the indicator file mq4 ( in this case P) in my expert advisor 

Fernando Carreiro
3942
Fernando Carreiro  
domdix27: Oh I have thought about it thanks for the confirmation so please tell me one thing how i call an element on the indicator file mq4 ( in this case P) in my expert advisor 

You access the Indicator data from the EA by using the iCustom() function.

I suggest you first have a look at the many examples in the CodeBase section and really go through the documentation thoroughly.

iCustom - Technical Indicators - MQL4 Reference
iCustom - Technical Indicators - MQL4 Reference
  • docs.mql4.com
iCustom - Technical Indicators - MQL4 Reference
Fernando Carreiro
3942
Fernando Carreiro  
domdix27: Oh I have thought about it thanks for the confirmation so please tell me one thing how i call an element on the indicator file mq4 ( in this case P) in my expert advisor 

Also note that you cannot access the "P" variable directly from the EA. You can only access the Indicators "buffers", so your indicator code has to written correctly in order to store the required infomation in the "buffers" (arrays).

123
To add comments, please log in or register