OrderSend Error 130 Unable to resolve

 

Hi,


I've been trying to figure out why I'm getting a OrderSend #130 error. I'm familiar with the error and have resolved similar errors in the past however I'm unable to debug this one. I could use a 2nd pair of eyes to review the code to figure out what the issue is. 

Below is a summary of the code as well as the .mq4 file and pastebin:

1. When no orders exist, run through rules to determine Buy or Sell.

2. Open initial order.

3. Open Pending Orders that are stacked by lastOrderOpenPrice + targetProfit(targetPips) value.

4. Set StopLoss when in profit.

5. Close all positions + Pending Positions when active Profit vs Loss is > x. 

6. Repeat steps 1-5

***Error occurs on part 3 on the 2nd go round. Sells seem to work fine. 

Testing: 1/1/21 - 2/1/21. Error can be witnessed on 1/4/21 once the first set of pending orders are all closed out.

This script is not finished yet so please don't judge the results. Also if you find anything in the code that can be improved for speed, performance, or a simpler way to write something, I don't mind the criticism - feedback is appreciated but my main goal is resolving the #130 error.

Pastebin: https://pastebin.com/LATX8Pmc

Any help is appreciated!!

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
v1.0.11.mq4  21 kb
 
  1. Your broker is in the US. FIFO rules apply. Without proper SL/TP management, other orders could be closed first.
              How to Resolve Erroneous FIFO Violation Message on My Trade Copy Signal? - Forex Signal Providers - General - MQL5 programming forum (2020.11.09)

  2. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
 
"William Roeder #:
  1. Your broker is in the US. FIFO rules apply. Without proper SL/TP management, other orders could be closed first.
              How to Resolve Erroneous FIFO Violation Message on My Trade Copy Signal? - Forex Signal Providers - General - MQL5 programming forum (2020.11.09)

  2. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order."
Thanks for the reply! 

1. My broker is not in the US. FIFO rules could possibly apply however I'll need to confirm.

2. I close orders in reverse starting from top to bottom in case specific index gets deleted first.

3. The code works most of the time. It will open pending orders and it will close them just fine but not all the time. Sell side seems to be working much better however the logic between buys and sells is similar with the exception of Bid/Ask. 

4. I do have code that handles acceptable STOP/TP distance and adjusts accordingly by calling MODE_STOPLEVEL using the minStopLevel function in the code. 

5. The slight advantage of quicker fills is worth it. Right now I'm testing on 5m frame but it's not final, I could possibly switch to 1m scalping as well so I would like the code to handle pending orders.

Would you be willing to glance at the code and see if you spot the issue? I've been starring at it for the past week to no avail. 

Thank you!
Reason: