This isn't the original one - correct?
So compare your changes against the original.
You can even start the debugger to see what if-clause might be the reason...
Learn mql4 by reading the references--
if(Close[0]>Close[1]) ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue); ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
- Why to you open a buy when Bid > last candle close but always open a sell?
- Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
This isn't the original one - correct?
So compare your changes against the original.
You can even start the debugger to see what if-clause might be the reason...
Learn mql4 by reading the references--
Thank you for your reply. No its not the original code. Here is the original code:
//| mo_bidir_v0_1.mq4 | //| | //| - Works best in 5M timeframe | //| - Bug//+------------------------------------------------------------------+ fix to stop_loss in line 22 2010.04.07 | //+------------------------------------------------------------------+ #property copyright "Copyright ɠ2010 - Monu Ogbe" #define MAGIC 1234 #define IDENT "mo_bidir_v0_1" extern double lots = 1; extern double stop_loss = 80; // (8 pips) optimise 50-2000 extern double take_profit = 750; // (75 pips) optimise 50-2000 int last_bar = 0; int start(){ if (last_bar == Bars) return(0); last_bar = Bars; if (OrdersTotal() == 0){ OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue); OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red); } return(0); }
I basically replaced the last bar function under int start with (Close[0]>Close[1] because I do not want the EA to indiscriminately open a buy and sell order as soon as I attach the EA to the chart. Instead I want to add my own priceaction parameters as per the insert in post #1. At the moment it completely ignores the closing prices and still opens a buy trade as soon as I attach the EA. It ignores the function I added. Do I need to define what "Close" means as a Global variable, perhaps?
This modified code returns no errors or warnings. I also ran the debug function as suggested (thanks), and there were no errors in the journal.
Thank you also for the links to the return functions, but mine appear to be correct because there are no errors/ warnings.
Thank you for any help
Asked and answered: "Why to you open a buy when Bid > last candle close but always open a sell?" | if(Close[0]>Close[1]) ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue); ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red); |
Compare the two versions. There are two differences. One is "Close[0]>Close[1]" | if (OrdersTotal() == 0){ OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue); OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red); } |
Hi Everyone
I simply want the EA to open both a buy and sell order at the same time, but only do so when no other orders are open. And I want it to open the orders when the closing price of the current bar is higher than the closing price of the previous bar. It appears to me as if the EA ignores this and places the order as soon as I add it to the chart.
Hi, just add brackets "{..}" as below and try again:
if(Close[0]>Close[1]) { ticket=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue); ticket=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red); }
Thank you WHRoeder for pointing that out. I did see the issue with the brackets and thank you jollydragon for pointing that out as well. I have added the brackets and also added another trade condition. It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked. So as it stands, no trades are being placed even if any of the trade conditions are met (candle closing higher or lower). Thank you very much for your help so far ...... it feels as if I'm on the brink of a breakthrough in my learning process.
//+------------------------------------------------------------------+ //| mo_bidir_v0_1.mq4 | //| | //| - Works best in 5M timeframe | //| - Bug fix to stop_loss in line 22 2010.04.07 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010 - Monu Ogbe" #define MAGIC 1234 #define IDENT "mo_bidir_v0_1" extern double lots = 0.01; int ticket,total; int start(){ total=OrdersTotal(); if(total<1) { if(Close[0]>Close[1]) // OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue); if(Close[0]<Close[1]) // OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, 0, 0, IDENT, MAGIC, 0, Red); } return(0); }
Trader3000: It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked.
| Asked and answered (#2) |
Trader3000: It compiles with no errors and two warnings namely that the return value of both the Ordersend functions should be checked. | Asked and answered (#2) |
//+------------------------------------------------------------------+ //| mo_bidir_v0_1.mq4 | //| | //| - Works best in 5M timeframe | //| - Bug fix to stop_loss in line 22 2010.04.07 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010 - Monu Ogbe" #define MAGIC 1234 #define IDENT "mo_bidir_v0_1" extern double lots = 0.01; int result,total; int start(){ total=OrdersTotal(); if(total<1) { if(Close[0]>Close[1]) result=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue); if(Close[0]<Close[1]) result=OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, 0, 0, IDENT, MAGIC, 0, Red); } return(0); }
Close[0] is the current Bid price. Bar[0] hasn't closed yet so it can only be the last available price
It may be that what you are actually trying to do is
if(Close[1]>Close[2]) result=OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, 0, 0, IDENT, MAGIC, 0, Blue);
which is the last completed bar closed higher than the previous bar's close

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Everyone
While not new to trading, I am new to programming. I want to learn how to do it and I do have the MQL4 book and reading it. I thought that I would start with something really simple and basic and use this as a base to build a more sophisticated EA, but I cannot even get this EA to work properly. I have been struggling for 3 weeks now, and realized it is time to ask for help. I simply want the EA to open both a buy and sell order at the same time, but only do so when no other orders are open. And I want it to open the orders when the closing price of the current bar is higher than the closing price of the previous bar. It appears to me as if the EA ignores this and places the order as soon as I add it to the chart. And then some other times it appears as if it won't trade at all. There are no warnings or errors. Will someone please, please take a look for me. I want to learn. Thank you. Gary