Spread protector

 

I am trying to code an EA to, each time an order is opened, it will change the SL to a "bigger loss" to avoid the spread.

Ex. The SL is 0.7400 and the broker has a 10 points spread, so ne=the new SL should be  0.7390 or something like that.

I am not getting a good result with the code bellow. How to fix to work with silver, gold and so on ?


         double Spread = SymbolInfoInteger(OrderSymbol(), SYMBOL_SPREAD);

         double SL = NormalizeDouble(OrderStopLoss()-(Spread/10000),(int)MarketInfo(OrderSymbol(),MODE_DIGITS));

         Print("Spread:", Spread, ", Old SL:", OrderStopLoss(), ", New SL:", SL);

         bool res=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Blue);

 
Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.


You don't need to adjust your SL. The spread is already included.


 
Spread/MathPow(10, symbol_digits)

Think about it.
 
Caporal: Ex. The SL is 0.7400 and the broker has a 10 points spread, so ne=the new SL should be  0.7390 or something like that.

Why do you think you want that? You've already paid the spread when you opened the buy order.

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).