My EA only seems to place buy orders. I need a helping hand so it can place both buy and sell orders!
- Don't double post
StopLoss = AccountBalance()*MAX_RISK; //This ensures that only a certain percentage is risked of the account at each new tick.
SL is a price not a dollar amount. Reread risk and solve the equation.
So I changed the StopLoss value to a fixed constant. Why is it that this EA only buys and not sell?
StopLoss = AccountBalance()*MAX_RISK;
double MAX_RISK = 0.005;
What does this give you?
On a 10,000 balance, it gives you 50.0000
If you were manually opening a buy on EURUSD at 1.1000 would you set the stoploss at 50.0000?
What does this give you?
On a 10,000 balance, it gives you 50.0000
If you were manually opening a buy on EURUSD at 1.1000 would you set the stoploss at 50.0000?
I understand my error in this. Thanks for pointing it out. My question is ,however, why do you think the code is only placing buy orders and not sell orders?
What GumRai is trying to tell you, and what WHRoeder has already told you, is that the stop-loss value for OrderSend() should be a price, not a cash amount. Your value for the Stoploss variable is currently going to be a value such as 50 (i.e. $50). It needs to be a price such as 1.0498. If you want the stop-loss to equate to $50, or 0.5% of your account, then you need to calculate the equivalent price.
What you are doing will make a buy order fail on most symbols because MT4 is expecting a price for the stop-loss rather than a cash amount; you are giving it a price such as 50; that price is higher than the current ask price (e.g. 1.0596 on EUR/USD); and therefore the buy order is rejected because the stop-loss is not valid.
Your code "works" on sell orders because the value such as 50 is higher than the bid price on most symbols, and therefore the value of 50 is acceptable as a stop-loss - though does not have the meaning you intend, and will be creating a far larger risk than you intend.
Once last attempt at making this clear: when your code "successfully" creates a sell order with an s/l of something like 50, that means that that order will be closed when the symbol's price hits 50, not when the cash loss reaches $50.
What GumRai is trying to tell you, and what WHRoeder has already told you, is that the stop-loss value for OrderSend() should be a price, not a cash amount. Your value for the Stoploss variable is currently going to be a value such as 50 (i.e. $50). It needs to be a price such as 1.0498. If you want the stop-loss to equate to $50, or 0.5% of your account, then you need to calculate the equivalent price.
What you are doing will make a buy order fail on most symbols because MT4 is expecting a price for the stop-loss rather than a cash amount; you are giving it a price such as 50; that price is higher than the current ask price (e.g. 1.0596 on EUR/USD); and therefore the buy order is rejected because the stop-loss is not valid.
Your code "works" on sell orders because the value such as 50 is higher than the bid price on most symbols, and therefore the value of 50 is acceptable as a stop-loss - though does not have the meaning you intend, and will be creating a far larger risk than you intend.
Thank you very much for this. It is extremely helpful. May I ask how long have you been coding MQL4? I've only started coding 7 weeks ago in C as part of my degree (which is not computer science) and I've been doing MQL4 for only 3 weeks.
There's nothing wrong with your code per se; in fact it's unusually clean by the standards of this forum, and avoids common problems such as looping up to OrdersTotal() when closing positions. The issue is your understanding of trading platforms in general, and MT4 in particular, not the code.
There's nothing wrong with your code per se; in fact it's unusually clean by the standards of this forum, and avoids common problems such as looping up to OrdersTotal() when closing positions. The issue is your understanding of trading platforms in general, and MT4 in particular, not the code.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
As the title states I need my EA to place both buy and sell orders. However, it seems to only place by orders. Any help would be greatly appreciated. Here is my EA: