MetaTrader 5 Python User Group - the summary - page 23

Irfan Harun
11
Irfan Harun  

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'


Keith Watford
Moderator
30471
Keith Watford  
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.
Joao Pedro Aguilera Cardoso
187
Joao Pedro Aguilera Cardoso  

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!

iloveforex
975
iloveforex  

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.