What were the current prices (Bid and Ask)? What was the freeze level (SYMBOL_TRADE_FREEZE_LEVEL)?
Not sure about the FREEZE levels?
Ask price is being used for my buy and bid price for my shorts.
I have tried hardcoding my sl and tp now.
Just strange that it will Buy and Sell perfect for hours and then stop.
price_ask = mt5.symbol_info_tick(symbol).ask # Buying
####################
##cONDITION FOR buy
if close_past < close_current and position_type(symbol) != 0 or position_type(symbol) == None:
mt5.Close(symbol)
buy(symbol, lot, price_ask, point, digits)
print('{} LONG Postition bought!'.format(symbol))
####################
##### Function to BUY
def buy(symbol, lot, price, point, digits):
request_buy = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_BUY,
"price": round(price, digits),
"sl": round((price - 100 * point),digits),
"tp": round((price + 300 * point),digits),
"magic": 234000,
"comment": "{} Buy.".format(symbol),
"type_time": mt5.ORDER_TIME_GTC,
}
# send a trading request
result_buy = mt5.order_send(request_buy)
# check the execution result
print("1.BUY order send(): by {} {} lots at {}".format(symbol,lot,price));
if result_buy.retcode != mt5.TRADE_RETCODE_DONE:
print("2. order_send failed, retcode={}".format(result_buy.retcode))
# request the result as a dictionary and display it element by element
result_dict=result_buy._asdict()
for field in result_dict.keys():
print(" {}={}".format(field,result_dict[field]))
# ifthis 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]))
else:
return result_buy
#########
The new MT5-terminal beta along with the MetaTrader5 5.0.31 can only sustain about 7700 calls to copy_rates_from_pos() before it completely locks up the thread. In order to demonstrate this bug I have implemented a timeout using asyncio. The timeout works on the control, however, it does not work for copy_rates_from_pos because the thread completely freezes due to the bug.
import asyncio
import time
import MetaTrader5 as mt5
async def proof_timeout_works_on_blocking_call():
loop = asyncio.get_running_loop()
future = loop.run_in_executor(None, time.sleep, 5.0)
try:
await asyncio.wait_for(future, timeout=0.5)
except asyncio.TimeoutError:
print("Timeout works as expected for blocking calls!")
async def async_copy_rates(*args, timeout):
loop = asyncio.get_running_loop()
future = loop.run_in_executor(None, mt5.copy_rates_from_pos, *args)
try:
return await asyncio.wait_for(future, timeout=timeout)
except asyncio.TimeoutError:
print(f"async_copy_rates timed out")
print('mql errors =', *mt5.last_error())
raise
async def async_last_error():
loop = asyncio.get_running_loop()
err = await loop.run_in_executor(None, mt5.last_error)
return err
async def main():
await proof_timeout_works_on_blocking_call()
maxbars = mt5.terminal_info().maxbars
for i in range(1, maxbars):
r = await async_copy_rates('EURUSD', mt5.TIMEFRAME_M1, 0, i, timeout=0.5)
if r is not None:
print(f'{i} -> {len(r)}')
if __name__ == '__main__':
mt5.initialize()
print(mt5.__version__)
asyncio.run(main())
mt5.shutdown()
There is a bug in copy rates. I posted this several times but nobody has acknowledged it.
for count in range(maxbars):
rates = mt5.copy_rates_from_pos('EURUSD', mt5.TIMEFRAME_M1, 0, count)
errno, strerror = mt5.last_error()
if errno != mt5.RES_S_OK:
print(f"Failed on count={count} with strerror={strerror}")
break
I was getting failures requesting maxbars as the "count" parameter so I wrote a simple routine that grows the number of bars requested on each iteration as a unit-test to test the function. This should complete without issues, but given that you can only call it a finite number of times makes it unreliable.
I was getting failures requesting maxbars as the "count" parameter so I wrote a simple routine that grows the number of bars requested on each iteration as a unit-test to test the function. This should complete without issues, but given that you can only call it a finite number of times makes it unreliable.
This issue fixed in 5.0.33, it was released with not automatically freeing numpy arrays.
if r.retcode != TRADE_RETCODE_REQUOTE and r.retcode != TRADE_RETCODE_PRICE_OFF: AttributeError: 'NoneType' object has no attribute 'retcode'
Example from help works. Applying an example in my function, it says
if result_buy.retcode != mt5.TRADE_RETCODE_DONE: AttributeError: 'NoneType' object has no attribute 'retcode'
Update to 5.0.33, Buy/Close script is fixed there, they will still return error as None, but now you can call mt5.last_error() without exception and see error code.
What were the current prices (Bid and Ask)? What was the freeze level (SYMBOL_TRADE_FREEZE_LEVEL)?
Not sure about the FREEZE levels?
Ask price is being used for my buy and bid price for my shorts.
I have tried hardcoding my sl and tp now.
Just strange that it will Buy and Sell perfect for hours and then stop.
Still writes:
if r.retcode != TRADE_RETCODE_REQUOTE and r.retcode != TRADE_RETCODE_PRICE_OFF:
AttributeError: 'NoneType' object has no attribute 'retcode'
Run Help Example for https://www.mql5.com/ru/docs/integration/python_metatrader5/mt5ordersend_py
Example from help works.
I used an example in my function and wrote:
if result_buy.retcode != mt5.TRADE_RETCODE_DONE:
AttributeError: 'NoneType' object has no attribute 'retcode'
It still writes:
The example from the help works.Applying the example in my function, it says:
if result_buy.retcode != mt5.TRADE_RETCODE_DONE:
AttributeError: 'NoneType' object has no attribute 'retcode'
So you got result_buy==None, didn't check the value and tried to get the retcode from None?
The new MT5-terminal beta along with the MetaTrader5 5.0.31 can only sustain about 7700 calls to copy_rates_from_pos() before it completely locks up the thread. In order to demonstrate this bug I have implemented a timeout using asyncio. The timeout works on the control, however, it does not work for copy_rates_from_pos because the thread completely freezes due to the bug.
Even shutting down and re-initialising the connection doesn't help.
There is a bug in copy rates. I posted this several times but nobody has acknowledged it.
Question: What is this cycle for?
Question: Why this cycle?
I was getting failures requesting maxbars as the "count" parameter so I wrote a simple routine that grows the number of bars requested on each iteration as a unit-test to test the function. This should complete without issues, but given that you can only call it a finite number of times makes it unreliable.
I was getting failures requesting maxbars as the "count" parameter so I wrote a simple routine that grows the number of bars requested on each iteration as a unit-test to test the function. This should complete without issues, but given that you can only call it a finite number of times makes it unreliable.
Still writes:
if r.retcode != TRADE_RETCODE_REQUOTE and r.retcode != TRADE_RETCODE_PRICE_OFF:
AttributeError: 'NoneType' object has no attribute 'retcode'
Example from help works.
Applying an example in my function, it says
if result_buy.retcode != mt5.TRADE_RETCODE_DONE:
AttributeError: 'NoneType' object has no attribute 'retcode'
ckeiderling:
...
I get the same problem using mt5.copy_ticks_range () and mt5.copy_rates_range (). I have to reset the kernel in order to clear out the memory.
...