質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

685閲覧

FX MT5のエラーについて

pythonlearning

総合スコア5

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/03/27 17:32

実現したいこと

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の環境を作成し、実施しています。
必要なライブラリ等は揃っています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

authorized = mt5.login(accountID,password="<password>",server="server名",)
にしたら動きました。

投稿2023/03/27 19:52

pythonlearning

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問