![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Small change as it was checking to close an order when there wasn't one.
According to the code there is no reason for a sell to be opened immediately a buy closes.
The condition to exit a buy is not the same as the conditions to open a sell .
Remember that as you are only checking on the candle open, Close[0] will be the bid value of the first tick received for the candle.
Hey GumRai,
I was wondering if you'd be able/willing to help me a bit more with my code? If you could just tell me what codes I should look into, I'll try coding it myself first and then just come back with any questions .
What I want is that once a trade is placed, it puts a Stop loss at, say, 50 pips (which is what it does now). However, once price moves 60 pips in the direction of the trade, it moves SL to breakeven, and further on becomes a trailing stop 70 pips away from the trade.
How would I go about doing this? As always, any help would be greatly appreciated.
For the break-even, I always move the SL to break-even + 1 point at least. That avoids any possible problems with comparing doubles at later checks
It's simply a case of checking 2 conditions (for a buy )
and
then modify the order so that the SL is the open price +1 point or whatever amount you may decide to lock in.
For a sell it is the opposite conditions, but you should also code to account for if SL is 0 initially
OrderOpenPrice()>OrderStopLoss() || OrderStopLoss()==0
For the break-even, I always move the SL to break-even + 1 point at least. That avoids any possible problems with comparing doubles at later checks
It's simply a case of checking 2 conditions (for a buy )
and
then modify the order so that the SL is the open price +1 point or whatever amount you may decide to lock in.
For a sell it is the opposite conditions, but you should also code to account for if SL is 0 initially
Thanks a lot. In fact, what I'll do is add enough points so that it just (roughly) covers the cost of brokerage for the trade.
For the second line..why would I use Orderclose price-OrderOpenPrice? I didn't quite understand that code. Shouldn't I use Bid-OrderOpenPrice, since that would track how much the price has moved?
Am I even close with what I'm doing here below?:
I've declared commission above as an extern. And I used Bid instead.
Edit:
So I guess (based on what I found below) that the code I have posted above is incorrect? I found the following in the MQL4 book/help...It seems to be just a trailing stop, whereas what I need is a Breakeven stop first, followed by trailing stop. How would I modify this?
Your whole block of code is conditional on BuyTicket==-1
If an order is opened, then BuyTicket will not = -1, so no checks will be done for moving to BE while the trade is open.
What does the ticket number have to do with the price?
You must select an order before using OrderOpenPrice()
OrderClosePrice() is the Bid at the time that the order is selected for a buy, ask for a sell.(while the order is still open)
So I guess (based on what I found below) that the code I have posted above is incorrect? I found the following in the MQL4 book/help...It seems to be just a trailing stop, whereas what I need is a Breakeven stop first, followed by trailing stop. How would I modify this?
Try this
Thanks for the points! I've made the necessary changes. When I compile, it shows an alert that "return value of 'OrderSelect' should be checked. This, strangely, translates to only one buy being triggered and then for the rest it's a 4108 error code again. Sell works okay (I haven't made any changes to that).
Here's the entire section of the buy code, in case I'm doing something wrong--but that part shouldn't be, since all I've added is this code in the middle. The rest was as before which worked perfectly well. What am I getting wrong here?
Update:
Googled some more, and changed it to the following:
This removes the error generated when I click compile. But it still creates OrderModify error 4108 in the Journal.
All your code is contained in the if condition
so it is only executed once per new bar.
When BuyTicket == -1 you are trying to select the order with a ticket number of -1 and of course it doesn't exist
Check that BuyTicket>0 before trying to select an order
All your code is contained in the if condition
so it is only executed once per new bar.
Thanks for pointing this out.
So, for this, I created an extra bracket on the top, right after Start like:
int start() { {
and then closed one bracket (which ended the whole if(bar_time!=Time[0]) section and then placed the code at the end followed by a return (0); and end bracket to close the start().
No errors, either in the code, or the backtest Journal but it only took one buy trade and that's it--that too which it closed right at open although price didn't budge any higher from the open--let alone move 50 pips.
Think this might have to do with the BuyTicket being == -1 that you mentioned. But I honestly have no idea how to change that. I'd put that -1 thing in based on your recommendation for the initial code--which worked fantastically. But how would I check that BuyTicket>0 before trying to select an order?
But how would I check that BuyTicket>0 before trying to select an order?
I am sorry, but if you have to ask this, you are trying to write code way too complicated for your knowledge level.
I can't write your code for you a bit at a time.