基于价格与移动平均线距离的EA策略及编程思路-EA分享网_交易学习APP_国内知名的交易学习教程及外汇智能量化交易EA分享网站MQL4/MQL5编程社群-EA分享网_交易学习APP_国内知名的交易学习教程及外汇智能量化交易EA分享网站交易软件-EA分享网_交易学习APP_国内知名的交易学习教程及外汇智能量化交易EA分享网站EA分享网社群_交易学习APP

基于价格与移动平均线距离的EA策略及编程思路

当往mt4图表上插入一条移动平均线(Moving Average)后,整个图表就含有两个主要对象,一是插入的这条移动平均线,二是蜡烛图(或者K线图)。这时候整个图表看起来比较简洁,当然,也可以说简单。

如果就以这一条均线结合k线价格来开发EA,看起来是有些单纯,可能盈利的概率也不是很大,但作为一种爱好,也不凡研究研究,折腾折腾。

基于移动平均线的EA策略及编程思路

要根据这种图表信号开发EA,首先要获取价格及K线的相关数据,假设插入的是一条200SMA,那就先用iMA()函数获取几个单位均线的值,如:

<span class=“kwd”>double</span><span class=“pln”> MA_200_0 </span><span class=“pun”>=</span><span class=“pln”> iMA</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>200</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>,</span><span class=“pln”> MODE_SMA</span><span class=“pun”>,</span><span class=“pln”> PRICE_CLOSE</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>);</span>
<span class=“kwd”>double</span><span class=“pln”> MA_200_1 </span><span class=“pun”>=</span><span class=“pln”> iMA</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>200</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>,</span><span class=“pln”> MODE_SMA</span><span class=“pun”>,</span><span class=“pln”> PRICE_CLOSE</span><span class=“pun”>,</span> <span class=“lit”>1</span><span class=“pun”>);</span>
<span class=“kwd”>double</span><span class=“pln”> MA_200_2 </span><span class=“pun”>=</span><span class=“pln”> iMA</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>200</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>,</span><span class=“pln”> MODE_SMA</span><span class=“pun”>,</span><span class=“pln”> PRICE_CLOSE</span><span class=“pun”>,</span> <span class=“lit”>2</span><span class=“pun”>);</span>

当然,也可以定义一个数组,然后结合for循环来获取所需的均线值。iMA()函数中的参量,根据策略的不同来变更参数类型。

要比较价格和均线的变化,那还需要获取K线相关的价格,主要是四个:开盘价iOpen、收盘价iClose、最低价iLow、最高价iHigh。如:

<span class=“kwd”>double</span> <span class=“typ”>Open_0</span> <span class=“pun”>=</span><span class=“pln”> iOpen</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Open_1</span> <span class=“pun”>=</span><span class=“pln”> iOpen</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>1</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Open_2</span> <span class=“pun”>=</span><span class=“pln”> iOpen</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>2</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Close_0</span> <span class=“pun”>=</span><span class=“pln”> iClose</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Close_1</span> <span class=“pun”>=</span><span class=“pln”> iClose</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>1</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Close_2</span> <span class=“pun”>=</span><span class=“pln”> iClose</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>2</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Low_0</span> <span class=“pun”>=</span><span class=“pln”> iLow</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Low_1</span> <span class=“pun”>=</span><span class=“pln”> iLow</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>1</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>Low_2</span> <span class=“pun”>=</span><span class=“pln”> iLow</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>2</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>High_0</span> <span class=“pun”>=</span><span class=“pln”> iHigh</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>0</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>High_1</span> <span class=“pun”>=</span><span class=“pln”> iHigh</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>1</span><span class=“pun”>);</span>
<span class=“kwd”>double</span> <span class=“typ”>High_2</span> <span class=“pun”>=</span><span class=“pln”> iHigh</span><span class=“pun”>(</span><span class=“typ”>Symbol</span><span class=“pun”>(),</span><span class=“lit”>0</span><span class=“pun”>,</span> <span class=“lit”>2</span><span class=“pun”>);</span>

上面函数中的0、1、2分别是指从图表右侧开始第一、第二、第三个K线所对应的数据值,包括均线也是如此排序。

作为一条均线,其最简单的交易思路是价格站稳均线上方买入,站稳下方卖出。如:

<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“typ”>Close_0</span> <span class=“pun”>></span><span class=“pln”> MA_0 </span><span class=“pun”>&&</span> <span class=“typ”>Close_1</span> <span class=“pun”>></span><span class=“pln”> MA_1</span><span class=“pun”>)</span><span class=“com”>//简单定义两个单位价格站稳均线上方</span>
<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“typ”>Close_0</span> <span class=“pun”><</span><span class=“pln”> MA_0 </span><span class=“pun”>&&</span> <span class=“typ”>Close_1</span> <span class=“pun”><</span><span class=“pln”> MA_1</span><span class=“pun”>)</span><span class=“com”>//简单定义两个单位价格站稳均线下方</span>

进一步控制买卖条件,可以定义均线处于上升状态买入,下降状态卖出。如:

<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“pln”>MA_0 </span><span class=“pun”>></span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span><span class=“pln”> MA_1 </span><span class=“pun”>></span><span class=“pln”> MA_2</span><span class=“pun”>)</span><span class=“com”>//简单定义两个单位均线在上升</span>
<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“pln”>MA_0 </span><span class=“pun”><</span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span><span class=“pln”> MA_1 </span><span class=“pun”><</span><span class=“pln”> MA_2</span><span class=“pun”>)</span><span class=“com”>//简单定义两个单位均线在下降</span>

有些策略可以要求定义均线由下往上突破均线为买入信号,由上往下突破为卖出信号,那就定义突破的概念。如:

<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“typ”>Close_0</span> <span class=“pun”>></span><span class=“pln”> MA_0 </span><span class=“pun”>&&</span> <span class=“typ”>Close_1</span> <span class=“pun”>></span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span> <span class=“typ”>Open_1</span> <span class=“pun”><</span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span> <span class=“typ”>High_2</span> <span class=“pun”><</span><span class=“pln”> MA_2</span><span class=“pun”>)</span><span class=“com”>//简单定义价格由下往上突破均线</span>
<span class=“kwd”>if</span><span class=“pun”>(</span><span class=“typ”>Close_0</span> <span class=“pun”><</span><span class=“pln”> MA_0 </span><span class=“pun”>&&</span> <span class=“typ”>Close_1</span> <span class=“pun”><</span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span> <span class=“typ”>Open_1</span> <span class=“pun”>></span><span class=“pln”> MA_1 </span><span class=“pun”>&&</span> <span class=“typ”>Low_2</span> <span class=“pun”>></span><span class=“pln”> MA_2</span><span class=“pun”>)</span><span class=“com”>//简单定义价格由上往下突破均线</span>

当然,这种定义有些简单,如果有需要可以进一步定义之前若干个单位K线价格和均线的位置来实现是在一段涨跌过后才出现的突破信号。

一些严谨的策略,可能需要限制开仓时价格离均线的距离,太远了入场容易止损。这点也不难理解,也很好定义。用输入变量定义一个距离参数,然后比较价格和均线加减这个距离。如:

<span class=“pln”>input </span><span class=“kwd”>int</span> <span class=“typ”>Max_Point</span> <span class=“pun”>=</span> <span class=“lit”>200</span><span class=“pun”>;</span><span class=“com”>//开仓价格离均线的最大距离</span>
<span class=“typ”>Ask</span> <span class=“pun”><=</span><span class=“pln”> MA_0 </span><span class=“pun”>+</span> <span class=“pun”>(</span><span class=“typ”>Max_Point</span><span class=“pun”>*</span><span class=“typ”>Point</span><span class=“pun”>)</span><span class=“com”>//限制买单价格必须小于设定的距离</span>
<span class=“typ”>Bid</span> <span class=“pun”>>=</span><span class=“pln”> MA_0 </span><span class=“pun”></span> <span class=“pun”>(</span><span class=“typ”>Max_Point</span><span class=“pun”>*</span><span class=“typ”>Point</span><span class=“pun”>)</span><span class=“com”>//限制卖单价格必须在设定的距离上方</span>

对于单一的顺信号止损止盈的策略,它有个难以避免的结果,那就是资金会在市场无常的波动中慢慢被止损掉。于是,很多纠错类的策略就出来了。如马丁、倍投等等。

在用一条均线研发马丁类的EA时,可以根据均线的突破信号作为第一单开仓信号,开仓后可以根据某些特定的限制条件来进行加仓。

如果是顺势加仓,那么入场后,可以定义均线在上升,价格在均线上方来持续加仓。

当然,不管是顺势加仓还是逆势加仓,都要合理的控制风险。加仓类的策略,大部分时候容易获利出场。

对于利用一条均线倍投类的策略,不多见,但也不能说没有研究的价值,以严谨的突破信号开仓,错了下个信号加倍投注,总会有对的时候,前提依然是要研究能容忍多少次连错。

请登录后发表评论

    没有回复内容