Download MetaTrader 5

Why does this code produce wrong SL half the time?

To add comments, please log in or register
nadiawicket
417
nadiawicket  

Why does this code produce a wrong SL sometimes yes sometimes no?

OrderSend(_Symbol,OP_SELL,.01,Bid,0,Bid,0,NULL,0,0,clrNONE);
honest_knave
Moderator
2293
honest_knave  

Your code is opening a sell order with a SL below the current order close price. Remember a sell order is closed at the Ask.

If you get into the habit of checking the return code on your OrderSend it will help you troubleshoot. There is an example in the OrderSend() documentation.

Keith Watford
Moderator
10541
Keith Watford  
nadiawicket:

How do you do if you want to open a sell that has the SL at the exact price in which you opened from the moment the order is fired that works 100 per cent of the time?

Not Possible
honest_knave
Moderator
2293
honest_knave  
nadiawicket:

The following code uses Ask as the SL and it fires right sometimes yes sometimes not. Why?

How do you do if you want to open a sell that has the SL at the exact price in which you opened from the moment the order is fired that works 100 per cent of the time?


The code you posted still isn't checking the return code - take a look at the documentation for an example on how to do it. If you check the return code, you don't have to guess why your OrderSend isn't working.

I also suggest reading up about stops level. Your SL and TP must be a minimum distance away (set by the broker). So what you want is not really possible, if I'm understanding you correctly.

Here is a table from the article I have linked:

StopLevel Minimum Distance Limitation.

A trade operation will not be performed if any of the following conditions is disrupted.

Order Type
Open PriceStopLoss (SL)TakeProfit (TP)
Buy
Modification is prohibited
Bid-SLStopLevelTP-BidStopLevel
Sell
Modification is prohibitedSL-AskStopLevelAsk-TPStopLevel
BuyLimit
Ask-OpenPriceStopLevelOpenPrice-SLStopLevelTP-OpenPriceStopLevel
SellLimit
OpenPrice-BidStopLevelSL-OpenPriceStopLevelOpenPrice-TPStopLevel
BuyStop
OpenPrice-AskStopLevelOpenPrice-SLStopLevelTP-OpenPriceStopLevel
SellStop
Bid-OpenPriceStopLevelSL-OpenPriceStopLevelOpenPrice-TPStopLevel
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
nadiawicket
417
nadiawicket  
honest_knave:


The code you posted still isn't checking the return code - take a look at the documentation for an example on how to do it. If you check the return code, you don't have to guess why your OrderSend isn't working.

I also suggest reading up about stops level. Your SL and TP must be a minimum distance away (set by the broker). So what you want is not really possible, if I'm understanding you correctly.

Here is a table from the article I have linked:

StopLevel Minimum Distance Limitation.

A trade operation will not be performed if any of the following conditions is disrupted.

Order Type
Open PriceStopLoss (SL)TakeProfit (TP)
Buy
Modification is prohibited
Bid-SLStopLevelTP-BidStopLevel
Sell
Modification is prohibitedSL-AskStopLevelAsk-TPStopLevel
BuyLimit
Ask-OpenPriceStopLevelOpenPrice-SLStopLevelTP-OpenPriceStopLevel
SellLimit
OpenPrice-BidStopLevelSL-OpenPriceStopLevelOpenPrice-TPStopLevel
BuyStop
OpenPrice-AskStopLevelOpenPrice-SLStopLevelTP-OpenPriceStopLevel
SellStop
Bid-OpenPriceStopLevelSL-OpenPriceStopLevelOpenPrice-TPStopLevel
Thanks a ton; However, broker has no StopLevel minimum requirements. GetLastError() gives no error. Is this what you mean by check return codes? Ive tried to find out how to check return codes in the past;  https://www.mql5.com/en/forum/139592 post just suggests GetLastError()? Documentation is not very clear on this that I can find. Will keep trying to find out how to get these "return codes". 
honest_knave
Moderator
2293
honest_knave  

This post helps explain about return codes.

A sell order is opened at the Bid, and closed at the Ask (which is above the Bid).

A sell SL will trigger when the Ask price is at or above the SL price.

If you set the SL to the Bid price, the Ask is already above this...

nadiawicket
417
nadiawicket  
Keith Watford:
Not Possible
You serious?
nadiawicket
417
nadiawicket  
honest_knave:

This post helps explain about return codes.

A sell order is opened at the Bid, and closed at the Ask (which is above the Bid).

A sell SL will trigger when the Ask price is at or above the SL price.

If you set the SL to the Bid price, the Ask is already above this...

      OrderSend(_Symbol,OP_SELL,.01,MarketInfo(_Symbol,MODE_BID),0,MarketInfo(_Symbol,MODE_ASK),0,NULL,0,0,clrNONE);

Gives no errors. Can't find any more info on return codes that could help. Will keep searching.

Friend here said that its just not possible. Is he correct?

Just not possible? Above code seems like it should work.
whroeder1
16575
whroeder1  
nadiawicket:

Friend here said that its just not possible. Is he correct?

Just not possible? Above code seems like it should work.
  1. Violates Ask-TP≥StopLevel for nonzero StopLevels
  2. Means the order's TP and the Ask are identical, the order would be immediately closed.
nadiawicket
417
nadiawicket  
whroeder1:

  1. Violates Ask-TP≥StopLevel for nonzero StopLevels
  2. Means the order's TP and the Ask are identical, the order would be immediately closed.

1. No minimum Stop Level with my broker.. How does this violate if that rule is for nonzero StopLevels?

2. I really don't understand why you are mentioning Take Profits at all? Maybe I'm missing your point or something.We are dealing with Stop Loss. ( which I'm using to effectively create a Trailing Stop)


Something else; no matter how I set the initial Stop Loss of my OrderSends, they bork out like 50 per cent of the time like its some glitch or I'm just not doing it right. If anyone could post the correct code for an OrderSend that puts the right Stop Loss from the get go without having to ordermodify afterwards, I'd really appreciate it.

    OrderSend(_Symbol,OP_SELL,.01,MarketInfo(_Symbol,MODE_BID),0,MarketInfo(_Symbol,MODE_ASK)+10*Point,0,NULL,0,0,clrNONE);

For example the code above; whats wrong with it that it fails about 50 per cent of the times at setting the correct SL (10 points above) from the get go? They keep telling me to check return codes; however I get nothing with GetLastError() thats not a 0. What else can I check about "return codes"? The standard link doesn't say anything else I can make sense out of other than using the GetLastError()? Any other way to retrieve other return codes thats not GetlastError()?


Thanks a zillion.

honest_knave
Moderator
2293
honest_knave  

Example:

Ask: 1.10020

Bid: 1.10010

(spread 10 points)

If you set your SL on a sell order to 1.10010 (the Bid) then the closing price (Ask) is already above it. If this worked, your order would instantly close as the SL has been passed.

If you feed those numbers into the equation on the table:

A trade operation will not be performed if any of the following conditions is disrupted. 

SL - Ask ≥ StopLevel

1.10010 - 1.10020 = - 10 points

Even if your StopLevel is 0, a negative number is still less than 0.

12
To add comments, please log in or register