EA stops after first order

 

i made this ea.

it opens the first buystop trade and if the price made a new low(invalidation) below y, it deletes the order.

it opens the second order and doesnt delete it if invalidated.

it opens third, and doesnt delete it. and so on.

i know the error is in static y.

but how to make it work?

i need it to repeat like the first trade.

if order placed,i want price to stay above(y of this order) until order is activated.




void buy(){
double  c=High[iHighest(_Symbol,_Period,MODE_HIGH, 3,1)];
double static  y=Low[iLowest(_Symbol,_Period,MODE_LOW,3,1)];
int  x=(c-y)/_Point;
if(Close [1] > Open [1] && Close[2]>Open[2] && Close[3]>Open[3]&& Close[4]<Open[4]){
OrderSend(Symbol(),OP_BUYSTOP,lot,(Ask+x*_Point*2.5),3,(Ask+x*_Point*1.5),(Ask+x*_Point*5),NULL,MagicNumber,0,clrAliceBlue);
}
else if(Ask<y){
OrderDelete(OrderSelect(0,SELECT_BY_POS,MODE_TRADES),clrAliceBlue);}
}

 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
double static  y=Low[iLowest(_Symbol,_Period,MODE_LOW,3,1)];

That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder:

That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
how to delete order after ask<y in every order?
 
شوقي محمد: how to delete order after ask<y in every order?
  1. Fix your y. #2

  2. OrderDelete(OrderSelect(0,SELECT_BY_POS,MODE_TRADES),clrAliceBlue);}
    OrderDelete requires a ticket number. OrderSelect returns a bool. Fix your broken code.
 

i solved the problem by putting the y values in array and getting the max value.

if ask<max value

the EA delete the last order