使用MT5 API和Python进行交易
Posted 平静愉悦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用MT5 API和Python进行交易相关的知识,希望对你有一定的参考价值。
第一批关键功能之一就是以编程方式打开交易。既然您已经了解了如何打开与MT5的连接并提取数据。在今天的帖子中,我将向您展示如何使用MT5 API和Python进行交易。
验证仪器是否存在
让我们从上一篇文章的连接代码开始:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">import</span> MetaTrader5 <span style="color:#c72c4c">as</span> mt5
<span style="color:#c72c4c">from</span> datetime <span style="color:#c72c4c">import</span> datetime
account <span style="color:#c72c4c">=</span> <em><span style="color:#407b08">int</span></em><span style="color:#5f5f5f">(</span>YOUR ID HERE<span style="color:#5f5f5f">)</span>
mt5<span style="color:#5f5f5f">.</span>initialize<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span>
authorized<span style="color:#c72c4c">=</span>mt5<span style="color:#5f5f5f">.</span>login<span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> authorized<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Connected: Connecting to MT5 Client"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">else</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Failed to connect at account #, error code: "</span>
<span style="color:#5f5f5f">.</span><em><span style="color:#407b08">format</span></em><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">,</span> mt5<span style="color:#5f5f5f">.</span>last_error<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span></code></span></span>
让我们通过建立一个使用帐号的名为connect的方法来进行清理:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">import</span> MetaTrader5 <span style="color:#c72c4c">as</span> mt5
<span style="color:#c72c4c">from</span> datetime <span style="color:#c72c4c">import</span> datetime
<em><span style="color:#c72c4c">def</span></em> <span style="color:#0077aa">connect</span><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
account <span style="color:#c72c4c">=</span> <em><span style="color:#407b08">int</span></em><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span>
mt5<span style="color:#5f5f5f">.</span>initialize<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span>
authorized<span style="color:#c72c4c">=</span>mt5<span style="color:#5f5f5f">.</span>login<span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> authorized<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Connected: Connecting to MT5 Client"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">else</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Failed to connect at account #, error code: "</span>
<span style="color:#5f5f5f">.</span><em><span style="color:#407b08">format</span></em><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">,</span> mt5<span style="color:#5f5f5f">.</span>last_error<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span>
</code></span></span>
接下来,您将要创建另一个方法,并使用以下签名将其命名为open_position:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><em><span style="color:#c72c4c">def</span></em> <span style="color:#0077aa">openPosition</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> order_type<span style="color:#5f5f5f">,</span> size<span style="color:#5f5f5f">,</span> tp_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">,</span> stop_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span></code></span></span>
在开始交易之前,建议添加一些安全检查。这是要确保要执行其订单的工具存在并且可以与经纪人进行交易:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><em><span style="color:#c72c4c">def</span></em> <span style="color:#0077aa">open_position</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> order_type<span style="color:#5f5f5f">,</span> size<span style="color:#5f5f5f">,</span> tp_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">,</span> stop_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
symbol_info <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> symbol_info <span style="color:#c72c4c">is</span> <span style="color:#990055">None</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"not found"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">return</span>
<span style="color:#c72c4c">if</span> <span style="color:#c72c4c">not</span> symbol_info<span style="color:#5f5f5f">.</span>visible<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"is not visible, trying to switch on"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> <span style="color:#c72c4c">not</span> mt5<span style="color:#5f5f5f">.</span>symbol_select<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#990055">True</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"symbol_select() failed, exit"</span><span style="color:#5f5f5f">,</span>pair<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">return</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"found!"</span><span style="color:#5f5f5f">)</span></code></span></span>
为确保此代码有效,让我们尝试连接并打开不存在的仪器上的位置:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> connect<span style="color:#5f5f5f">(</span><span style="color:#990055">39672374</span><span style="color:#5f5f5f">)</span>
Connected<span style="color:#5f5f5f">:</span> Connecting to MT5 Client
<span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> open_position<span style="color:#5f5f5f">(</span><span style="color:#407b08">"Fake_Pair"</span><span style="color:#5f5f5f">,</span> <span style="color:#407b08">"BUY"</span><span style="color:#5f5f5f">,</span> <span style="color:#990055">1</span><span style="color:#5f5f5f">)</span>
Fake_Pair <span style="color:#c72c4c">not</span> found</code></span></span>
当我们使用参数“ Fake_Pair”调用open_position方法时,它未能按预期找到该对。
现在尝试使用有效的货币对,例如“ EURUSD”:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> connect<span style="color:#5f5f5f">(</span><span style="color:#990055">39672374</span><span style="color:#5f5f5f">)</span>
Connected<span style="color:#5f5f5f">:</span> Connecting to MT5 Client
<span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> open_position<span style="color:#5f5f5f">(</span><span style="color:#407b08">"EURUSD"</span><span style="color:#5f5f5f">,</span> <span style="color:#407b08">"BUY"</span><span style="color:#5f5f5f">,</span> <span style="color:#990055">1.0</span><span style="color:#5f5f5f">)</span>
EURUSD found!</code></span></span>
您可以在上面看到这对货币对已经找到并且可以交易了。
执行订单
现在,我们准备使用带有Python的MT5 API进行交易。您需要做的第一件事是获取您正在交易的工具的“点”。该点定义为工具可能发生的最小价格变动幅度。
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python">point <span style="color:#c72c4c">=</span> symbol_info<span style="color:#5f5f5f">.</span>point
volume_step <span style="color:#c72c4c">=</span> symbol_info<span style="color:#5f5f5f">.</span>volume_step</code></span></span>
接下来,创建一个if语句来检查您的订单是买还是卖。这很重要,因为根据您是购买还是出售,止损和获利的计算方法有所不同。
方法签名中的止损和获利是距原始价格的点差。对于MT5,您需要提供止损并获利价格(如果您使用止损并获利–它们也可以留为空白)。要计算此值,您必须采用当前价格,然后将止损/获利乘以点,再加上或减去。下面是一个示例:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>order_type <span style="color:#c72c4c">==</span> <span style="color:#407b08">"BUY"</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
order <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TYPE_BUY
price <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info_tick<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">.</span>ask
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>stop_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
sl <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">-</span> <span style="color:#5f5f5f">(</span>stop_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>tp_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
tp <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">+</span> <span style="color:#5f5f5f">(</span>tp_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>order_type <span style="color:#c72c4c">==</span> <span style="color:#407b08">"SELL"</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
order <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TYPE_SELL
price <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info_tick<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">.</span>bid
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>stop_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
sl <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">+</span> <span style="color:#5f5f5f">(</span>stop_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>tp_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
tp <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">-</span> <span style="color:#5f5f5f">(</span>tp_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span></code></span></span>
现在,该调用实际提交订单的API了。为此,我们将使用MT5 API中的order_send()方法。但是首先,让我们建立我们的请求。可以在此处找到MT5 API可用方法的完整列表。
该请求是具有以下键的字典:
- 行动
- 符号
- 卷
- 类型
- 价钱
- 魔法
- 评论
- type_time
- 类型填充
如果您想知道神秘的参数“ magic”是什么意思,它实际上只是一个客户ID(可以设置为任何数字)。在我们的例子中,请求的编码如下:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python">request <span style="color:#c72c4c">=</span> <span style="color:#5f5f5f"></span>
<span style="color:#407b08">"action"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>TRADE_ACTION_DEAL<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"symbol"</span><span style="color:#5f5f5f">:</span> pair<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"volume"</span><span style="color:#5f5f5f">:</span> <em><span style="color:#407b08">float</span></em><span style="color:#5f5f5f">(</span>size<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type"</span><span style="color:#5f5f5f">:</span> order<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"price"</span><span style="color:#5f5f5f">:</span> price<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"sl"</span><span style="color:#5f5f5f">:</span> sl<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"tp"</span><span style="color:#5f5f5f">:</span> tp<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"magic"</span><span style="color:#5f5f5f">:</span> <span style="color:#990055">234000</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"comment"</span><span style="color:#5f5f5f">:</span> <span style="color:#407b08">"My first trade"</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type_time"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TIME_GTC<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type_filling"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>ORDER_FILLING_IOC<span style="color:#5f5f5f">,</span>
<span style="color:#5f5f5f"></span></code></span></span>
要发送请求,请使用order_send()方法:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"> result <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>order_send<span style="color:#5f5f5f">(</span>request<span style="color:#5f5f5f">)</span></code></span></span>
最后,您将要检查您的订单是否成功。您可以转到MT5桌面应用程序并检查您的帐户,或者在order_send下添加以下代码来执行此操作:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">if</span> result<span style="color:#5f5f5f">.</span>retcode <span style="color:#c72c4c">!=</span> mt5<span style="color:#5f5f5f">.</span>TRADE_RETCODE_DONE<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Failed to send order :("</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">else</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span> <span style="color:#5f5f5f">(</span><span style="color:#407b08">"Order successfully placed!"</span><span style="color:#5f5f5f">)</span></code></span></span>
测试我们的代码
如果您在本教程中一直遵循,那么您应该具有与以下代码相似的内容:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">import</span> MetaTrader5 <span style="color:#c72c4c">as</span> mt5
<span style="color:#c72c4c">from</span> datetime <span style="color:#c72c4c">import</span> datetime
<em><span style="color:#c72c4c">def</span></em> <span style="color:#0077aa">connect</span><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
account <span style="color:#c72c4c">=</span> <em><span style="color:#407b08">int</span></em><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span>
mt5<span style="color:#5f5f5f">.</span>initialize<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span>
authorized<span style="color:#c72c4c">=</span>mt5<span style="color:#5f5f5f">.</span>login<span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> authorized<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Connected: Connecting to MT5 Client"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">else</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Failed to connect at account #, error code: "</span>
<span style="color:#5f5f5f">.</span><em><span style="color:#407b08">format</span></em><span style="color:#5f5f5f">(</span>account<span style="color:#5f5f5f">,</span> mt5<span style="color:#5f5f5f">.</span>last_error<span style="color:#5f5f5f">(</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">)</span>
<em><span style="color:#c72c4c">def</span></em> <span style="color:#0077aa">open_position</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> order_type<span style="color:#5f5f5f">,</span> size<span style="color:#5f5f5f">,</span> tp_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">,</span> stop_distance<span style="color:#c72c4c">=</span><span style="color:#990055">None</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
symbol_info <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> symbol_info <span style="color:#c72c4c">is</span> <span style="color:#990055">None</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"not found"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">return</span>
<span style="color:#c72c4c">if</span> <span style="color:#c72c4c">not</span> symbol_info<span style="color:#5f5f5f">.</span>visible<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"is not visible, trying to switch on"</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> <span style="color:#c72c4c">not</span> mt5<span style="color:#5f5f5f">.</span>symbol_select<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#990055">True</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"symbol_select() failed, exit"</span><span style="color:#5f5f5f">,</span>pair<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">return</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">,</span> <span style="color:#407b08">"found!"</span><span style="color:#5f5f5f">)</span>
point <span style="color:#c72c4c">=</span> symbol_info<span style="color:#5f5f5f">.</span>point
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>order_type <span style="color:#c72c4c">==</span> <span style="color:#407b08">"BUY"</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
order <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TYPE_BUY
price <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info_tick<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">.</span>ask
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>stop_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
sl <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">-</span> <span style="color:#5f5f5f">(</span>stop_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>tp_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
tp <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">+</span> <span style="color:#5f5f5f">(</span>tp_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>order_type <span style="color:#c72c4c">==</span> <span style="color:#407b08">"SELL"</span><span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
order <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TYPE_SELL
price <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>symbol_info_tick<span style="color:#5f5f5f">(</span>pair<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">.</span>bid
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>stop_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
sl <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">+</span> <span style="color:#5f5f5f">(</span>stop_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span><span style="color:#5f5f5f">(</span>tp_distance<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">:</span>
tp <span style="color:#c72c4c">=</span> price <span style="color:#c72c4c">-</span> <span style="color:#5f5f5f">(</span>tp_distance <span style="color:#c72c4c">*</span> point<span style="color:#5f5f5f">)</span>
request <span style="color:#c72c4c">=</span> <span style="color:#5f5f5f"></span>
<span style="color:#407b08">"action"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>TRADE_ACTION_DEAL<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"symbol"</span><span style="color:#5f5f5f">:</span> pair<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"volume"</span><span style="color:#5f5f5f">:</span> <em><span style="color:#407b08">float</span></em><span style="color:#5f5f5f">(</span>size<span style="color:#5f5f5f">)</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type"</span><span style="color:#5f5f5f">:</span> order<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"price"</span><span style="color:#5f5f5f">:</span> price<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"sl"</span><span style="color:#5f5f5f">:</span> sl<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"tp"</span><span style="color:#5f5f5f">:</span> tp<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"magic"</span><span style="color:#5f5f5f">:</span> <span style="color:#990055">234000</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"comment"</span><span style="color:#5f5f5f">:</span> <span style="color:#407b08">""</span><span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type_time"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>ORDER_TIME_GTC<span style="color:#5f5f5f">,</span>
<span style="color:#407b08">"type_filling"</span><span style="color:#5f5f5f">:</span> mt5<span style="color:#5f5f5f">.</span>ORDER_FILLING_IOC<span style="color:#5f5f5f">,</span>
<span style="color:#5f5f5f"></span>
result <span style="color:#c72c4c">=</span> mt5<span style="color:#5f5f5f">.</span>order_send<span style="color:#5f5f5f">(</span>request<span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">if</span> result<span style="color:#5f5f5f">.</span>retcode <span style="color:#c72c4c">!=</span> mt5<span style="color:#5f5f5f">.</span>TRADE_RETCODE_DONE<span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span><span style="color:#5f5f5f">(</span><span style="color:#407b08">"Failed to send order :("</span><span style="color:#5f5f5f">)</span>
<span style="color:#c72c4c">else</span><span style="color:#5f5f5f">:</span>
<span style="color:#c72c4c">print</span> <span style="color:#5f5f5f">(</span><span style="color:#407b08">"Order successfully placed!"</span><span style="color:#5f5f5f">)</span>
</code></span></span>
要测试上面的代码是否有效,您需要调用这两种方法。在下面的示例中,我使用1标准手,300的止盈距离和100的止损距离开立EURUSD的交易:
<span style="color:#4c4c4c"><span style="color:#1f1e1e"><code class="language-python"><span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> connect<span style="color:#5f5f5f">(</span><span style="color:#990055">39672374</span><span style="color:#5f5f5f">)</span>
Connected<span style="color:#5f5f5f">:</span> Connecting to MT5 Client
<span style="color:#c72c4c">>></span><span style="color:#c72c4c">></span> open_position<span style="color:#5f5f5f">(</span><span style="color:#407b08">"EURUSD"</span><span style="color:#5f5f5f">,</span> <span style="color:#407b08">"BUY"</span><span style="color:#5f5f5f">,</span> <span style="color:#990055">1</span><span style="color:#5f5f5f">,</span> <span style="color:#990055">300</span><span style="color:#5f5f5f">,</span> <span style="color:#990055">100</span><span style="color:#5f5f5f">)</span>
EURUSD found!
Order successfully placed!</code></span></span>
如果您回到MT5桌面应用程序,则应该在您的帐户上进行新交易:
您可以在此处看到买入价为1.21717,止损比买入价(1.21617)低100点,止盈为300(1.22017)多。
在这里还是要推荐下我自己建的Python学习群:721195303,群里都是学Python的,如果你想学或者正在学习Python ,欢迎你加入,大家都是软件开发党,不定期分享干货(只有Python软件开发相关的),包括我自己整理的一份2021最新的Python进阶资料和零基础教学,欢迎进阶中和对Python感兴趣的小伙伴加入!
以上是关于使用MT5 API和Python进行交易的主要内容,如果未能解决你的问题,请参考以下文章