Opening Order at Previous Candle's Highest and Lowest.

To add comments, please log in or register

Hi, I have recently taken to doing some coding for MQL4. I would like to create an expert adviser that can open order when current candle hits the previous's highest or lowest. I have done up the majority of the framework using the Sample MACD file as a reference however the EA is not making trades at the values I wanted. 

 I have attached what I have done so far Below... Anyone can give me any pointers on where I have gone wrong? Thanks alot!


The price is not going to land exactly on the prev high. Prices does not always move 1_point at a time. Changing your code to if( Ask == Val ) is not going to help either because prices are almost never equal. if( Ask >= Val ) is your best bet. <but dont expect it to place order @ exectly prev high.

William Roeder
William Roeder  
  1. val = High[1];

    Mq4 charts are bid chart. Why don't you wait for the Bid to exceed the previous bar's highest bid and then open at the Ask?

     val2 = Low[1];
    Like wise wait for the Bid to go below then open.

  2. if(total<1) 
    // it is important to enter the market correctly, 
    // but it is more important to exit it correctly...
    if(OrderType()==OP_BUY)// long position is opened <== No OrderSelect since the beginning of start()
    Can't use OrderType, OrderStopLoss, etc unless you do a OrderSelect. The one after the open is irrelevant (You returned)

  3. Not adjusting for 4/5 digit broker (TP, SL, AND slippage) Not compatible with ECN brokers. 
  4. total=OrdersTotal();
    Not using a magic number means the EA is incompatible with every other, including itself on other charts and manual trading

Hmm...Okay! I think I have roughly gotten what I needed thanks to both your inputs!

Thanks so much guys!  Will get back to you here once I modify the codes!


Hi again Guys, I'd like to inquiry. 

Lets say instead of opening a trade when the current candle hits the previous candle's High or Low...

I open a Pending order at the close of the previous candle with the value of the highest high and lowest low, +/- 1 pips.

How would my code roughly look like?

I just need a rough guideline when comparing with my current code.

Thanks guys! 

William Roeder
William Roeder  
Animas90: How would my code roughly look like?
Exactly like your current code except you wouldn't be opening a buy/sell at the Ask/Bid

Hi Guy,s I need your help plz i have done this EA on Moving Averages  I want to make changes in this EA first change i want to make it martingale formula like 0.01,0.02,0.03,.0.04,0.4,0.05,0.06,0,.07,0.08,0.09,0.10,0.11,0.120.13,0.14,0.15 like that and in EA parameter need option like next trade will open after 50 or 100 pips for example if put in EA parameter 100 so first trade 0.01 so next trade will open after 100 pips which will be 0.02 after 100 pips more EA will open 0.03 Trade open close trades will be on below condition plz


Now current condition of EA is When Candle cross 40 MA on above on the 3rd candle EA will place Buy order and when Candle cross MA on down side EA palce Sell Trade i want to i wan to make changes in this EA from your help 

Changes Condition 

When Candle cross MA on above when candle are above on MA and next candle higher then the previos candle EA will place Buy order and on every next candle which above MA ea will palce 0.01 next candle 0.02 next candle 0.03 and so on and same when candle cross MA on below EA and next candle higher then previous 1 EA will place sell trade and on every next candle EA will place sell trade like 0.01 on next candle 0.02 and on next candle 0.03 and 



The Important thing i want to add is when candle above moving average EA will place buy trade but important is when next candle is high then previous and after that on every candle it will place buy trade and volume will be increase step by step 0.01,0.02,0.03 same criteria for SELL trade as well thanks i am waiting for you helpfull reponse 

Below is coding what i need to add



#property copyright "Copyright 2015"


#property link      ""

#property version   "2.0"

input bool    CloseAtOppSignal  = false;

input double  Lots             = 0.01;


input double  TakeProfit        = 200,

              StopLoss          = 0;

input double  TrailStop         = 0;                 

input double  TrailStep         = 0;               

input int     MagicNumber       = 3241671;  

input string  Ordercomment      = ""; 

input string   MASETTINGS        = "";//MA SETTINGS

input int      MAPeriod          = 40,

               MAMethod          = MODE_SMA,

               MAPrice           = PRICE_CLOSE;

int PF;

datetime LastBuyTime,LastSellTime;


//| Expert initialization function                                   |


int init() {

if(Digits==2||Digits==5) PF=1; else PF=10;

if(TimeCurrent()>D'2015.06.16 00:00')


     Alert("EA expired at "+TimeToStr(D'2015.06.16 00:00')+", please paid the remaining ammount to get fresh version !!");

   return (0);








//| Expert deinitialization function                                 |


void OnDeinit(const int reason)






//| Expert tick function                                             |


void OnTick()



   if(TimeCurrent()>D'2018.06.16') return;


   bool IsBUY = false, IsSELL = false;


      double MA = iMA(NULL,0,MAPeriod,0,MAMethod,MAPrice,3);



      if(Close[3] > MA && Open[3] <= MA)IsBUY  = true;

      if(Close[3] < MA && Open[3] >= MA)IsSELL = true;







To add comments, please log in or register