Buy if current price > x

 
Hi,
I am looking for help to code theses lines:

- if bid (not open or Close) > x :
Close all sell positions,
buy if there is no buy trade

- if bid < y:
Close all buy positions,
Sell if there is no open short trade.

Thank you !
 
Help you with what? You haven't stated a problem, you stated a want.
     How To Ask Questions The Smart Way. 2004
          Prune pointless queries.

You have only four choices:

  1. Search for it. Do you expect us to do your research for you?

  2. Beg at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  4. or pay (Freelance) someone to code it. Top of every page is the link Code Base.
              Hiring to write script - General - MQL5 programming forum 2019.08.21

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help 2017.04.21

 

After some coding :


#property copyright ""
#property link      ""



extern double LevelExposure1   = 12400;
extern double LevelExposure2   = 12400;
extern double Lots            = 1.0;
extern int    Slippage        = 2000;

int ID=234;

int init() {

   return(0);
  }

int deinit() {
   return(0);
  }
  
  
  
int getCurrentOrderType() {
   int semua=OrdersTotal();
   for(int a=semua-1;semua>=0;a--) {
      if(OrderSelect(a,SELECT_BY_POS)) {
         if(OrderSymbol()!=Symbol()) continue;
         if(OrderType()>1) continue;

         return OrderType();
         break;
      }
   }
   return;
}

void OnTick() {
   int tiketnya;
   int tipe;
   tipe = getCurrentOrderType();
      
      // currently one selling trade, but now price > target => close + open a buy
      if(tipe==OP_SELL && Ask>=LevelExposure1) {
      Print("sell ouvert, a fermer");
      
         if(close_positions(OP_SELL)==true) {
            tiketnya=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"TheGuardian",ID,0,DeepSkyBlue);
         }
      }
      
      // currently one buying trade, but now price < target 2 => close + open a sell
      if(tipe==OP_BUY && Bid<=LevelExposure2) {

         if(close_positions(OP_BUY)==true) {
            tiketnya=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"TheGuardian",ID,0,Red);
         }
      }

   return(0);
  }




int close_positions(int t) {
   int hasil=false;
   int semua=OrdersTotal();
   for(int a=semua-1;semua>=0;a--) {
      if(OrderSelect(a,SELECT_BY_POS)) {
         if(OrderSymbol()!=Symbol()) continue;
         if(OrderType()==t&&OrderType()==OP_SELL) {
            hasil=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE);
         }
         if(OrderType()==t&&OrderType()==OP_BUY) {
            hasil=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE);
         }
         break;
      }
   }
   return(hasil);
}
 
I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
Reason: