MetaTrader 5 Python User Group - the summary - page 23

 

Hello All, 


I've recently started using python with MT5. I'm currently able to enter and exit trades using python API but I'm not able to update the stop loss with python api

on executing the function, I don't get any error or success message


def check_if_long_sl_can_be_improved(symbol, lot):

    last_tick()

    print()

    row = last_tick()

    row = row.tail(1)

    fast_wma = row['fast_wma']

    slow_wma = row['slow_wma']

    rsi = row['rsi14']

    open_trades = TradeHistory.objects.filter(type='long', status='open')

    try:

        if open_trades[0].type == 'long':

            print('check if long sl can be improved')

            price=mt5.symbol_info_tick(symbol).bid

            entry_price = open_trades[0].entry_price

            initial_stoploss = open_trades[0].initial_stoploss


            # if round(price - entry_price, 2) > 0.1:

            if True:

                profit = price - entry_price

                latest_stoploss = initial_stoploss + profit

                temp_take_profit = entry_price + 3

                request = {

                    "action": mt5.TRADE_ACTION_SLTP,

                    "symbol": symbol,

                    "volume": lot,

                    "type": mt5.ORDER_TYPE_BUY,

                    # "type": mt5.ORDER_TYPE_SELL,

                    "position": open_trades[0].order_id,

                    "price_open": entry_price,

                    "sl": latest_stoploss,

                    "tp": temp_take_profit,

                    "deviation": 20,

                    "magic": 333333,

                    "comment": "python sl updated",

                    "type_time": mt5.ORDER_TIME_GTC,

                    "type_filling": mt5.ORDER_FILLING_FOK,

                    "ENUM_ORDER_STATE": mt5.ORDER_FILLING_RETURN,

                }

                print(request)

                try:

                    print('try')

                    result = mt5.order_send(request)

                    print(result)

                    print('try')

                except Exception as e:

                    print(e)



                if result.retcode != mt5.TRADE_RETCODE_DONE:

                    show_buy_error(result)

                else:

                    show_buy_Success(result)

                    trade_to_update = TradeHistory.objects.get(order_id=open_trades[0].order_id)

                    trade_to_update.latest_stoploss = latest_stoploss

                    trade_to_update.save()

                    print(trade_to_update.get_fields())

    except:

        pass

    print()

    return 'check_if_long_sl_can_be_improved'


 
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.
 

Hey everyone! I have a question:

I see that is possible to get MetaTrader data into Python, but I'm trying to do something a little different.

I want to get information via API of an exchange and then generate a graph on MetaTrader. Basically, send historical and live prices data from python to metatrader, who will receive this data and then plot the graph into a custom symbol.

Is this possible?

After that I want to run and test some EAs on the symbol, but this is for another project phase!


Thanks!

 

Hi,

How to run MetaTrader 5 strategy tester in the background using a Python script? 

I would like to send several EAs for backtesting in MT5 using multiple symbols and multiple timeframes. Then obtain the results.

 

Hello,

I'am in ubuntu 20.04 and metatrader5 runs fine in the current wine environment. I have a lot of mql5-code i want to migrate to python.

Does anyone now, how to make metatrade5 python environment work on linux-wine environment?

That would be very helpful and i hope, i'am not the only one with this problem.

many thanks in Advance

Claus

 
"MetaTrader 5 Python Integration for Beginners" - the forum thread (in Russian, with screenshots and examples)
 

Having problems with history_deals_get() - it does not fetch closed orders until after a long time (hours), once an order is closed I need to access it. Any ideas/help?

Also the "group" parameter does nothing. 

 
Mike Oregon :

Having problems with history_deals_get() - it does not fetch closed orders until after a long time (hours), once an order is closed I need to access it. Any ideas/help?

Also the "group" parameter does nothing. 

Quick solution: add a few days to 'to_date'.

 
Vladimir Karputov:

Quick solution: add a few days to 'to_date'.

Worked like a charm, thanks Vladimir

 

Hello,

I'm unable to close a trade(manually opened for testing purposes) with the mt5.Close() function, using the symbol and ticket number,

i.e: mt5.Close(symbol='GBPJPY', ticket = 233458963)

Does it still work?

I'm using 'MetaTrader5 - 3.0.34'

Reason: