Very Basic Questions for First EA

To add comments, please log in or register


Please forgive my ignorance with all of this. I have started to learn mql4 a couple of days ago and I have no previous coding experience. I was very proud of my first script when I realised that I misunderstood how scripts work. Of course what I need is an EA. The basis of what I need it to do is extremely simple, but I am getting very confused with trying to translate my -broken because they don't work how I thought they do- script into a usable EA. When I try to backtest how I think it might work, I end up in infinite loops or with nothing happening at all. I think it should be some simple extra functions which I need to input but I could use some guidance as I am getting frustrated. 

Here is my very clunky code at the moment. Hopefully it is clear what I want to do.

extern int TakeProfit = 0;
extern int StopLoss = 200;

void OnStart()

   double StopLossLevel;
   double price= 1.81550;
   double lotsize1= 0.01;
   StopLossLevel = price+0.0050 - StopLoss*Point;
   int ticket;
   ticket = OrderSend (Symbol(), OP_BUYSTOP,lotsize1,price,10,0,0,"Lot1",0,0,clrRed);
   int ticket2;
   ticket2 = OrderSend (Symbol(), OP_BUYSTOP,lotsize1,price+0.0050,10,StopLossLevel,0,"Lot2",0,0,clrRed);
   if(Ask > price+0.005)
      Alert ("ticket modified");
      bool res;
      res = OrderModify(ticket,price,StopLossLevel,0,0,clrNONE);
      int ticket3;
      ticket3 = OrderSend (Symbol(), OP_BUYLIMIT,lotsize1,StopLossLevel+0.0002,10,0,0,"LotLimit1",0,0,clrRed);

I just want the EA to place 2 Buy stops. If the price reaches the second Buy Stop, I want the one lower to place a Stop Loss in the same place as the second, as well as this I want a new Buy Limit to be activated 2 pips above the stop loss. 

I only want this to be done once.

Any help with how to go about this would be greatly appreciated.

Thanks so much.


Icham Aidibe
Icham Aidibe  

First, you should use OnTick() instead of OnStart().

Brian Rumbles
Brian Rumbles  
The double price=1.81550 means that your if statement will either always be true every tick or false every tick and it will do things ga over and over.

At some point i think you should update this price variable. 

Besides this to do things just once you can use bool variables which you could think of as gates, either open or closed. The relevant gate is open just once to allow for example an OrderSend to happen then in the next line that bool variable is set to false, but perhaps in the very next line the next relevant bool gate variable is set to open, and so you build the logic. 

Also look at the documentation for the fumctions that start with Order because they can be helpful, eg. OrdersTotal OrderSelect OrderProfit etc. Eg if ( OrdersTotal() < 1) {}  can be used to check if there is no trade open and to only allow one trade open at a time for example
To add comments, please log in or register