 | |  | | 初步创建一简易的人工智能系统
让我们将MACD指标同获利能力、支撑移动止损位以及操作安全等因素结合起来考虑以创建人工智能系统。下面的例子是开立和控制一个单独的头寸。
交易原则:
.做多(买入)信号—即当MACD 指针是在0轴在以下,为向上的趋势并与向下的信号线相交
.做空(卖出) 信号—即当MACD 指针是在0轴以上,为向下趋势并与向上的信号线相交
.多头平仓信号—即执行限价平仓指令或移动止损指令以获得利润或者在MACD指针与信号线相交(MACD指针在0轴以上且为向下趋势并与向上趋势的信号线相交)时平仓
.空头平仓信号—即执行限价平仓指令或移动止损指令以获得利润或者在MACD指针与信号线相交(MACD指针在0轴以下且为向上趋势并与向下趋势的信号线相交)时平仓
关于MT软件的人工智能实践小小篇
第一步:撰写人工智能系统说明
将鼠标指在导航窗口的人工智能系统,点击鼠标右键在弹出的菜单中CREATE A NEW EXPERT(创建一个智能系统)命令. 正在初始化的WISARD OF EXPERT ADVISOR 会问你是否要输入数据.在弹出的窗口中你得写下NAME名字(人工智能系统的名字) 、AUTHOR作者、与你的网址链接、须知—人工智能系统的测试样本.你也可以设定你想要的Lots(交易单位), Stop Loss(止损点), Take Profit(平仓) 和 Trailing Stop(移动止损)的默认值
第二步:创立程序的初步结构
测试系统的代码仅仅为几页纸,即使是这几页纸仍然是难以理解的,特别是在我们这些不是专业的程序员的眼里是非常难的.不然,我们也不必写下这段说明,不是吗?<IMG border=0 SRC=http://talkforex.com/image/regular_smile.gif>
为了了解标准的人工智能系统的结构,我们来看一下下面的解释:
1.初始资料检查
.检查图表,图表上棍的数量
.检查外部变数值OTS,S/L,T/P,T/S
2.设置为快速数据存取的内部变量
3检查交易终端—是否有空间?如果有,然后
.检查账户中的可用资金
.是否可以做多(买入)
.建仓买入和平仓
.是否可以做空(卖出)
.建仓卖出和平仓
4. 定期控制已开立的头寸
..若是多头合约
.是否要平仓
.是否要重新设定移动止损点
..若是空头合约
.是否要平仓
.是否要重新设定移动止损点
这是相对简单的样板,仅仅只有4个主要单元.
现在我们来试着逐渐将结构表中的每一部分的代码做出来:
1.初始资料检查
这一块的数据通常是经过稍稍修改后从一个系统移至另一系统的—这实际上是一单元检查.
If Bars<;200 Then Exit; // the chart has less than 200 bars - exit
If TakeProfit<;10 Then Exit; // wrong takeprofit parameters
2设置为快速数据存取的内部变量
在程序代码中,有的是经常需要存取的指示值和操做的计算值.为了简化译码和加速存取,数据最初便在内部变数中嵌套进去
.MacdCurrent=iMACD(12,26,9,MODE_MAIN,0); // MACD value on the current bar
MacdPrevious=iMACD(12,26,9,MODE_MAIN,1);// MACD value on the previous bar
SignalCurrent=iMACD(12,26,9,MODE_SIGNAL,0); // Signal Line value on the current bar
SignalPrevious=iMACD(12,26,9,MODE_SIGNAL,1);// Signal Line value on the previous bar
MaCurrent=iMA(MATrendPeriod,MODE_EMA,0);// moving average value on the current bar
MaPrevious=iMA(MATrendPeriod,MODE_EMA,1); // moving average value on the previous bar
现在,我们以在程序中简单的写入字符 MacdCurrent代替晦涩难懂的iMACD(12,26,9,MODE_MAIN,0).所有的人工智能系统中的变量都依据MQL II语言进行基本的解释.
var: MacdCurrent(0), MacdPrevious(0), SignalCurrent(0), SignalPrevious(0);
var: MaCurrent(0), MaPrevious(0);
MQL II语言还另外推出一种的用户自定义变量,它可以在程序外设定而无须任何系统程序下的源程序正文的参考.这个特点使程序更具灵活性MATrendPeriod变量就是一个这种类型的用户自定义变量,因此,我们在程序的开头加入这段说明.
defines: MATrendPeriod(56);
3. 检查交易终端是否有空间?如果有,然后
在我们的人工智能系统中,我们只能使用现时头寸而不能操作延迟的买卖盘.为了安全起见,我们介绍一种核对过去交易终端已下买卖盘的程序.
If TotalTrades<;1 then // no opened orders identified
{
3.检查: 账户的可用资金……. 在分析市场状况之前最好先检查一下你的账户的资金情况, 以确保账户中有开立头寸的资金.
If FreeMargin<;1000 then Exit;// no funds – exit
. 是否可以做多(买入)
买入的条件信号:MACD指标在0轴以下,为向上趋势且与向下趋势的信号线相交。这就是我们在MQL II语言中如何描述它的(说明我们如何操作过去在变量中存入的指示值)
If MacdCurrent<;0 and MacdCurrent>;SignalCurrent and
MacdPrevious<;SignalPrevious and// a cross-section exists
Abs(MacdCurrent)>;(MACDOpenLevel*Point) and // the indicator plotted a decent 'hillock'
MaCurrent>;MaPrevious then// 'bull' trend
{
SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED); // executing
Exit; // exiting, since after the execution of a trade
// there is a 10-second trading timeout
};
前面我们提到了一种监控图表中所要显示“小丘”的大小的一种方法。MACDOpenLevel 变量是自定义变量,它可以不影响程序正本而改变同时,还确保了更多的灵活性。在程序的初始,我们加入一段这个变量的描述.
defines: MACDOpenLevel(3), MACDCloseLevel(2);
是否可以做空(卖出)?
卖出的条件信号:
MACD指标在0轴以上,为向下趋势且与向上趋势的信号线相交.符号如下:
If MacdCurrent>;0 and MacdCurrent<;SignalCurrent and
MacdPrevious>;SignalPrevious and MacdCurrent>;(MACDOpenLevel*Point) and
MaCurrent<;MaPrevious then
{
SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED); // executing
Exit; // exiting
};
Exit; // no new positions opened - just exit
};
4.定期控制已开立的头寸
for cnt=1 to TotalTrades
{
if OrderValue(cnt,VAL_TYPE)<;=OP_SELL and // is it an open position?
OrderValue(cnt,VAL_SYMBOL)=Symbol then// position from "our" chart?
{
CNT是周期变量,是在程序之开端进行描述的,方式如下:
var: Cnt(0);
. 若是买入合约
If OrderValue(cnt,VAL_TYPE)=OP_BUY then // long position opened
{
. 是否需平仓? 平仓的条件信号:
MACD指针与信号线相交,MACD指针在0轴以上,为向下趋势且与向上趋势的信号线相交.
If MacdCurrent>;0 and MacdCurrent<;SignalCurrent and
MacdPrevious>;SignalPrevious and MacdCurrent>;(MACDCloseLevel*Point) then
{
CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,3,Violet);
Exit; // exit
};
. 是否需要重新设定移动止损点? 我们仅在持仓并已超过移动止损点数点还获利的情况下设定移动止损点, 即新的移动止损点比以前的更精确时才重设.
If TrailingStop>;0 then // if trailing stops are used
{
If (Bid-OrderValue(cnt,VAL_OPENPRICE))>;(Point*TrailingStop) then
{
If OrderValue(cnt,VAL_STOPLOSS)<;(Bid-Point*TrailingStop) then
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),
Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red);
Exit;
};
};
. 若是空头合约
else // otherwise it is a short position
{
. 是否需平仓? 平仓的条件信号:
MACD指针与信号线相交,MACD指针在0轴以下,为向上趋势且与向下趋势的信号线相交.
If MacdCurrent<;0 and MacdCurrent>;SignalCurrent and
MacdPrevious<;SignalPrevious and Abs(MacdCurrent)>;(MACDCloseLevel*Point) then
{
CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet);
Exit; // exit
};
.是否需要重新设定移动止损点? 我们仅在持仓并已超过移动止损点数点还获利的情况下设定移动止损点, 即新的移动止损点比以前的更精确时才重设.
If TrailingStop>;0 then// the user has put a trailing stop in his settings
{// so, we set out to check it
If (OrderValue(cnt,VAL_OPENPRICE)-Ask)>;(Point*TrailingStop) then
{
If OrderValue(cnt,VAL_STOPLOSS)=0 or
OrderValue(cnt,VAL_STOPLOSS)>;(Ask+Point*TrailingStop) then
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),
Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red);
Exit;
};
};
};
// end. Closing all the curly bracket which remain open.
};
};
};
这样,跟着这套初学渐进程序,我们就学会了编写自己的人工智能系统
第三步:将所有程序代码集合起来
我们将前面所有的代码集合过来
defines: MACDOpenLevel(3),MACDCloseLevel(2);
defines: MATrendPeriod(56);
var: MacdCurrent(0),MacdPrevious(0),SignalCurrent(0),SignalPrevious(0);
var: MaCurrent(0),MaPrevious(0);
var: cnt(0);
// initial data checks
// it is important to make sure that the Expert Advisor runs on a normal chart and that
// the user has correctly set the external variables (Lots, StopLoss,
// TakeProfit, TrailingStop)
// in our case we only check the TakeProfit
If Bars<;200 or TakeProfit<;10 then Exit;// less than 200 bars on the chart
// to simplify and speed up the procedure, we store the necessary
// indicator data in temporary variables
MacdCurrent=iMACD(12,26,9,0,MODE_MAIN);
MacdPrevious=iMACD(12,26,9,1,MODE_MAIN);
SignalCurrent=iMACD(12,26,9,0,MODE_SIGNAL);
SignalPrevious=iMACD(12,26,9,1,MODE_SIGNAL);
MaCurrent=iMA(MATrendPeriod,MODE_EMA,0);
MaPrevious=iMA(MATrendPeriod,MODE_EMA,1);
// now we have to check the status of the trading terminal.
// we are going to see whether there are any previously opened positions or orders.
If TotalTrades<;1 then
{// there are no opened orders
// just to be on the safe side, we make sure we have free funds on our account.
// the "1000" value is taken just as an example, usually it is possible to open 1 lot
If FreeMargin<;1000 then Exit;// no money - we exit
// checking for the possibility to take a long position (BUY)
If MacdCurrent<;0 and MacdCurrent>;SignalCurrent and
MacdPrevious<;SignalPrevious and Abs(MacdCurrent)>;(MACDOpenLevel*Point) and
MaCurrent>;MaPrevious then
{
SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED); // executing
Exit; // exiting, since after the execution of a trade
// there is a 10-second trading timeout
};
// checking for the possibility of taking a short position (SELL)
If MacdCurrent>;0 and MacdCurrent<;SignalCurrent and
MacdPrevious>;SignalPrevious and MacdCurrent>;(MACDOpenLevel*Point) and
MaCurrent<;MaPrevious then
{
SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED); // executing
Exit; // exiting
};
// here we completed the check for the possibility of opening new positions.
// no new positions were opened and we simply exit the programme using the Exit command, as
// there is nothing to analyze
Exit;
};
// we come over to an important part of the Expert Advisor - the control of open positions
// 'it is important to enter the market correctly, but it is even more important to exit it...'
for cnt=1 to TotalTrades
{
if OrderValue(cnt,VAL_TYPE)<;=OP_SELL and // is this an open position? OP_BUY or OP_SELL
OrderValue(cnt,VAL_SYMBOL)=Symbol then// does the instrument match?
{
If OrderValue(cnt,VAL_TYPE)=OP_BUY then // long position opened
{
// we check - maybe, it's already time to close it?
If MacdCurrent>;0 and MacdCurrent<;SignalCurrent and
MacdPrevious>;SignalPrevious and MacdCurrent>;(MACDCloseLevel*Point) then
{
CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,3,Violet);
Exit; // exiting
};
// we check - maybe, we already may or it's already time to set a trailing stop?
If TrailingStop>;0 then// the user has put a trailing stop in his settings
{ // so, we set out to check it
If (Bid-OrderValue(cnt,VAL_OPENPRICE))>;(Point*TrailingStop) then
{
If OrderValue(cnt,VAL_STOPLOSS)<;(Bid-Point*TrailingStop) then
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),
Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red);
Exit;
};
};
};
}
else // otherwise it is a long position
{
// we check - maybe, it's already time to close it?
If MacdCurrent<;0 and MacdCurrent>;SignalCurrent and
MacdPrevious<;SignalPrevious and Abs(MacdCurrent)>;(MACDCloseLevel*Point) then
{
CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet);
Exit; // exiting
};
// we check - maybe, we already may or it's already time to set a trailing stop?
If TrailingStop>;0 then// the user has put a trailing stop in his settings
{// so, we set out to check it
If (OrderValue(cnt,VAL_OPENPRICE)-Ask)>;(Point*TrailingStop) then
{
If OrderValue(cnt,VAL_STOPLOSS)=0 or
OrderValue(cnt,VAL_STOPLOSS)>;(Ask+Point*TrailingStop) then
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),
Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red);
Exit;
};
};
};
};
};
};
// the end.
现在,我们只需给以下外部变量赋值就可以完成安装人工智能系统的全过程.
LOTS=1,STOP LOSS(S/L)=0, TAKE PROFIT(T/P)=120(适用于一个小时的间
隔),TRAILING STOP(T/S)=30,当然你自己可以设定这些值.
按VERIFY按纽,若无别的错误就按SAVE按纽.
现在,我们来编辑人工智能系统.在MQL编辑器点击顶端的VERIFY图示(像一张有检查标记的纸.)。 |  |  |  |  |
|