Some comments on this code
int start() { if(OrdersTotal()>0) { OrderSelect(0,SELECT_BY_POS); if((OrderType()==OP_SELL && Open[1]>Close[1]) || (OrderType()==OP_BUY && Open[1]<=Close[1])) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE); // if Sell order exists and previous bar Open > prev bar Close then close trade OR if Buy order exists // and prev bar Open <= prev bar Close then close trade else if(OrderType()==OP_SELL && Open[1]<=Close[1]) OrderModify(OrderTicket(),OrderOpenPrice(),Bid+(OrderPips+StopLossPips)*Point,Ask-TakeProfitPips*Point,0,CLR_NONE); // if sell trade already on and sell signals still exist then modify SL and TP to account for new Bid/Ask if(OrderType()==OP_BUY && Open[1]>Close[1]) OrderModify(OrderTicket(),OrderOpenPrice(),Ask-(OrderPips+StopLossPips)*Point,Bid+TakeProfitPips*Point,0,CLR_NONE); // if buy trade already on and buy signals still exist then modify SL and TP to account for new Bid/Ask if((OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)&& (TimeCurrent() - OrderOpenTime() >= OrderExpiryTime)) OrderDelete(OrderTicket(),CLR_NONE); // if PENDING Sell or Buy order exists & more than 1 hour (3600 seconds) has elapsed since order was opended then close Pending order } if(OrdersTotal()==0) if(Open[1]<=Close[1]) OrderSend(Symbol(),OP_SELLLIMIT,Lots,Bid+OrderPips*Point,5,Bid+(OrderPips+StopLossPips)*Point,Bid-(TakeProfitPips-OrderPips)*Point,NULL,0,TimeCurrent()+OrderExpiryTime,Green); else OrderSend(Symbol(),OP_BUYLIMIT,Lots,Ask-OrderPips*Point,5,Ask-(OrderPips+StopLossPips)*Point,Ask+(TakeProfitPips-OrderPips)*Point,NULL,0,TimeCurrent()+OrderExpiryTime,Red); // if previous bar Open <= prev bar Close then enter a pending Sell order at a price 5 pips (0.0005) from current Bid // else [previous bar Open > prev bar Close] enter a pending Buy order at a price 5 pips (0.0005) from current Ask return(0); }
You need to check if the OrderSelect was successful.
You need to add some more braces to make this code work the way it is indented.
Try making the lines shorter so they are easier to read. You can do this by not putting everything on the same line.
New to MQL programming.
I would like to use the open/close of 1-hour bars (but have the code flexible if possible to adjust to any period) for signals to trade but I'm having issues.
In the init function test if you are on an H1 chart and complain if you are not,
OR use iOpen and iClose and set the timeframe to H1
or use iOpen and iClose and put the timeframe in as an extern variable so you can adjust it.
In the init function test if you are on an H1 chart and complain if you are not,
OR use iOpen and iClose and set the timeframe to H1
or use iOpen and iClose and put the timeframe in as an extern variable so you can adjust it.
Thanks for the help. I took your advice and added brackets ( { } ) and used the iOpen and iClose functions. Unfortunately I get the same results.
My goal is to have orders created, positions closed out (if signal changes), new orders entered in to and current trades modified, all at the top of every hour. During the hour, orders can be filled, and SL & TP can be triggered, but orders (pending or live) should not be modified nor should new pending orders be created.
Attached is a revised version.
I've searched for some similar EAs but no luck. If anyone knows of any please point me to them.
Am I way off base here? Is what I'm looking for possible with MQL?
Thanks.Thanks for the help. I took your advice and added brackets ( { } ) and used the iOpen and iClose functions. Unfortunately I get the same results.
...Am I way off base here? Is what I'm looking for possible with MQL?
Well done. It's really easy to do the next bit ...
static datetime lastHour=0; if( lastHour == iTime(NULL, TimePeriod,0) ) return( 0 ); lastHour = iTime(NULL, TimePeriod,0);
Just add that code at the start of the start function.
Well done. It's really easy to do the next bit ...
Just add that code at the start of the start function.
Sweet! That did just the trick...much better than the flags and countless if-then statements I added...cut the amount of code in half.
I can imagine that basing it on one minute, day, week might be nearly as easy...what about 4-hours? Is there something just as easy? Not looking for code (unless it's just as concise), just curious.
Thanks.Sweet! That did just the trick...much better than the flags and countless if-then statements I added...cut the amount of code in half.
I can imagine that basing it on one minute, day, week might be nearly as easy...what about 4-hours? Is there something just as easy? Not looking for code (unless it's just as concise), just curious.
Thanks.Nevermind, figured out that it's all determined by TimePeriod...change the TimePeriod to minute, hour, 2-hour, 5.78 hour...whatever...and the code never needs to change --> no trades will happen until the next bar, whatever time frame it is.
Thanks.
Nevermind, figured out that it's all determined by TimePeriod...change the TimePeriod to minute, hour, 2-hour, 5.78 hour...whatever...and the code never needs to change --> no trades will happen until the next bar, whatever time frame it is.
Well done.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New to MQL programming.
I would like to use the open/close of 1-hour bars (but have the code flexible if possible to adjust to any period) for signals to trade but I'm having issues.
Idea
1a. If previous Open <= previous Close then create an order to sell at 5 pips higher than current 1-hour bar's Open price (with take profit and stop loss) or,
1b. If previous Open > previous Close then create an order to buy at 5 pips lower than current 1-hour bar's Open price (with take profit and stop loss).
2. Cancel the order if it has not been filled after 1 hour.
3. If order has been filled, trade should be closed if TP or SL are reached during the current 1-hour bar (i.e. not within 1 hour of trade being executed)
4. After 1-hour bar if trade is still open then check criteria again: a) if new signal is a buy (sell) and there is a live sell (buy) trade then close the sell (buy) trade and create a buy (sell) order [as in 1b (1a)], b) if new signal is a buy (sell) and there is a live buy (sell) trade already on then adjust current trades SL and TP based on current bid/ask levels.
When backtesting I choose H1 as the period.
Problems I'm having:
i. it is adjusting TP/SL every tick, not every hour.
ii. not opening/closing trades properly on the hour (i.e. sell trade is on but at top of new hour the signal has reversed but sell trade is still on while is should be closed and new buy order should be created).
I'm fairly new to MQL code so what I've done maybe be completely wrong, or may not be possible.
See attached file for my code.
Any help would be much appreciated.
Thanks