How to trade with the smallest stop-loss

 

Hi, in the particular trade I'm creating I want to open a position with the smallest stop-loss that my broker will allow.

I've modified some code I found on these forums but it's still not working right and I'm not sure what I'm doing wrong.

Here's a snippet of my code:

if (type == POSITION_TYPE_BUY)
{
   currentPrice = State.Symbol.Ask();
   slPriceLimit = currentPrice-MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL))*Point();
}
else
{
   currentPrice = State.Symbol.Bid();
   slPriceLimit = currentPrice+MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL))*Point();
}

Now, my broker publishes the SYMBOL_TRADE_STOPS_LEVEL with a value of 40 and as a result, when opening a BUY position on EURUSD, the stop-loss is 0.0004 below the ASK price.

My problem is that sometimes this stop-loss still results in a failed trade.  Here's a sample from my journal:

Type = POSITION_TYPE_BUY
Lot = 3.18000000
Symbol = EURUSD
Ask = 1.14844000
Bid = 1.14827000
StopLoss = 1.14804000
TakeProfit = 0.00000000
failed market buy 3.18 EURUSD sl: 1.14804 [Invalid stops]
CTrade::OrderSend: market buy 3.18 EURUSD sl: 1.14804 [invalid stops]
ERROR: method Buy returned the code: 10016, message: invalid stops

My question is, should I change my code or my broker?

 
Change broker. Stop level >0 is not from this time of age for quite some time.
 

See the Broker's STOP_LEVEL for the instrument + mind the slippage parameter you are using when send the open order... and the return code you get from the execution of your instruction.

(some Broker will ask you to proceed in 2 steps, first opening the position then set the limits) 

 
theoretically 
smart calculated hedging might work
but then you might have issue with spread and slippage.
thus ... don't count on it.
 
jacques366: (some Broker will ask you to proceed in 2 steps, first opening the position then set the limits) 
TP/SL on OrderSend has been fine for years.
          Build 500 № 9 2013.05.09
          Need help me mql4 guru add take profit to this EA - Take Profit - MQL4 programming forum 2017.09.27
 
chris_turner:

Hi, in the particular trade I'm creating I want to open a position with the smallest stop-loss that my broker will allow.

I've modified some code I found on these forums but it's still not working right and I'm not sure what I'm doing wrong.

Here's a snippet of my code:

Now, my broker publishes the SYMBOL_TRADE_STOPS_LEVEL with a value of 40 and as a result, when opening a BUY position on EURUSD, the stop-loss is 0.0004 below the ASK price.

My problem is that sometimes this stop-loss still results in a failed trade.  Here's a sample from my journal:

My question is, should I change my code or my broker?

Have you tried this.

if (type == POSITION_TYPE_BUY)
{
   currentPrice = State.Symbol.Bid();
   slPriceLimit = currentPrice-MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL))*Point();
}
else
{   
   currentPrice = State.Symbol.Ask();
   slPriceLimit = currentPrice+MathMax(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL))*Point();
}

have you checked the biggest spread possible with your broker? If not yet, you may use a script to check that. Here's one. https://www.mql5.com/en/code/27942. You just need to convert it to MQL5.

Also, try to avoid oveernight spread because it's huge in most brokers.

Just my 2 cents. I hope it helps.

SpreadWatch
SpreadWatch
  • www.mql5.com
This indicator is ideal for scalpers who need to know the biggest spread possible with his broker. This will limit his stop-loss. Stop-loss should be higher than the biggest spread to minimize stop-hunt and slippage issues.  Included lowest spread.
Reason: