Thanks for sharing this code ! I am able to close orders, for example a buy, by doing a sell and then doing a close_by...but when I look at the
history of the deals these operations are missing
You need to convert the namedtuples being returned to dict. I added a helper function
to the pymt5adapter package to help with that.
import pymt5adapter as mta
import pandas as pd
def main():
deals = mta.history_deals_get()
deals = mta.as_dict_all(deals)
history_deals_df = pd.DataFrame(deals, columns=deals[0].keys())
print(history_deals_df)
if __name__ == '__main__':
with mta.connected():
main()
...and if you only want to work exclusively with dict return types for all functions you can modify the API via the context manager kwarg
"return_as_dict".
import pymt5adapter as mta
import pandas as pd
def main():
deals = mta.history_deals_get()
history_deals_df = pd.DataFrame(deals, columns=deals[0].keys())
print(history_deals_df)
if __name__ == '__main__':
with mta.connected(return_as_dict=True):
main()
That's how you close either with MT5. In fact, there is even a Close function included in the MetaTrader5 module so your code could be greatly reduced and refactored to something like this.
# request the result as a dictionary and display it element by element # request the result as a dictionary and display it element by element 2. order_send done, OrderSendResult(retcode=10009, deal=535084512, order=557416535, volume=0.1, price=108.023, ...
had the same problem when working with a Pandas Dataframe. fix it by changing the data type of position_ticket. initially it was sent as <class 'numpy.int64'>, it was changed to type <class 'int'>.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Thanks for sharing this code ! I am able to close orders, for example a buy, by doing a sell and then doing a close_by...but when I look at the history of the deals these operations are missing
do you also have this problem?
from_date=dt.datetime(2000,1,1)
to_date=dt.datetime.now()
history_deals=mt5.history_deals_get(from_date, to_date)
history_deals_df=pd.DataFrame(list(history_deals),columns=history_deals[0]._asdict().keys())
Problem is here
You need to convert the namedtuples being returned to dict. I added a helper function to the pymt5adapter package to help with that.
...and if you only want to work exclusively with dict return types for all functions you can modify the API via the context manager kwarg "return_as_dict".
That's how you close either with MT5. In fact, there is even a Close function included in the MetaTrader5 module so your code could be greatly reduced and refactored to something like this.
Hi All
I'm new and am trying to create a python bot. I found codes on MT5 forum that execute trades, however it always returns None.
Would you guys know what is the problem, as I cant seem to know why its always returning None when i have all the required paramenters.
trade = open_trade('buy', currency, 10, 500, 2000, 10) print(trade) ====================================== C:\Users\andyq\PycharmProjects\BinanceTrading\venv\Scripts\python.exe "C:/Users/andyq/PycharmProjects/BinanceTrading/data preparation.py" (None, {'action': 1, 'symbol': 'GBPUSD', 'volume': 10, 'type': 0, 'price': 1.26224, 'sl': 1.26174, 'tp': 1.26424, 'deviation': 10, 'magic': 9986989, 'comment': 'sent by python', 'type_time': 0, 'type_filling': 2}) Process finished with exit code 0I was also having the problem of result = None.
I got an answer when I changed the type of the value that I was putting at request.volume from int to float, for example:
After casting to float, it worked. (you might as well already use a variable that is a float)
Hope it helps anybody.
had the same problem when working with a Pandas Dataframe. fix it by changing the data type of position_ticket. initially it was sent as <class 'numpy.int64'>, it was changed to type <class 'int'>.
import MetaTrader5 as mt5
import MetaTrader5 as mt5
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.
I was also having the problem of result = None.
I got an answer when I changed the type of the value that I was putting at request.volume from int to float, for example:
After casting to float, it worked. (you might as well already use a variable that is a float)
Hope it helps anybody.
this works for me
Thanks!
Just want to add that I had the same problem of getting None returned after calling order_send.
My problem was the comment was too long. I am not sure what the limit is, but it cannot be too long.
Guys, I did it this way and it worked
close_requisicao = { "action": mt5.TRADE_ACTION_DEAL, "symbol": ativo.nome(), "volume": float(df_posicoes.loc[operacao, "volume"]), "type": typetrade, "position": int(df_posicoes.loc[operacao, "ticket"]), "price": float(df_posicoes.loc[operacao, "price_current"]), "deviation": 50, "magic": int(df_posicoes.loc[operacao, "magic"]), "comment": df_posicoes.loc[operacao, "comment"], "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_IOC, }