Error code 130

 

I'm trading BTCUSDT which has a minimum stop loss of 50 pips. My stop loss is roughly $5 away (0.02%) for every trade. Only about 10% of the trades actually go through, and every time it throws an error I print the price, SL and TP. All the calculations are correct and exactly the same when it goes through and also when it throws an error which makes no sense. Whats going on??

Here is the snippet of code where I calculate everything:


if (enterShort) {

      if (OrdersTotal() == 0) {

         RefreshRates();

         double price = Bid;

         double risk = NormalizeDouble(accountBalance / price, decimals);

         double takeProfit = NormalizeDouble(price * (1 - 0.8 * 0.01), decimals);

         double stopLoss = NormalizeDouble(price * (1 + 0.02 * 0.01), decimals);

         

         int ticket = OrderSend(symbol, OP_SELL, risk, price, maxSlippagePercent, stopLoss, takeProfit);

         if (ticket > 0) {

            Print("Short Order succesful!");

         } else {

            Print("Short Order failed with error #", GetLastError());

            Print("Price:" + price + "TP:" + takeProfit + "SL:" + stopLoss);

         }

      }

   }


And here are the calculations from when it throws an error (going short) (I've also attached a screenshot):

Price: 27915

Take Profit: 27691.68

Stop Loss: 27920.58

 
  1.          int ticket = OrderSend(symbol, OP_SELL, risk, price, maxSlippagePercent, stopLoss, takeProfit);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.          double price = Bid;
             double takeProfit = NormalizeDouble(price * (1 - 0.8 * 0.01), decimals);
             double stopLoss = NormalizeDouble(price * (1 + 0.02 * 0.01), decimals);
    

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by 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 at 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 shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. nameless nameless: All the calculations are correct and exactly the same when it goes through and also when it throws an error which makes no sense. Whats going on??

    Stop guessing and find out.

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
I already tried placing the stop losses and take profits off of the ask for short and bid for long. I'm not guessing I've been debugging for hours, I have a good amount of programming experience but this is my first time working the mql4 so I wanted to see if anyone else would know how to fix this issue while I continue to debug.
 
nameless nameless #:
I already tried placing the stop losses and take profits off of the ask for short and bid for long. I'm not guessing I've been debugging for hours, I have a good amount of programming experience but this is my first time working the mql4 so I wanted to see if anyone else would know how to fix this issue while I continue to debug.
I hard coded the stop loss to $10 less than the entry price and it seems to work the majority of the time. However when I code it to a percentage that equals roughly $10 (or even $20) it doesn't work at all. That means the issue has nothing to do with the stop loss being too tight.
Reason: