実現したいこと
mt5のAPIを使ってDiscordで「USDJPY ショート」と入れたらその文字を取得して
MT5のAPIで自動売買が出来るようにしようとしています。
前提
ChatGPTに質問しましたが、MT5のアカウントIDとパスワードが違うとしか言われないので、
解決に至りませんでした。
発生している問題・エラーメッセージ
Failed to connect to account at MetaTrader terminal, error code = (-2, 'Terminal: Invalid params')
アカウントIDとパスワードが違うと言われるのですが、そもそもAPIの指定の仕方が違うのではないかと思っています。
詳しい方、教えていただけますと幸いです。
該当のソースコード
python
1import discord 2import MetaTrader5 as mt5 3from discord.ext import commands 4from pip._vendor.rich.prompt import password 5 6TOKEN = 'TOKEN_ID 7CHANNEL_ID = CHANNEL_ID # Replace with your channel ID 8KEYWORDS = ['ロング', 'ショート'] 9 10intents = discord.Intents.all() 11 12if not mt5.initialize(): 13 print("initialize() failed, error code =", mt5.last_error()) 14 quit() 15 16authorized = mt5.login(accountID,password) 17if not authorized: 18 print("Failed to connect to account at MetaTrader terminal, error code =", mt5.last_error()) 19 mt5.shutdown() 20 quit() 21 22 23 24bot = commands.Bot(command_prefix='$', intents=intents) 25 26 27@bot.event 28async def on_ready(): 29 print(f'{bot.user.name} is connected!') 30 31 32@bot.event 33async def on_message(message): 34 if message.author == bot.user: 35 return 36 37 if message.channel.id == CHANNEL_ID: 38 print(f'Message received in the correct channel: {message.content}') 39 for keyword in KEYWORDS: 40 if keyword in message.content: 41 print(f'Message saved: {message.content}') 42 # Execute trade if the message contains "USDJPY short" 43 if 'USDJPY ショート' in message.content: 44 trade_short_usdjpy() 45 break 46 else: 47 print(f'Message received in the wrong channel: {message.content}') 48 49 50def trade_short_usdjpy(): 51 symbol = "USDJPY" 52 lot_size = 0.01 # Replace with your desired lot size 53 deviation = 20 # Replace with your desired deviation 54 request = { 55 "action": mt5.TRADE_ACTION_DEAL, 56 "symbol": symbol, 57 "volume": lot_size, 58 "type": mt5.ORDER_TYPE_SELL, 59 "price": mt5.symbol_info_tick(symbol).ask, 60 "sl": 0, 61 "tp": 0, 62 "deviation": deviation, 63 "magic": 0, 64 "comment": "Short USDJPY via Discord", 65 "type_time": mt5.ORDER_TIME_GTC, 66 "type_filling": mt5.ORDER_FILLING_FOK, 67 } 68 69 result = mt5.order_send(request) 70 if result.retcode != mt5.TRADE_RETCODE_DONE: 71 print("Failed to send order:", result.comment) 72 else: 73 print("USDJPY short order successfully placed with ticket #", result.order) 74 75 76@bot.event 77async def on_disconnect(): 78 mt5.shutdown() 79 print("MetaTrader 5 connection closed.") 80 81 82bot.run(TOKEN) 83
試したこと
補足情報(FW/ツールのバージョンなど)
eclipseでpythonの環境を作成し、実施しています。
必要なライブラリ等は揃っています。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。