Why this code doesn't work? I have 10030 error or 10016 after change

 

Hello, I try to practice some Algotrading by using MetaTrader5 with Python.

Here's my situation.

I try to send (buy) order request but I get 10030 error. I managed to deal with it by changing  ORDER_FILLING_RETURN to  ORDER_FILLING_IOC.

After that another error appeard, its 10016 and I guess it is related  with  ORDER_FILLING_IOC, but getting to the point I copied whole code from the MetaTrader for Python and of course I've changed everything what is specified for particular user but the code stil doesnt work, so is it the problem with my broker or code itself?

Here is my code, I've suported myself with docs and examples of course:

-----------------------------------------------------------------------------------------------------------------------

import random
import MetaTrader5 as mt5

symbol = random.choice(BUY_LIST)
symbol_info = mt5.symbol_info(symbol)

if symbol_info == None:
    print(f'{symbol} not found, we could not call order.')
    print('Terminating...')
    time.sleep(10)
    mt5.shutdown()
    quit()
    
lot = 0.3
price = mt5.symbol_info_tick(symbol).ask,
deviation = 50
symbols = mt5.symbols_get()
for i in symbols:
        if i.name == symbol:
            close_p = i.session_close
            open_p = i.session_open

diff = close_p - open_p
price = mt5.symbol_info_tick(symbol).ask
sl = price - diff
tp = price + diff


request = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": symbol,
    "volume": lot,
    "type": mt5.ORDER_TYPE_BUY,
    "price": price,
    "sl": sl,
    "tp": tp,
    "deviation": deviation,
    "magic": 239980,
    "comment": "python script open",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_IOC,
}



result = mt5.order_send(request)


print("1. order_send(): by {} {} lots at {} with deviation={} points".format(symbol,lot,price,deviation));
if result.retcode != mt5.TRADE_RETCODE_DONE:
    print("2. order_send failed, retcode={}".format(result.retcode))
    # request the result as a dictionary and display it element by element
    result_dict=result._asdict()
    for field in result_dict.keys():
        print("   {}={}".format(field,result_dict[field]))
        # if this is a trading request structure, display it element by element as well
        if field=="request":
            traderequest_dict=result_dict[field]._asdict()
            for tradereq_filed in traderequest_dict:
                print("       traderequest: {}={}".format(tradereq_filed,traderequest_dict[tradereq_filed]))
    print("shutdown() and quit")
    
else:
    print("2. order_send done, ", result)
    print("   opened position with POSITION_TICKET={}".format(result.order))
    print("   sleep 2 seconds before closing position #{}".format(result.order))
    time.sleep(2)

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
 
Mateusz Konieczny:


Please insert the code correctly: when editing a message, press the button     Codeand paste your code into the pop-up window.

 
Vladimir Karputov:

Please insert the code correctly: when editing a message, press the button     and paste your code into the pop-up window.

Sure,  sorry
 
Hi the order gives back the following 
 order_send failed, retcode=10016

   retcode=10016

Because of invalid stops, check your stoploss and take profit. It should work fine. Any further questions let me know

Regards

Suhail