Trading bot problem ( Python script)

 
Does anybody know how to fix this?
Files:
test.png  30 kb
 
sasoprendl :
Does anybody know how to fix this?

The picture is difficult to treat. Usually open source is needed for treatment.

 
Vladimir Karputov:

The picture is difficult to treat. Usually open source is needed for treatment.

***


This is the code, the error is in the ( price = mt5.symbol_info_tick(symbol).bid ) line

Documentation on MQL5: Integration / MetaTrader for Python / order_send
Documentation on MQL5: Integration / MetaTrader for Python / order_send
  • www.mql5.com
order_send - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
sasoprendl :

Please correct your message - the code must be inserted using the button  Code

 
price = mt5.symbol_info_tick(symbol).bid is the error
def close_trade(action, buy_request, result, deviation, magic):
    print('closing')
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
    '''
    # create a close request
    symbol = buy_request['symbol']
    if action == 'buy':
        trade_type = mt5.ORDER_TYPE_BUY
        price = mt5.symbol_info_tick(symbol).ask
    elif action == 'sell':
        trade_type = mt5.ORDER_TYPE_SELL
        price = mt5.symbol_info_tick(symbol).bid
    position_id = result.order
    lot = buy_request['volume']

    close_request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": trade_type,
        "position": position_id,
        "price": price,
        "deviation": deviation,
        "magic": magic,
        "comment": "python script close",
        "type_time": mt5.ORDER_TIME_GTC,  # good till cancelled
        "type_filling": mt5.ORDER_FILLING_FOK,
    }
    # send a close request
    result = mt5.order_send(close_request)
    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]))
    mt5.shutdown()


 
sasoprendl :
price = mt5.symbol_info_tick(symbol).bid is the error

Doesn't compile. Lots of compilation errors.

 

Script - No Compilation Errors:

# Copyright © 2020, Vladimir Karputov
# https://www.mql5.com

from datetime import datetime
import MetaTrader5 as mt5

mt5.initialize()

def close_trade(action, buy_request, result, deviation, magic):
    print('closing')
    '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
    '''
    # create a close request
    symbol = buy_request['symbol']
    if action == 'buy':
        trade_type = mt5.ORDER_TYPE_BUY
        price = mt5.symbol_info_tick(symbol).ask
    elif action == 'sell':
        trade_type = mt5.ORDER_TYPE_SELL
        price = mt5.symbol_info_tick(symbol).bid
    position_id = result.order
    lot = buy_request['volume']

    close_request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": trade_type,
        "position": position_id,
        "price": price,
        "deviation": deviation,
        "magic": magic,
        "comment": "python script close",
        "type_time": mt5.ORDER_TIME_GTC,  # good till cancelled
        "type_filling": mt5.ORDER_FILLING_FOK,
    }
    # send a close request
    result = mt5.order_send(close_request)
    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]))


mt5.shutdown()
Files:
test.py  2 kb
 

hello can any one help me how can create code by python to read open ,high,low,close for when  real time create candle